Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Filmstrip.js 20KB

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