您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Filmstrip.js 18KB

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