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