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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* global $, APP, interfaceConfig */
  2. import { setFilmstripVisibility } from '../../../react/features/filmstrip';
  3. import UIEvents from "../../../service/UI/UIEvents";
  4. import UIUtil from "../util/UIUtil";
  5. import { sendEvent } from '../../../react/features/analytics';
  6. const Filmstrip = {
  7. /**
  8. *
  9. * @param eventEmitter the {EventEmitter} through which {Filmstrip} is to
  10. * emit/fire {UIEvents} (such as {UIEvents.TOGGLED_FILMSTRIP}).
  11. */
  12. init (eventEmitter) {
  13. this.iconMenuDownClassName = 'icon-menu-down';
  14. this.iconMenuUpClassName = 'icon-menu-up';
  15. this.filmstripContainerClassName = 'filmstrip';
  16. this.filmstrip = $('#remoteVideos');
  17. this.filmstripRemoteVideos = $('#filmstripRemoteVideosContainer');
  18. this.eventEmitter = eventEmitter;
  19. // Show the toggle button and add event listeners only when out of
  20. // filmstrip only mode.
  21. if (!interfaceConfig.filmStripOnly) {
  22. this._initFilmstripToolbar();
  23. this.registerListeners();
  24. }
  25. },
  26. /**
  27. * Initializes the filmstrip toolbar.
  28. */
  29. _initFilmstripToolbar() {
  30. let toolbarContainerHTML = this._generateToolbarHTML();
  31. let className = this.filmstripContainerClassName;
  32. let container = document.querySelector(`.${className}`);
  33. UIUtil.prependChild(container, toolbarContainerHTML);
  34. let iconSelector = '#toggleFilmstripButton i';
  35. this.toggleFilmstripIcon = document.querySelector(iconSelector);
  36. },
  37. /**
  38. * Generates HTML layout for filmstrip toggle button and wrapping container.
  39. * @returns {HTMLElement}
  40. * @private
  41. */
  42. _generateToolbarHTML() {
  43. let container = document.createElement('div');
  44. let isVisible = this.isFilmstripVisible();
  45. container.className = 'filmstrip__toolbar';
  46. container.innerHTML = `
  47. <button id="toggleFilmstripButton">
  48. <i class="icon-menu-${isVisible ? 'down' : 'up'}">
  49. </i>
  50. </button>
  51. `;
  52. return container;
  53. },
  54. /**
  55. * Attach 'click' listener to "hide filmstrip" button
  56. */
  57. registerListeners() {
  58. // Important:
  59. // Firing the event instead of executing toggleFilmstrip method because
  60. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  61. // to correctly resize the video area.
  62. $('#toggleFilmstripButton').on('click',
  63. () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP));
  64. this._registerToggleFilmstripShortcut();
  65. },
  66. /**
  67. * Registering toggle filmstrip shortcut
  68. * @private
  69. */
  70. _registerToggleFilmstripShortcut() {
  71. let shortcut = 'F';
  72. let shortcutAttr = 'filmstripPopover';
  73. let description = 'keyboardShortcuts.toggleFilmstrip';
  74. // Important:
  75. // Firing the event instead of executing toggleFilmstrip method because
  76. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  77. // to correctly resize the video area.
  78. let handler = () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP);
  79. APP.keyboardshortcut.registerShortcut(
  80. shortcut,
  81. shortcutAttr,
  82. handler,
  83. description
  84. );
  85. },
  86. /**
  87. * Changes classes of icon for showing down state
  88. */
  89. showMenuDownIcon() {
  90. let icon = this.toggleFilmstripIcon;
  91. if(icon) {
  92. icon.classList.add(this.iconMenuDownClassName);
  93. icon.classList.remove(this.iconMenuUpClassName);
  94. }
  95. },
  96. /**
  97. * Changes classes of icon for showing up state
  98. */
  99. showMenuUpIcon() {
  100. let icon = this.toggleFilmstripIcon;
  101. if(icon) {
  102. icon.classList.add(this.iconMenuUpClassName);
  103. icon.classList.remove(this.iconMenuDownClassName);
  104. }
  105. },
  106. /**
  107. * Toggles the visibility of the filmstrip.
  108. *
  109. * @param visible optional {Boolean} which specifies the desired visibility
  110. * of the filmstrip. If not specified, the visibility will be flipped
  111. * (i.e. toggled); otherwise, the visibility will be set to the specified
  112. * value.
  113. * @param {Boolean} sendAnalytics - True to send an analytics event. The
  114. * default value is true.
  115. *
  116. * Note:
  117. * This method shouldn't be executed directly to hide the filmstrip.
  118. * It's important to hide the filmstrip with UI.toggleFilmstrip in order
  119. * to correctly resize the video area.
  120. */
  121. toggleFilmstrip(visible, sendAnalytics = true) {
  122. const isVisibleDefined = typeof visible === 'boolean';
  123. if (!isVisibleDefined) {
  124. visible = this.isFilmstripVisible();
  125. } else if (this.isFilmstripVisible() === visible) {
  126. return;
  127. }
  128. if (sendAnalytics) {
  129. sendEvent('toolbar.filmstrip.toggled');
  130. }
  131. this.filmstrip.toggleClass("hidden");
  132. if (visible) {
  133. this.showMenuUpIcon();
  134. } else {
  135. this.showMenuDownIcon();
  136. }
  137. // Emit/fire UIEvents.TOGGLED_FILMSTRIP.
  138. const eventEmitter = this.eventEmitter;
  139. const isFilmstripVisible = this.isFilmstripVisible();
  140. if (eventEmitter) {
  141. eventEmitter.emit(
  142. UIEvents.TOGGLED_FILMSTRIP,
  143. this.isFilmstripVisible());
  144. }
  145. APP.store.dispatch(setFilmstripVisibility(isFilmstripVisible));
  146. },
  147. /**
  148. * Shows if filmstrip is visible
  149. * @returns {boolean}
  150. */
  151. isFilmstripVisible() {
  152. return !this.filmstrip.hasClass('hidden');
  153. },
  154. /**
  155. * Adjusts styles for filmstrip-only mode.
  156. */
  157. setFilmstripOnly() {
  158. this.filmstrip.addClass('filmstrip__videos-filmstripOnly');
  159. },
  160. /**
  161. * Returns the height of filmstrip
  162. * @returns {number} height
  163. */
  164. getFilmstripHeight() {
  165. // FIXME Make it more clear the getFilmstripHeight check is used in
  166. // horizontal film strip mode for calculating how tall large video
  167. // display should be.
  168. if (this.isFilmstripVisible() && !interfaceConfig.VERTICAL_FILMSTRIP) {
  169. return $(`.${this.filmstripContainerClassName}`).outerHeight();
  170. } else {
  171. return 0;
  172. }
  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 or we're in vertical
  229. // filmstrip mode we don't need to calculate further any adjustments
  230. // to width based on the number of videos present.
  231. if (numvids && !interfaceConfig.VERTICAL_FILMSTRIP) {
  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. * remoteThumbsInRow - number of remote thumbnails in a row (we have
  297. * only one local thumbnail) next to the local thumbnail. In vertical
  298. * filmstrip mode, this will always be 0.
  299. *
  300. * Since the height for local thumbnail = height for remote thumbnail
  301. * and we know the ratio (width:height) for the local and for the
  302. * remote thumbnail we can find rW/lW:
  303. * rW / remoteRatio = lW / localRatio then -
  304. * remoteLocalWidthRatio = rW / lW = remoteRatio / localRatio
  305. * and rW = lW * remoteRatio / localRatio = lW * remoteLocalWidthRatio
  306. * And the total width for the thumbnails is:
  307. * totalWidth = rW * remoteThumbsInRow + lW
  308. * = lW * remoteLocalWidthRatio * remoteThumbsInRow + lW =
  309. * lW * (remoteLocalWidthRatio * remoteThumbsInRow + 1)
  310. * and the h = lW/localRatio
  311. *
  312. * In order to fit all the thumbails in the area defined by
  313. * availableWidth * availableHeight we should check one of the
  314. * following options:
  315. * 1) if availableHeight == h - totalWidth should be less than
  316. * availableWidth
  317. * 2) if availableWidth == totalWidth - h should be less than
  318. * availableHeight
  319. *
  320. * 1) or 2) will be true and we are going to use it to calculate all
  321. * sizes.
  322. *
  323. * if 1) is true that means that
  324. * availableHeight/h > availableWidth/totalWidth otherwise 2) is true
  325. */
  326. const remoteThumbsInRow = interfaceConfig.VERTICAL_FILMSTRIP
  327. ? 0 : this.getThumbs(true).remoteThumbs.length;
  328. const remoteLocalWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO /
  329. interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  330. const lW = Math.min(availableWidth /
  331. (remoteLocalWidthRatio * remoteThumbsInRow + 1), availableHeight *
  332. interfaceConfig.LOCAL_THUMBNAIL_RATIO);
  333. const h = lW / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  334. const remoteVideoWidth = lW * remoteLocalWidthRatio;
  335. let localVideo;
  336. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  337. localVideo = {
  338. thumbWidth: remoteVideoWidth,
  339. thumbHeight: h * remoteLocalWidthRatio
  340. };
  341. } else {
  342. localVideo = {
  343. thumbWidth: lW,
  344. thumbHeight: h
  345. };
  346. }
  347. return {
  348. localVideo,
  349. remoteVideo: {
  350. thumbWidth: remoteVideoWidth,
  351. thumbHeight: h
  352. }
  353. };
  354. },
  355. /**
  356. * Resizes thumbnails
  357. * @param local
  358. * @param remote
  359. * @param animate
  360. * @param forceUpdate
  361. * @returns {Promise}
  362. */
  363. resizeThumbnails(local, remote, animate = false, forceUpdate = false) {
  364. return new Promise(resolve => {
  365. let thumbs = this.getThumbs(!forceUpdate);
  366. let promises = [];
  367. if(thumbs.localThumb) {
  368. promises.push(new Promise((resolve) => {
  369. thumbs.localThumb.animate({
  370. height: local.thumbHeight,
  371. width: local.thumbWidth
  372. }, this._getAnimateOptions(animate, resolve));
  373. }));
  374. }
  375. if(thumbs.remoteThumbs) {
  376. promises.push(new Promise((resolve) => {
  377. thumbs.remoteThumbs.animate({
  378. height: remote.thumbHeight,
  379. width: remote.thumbWidth
  380. }, this._getAnimateOptions(animate, resolve));
  381. }));
  382. }
  383. promises.push(new Promise((resolve) => {
  384. // Let CSS take care of height in vertical filmstrip mode.
  385. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  386. $('#filmstripLocalVideo').animate({
  387. // adds 4 px because of small video 2px border
  388. width: local.thumbWidth + 4
  389. }, this._getAnimateOptions(animate, resolve));
  390. } else {
  391. this.filmstrip.animate({
  392. // adds 4 px because of small video 2px border
  393. height: remote.thumbHeight + 4
  394. }, this._getAnimateOptions(animate, resolve));
  395. }
  396. }));
  397. promises.push(new Promise(() => {
  398. let { localThumb } = this.getThumbs();
  399. let height = localThumb ? localThumb.height() : 0;
  400. let fontSize = UIUtil.getIndicatorFontSize(height);
  401. this.filmstrip.find('.indicator').animate({
  402. fontSize
  403. }, this._getAnimateOptions(animate, resolve));
  404. }));
  405. if (!animate) {
  406. resolve();
  407. }
  408. Promise.all(promises).then(resolve);
  409. });
  410. },
  411. /**
  412. * Helper method. Returns options for jQuery animation
  413. * @param animate {Boolean} - animation flag
  414. * @param cb {Function} - complete callback
  415. * @returns {Object} - animation options object
  416. * @private
  417. */
  418. _getAnimateOptions(animate, cb = $.noop) {
  419. return {
  420. queue: false,
  421. duration: animate ? 500 : 0,
  422. complete: cb
  423. };
  424. },
  425. /**
  426. * Returns thumbnails of the filmstrip
  427. * @param only_visible
  428. * @returns {object} thumbnails
  429. */
  430. getThumbs(only_visible = false) {
  431. let selector = 'span';
  432. if (only_visible) {
  433. selector += ':visible';
  434. }
  435. let localThumb = $("#localVideoContainer");
  436. let remoteThumbs = this.filmstripRemoteVideos.children(selector);
  437. // Exclude the local video container if it has been hidden.
  438. if (localThumb.hasClass("hidden")) {
  439. return { remoteThumbs };
  440. } else {
  441. return { remoteThumbs, localThumb };
  442. }
  443. }
  444. };
  445. export default Filmstrip;