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

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