Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Filmstrip.js 16KB

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