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

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