Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Filmstrip.js 17KB

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