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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* global $, APP, JitsiMeetJS, 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_FILMSTRIP}).
  9. */
  10. init (eventEmitter) {
  11. this.iconMenuDownClassName = 'icon-menu-down';
  12. this.iconMenuUpClassName = 'icon-menu-up';
  13. this.filmstripContainerClassName = 'filmstrip';
  14. this.filmstrip = $('#remoteVideos');
  15. this.filmstripRemoteVideos = $('#filmstripRemoteVideosContainer');
  16. this.eventEmitter = eventEmitter;
  17. // Show the toggle button and add event listeners only when out of
  18. // filmstrip only mode.
  19. if (!interfaceConfig.filmStripOnly) {
  20. this._initFilmstripToolbar();
  21. this.registerListeners();
  22. }
  23. },
  24. /**
  25. * Initializes the filmstrip toolbar.
  26. */
  27. _initFilmstripToolbar() {
  28. let toolbarContainerHTML = this._generateToolbarHTML();
  29. let className = this.filmstripContainerClassName;
  30. let container = document.querySelector(`.${className}`);
  31. UIUtil.prependChild(container, toolbarContainerHTML);
  32. let iconSelector = '#toggleFilmstripButton i';
  33. this.toggleFilmstripIcon = document.querySelector(iconSelector);
  34. },
  35. /**
  36. * Generates HTML layout for filmstrip toggle button and wrapping container.
  37. * @returns {HTMLElement}
  38. * @private
  39. */
  40. _generateToolbarHTML() {
  41. let container = document.createElement('div');
  42. let isVisible = this.isFilmstripVisible();
  43. container.className = 'filmstrip__toolbar';
  44. container.innerHTML = `
  45. <button id="toggleFilmstripButton">
  46. <i class="icon-menu-${isVisible ? 'down' : 'up'}">
  47. </i>
  48. </button>
  49. `;
  50. return container;
  51. },
  52. /**
  53. * Attach 'click' listener to "hide filmstrip" button
  54. */
  55. registerListeners() {
  56. // Important:
  57. // Firing the event instead of executing toggleFilmstrip method because
  58. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  59. // to correctly resize the video area.
  60. $('#toggleFilmstripButton').on('click',
  61. () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP));
  62. this._registerToggleFilmstripShortcut();
  63. },
  64. /**
  65. * Registering toggle filmstrip shortcut
  66. * @private
  67. */
  68. _registerToggleFilmstripShortcut() {
  69. let shortcut = 'F';
  70. let shortcutAttr = 'filmstripPopover';
  71. let description = 'keyboardShortcuts.toggleFilmstrip';
  72. // Important:
  73. // Firing the event instead of executing toggleFilmstrip method because
  74. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  75. // to correctly resize the video area.
  76. let handler = () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP);
  77. APP.keyboardshortcut.registerShortcut(
  78. shortcut,
  79. shortcutAttr,
  80. handler,
  81. description
  82. );
  83. },
  84. /**
  85. * Changes classes of icon for showing down state
  86. */
  87. showMenuDownIcon() {
  88. let icon = this.toggleFilmstripIcon;
  89. if(icon) {
  90. icon.classList.add(this.iconMenuDownClassName);
  91. icon.classList.remove(this.iconMenuUpClassName);
  92. }
  93. },
  94. /**
  95. * Changes classes of icon for showing up state
  96. */
  97. showMenuUpIcon() {
  98. let icon = this.toggleFilmstripIcon;
  99. if(icon) {
  100. icon.classList.add(this.iconMenuUpClassName);
  101. icon.classList.remove(this.iconMenuDownClassName);
  102. }
  103. },
  104. /**
  105. * Toggles the visibility of the filmstrip.
  106. *
  107. * @param visible optional {Boolean} which specifies the desired visibility
  108. * of the filmstrip. If not specified, the visibility will be flipped
  109. * (i.e. toggled); otherwise, the visibility will be set to the specified
  110. * value.
  111. * @param {Boolean} sendAnalytics - True to send an analytics event. The
  112. * default value is true.
  113. *
  114. * Note:
  115. * This method shouldn't be executed directly to hide the filmstrip.
  116. * It's important to hide the filmstrip with UI.toggleFilmstrip in order
  117. * to correctly resize the video area.
  118. */
  119. toggleFilmstrip(visible, sendAnalytics = true) {
  120. const isVisibleDefined = typeof visible === 'boolean';
  121. if (!isVisibleDefined) {
  122. visible = this.isFilmstripVisible();
  123. } else if (this.isFilmstripVisible() === visible) {
  124. return;
  125. }
  126. if (sendAnalytics) {
  127. JitsiMeetJS.analytics.sendEvent('toolbar.filmstrip.toggled');
  128. }
  129. this.filmstrip.toggleClass("hidden");
  130. if (visible) {
  131. this.showMenuUpIcon();
  132. } else {
  133. this.showMenuDownIcon();
  134. }
  135. // Emit/fire UIEvents.TOGGLED_FILMSTRIP.
  136. const eventEmitter = this.eventEmitter;
  137. if (eventEmitter) {
  138. eventEmitter.emit(
  139. UIEvents.TOGGLED_FILMSTRIP,
  140. this.isFilmstripVisible());
  141. }
  142. },
  143. /**
  144. * Shows if filmstrip is visible
  145. * @returns {boolean}
  146. */
  147. isFilmstripVisible() {
  148. return !this.filmstrip.hasClass('hidden');
  149. },
  150. /**
  151. * Adjusts styles for filmstrip-only mode.
  152. */
  153. setFilmstripOnly() {
  154. this.filmstrip.addClass('filmstrip__videos-filmstripOnly');
  155. },
  156. /**
  157. * Returns the height of filmstrip
  158. * @returns {number} height
  159. */
  160. getFilmstripHeight() {
  161. // FIXME Make it more clear the getFilmstripHeight check is used in
  162. // horizontal film strip mode for calculating how tall large video
  163. // display should be.
  164. if (this.isFilmstripVisible() && !interfaceConfig.VERTICAL_FILMSTRIP) {
  165. return $(`.${this.filmstripContainerClassName}`).outerHeight();
  166. } else {
  167. return 0;
  168. }
  169. },
  170. /**
  171. * Returns the width of filmstip
  172. * @returns {number} width
  173. */
  174. getFilmstripWidth() {
  175. return this.filmstrip.innerWidth()
  176. - parseInt(this.filmstrip.css('paddingLeft'), 10)
  177. - parseInt(this.filmstrip.css('paddingRight'), 10);
  178. },
  179. /**
  180. * Calculates the size for thumbnails: local and remote one
  181. * @returns {*|{localVideo, remoteVideo}}
  182. */
  183. calculateThumbnailSize() {
  184. let availableSizes = this.calculateAvailableSize();
  185. let width = availableSizes.availableWidth;
  186. let height = availableSizes.availableHeight;
  187. return this.calculateThumbnailSizeFromAvailable(width, height);
  188. },
  189. /**
  190. * Calculates available size for one thumbnail according to
  191. * the current window size.
  192. *
  193. * @returns {{availableWidth: number, availableHeight: number}}
  194. */
  195. calculateAvailableSize() {
  196. let availableHeight = interfaceConfig.FILM_STRIP_MAX_HEIGHT;
  197. let thumbs = this.getThumbs(true);
  198. let numvids = thumbs.remoteThumbs.length;
  199. let localVideoContainer = $("#localVideoContainer");
  200. /**
  201. * If the videoAreaAvailableWidth is set we use this one to calculate
  202. * the filmstrip width, because we're probably in a state where the
  203. * filmstrip size hasn't been updated yet, but it will be.
  204. */
  205. let videoAreaAvailableWidth
  206. = UIUtil.getAvailableVideoWidth()
  207. - this._getFilmstripExtraPanelsWidth()
  208. - UIUtil.parseCssInt(this.filmstrip.css('right'), 10)
  209. - UIUtil.parseCssInt(this.filmstrip.css('paddingLeft'), 10)
  210. - UIUtil.parseCssInt(this.filmstrip.css('paddingRight'), 10)
  211. - UIUtil.parseCssInt(this.filmstrip.css('borderLeftWidth'), 10)
  212. - UIUtil.parseCssInt(this.filmstrip.css('borderRightWidth'), 10)
  213. - 5;
  214. let availableWidth = videoAreaAvailableWidth;
  215. // If local thumb is not hidden
  216. if(thumbs.localThumb) {
  217. availableWidth = Math.floor(
  218. (videoAreaAvailableWidth - (
  219. UIUtil.parseCssInt(
  220. localVideoContainer.css('borderLeftWidth'), 10)
  221. + UIUtil.parseCssInt(
  222. localVideoContainer.css('borderRightWidth'), 10)
  223. + UIUtil.parseCssInt(
  224. localVideoContainer.css('paddingLeft'), 10)
  225. + UIUtil.parseCssInt(
  226. localVideoContainer.css('paddingRight'), 10)
  227. + UIUtil.parseCssInt(
  228. localVideoContainer.css('marginLeft'), 10)
  229. + UIUtil.parseCssInt(
  230. localVideoContainer.css('marginRight'), 10)))
  231. );
  232. }
  233. // If the number of videos is 0 or undefined we don't need to calculate
  234. // further.
  235. if (numvids) {
  236. let remoteVideoContainer = thumbs.remoteThumbs.eq(0);
  237. availableWidth = Math.floor(
  238. (videoAreaAvailableWidth - numvids * (
  239. UIUtil.parseCssInt(
  240. remoteVideoContainer.css('borderLeftWidth'), 10)
  241. + UIUtil.parseCssInt(
  242. remoteVideoContainer.css('borderRightWidth'), 10)
  243. + UIUtil.parseCssInt(
  244. remoteVideoContainer.css('paddingLeft'), 10)
  245. + UIUtil.parseCssInt(
  246. remoteVideoContainer.css('paddingRight'), 10)
  247. + UIUtil.parseCssInt(
  248. remoteVideoContainer.css('marginLeft'), 10)
  249. + UIUtil.parseCssInt(
  250. remoteVideoContainer.css('marginRight'), 10)))
  251. );
  252. }
  253. let maxHeight
  254. // If the MAX_HEIGHT property hasn't been specified
  255. // we have the static value.
  256. = Math.min(interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120,
  257. availableHeight);
  258. availableHeight
  259. = Math.min(maxHeight, window.innerHeight - 18);
  260. return { availableWidth, availableHeight };
  261. },
  262. /**
  263. * Traverse all elements inside the filmstrip
  264. * and calculates the sum of all of them except
  265. * remote videos element. Used for calculation of
  266. * available width for video thumbnails.
  267. *
  268. * @returns {number} calculated width
  269. * @private
  270. */
  271. _getFilmstripExtraPanelsWidth() {
  272. let className = this.filmstripContainerClassName;
  273. let width = 0;
  274. $(`.${className}`)
  275. .children()
  276. .each(function () {
  277. if (this.id !== 'remoteVideos') {
  278. width += $(this).outerWidth();
  279. }
  280. });
  281. return width;
  282. },
  283. /**
  284. Calculate the thumbnail size in order to fit all the thumnails in passed
  285. * dimensions.
  286. * NOTE: Here we assume that the remote and local thumbnails are with the
  287. * same height.
  288. * @param {int} availableWidth the maximum width for all thumbnails
  289. * @param {int} availableHeight the maximum height for all thumbnails
  290. * @returns {{localVideo, remoteVideo}}
  291. */
  292. calculateThumbnailSizeFromAvailable(availableWidth, availableHeight) {
  293. /**
  294. * Let:
  295. * lW - width of the local thumbnail
  296. * rW - width of the remote thumbnail
  297. * h - the height of the thumbnails
  298. * remoteRatio - width:height for the remote thumbnail
  299. * localRatio - width:height for the local thumbnail
  300. * numberRemoteThumbs - number of remote thumbnails (we have only one
  301. * local thumbnail)
  302. *
  303. * Since the height for local thumbnail = height for remote thumbnail
  304. * and we know the ratio (width:height) for the local and for the
  305. * remote thumbnail we can find rW/lW:
  306. * rW / remoteRatio = lW / localRatio then -
  307. * remoteLocalWidthRatio = rW / lW = remoteRatio / localRatio
  308. * and rW = lW * remoteRatio / localRatio = lW * remoteLocalWidthRatio
  309. * And the total width for the thumbnails is:
  310. * totalWidth = rW * numberRemoteThumbs + lW
  311. * = lW * remoteLocalWidthRatio * numberRemoteThumbs + lW =
  312. * lW * (remoteLocalWidthRatio * numberRemoteThumbs + 1)
  313. * and the h = lW/localRatio
  314. *
  315. * In order to fit all the thumbails in the area defined by
  316. * availableWidth * availableHeight we should check one of the
  317. * following options:
  318. * 1) if availableHeight == h - totalWidth should be less than
  319. * availableWidth
  320. * 2) if availableWidth == totalWidth - h should be less than
  321. * availableHeight
  322. *
  323. * 1) or 2) will be true and we are going to use it to calculate all
  324. * sizes.
  325. *
  326. * if 1) is true that means that
  327. * availableHeight/h > availableWidth/totalWidth otherwise 2) is true
  328. */
  329. const numberRemoteThumbs = this.getThumbs(true).remoteThumbs.length;
  330. const remoteLocalWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO /
  331. interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  332. const lW = Math.min(availableWidth /
  333. (remoteLocalWidthRatio * numberRemoteThumbs + 1), availableHeight *
  334. interfaceConfig.LOCAL_THUMBNAIL_RATIO);
  335. const h = lW / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  336. const removeVideoWidth = lW * remoteLocalWidthRatio;
  337. let localVideo;
  338. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  339. // scale both width and height
  340. localVideo = {
  341. thumbWidth: removeVideoWidth,
  342. thumbHeight: h * remoteLocalWidthRatio
  343. };
  344. } else {
  345. localVideo = {
  346. thumbWidth: lW,
  347. thumbHeight: h
  348. };
  349. }
  350. return {
  351. localVideo,
  352. remoteVideo: {
  353. thumbWidth: removeVideoWidth,
  354. thumbHeight: h
  355. }
  356. };
  357. },
  358. /**
  359. * Resizes thumbnails
  360. * @param local
  361. * @param remote
  362. * @param animate
  363. * @param forceUpdate
  364. * @returns {Promise}
  365. */
  366. resizeThumbnails(local, remote, animate = false, forceUpdate = false) {
  367. return new Promise(resolve => {
  368. let thumbs = this.getThumbs(!forceUpdate);
  369. let promises = [];
  370. if(thumbs.localThumb) {
  371. promises.push(new Promise((resolve) => {
  372. thumbs.localThumb.animate({
  373. height: local.thumbHeight,
  374. width: local.thumbWidth
  375. }, this._getAnimateOptions(animate, resolve));
  376. }));
  377. }
  378. if(thumbs.remoteThumbs) {
  379. promises.push(new Promise((resolve) => {
  380. thumbs.remoteThumbs.animate({
  381. height: remote.thumbHeight,
  382. width: remote.thumbWidth
  383. }, this._getAnimateOptions(animate, resolve));
  384. }));
  385. }
  386. promises.push(new Promise((resolve) => {
  387. // Let CSS take care of height in vertical filmstrip mode.
  388. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  389. resolve();
  390. } else {
  391. this.filmstrip.animate({
  392. // adds 2 px because of small video 1px border
  393. height: remote.thumbHeight + 2
  394. }, this._getAnimateOptions(animate, resolve));
  395. }
  396. }));
  397. promises.push(new Promise(() => {
  398. let { localThumb } = this.getThumbs();
  399. let height = localThumb.height();
  400. let fontSize = UIUtil.getIndicatorFontSize(height);
  401. this.filmstrip.find('.indicator').animate({
  402. fontSize
  403. }, this._getAnimateOptions(animate, resolve));
  404. }));
  405. if (!animate) {
  406. resolve();
  407. }
  408. Promise.all(promises).then(resolve);
  409. });
  410. },
  411. /**
  412. * Helper method. Returns options for jQuery animation
  413. * @param animate {Boolean} - animation flag
  414. * @param cb {Function} - complete callback
  415. * @returns {Object} - animation options object
  416. * @private
  417. */
  418. _getAnimateOptions(animate, cb = $.noop) {
  419. return {
  420. queue: false,
  421. duration: animate ? 500 : 0,
  422. complete: cb
  423. };
  424. },
  425. /**
  426. * Returns thumbnails of the filmstrip
  427. * @param only_visible
  428. * @returns {object} thumbnails
  429. */
  430. getThumbs(only_visible = false) {
  431. let selector = 'span';
  432. if (only_visible) {
  433. selector += ':visible';
  434. }
  435. let localThumb = $("#localVideoContainer");
  436. let remoteThumbs = this.filmstripRemoteVideos.children(selector);
  437. // Exclude the local video container if it has been hidden.
  438. if (localThumb.hasClass("hidden")) {
  439. return { remoteThumbs };
  440. } else {
  441. return { remoteThumbs, localThumb };
  442. }
  443. }
  444. };
  445. export default Filmstrip;