You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilmStrip.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* global $, interfaceConfig */
  2. import UIEvents from "../../../service/UI/UIEvents";
  3. import UIUtil from "../util/UIUtil";
  4. const FilmStrip = {
  5. /**
  6. *
  7. * @param eventEmitter the {EventEmitter} through which {FilmStrip} is to
  8. * emit/fire {UIEvents} (such as {UIEvents.TOGGLED_FILM_STRIP}).
  9. */
  10. init (eventEmitter) {
  11. this.iconMenuDownClassName = 'icon-menu-down';
  12. this.iconMenuUpClassName = 'icon-menu-up';
  13. this.filmStrip = $('#remoteVideos');
  14. this.eventEmitter = eventEmitter;
  15. this._initFilmStripToolbar();
  16. this.registerListeners();
  17. },
  18. /**
  19. * Initializes the filmstrip toolbar
  20. */
  21. _initFilmStripToolbar() {
  22. let toolbar = this._generateFilmStripToolbar();
  23. let container = document.querySelector('.filmstrip');
  24. UIUtil.prependChild(container, toolbar);
  25. let iconSelector = '#hideVideoToolbar i';
  26. this.toggleFilmStripIcon = document.querySelector(iconSelector);
  27. },
  28. /**
  29. * Generates HTML layout for filmstrip toolbar
  30. * @returns {HTMLElement}
  31. * @private
  32. */
  33. _generateFilmStripToolbar() {
  34. let container = document.createElement('div');
  35. let isVisible = this.isFilmStripVisible();
  36. container.className = 'filmstrip__toolbar';
  37. container.innerHTML = `
  38. <button id="hideVideoToolbar">
  39. <i class="icon-menu-${isVisible ? 'down' : 'up'}">
  40. </i>
  41. </button>
  42. `;
  43. return container;
  44. },
  45. /**
  46. * Attach 'click' listener to "hide filmstrip" button
  47. */
  48. registerListeners() {
  49. let toggleFilmstripMethod = this.toggleFilmStrip.bind(this);
  50. let selector = '#hideVideoToolbar';
  51. $('#videospace').on('click', selector, toggleFilmstripMethod);
  52. let eventEmitter = this.eventEmitter;
  53. let event = UIEvents.TOGGLE_FILM_STRIP;
  54. if (eventEmitter) {
  55. eventEmitter.addListener(event, toggleFilmstripMethod);
  56. }
  57. },
  58. /**
  59. * Changes classes of icon for showing down state
  60. */
  61. showMenuDownIcon() {
  62. let icon = this.toggleFilmStripIcon;
  63. icon.classList.add(this.iconMenuDownClassName);
  64. icon.classList.remove(this.iconMenuUpClassName);
  65. },
  66. /**
  67. * Changes classes of icon for showing up state
  68. */
  69. showMenuUpIcon() {
  70. let icon = this.toggleFilmStripIcon;
  71. icon.classList.add(this.iconMenuUpClassName);
  72. icon.classList.remove(this.iconMenuDownClassName);
  73. },
  74. /**
  75. * Toggles the visibility of the film strip.
  76. *
  77. * @param visible optional {Boolean} which specifies the desired visibility
  78. * of the film strip. If not specified, the visibility will be flipped
  79. * (i.e. toggled); otherwise, the visibility will be set to the specified
  80. * value.
  81. */
  82. toggleFilmStrip(visible) {
  83. let isVisibleDefined = typeof visible === 'boolean';
  84. if (!isVisibleDefined) {
  85. visible = this.isFilmStripVisible();
  86. } else if (this.isFilmStripVisible() === visible) {
  87. return;
  88. }
  89. this.filmStrip.toggleClass("hidden");
  90. if (!visible) {
  91. this.showMenuDownIcon();
  92. } else {
  93. this.showMenuUpIcon();
  94. }
  95. // Emit/fire UIEvents.TOGGLED_FILM_STRIP.
  96. var eventEmitter = this.eventEmitter;
  97. if (eventEmitter) {
  98. eventEmitter.emit(
  99. UIEvents.TOGGLED_FILM_STRIP,
  100. this.isFilmStripVisible());
  101. }
  102. },
  103. /**
  104. * Shows if filmstrip is visible
  105. * @returns {boolean}
  106. */
  107. isFilmStripVisible() {
  108. return !this.filmStrip.hasClass('hidden');
  109. },
  110. setupFilmStripOnly() {
  111. this.filmStrip.css({
  112. padding: "0px 0px 18px 0px",
  113. right: 0
  114. });
  115. },
  116. /**
  117. * Returns the height of filmstrip
  118. * @returns {number} height
  119. */
  120. getFilmStripHeight() {
  121. if (this.isFilmStripVisible()) {
  122. return this.filmStrip.outerHeight();
  123. } else {
  124. return 0;
  125. }
  126. },
  127. /**
  128. * Returns the width of filmstip
  129. * @returns {number} width
  130. */
  131. getFilmStripWidth() {
  132. return this.filmStrip.innerWidth()
  133. - parseInt(this.filmStrip.css('paddingLeft'), 10)
  134. - parseInt(this.filmStrip.css('paddingRight'), 10);
  135. },
  136. /**
  137. * Calculates the size for thumbnails: local and remote one
  138. * @returns {*|{localVideo, remoteVideo}}
  139. */
  140. calculateThumbnailSize() {
  141. let availableSizes = this.calculateAvailableSize();
  142. let width = availableSizes.availableWidth;
  143. let height = availableSizes.availableHeight;
  144. return this.calculateThumbnailSizeFromAvailable(width, height);
  145. },
  146. /**
  147. * Normalizes local and remote thumbnail ratios
  148. */
  149. normalizeThumbnailRatio() {
  150. let remoteHeightRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO_HEIGHT;
  151. let remoteWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO_WIDTH;
  152. let localHeightRatio = interfaceConfig.LOCAL_THUMBNAIL_RATIO_HEIGHT;
  153. let localWidthRatio = interfaceConfig.LOCAL_THUMBNAIL_RATIO_WIDTH;
  154. let commonHeightRatio = remoteHeightRatio * localHeightRatio;
  155. let localRatioCoefficient = localWidthRatio / localHeightRatio;
  156. let remoteRatioCoefficient = remoteWidthRatio / remoteHeightRatio;
  157. remoteWidthRatio = commonHeightRatio * remoteRatioCoefficient;
  158. remoteHeightRatio = commonHeightRatio;
  159. localWidthRatio = commonHeightRatio * localRatioCoefficient;
  160. localHeightRatio = commonHeightRatio;
  161. let localRatio = {
  162. widthRatio: localWidthRatio,
  163. heightRatio: localHeightRatio
  164. };
  165. let remoteRatio = {
  166. widthRatio: remoteWidthRatio,
  167. heightRatio: remoteHeightRatio
  168. };
  169. return { localRatio, remoteRatio };
  170. },
  171. /**
  172. * Calculates available size for one thumbnail according to
  173. * the current window size
  174. * @returns {{availableWidth: number, availableHeight: number}}
  175. */
  176. calculateAvailableSize() {
  177. let availableHeight = interfaceConfig.FILM_STRIP_MAX_HEIGHT;
  178. let thumbs = this.getThumbs(true);
  179. let numvids = thumbs.remoteThumbs.length;
  180. let localVideoContainer = $("#localVideoContainer");
  181. /**
  182. * If the videoAreaAvailableWidth is set we use this one to calculate
  183. * the filmStrip width, because we're probably in a state where the
  184. * film strip size hasn't been updated yet, but it will be.
  185. */
  186. let videoAreaAvailableWidth
  187. = UIUtil.getAvailableVideoWidth()
  188. - UIUtil.parseCssInt(this.filmStrip.css('right'), 10)
  189. - UIUtil.parseCssInt(this.filmStrip.css('paddingLeft'), 10)
  190. - UIUtil.parseCssInt(this.filmStrip.css('paddingRight'), 10)
  191. - UIUtil.parseCssInt(this.filmStrip.css('borderLeftWidth'), 10)
  192. - UIUtil.parseCssInt(this.filmStrip.css('borderRightWidth'), 10)
  193. - 5;
  194. let availableWidth = videoAreaAvailableWidth;
  195. // If local thumb is not hidden
  196. if(thumbs.localThumb) {
  197. availableWidth = Math.floor(
  198. (videoAreaAvailableWidth - (
  199. UIUtil.parseCssInt(
  200. localVideoContainer.css('borderLeftWidth'), 10)
  201. + UIUtil.parseCssInt(
  202. localVideoContainer.css('borderRightWidth'), 10)
  203. + UIUtil.parseCssInt(
  204. localVideoContainer.css('paddingLeft'), 10)
  205. + UIUtil.parseCssInt(
  206. localVideoContainer.css('paddingRight'), 10)
  207. + UIUtil.parseCssInt(
  208. localVideoContainer.css('marginLeft'), 10)
  209. + UIUtil.parseCssInt(
  210. localVideoContainer.css('marginRight'), 10)))
  211. );
  212. }
  213. // If the number of videos is 0 or undefined we don't need to calculate
  214. // further.
  215. if (numvids) {
  216. let remoteVideoContainer = thumbs.remoteThumbs.eq(0);
  217. availableWidth = Math.floor(
  218. (videoAreaAvailableWidth - numvids * (
  219. UIUtil.parseCssInt(
  220. remoteVideoContainer.css('borderLeftWidth'), 10)
  221. + UIUtil.parseCssInt(
  222. remoteVideoContainer.css('borderRightWidth'), 10)
  223. + UIUtil.parseCssInt(
  224. remoteVideoContainer.css('paddingLeft'), 10)
  225. + UIUtil.parseCssInt(
  226. remoteVideoContainer.css('paddingRight'), 10)
  227. + UIUtil.parseCssInt(
  228. remoteVideoContainer.css('marginLeft'), 10)
  229. + UIUtil.parseCssInt(
  230. remoteVideoContainer.css('marginRight'), 10)))
  231. );
  232. }
  233. let maxHeight
  234. // If the MAX_HEIGHT property hasn't been specified
  235. // we have the static value.
  236. = Math.min(interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120,
  237. availableHeight);
  238. availableHeight
  239. = Math.min(maxHeight, window.innerHeight - 18);
  240. return { availableWidth, availableHeight };
  241. },
  242. /**
  243. * Takes the available size for thumbnail and calculates
  244. * final size of thumbnails
  245. * @param availableWidth
  246. * @param availableHeight
  247. * @returns {{localVideo, remoteVideo}}
  248. */
  249. calculateThumbnailSizeFromAvailable(availableWidth, availableHeight) {
  250. let { localRatio, remoteRatio } = this.normalizeThumbnailRatio();
  251. let { remoteThumbs } = this.getThumbs(true);
  252. let remoteProportion = remoteRatio.widthRatio * remoteThumbs.length;
  253. let widthProportion = remoteProportion + localRatio.widthRatio;
  254. let heightUnit = availableHeight / localRatio.heightRatio;
  255. let widthUnit = availableWidth / widthProportion;
  256. if (heightUnit < widthUnit) {
  257. widthUnit = heightUnit;
  258. }
  259. else
  260. heightUnit = widthUnit;
  261. let localVideo = {
  262. thumbWidth: widthUnit * localRatio.widthRatio,
  263. thumbHeight: heightUnit * localRatio.heightRatio
  264. };
  265. let remoteVideo = {
  266. thumbWidth: widthUnit * remoteRatio.widthRatio,
  267. thumbHeight: widthUnit * remoteRatio.heightRatio
  268. };
  269. return {
  270. localVideo,
  271. remoteVideo
  272. };
  273. },
  274. /**
  275. * Resizes thumbnails
  276. * @param local
  277. * @param remote
  278. * @param animate
  279. * @param forceUpdate
  280. * @returns {Promise}
  281. */
  282. resizeThumbnails(local, remote,
  283. animate = false, forceUpdate = false) {
  284. return new Promise(resolve => {
  285. let thumbs = this.getThumbs(!forceUpdate);
  286. if(thumbs.localThumb)
  287. thumbs.localThumb.animate({
  288. height: local.thumbHeight,
  289. width: local.thumbWidth
  290. }, {
  291. queue: false,
  292. duration: animate ? 500 : 0,
  293. complete: resolve
  294. });
  295. if(thumbs.remoteThumbs)
  296. thumbs.remoteThumbs.animate({
  297. height: remote.thumbHeight,
  298. width: remote.thumbWidth
  299. }, {
  300. queue: false,
  301. duration: animate ? 500 : 0,
  302. complete: resolve
  303. });
  304. this.filmStrip.animate({
  305. // adds 2 px because of small video 1px border
  306. height: remote.thumbHeight + 2
  307. }, {
  308. queue: false,
  309. duration: animate ? 500 : 0
  310. });
  311. if (!animate) {
  312. resolve();
  313. }
  314. });
  315. },
  316. /**
  317. * Returns thumbnails of the filmstrip
  318. * @param only_visible
  319. * @returns {object} thumbnails
  320. */
  321. getThumbs(only_visible = false) {
  322. let selector = 'span';
  323. if (only_visible) {
  324. selector += ':visible';
  325. }
  326. let localThumb = $("#localVideoContainer");
  327. let remoteThumbs = this.filmStrip.children(selector)
  328. .not("#localVideoContainer");
  329. // Exclude the local video container if it has been hidden.
  330. if (localThumb.hasClass("hidden")) {
  331. return { remoteThumbs };
  332. } else {
  333. return { remoteThumbs, localThumb };
  334. }
  335. }
  336. };
  337. export default FilmStrip;