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

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