Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Filmstrip.js 18KB

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