You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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