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

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