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.

FilmStrip.js 16KB

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