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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if (this.isFilmstripVisible()) {
  162. return $(`.${this.filmstripContainerClassName}`).outerHeight();
  163. } else {
  164. return 0;
  165. }
  166. },
  167. /**
  168. * Returns the width of filmstip
  169. * @returns {number} width
  170. */
  171. getFilmstripWidth() {
  172. return this.filmstrip.innerWidth()
  173. - parseInt(this.filmstrip.css('paddingLeft'), 10)
  174. - parseInt(this.filmstrip.css('paddingRight'), 10);
  175. },
  176. /**
  177. * Calculates the size for thumbnails: local and remote one
  178. * @returns {*|{localVideo, remoteVideo}}
  179. */
  180. calculateThumbnailSize() {
  181. let availableSizes = this.calculateAvailableSize();
  182. let width = availableSizes.availableWidth;
  183. let height = availableSizes.availableHeight;
  184. return this.calculateThumbnailSizeFromAvailable(width, height);
  185. },
  186. /**
  187. * Calculates available size for one thumbnail according to
  188. * the current window size.
  189. *
  190. * @returns {{availableWidth: number, availableHeight: number}}
  191. */
  192. calculateAvailableSize() {
  193. let availableHeight = interfaceConfig.FILM_STRIP_MAX_HEIGHT;
  194. let thumbs = this.getThumbs(true);
  195. let numvids = thumbs.remoteThumbs.length;
  196. let localVideoContainer = $("#localVideoContainer");
  197. /**
  198. * If the videoAreaAvailableWidth is set we use this one to calculate
  199. * the filmstrip width, because we're probably in a state where the
  200. * filmstrip size hasn't been updated yet, but it will be.
  201. */
  202. let videoAreaAvailableWidth
  203. = UIUtil.getAvailableVideoWidth()
  204. - this._getFilmstripExtraPanelsWidth()
  205. - UIUtil.parseCssInt(this.filmstrip.css('right'), 10)
  206. - UIUtil.parseCssInt(this.filmstrip.css('paddingLeft'), 10)
  207. - UIUtil.parseCssInt(this.filmstrip.css('paddingRight'), 10)
  208. - UIUtil.parseCssInt(this.filmstrip.css('borderLeftWidth'), 10)
  209. - UIUtil.parseCssInt(this.filmstrip.css('borderRightWidth'), 10)
  210. - 5;
  211. let availableWidth = videoAreaAvailableWidth;
  212. // If local thumb is not hidden
  213. if(thumbs.localThumb) {
  214. availableWidth = Math.floor(
  215. (videoAreaAvailableWidth - (
  216. UIUtil.parseCssInt(
  217. localVideoContainer.css('borderLeftWidth'), 10)
  218. + UIUtil.parseCssInt(
  219. localVideoContainer.css('borderRightWidth'), 10)
  220. + UIUtil.parseCssInt(
  221. localVideoContainer.css('paddingLeft'), 10)
  222. + UIUtil.parseCssInt(
  223. localVideoContainer.css('paddingRight'), 10)
  224. + UIUtil.parseCssInt(
  225. localVideoContainer.css('marginLeft'), 10)
  226. + UIUtil.parseCssInt(
  227. localVideoContainer.css('marginRight'), 10)))
  228. );
  229. }
  230. // If the number of videos is 0 or undefined we don't need to calculate
  231. // further.
  232. if (numvids) {
  233. let remoteVideoContainer = thumbs.remoteThumbs.eq(0);
  234. availableWidth = Math.floor(
  235. (videoAreaAvailableWidth - numvids * (
  236. UIUtil.parseCssInt(
  237. remoteVideoContainer.css('borderLeftWidth'), 10)
  238. + UIUtil.parseCssInt(
  239. remoteVideoContainer.css('borderRightWidth'), 10)
  240. + UIUtil.parseCssInt(
  241. remoteVideoContainer.css('paddingLeft'), 10)
  242. + UIUtil.parseCssInt(
  243. remoteVideoContainer.css('paddingRight'), 10)
  244. + UIUtil.parseCssInt(
  245. remoteVideoContainer.css('marginLeft'), 10)
  246. + UIUtil.parseCssInt(
  247. remoteVideoContainer.css('marginRight'), 10)))
  248. );
  249. }
  250. let maxHeight
  251. // If the MAX_HEIGHT property hasn't been specified
  252. // we have the static value.
  253. = Math.min(interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120,
  254. availableHeight);
  255. availableHeight
  256. = Math.min(maxHeight, window.innerHeight - 18);
  257. return { availableWidth, availableHeight };
  258. },
  259. /**
  260. * Traverse all elements inside the filmstrip
  261. * and calculates the sum of all of them except
  262. * remote videos element. Used for calculation of
  263. * available width for video thumbnails.
  264. *
  265. * @returns {number} calculated width
  266. * @private
  267. */
  268. _getFilmstripExtraPanelsWidth() {
  269. let className = this.filmstripContainerClassName;
  270. let width = 0;
  271. $(`.${className}`)
  272. .children()
  273. .each(function () {
  274. if (this.id !== 'remoteVideos') {
  275. width += $(this).outerWidth();
  276. }
  277. });
  278. return width;
  279. },
  280. /**
  281. Calculate the thumbnail size in order to fit all the thumnails in passed
  282. * dimensions.
  283. * NOTE: Here we assume that the remote and local thumbnails are with the
  284. * same height.
  285. * @param {int} availableWidth the maximum width for all thumbnails
  286. * @param {int} availableHeight the maximum height for all thumbnails
  287. * @returns {{localVideo, remoteVideo}}
  288. */
  289. calculateThumbnailSizeFromAvailable(availableWidth, availableHeight) {
  290. /**
  291. * Let:
  292. * lW - width of the local thumbnail
  293. * rW - width of the remote thumbnail
  294. * h - the height of the thumbnails
  295. * remoteRatio - width:height for the remote thumbnail
  296. * localRatio - width:height for the local thumbnail
  297. * numberRemoteThumbs - number of remote thumbnails (we have only one
  298. * local thumbnail)
  299. *
  300. * Since the height for local thumbnail = height for remote thumbnail
  301. * and we know the ratio (width:height) for the local and for the
  302. * remote thumbnail we can find rW/lW:
  303. * rW / remoteRatio = lW / localRatio then -
  304. * remoteLocalWidthRatio = rW / lW = remoteRatio / localRatio
  305. * and rW = lW * remoteRatio / localRatio = lW * remoteLocalWidthRatio
  306. * And the total width for the thumbnails is:
  307. * totalWidth = rW * numberRemoteThumbs + lW
  308. * = lW * remoteLocalWidthRatio * numberRemoteThumbs + lW =
  309. * lW * (remoteLocalWidthRatio * numberRemoteThumbs + 1)
  310. * and the h = lW/localRatio
  311. *
  312. * In order to fit all the thumbails in the area defined by
  313. * availableWidth * availableHeight we should check one of the
  314. * following options:
  315. * 1) if availableHeight == h - totalWidth should be less than
  316. * availableWidth
  317. * 2) if availableWidth == totalWidth - h should be less than
  318. * availableHeight
  319. *
  320. * 1) or 2) will be true and we are going to use it to calculate all
  321. * sizes.
  322. *
  323. * if 1) is true that means that
  324. * availableHeight/h > availableWidth/totalWidth otherwise 2) is true
  325. */
  326. const numberRemoteThumbs = this.getThumbs(true).remoteThumbs.length;
  327. const remoteLocalWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO /
  328. interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  329. const lW = Math.min(availableWidth /
  330. (remoteLocalWidthRatio * numberRemoteThumbs + 1), availableHeight *
  331. interfaceConfig.LOCAL_THUMBNAIL_RATIO);
  332. const h = lW / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  333. return {
  334. localVideo:{
  335. thumbWidth: lW,
  336. thumbHeight: h
  337. },
  338. remoteVideo: {
  339. thumbWidth: lW * remoteLocalWidthRatio,
  340. thumbHeight: h
  341. }
  342. };
  343. },
  344. /**
  345. * Resizes thumbnails
  346. * @param local
  347. * @param remote
  348. * @param animate
  349. * @param forceUpdate
  350. * @returns {Promise}
  351. */
  352. resizeThumbnails(local, remote, animate = false, forceUpdate = false) {
  353. return new Promise(resolve => {
  354. let thumbs = this.getThumbs(!forceUpdate);
  355. let promises = [];
  356. if(thumbs.localThumb) {
  357. promises.push(new Promise((resolve) => {
  358. thumbs.localThumb.animate({
  359. height: local.thumbHeight,
  360. width: local.thumbWidth
  361. }, this._getAnimateOptions(animate, resolve));
  362. }));
  363. }
  364. if(thumbs.remoteThumbs) {
  365. promises.push(new Promise((resolve) => {
  366. thumbs.remoteThumbs.animate({
  367. height: remote.thumbHeight,
  368. width: remote.thumbWidth
  369. }, this._getAnimateOptions(animate, resolve));
  370. }));
  371. }
  372. promises.push(new Promise((resolve) => {
  373. this.filmstrip.animate({
  374. // adds 2 px because of small video 1px border
  375. height: remote.thumbHeight + 2
  376. }, this._getAnimateOptions(animate, resolve));
  377. }));
  378. promises.push(new Promise(() => {
  379. let { localThumb } = this.getThumbs();
  380. let height = localThumb.height();
  381. let fontSize = UIUtil.getIndicatorFontSize(height);
  382. this.filmstrip.find('.indicator').animate({
  383. fontSize
  384. }, this._getAnimateOptions(animate, resolve));
  385. }));
  386. if (!animate) {
  387. resolve();
  388. }
  389. Promise.all(promises).then(resolve);
  390. });
  391. },
  392. /**
  393. * Helper method. Returns options for jQuery animation
  394. * @param animate {Boolean} - animation flag
  395. * @param cb {Function} - complete callback
  396. * @returns {Object} - animation options object
  397. * @private
  398. */
  399. _getAnimateOptions(animate, cb = $.noop) {
  400. return {
  401. queue: false,
  402. duration: animate ? 500 : 0,
  403. complete: cb
  404. };
  405. },
  406. /**
  407. * Returns thumbnails of the filmstrip
  408. * @param only_visible
  409. * @returns {object} thumbnails
  410. */
  411. getThumbs(only_visible = false) {
  412. let selector = 'span';
  413. if (only_visible) {
  414. selector += ':visible';
  415. }
  416. let localThumb = $("#localVideoContainer");
  417. let remoteThumbs = this.filmstripRemoteVideos.children(selector);
  418. // Exclude the local video container if it has been hidden.
  419. if (localThumb.hasClass("hidden")) {
  420. return { remoteThumbs };
  421. } else {
  422. return { remoteThumbs, localThumb };
  423. }
  424. }
  425. };
  426. export default Filmstrip;