Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Filmstrip.js 18KB

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