選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Filmstrip.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* global $, APP, JitsiMeetJS, interfaceConfig */
  2. import { setFilmstripVisibility } from '../../../react/features/filmstrip';
  3. import UIEvents from "../../../service/UI/UIEvents";
  4. import UIUtil from "../util/UIUtil";
  5. const Filmstrip = {
  6. /**
  7. *
  8. * @param eventEmitter the {EventEmitter} through which {Filmstrip} is to
  9. * emit/fire {UIEvents} (such as {UIEvents.TOGGLED_FILMSTRIP}).
  10. */
  11. init (eventEmitter) {
  12. this.iconMenuDownClassName = 'icon-menu-down';
  13. this.iconMenuUpClassName = 'icon-menu-up';
  14. this.filmstripContainerClassName = 'filmstrip';
  15. this.filmstrip = $('#remoteVideos');
  16. this.filmstripRemoteVideos = $('#filmstripRemoteVideosContainer');
  17. this.eventEmitter = eventEmitter;
  18. // Show the toggle button and add event listeners only when out of
  19. // filmstrip only mode.
  20. if (!interfaceConfig.filmStripOnly) {
  21. this._initFilmstripToolbar();
  22. this.registerListeners();
  23. }
  24. },
  25. /**
  26. * Initializes the filmstrip toolbar.
  27. */
  28. _initFilmstripToolbar() {
  29. let toolbarContainerHTML = this._generateToolbarHTML();
  30. let className = this.filmstripContainerClassName;
  31. let container = document.querySelector(`.${className}`);
  32. UIUtil.prependChild(container, toolbarContainerHTML);
  33. let iconSelector = '#toggleFilmstripButton i';
  34. this.toggleFilmstripIcon = document.querySelector(iconSelector);
  35. },
  36. /**
  37. * Generates HTML layout for filmstrip toggle button and wrapping container.
  38. * @returns {HTMLElement}
  39. * @private
  40. */
  41. _generateToolbarHTML() {
  42. let container = document.createElement('div');
  43. let isVisible = this.isFilmstripVisible();
  44. container.className = 'filmstrip__toolbar';
  45. container.innerHTML = `
  46. <button id="toggleFilmstripButton">
  47. <i class="icon-menu-${isVisible ? 'down' : 'up'}">
  48. </i>
  49. </button>
  50. `;
  51. return container;
  52. },
  53. /**
  54. * Attach 'click' listener to "hide filmstrip" button
  55. */
  56. registerListeners() {
  57. // Important:
  58. // Firing the event instead of executing toggleFilmstrip method because
  59. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  60. // to correctly resize the video area.
  61. $('#toggleFilmstripButton').on('click',
  62. () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP));
  63. this._registerToggleFilmstripShortcut();
  64. },
  65. /**
  66. * Registering toggle filmstrip shortcut
  67. * @private
  68. */
  69. _registerToggleFilmstripShortcut() {
  70. let shortcut = 'F';
  71. let shortcutAttr = 'filmstripPopover';
  72. let description = 'keyboardShortcuts.toggleFilmstrip';
  73. // Important:
  74. // Firing the event instead of executing toggleFilmstrip method because
  75. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  76. // to correctly resize the video area.
  77. let handler = () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP);
  78. APP.keyboardshortcut.registerShortcut(
  79. shortcut,
  80. shortcutAttr,
  81. handler,
  82. description
  83. );
  84. },
  85. /**
  86. * Changes classes of icon for showing down state
  87. */
  88. showMenuDownIcon() {
  89. let icon = this.toggleFilmstripIcon;
  90. if(icon) {
  91. icon.classList.add(this.iconMenuDownClassName);
  92. icon.classList.remove(this.iconMenuUpClassName);
  93. }
  94. },
  95. /**
  96. * Changes classes of icon for showing up state
  97. */
  98. showMenuUpIcon() {
  99. let icon = this.toggleFilmstripIcon;
  100. if(icon) {
  101. icon.classList.add(this.iconMenuUpClassName);
  102. icon.classList.remove(this.iconMenuDownClassName);
  103. }
  104. },
  105. /**
  106. * Toggles the visibility of the filmstrip.
  107. *
  108. * @param visible optional {Boolean} which specifies the desired visibility
  109. * of the filmstrip. If not specified, the visibility will be flipped
  110. * (i.e. toggled); otherwise, the visibility will be set to the specified
  111. * value.
  112. * @param {Boolean} sendAnalytics - True to send an analytics event. The
  113. * default value is true.
  114. *
  115. * Note:
  116. * This method shouldn't be executed directly to hide the filmstrip.
  117. * It's important to hide the filmstrip with UI.toggleFilmstrip in order
  118. * to correctly resize the video area.
  119. */
  120. toggleFilmstrip(visible, sendAnalytics = true) {
  121. const isVisibleDefined = typeof visible === 'boolean';
  122. if (!isVisibleDefined) {
  123. visible = this.isFilmstripVisible();
  124. } else if (this.isFilmstripVisible() === visible) {
  125. return;
  126. }
  127. if (sendAnalytics) {
  128. JitsiMeetJS.analytics.sendEvent('toolbar.filmstrip.toggled');
  129. }
  130. this.filmstrip.toggleClass("hidden");
  131. if (visible) {
  132. this.showMenuUpIcon();
  133. } else {
  134. this.showMenuDownIcon();
  135. }
  136. // Emit/fire UIEvents.TOGGLED_FILMSTRIP.
  137. const eventEmitter = this.eventEmitter;
  138. const isFilmstripVisible = this.isFilmstripVisible();
  139. if (eventEmitter) {
  140. eventEmitter.emit(
  141. UIEvents.TOGGLED_FILMSTRIP,
  142. this.isFilmstripVisible());
  143. }
  144. APP.store.dispatch(setFilmstripVisibility(isFilmstripVisible));
  145. },
  146. /**
  147. * Shows if filmstrip is visible
  148. * @returns {boolean}
  149. */
  150. isFilmstripVisible() {
  151. return !this.filmstrip.hasClass('hidden');
  152. },
  153. /**
  154. * Adjusts styles for filmstrip-only mode.
  155. */
  156. setFilmstripOnly() {
  157. this.filmstrip.addClass('filmstrip__videos-filmstripOnly');
  158. },
  159. /**
  160. * Returns the height of filmstrip
  161. * @returns {number} height
  162. */
  163. getFilmstripHeight() {
  164. // FIXME Make it more clear the getFilmstripHeight check is used in
  165. // horizontal film strip mode for calculating how tall large video
  166. // display should be.
  167. if (this.isFilmstripVisible() && !interfaceConfig.VERTICAL_FILMSTRIP) {
  168. return $(`.${this.filmstripContainerClassName}`).outerHeight();
  169. } else {
  170. return 0;
  171. }
  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. * filmstrip 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 or we're in vertical
  228. // filmstrip mode we don't need to calculate further any adjustments
  229. // to width based on the number of videos present.
  230. if (numvids && !interfaceConfig.VERTICAL_FILMSTRIP) {
  231. let remoteVideoContainer = thumbs.remoteThumbs.eq(0);
  232. availableWidth = Math.floor(
  233. (videoAreaAvailableWidth - numvids * (
  234. UIUtil.parseCssInt(
  235. remoteVideoContainer.css('borderLeftWidth'), 10)
  236. + UIUtil.parseCssInt(
  237. remoteVideoContainer.css('borderRightWidth'), 10)
  238. + UIUtil.parseCssInt(
  239. remoteVideoContainer.css('paddingLeft'), 10)
  240. + UIUtil.parseCssInt(
  241. remoteVideoContainer.css('paddingRight'), 10)
  242. + UIUtil.parseCssInt(
  243. remoteVideoContainer.css('marginLeft'), 10)
  244. + UIUtil.parseCssInt(
  245. remoteVideoContainer.css('marginRight'), 10)))
  246. );
  247. }
  248. let maxHeight
  249. // If the MAX_HEIGHT property hasn't been specified
  250. // we have the static value.
  251. = Math.min(interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120,
  252. availableHeight);
  253. availableHeight
  254. = Math.min(maxHeight, window.innerHeight - 18);
  255. return { availableWidth, availableHeight };
  256. },
  257. /**
  258. * Traverse all elements inside the filmstrip
  259. * and calculates the sum of all of them except
  260. * remote videos element. Used for calculation of
  261. * available width for video thumbnails.
  262. *
  263. * @returns {number} calculated width
  264. * @private
  265. */
  266. _getFilmstripExtraPanelsWidth() {
  267. let className = this.filmstripContainerClassName;
  268. let width = 0;
  269. $(`.${className}`)
  270. .children()
  271. .each(function () {
  272. if (this.id !== 'remoteVideos') {
  273. width += $(this).outerWidth();
  274. }
  275. });
  276. return width;
  277. },
  278. /**
  279. Calculate the thumbnail size in order to fit all the thumnails in passed
  280. * dimensions.
  281. * NOTE: Here we assume that the remote and local thumbnails are with the
  282. * same height.
  283. * @param {int} availableWidth the maximum width for all thumbnails
  284. * @param {int} availableHeight the maximum height for all thumbnails
  285. * @returns {{localVideo, remoteVideo}}
  286. */
  287. calculateThumbnailSizeFromAvailable(availableWidth, availableHeight) {
  288. /**
  289. * Let:
  290. * lW - width of the local thumbnail
  291. * rW - width of the remote thumbnail
  292. * h - the height of the thumbnails
  293. * remoteRatio - width:height for the remote thumbnail
  294. * localRatio - width:height for the local thumbnail
  295. * remoteThumbsInRow - number of remote thumbnails in a row (we have
  296. * only one local thumbnail) next to the local thumbnail. In vertical
  297. * filmstrip mode, this will always be 0.
  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 * remoteThumbsInRow + lW
  307. * = lW * remoteLocalWidthRatio * remoteThumbsInRow + lW =
  308. * lW * (remoteLocalWidthRatio * remoteThumbsInRow + 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 remoteThumbsInRow = interfaceConfig.VERTICAL_FILMSTRIP
  326. ? 0 : this.getThumbs(true).remoteThumbs.length;
  327. const remoteLocalWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO /
  328. interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  329. const lW = Math.min(availableWidth /
  330. (remoteLocalWidthRatio * remoteThumbsInRow + 1), availableHeight *
  331. interfaceConfig.LOCAL_THUMBNAIL_RATIO);
  332. const h = lW / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  333. const remoteVideoWidth = lW * remoteLocalWidthRatio;
  334. let localVideo;
  335. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  336. localVideo = {
  337. thumbWidth: remoteVideoWidth,
  338. thumbHeight: h * remoteLocalWidthRatio
  339. };
  340. } else {
  341. localVideo = {
  342. thumbWidth: lW,
  343. thumbHeight: h
  344. };
  345. }
  346. return {
  347. localVideo,
  348. remoteVideo: {
  349. thumbWidth: remoteVideoWidth,
  350. thumbHeight: h
  351. }
  352. };
  353. },
  354. /**
  355. * Resizes thumbnails
  356. * @param local
  357. * @param remote
  358. * @param animate
  359. * @param forceUpdate
  360. * @returns {Promise}
  361. */
  362. resizeThumbnails(local, remote, animate = false, forceUpdate = false) {
  363. return new Promise(resolve => {
  364. let thumbs = this.getThumbs(!forceUpdate);
  365. let promises = [];
  366. if(thumbs.localThumb) {
  367. promises.push(new Promise((resolve) => {
  368. thumbs.localThumb.animate({
  369. height: local.thumbHeight,
  370. width: local.thumbWidth
  371. }, this._getAnimateOptions(animate, resolve));
  372. }));
  373. }
  374. if(thumbs.remoteThumbs) {
  375. promises.push(new Promise((resolve) => {
  376. thumbs.remoteThumbs.animate({
  377. height: remote.thumbHeight,
  378. width: remote.thumbWidth
  379. }, this._getAnimateOptions(animate, resolve));
  380. }));
  381. }
  382. promises.push(new Promise((resolve) => {
  383. // Let CSS take care of height in vertical filmstrip mode.
  384. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  385. $('#filmstripLocalVideo').animate({
  386. // adds 4 px because of small video 2px border
  387. width: local.thumbWidth + 4
  388. }, this._getAnimateOptions(animate, resolve));
  389. } else {
  390. this.filmstrip.animate({
  391. // adds 4 px because of small video 2px border
  392. height: remote.thumbHeight + 4
  393. }, this._getAnimateOptions(animate, resolve));
  394. }
  395. }));
  396. promises.push(new Promise(() => {
  397. let { localThumb } = this.getThumbs();
  398. let height = localThumb ? localThumb.height() : 0;
  399. let fontSize = UIUtil.getIndicatorFontSize(height);
  400. this.filmstrip.find('.indicator').animate({
  401. fontSize
  402. }, this._getAnimateOptions(animate, resolve));
  403. }));
  404. if (!animate) {
  405. resolve();
  406. }
  407. Promise.all(promises).then(resolve);
  408. });
  409. },
  410. /**
  411. * Helper method. Returns options for jQuery animation
  412. * @param animate {Boolean} - animation flag
  413. * @param cb {Function} - complete callback
  414. * @returns {Object} - animation options object
  415. * @private
  416. */
  417. _getAnimateOptions(animate, cb = $.noop) {
  418. return {
  419. queue: false,
  420. duration: animate ? 500 : 0,
  421. complete: cb
  422. };
  423. },
  424. /**
  425. * Returns thumbnails of the filmstrip
  426. * @param only_visible
  427. * @returns {object} thumbnails
  428. */
  429. getThumbs(only_visible = false) {
  430. let selector = 'span';
  431. if (only_visible) {
  432. selector += ':visible';
  433. }
  434. let localThumb = $("#localVideoContainer");
  435. let remoteThumbs = this.filmstripRemoteVideos.children(selector);
  436. // Exclude the local video container if it has been hidden.
  437. if (localThumb.hasClass("hidden")) {
  438. return { remoteThumbs };
  439. } else {
  440. return { remoteThumbs, localThumb };
  441. }
  442. }
  443. };
  444. export default Filmstrip;