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

FilmStrip.js 16KB

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