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

FilmStrip.js 14KB

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