您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Filmstrip.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. const toolbarContainerHTML = this._generateToolbarHTML();
  31. const className = this.filmstripContainerClassName;
  32. const container = document.querySelector(`.${className}`);
  33. UIUtil.prependChild(container, toolbarContainerHTML);
  34. const 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. const container = document.createElement('div');
  44. const 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. const shortcut = 'F';
  72. const shortcutAttr = 'filmstripPopover';
  73. const 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. const 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. const 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. const 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. // eslint-disable-next-line no-param-reassign
  125. visible = this.isFilmstripVisible();
  126. } else if (this.isFilmstripVisible() === visible) {
  127. return;
  128. }
  129. if (sendAnalytics) {
  130. sendEvent('toolbar.filmstrip.toggled');
  131. }
  132. this.filmstrip.toggleClass('hidden');
  133. if (visible) {
  134. this.showMenuUpIcon();
  135. } else {
  136. this.showMenuDownIcon();
  137. }
  138. // Emit/fire UIEvents.TOGGLED_FILMSTRIP.
  139. const eventEmitter = this.eventEmitter;
  140. const isFilmstripVisible = this.isFilmstripVisible();
  141. if (eventEmitter) {
  142. eventEmitter.emit(
  143. UIEvents.TOGGLED_FILMSTRIP,
  144. this.isFilmstripVisible());
  145. }
  146. APP.store.dispatch(setFilmstripVisibility(isFilmstripVisible));
  147. },
  148. /**
  149. * Shows if filmstrip is visible
  150. * @returns {boolean}
  151. */
  152. isFilmstripVisible() {
  153. return !this.filmstrip.hasClass('hidden');
  154. },
  155. /**
  156. * Adjusts styles for filmstrip-only mode.
  157. */
  158. setFilmstripOnly() {
  159. this.filmstrip.addClass('filmstrip__videos-filmstripOnly');
  160. },
  161. /**
  162. * Returns the height of filmstrip
  163. * @returns {number} height
  164. */
  165. getFilmstripHeight() {
  166. // FIXME Make it more clear the getFilmstripHeight check is used in
  167. // horizontal film strip mode for calculating how tall large video
  168. // display should be.
  169. if (this.isFilmstripVisible() && !interfaceConfig.VERTICAL_FILMSTRIP) {
  170. return $(`.${this.filmstripContainerClassName}`).outerHeight();
  171. }
  172. return 0;
  173. },
  174. /**
  175. * Calculates the size for thumbnails: local and remote one
  176. * @returns {*|{localVideo, remoteVideo}}
  177. */
  178. calculateThumbnailSize() {
  179. const availableSizes = this.calculateAvailableSize();
  180. const width = availableSizes.availableWidth;
  181. const 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. const thumbs = this.getThumbs(true);
  193. const numvids = thumbs.remoteThumbs.length;
  194. const 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. const 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. const 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. const 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,
  257. availableHeight };
  258. },
  259. /**
  260. * Traverse all elements inside the filmstrip
  261. * and calculates the sum of all of them except
  262. * remote videos element. Used for calculation of
  263. * available width for video thumbnails.
  264. *
  265. * @returns {number} calculated width
  266. * @private
  267. */
  268. _getFilmstripExtraPanelsWidth() {
  269. const className = this.filmstripContainerClassName;
  270. let width = 0;
  271. $(`.${className}`)
  272. .children()
  273. .each(function() {
  274. /* eslint-disable no-invalid-this */
  275. if (this.id !== 'remoteVideos') {
  276. width += $(this).outerWidth();
  277. }
  278. /* eslint-enable no-invalid-this */
  279. });
  280. return width;
  281. },
  282. /**
  283. Calculate the thumbnail size in order to fit all the thumnails in passed
  284. * dimensions.
  285. * NOTE: Here we assume that the remote and local thumbnails are with the
  286. * same height.
  287. * @param {int} availableWidth the maximum width for all thumbnails
  288. * @param {int} availableHeight the maximum height for all thumbnails
  289. * @returns {{localVideo, remoteVideo}}
  290. */
  291. calculateThumbnailSizeFromAvailable(availableWidth, availableHeight) {
  292. /**
  293. * Let:
  294. * lW - width of the local thumbnail
  295. * rW - width of the remote thumbnail
  296. * h - the height of the thumbnails
  297. * remoteRatio - width:height for the remote thumbnail
  298. * localRatio - width:height for the local thumbnail
  299. * remoteThumbsInRow - number of remote thumbnails in a row (we have
  300. * only one local thumbnail) next to the local thumbnail. In vertical
  301. * filmstrip mode, this will always be 0.
  302. *
  303. * Since the height for local thumbnail = height for remote thumbnail
  304. * and we know the ratio (width:height) for the local and for the
  305. * remote thumbnail we can find rW/lW:
  306. * rW / remoteRatio = lW / localRatio then -
  307. * remoteLocalWidthRatio = rW / lW = remoteRatio / localRatio
  308. * and rW = lW * remoteRatio / localRatio = lW * remoteLocalWidthRatio
  309. * And the total width for the thumbnails is:
  310. * totalWidth = rW * remoteThumbsInRow + lW
  311. * = lW * remoteLocalWidthRatio * remoteThumbsInRow + lW =
  312. * lW * (remoteLocalWidthRatio * remoteThumbsInRow + 1)
  313. * and the h = lW/localRatio
  314. *
  315. * In order to fit all the thumbails in the area defined by
  316. * availableWidth * availableHeight we should check one of the
  317. * following options:
  318. * 1) if availableHeight == h - totalWidth should be less than
  319. * availableWidth
  320. * 2) if availableWidth == totalWidth - h should be less than
  321. * availableHeight
  322. *
  323. * 1) or 2) will be true and we are going to use it to calculate all
  324. * sizes.
  325. *
  326. * if 1) is true that means that
  327. * availableHeight/h > availableWidth/totalWidth otherwise 2) is true
  328. */
  329. const remoteThumbsInRow = interfaceConfig.VERTICAL_FILMSTRIP
  330. ? 0 : this.getThumbs(true).remoteThumbs.length;
  331. const remoteLocalWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO
  332. / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  333. const lW = Math.min(availableWidth
  334. / ((remoteLocalWidthRatio * remoteThumbsInRow) + 1), availableHeight
  335. * interfaceConfig.LOCAL_THUMBNAIL_RATIO);
  336. const h = lW / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  337. const remoteVideoWidth = lW * remoteLocalWidthRatio;
  338. let localVideo;
  339. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  340. localVideo = {
  341. thumbWidth: remoteVideoWidth,
  342. thumbHeight: h * remoteLocalWidthRatio
  343. };
  344. } else {
  345. localVideo = {
  346. thumbWidth: lW,
  347. thumbHeight: h
  348. };
  349. }
  350. return {
  351. localVideo,
  352. remoteVideo: {
  353. thumbWidth: remoteVideoWidth,
  354. thumbHeight: h
  355. }
  356. };
  357. },
  358. /**
  359. * Resizes thumbnails
  360. * @param local
  361. * @param remote
  362. * @param animate
  363. * @param forceUpdate
  364. * @returns {Promise}
  365. */
  366. // eslint-disable-next-line max-params
  367. resizeThumbnails(local, remote, animate = false, forceUpdate = false) {
  368. return new Promise(resolve => {
  369. const thumbs = this.getThumbs(!forceUpdate);
  370. const promises = [];
  371. if (thumbs.localThumb) {
  372. // eslint-disable-next-line no-shadow
  373. promises.push(new Promise(resolve => {
  374. thumbs.localThumb.animate({
  375. height: local.thumbHeight,
  376. width: local.thumbWidth
  377. }, this._getAnimateOptions(animate, resolve));
  378. }));
  379. }
  380. if (thumbs.remoteThumbs) {
  381. // eslint-disable-next-line no-shadow
  382. promises.push(new Promise(resolve => {
  383. thumbs.remoteThumbs.animate({
  384. height: remote.thumbHeight,
  385. width: remote.thumbWidth
  386. }, this._getAnimateOptions(animate, resolve));
  387. }));
  388. }
  389. // eslint-disable-next-line no-shadow
  390. promises.push(new Promise(resolve => {
  391. // Let CSS take care of height in vertical filmstrip mode.
  392. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  393. $('#filmstripLocalVideo').animate({
  394. // adds 4 px because of small video 2px border
  395. width: local.thumbWidth + 4
  396. }, this._getAnimateOptions(animate, resolve));
  397. } else {
  398. this.filmstrip.animate({
  399. // adds 4 px because of small video 2px border
  400. height: remote.thumbHeight + 4
  401. }, this._getAnimateOptions(animate, resolve));
  402. }
  403. }));
  404. promises.push(new Promise(() => {
  405. const { localThumb } = this.getThumbs();
  406. const height = localThumb ? localThumb.height() : 0;
  407. const fontSize = UIUtil.getIndicatorFontSize(height);
  408. this.filmstrip.find('.indicator').animate({
  409. fontSize
  410. }, this._getAnimateOptions(animate, resolve));
  411. }));
  412. if (!animate) {
  413. resolve();
  414. }
  415. Promise.all(promises).then(resolve);
  416. });
  417. },
  418. /**
  419. * Helper method. Returns options for jQuery animation
  420. * @param animate {Boolean} - animation flag
  421. * @param cb {Function} - complete callback
  422. * @returns {Object} - animation options object
  423. * @private
  424. */
  425. _getAnimateOptions(animate, cb = $.noop) {
  426. return {
  427. queue: false,
  428. duration: animate ? 500 : 0,
  429. complete: cb
  430. };
  431. },
  432. /**
  433. * Returns thumbnails of the filmstrip
  434. * @param onlyVisible
  435. * @returns {object} thumbnails
  436. */
  437. getThumbs(onlyVisible = false) {
  438. let selector = 'span';
  439. if (onlyVisible) {
  440. selector += ':visible';
  441. }
  442. const localThumb = $('#localVideoContainer');
  443. const remoteThumbs = this.filmstripRemoteVideos.children(selector);
  444. // Exclude the local video container if it has been hidden.
  445. if (localThumb.hasClass('hidden')) {
  446. return { remoteThumbs };
  447. }
  448. return { remoteThumbs,
  449. localThumb };
  450. }
  451. };
  452. export default Filmstrip;