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

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