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.

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