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.

Prezi.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* global $, APP */
  2. /* jshint -W101 */
  3. import VideoLayout from "../videolayout/VideoLayout";
  4. import LargeContainer from '../videolayout/LargeContainer';
  5. import PreziPlayer from './PreziPlayer';
  6. import UIUtil from '../util/UIUtil';
  7. import UIEvents from '../../../service/UI/UIEvents';
  8. import messageHandler from '../util/MessageHandler';
  9. import ToolbarToggler from "../toolbars/ToolbarToggler";
  10. import SidePanelToggler from "../side_pannels/SidePanelToggler";
  11. import FilmStrip from '../videolayout/FilmStrip';
  12. /**
  13. * Example of Prezi link.
  14. */
  15. const defaultPreziLink = "http://prezi.com/wz7vhjycl7e6/my-prezi";
  16. const alphanumRegex = /^[a-z0-9-_\/&\?=;]+$/i;
  17. /**
  18. * Default aspect ratio for Prezi frame.
  19. */
  20. const aspectRatio = 16.0 / 9.0;
  21. /**
  22. * Default Prezi frame width.
  23. */
  24. const DEFAULT_WIDTH = 640;
  25. /**
  26. * Default Prezi frame height.
  27. */
  28. const DEFAULT_HEIGHT = 480;
  29. /**
  30. * Indicates if the given string is an alphanumeric string.
  31. * Note that some special characters are also allowed (-, _ , /, &, ?, =, ;) for the
  32. * purpose of checking URIs.
  33. * @param {string} unsafeText string to check
  34. * @returns {boolean}
  35. */
  36. function isAlphanumeric(unsafeText) {
  37. return alphanumRegex.test(unsafeText);
  38. }
  39. /**
  40. * Returns the presentation id from the given url.
  41. * @param {string} url Prezi link
  42. * @returns {string} presentation id
  43. */
  44. function getPresentationId (url) {
  45. let presId = url.substring(url.indexOf("prezi.com/") + 10);
  46. return presId.substring(0, presId.indexOf('/'));
  47. }
  48. /**
  49. * Checks if given string is Prezi url.
  50. * @param {string} url string to check.
  51. * @returns {boolean}
  52. */
  53. function isPreziLink(url) {
  54. if (url.indexOf('http://prezi.com/') !== 0 && url.indexOf('https://prezi.com/') !== 0) {
  55. return false;
  56. }
  57. let presId = url.substring(url.indexOf("prezi.com/") + 10);
  58. if (!isAlphanumeric(presId) || presId.indexOf('/') < 2) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. /**
  64. * Notify user that other user if already sharing Prezi.
  65. */
  66. function notifyOtherIsSharingPrezi() {
  67. messageHandler.openMessageDialog(
  68. "dialog.sharePreziTitle",
  69. "dialog.sharePreziMsg"
  70. );
  71. }
  72. /**
  73. * Ask user if he want to close Prezi he's sharing.
  74. */
  75. function proposeToClosePrezi() {
  76. return new Promise(function (resolve, reject) {
  77. messageHandler.openTwoButtonDialog(
  78. "dialog.removePreziTitle",
  79. null,
  80. "dialog.removePreziMsg",
  81. null,
  82. false,
  83. "dialog.Remove",
  84. function(e,v,m,f) {
  85. if (v) {
  86. resolve();
  87. } else {
  88. reject();
  89. }
  90. }
  91. );
  92. });
  93. }
  94. /**
  95. * Ask user for Prezi url to share with others.
  96. * Dialog validates client input to allow only Prezi urls.
  97. */
  98. function requestPreziLink() {
  99. const title = APP.translation.generateTranslationHTML("dialog.sharePreziTitle");
  100. const cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel");
  101. const shareButton = APP.translation.generateTranslationHTML("dialog.Share");
  102. const backButton = APP.translation.generateTranslationHTML("dialog.Back");
  103. const linkError = APP.translation.generateTranslationHTML("dialog.preziLinkError");
  104. const i18nOptions = {url: defaultPreziLink};
  105. const defaultUrl = APP.translation.translateString(
  106. "defaultPreziLink", i18nOptions
  107. );
  108. return new Promise(function (resolve, reject) {
  109. let dialog = messageHandler.openDialogWithStates({
  110. state0: {
  111. html: `
  112. <h2>${title}</h2>
  113. <input name="preziUrl" type="text"
  114. data-i18n="[placeholder]defaultPreziLink"
  115. data-i18n-options="${JSON.stringify(i18nOptions)}"
  116. placeholder="${defaultUrl}" autofocus>`,
  117. persistent: false,
  118. buttons: [
  119. {title: cancelButton, value: false},
  120. {title: shareButton, value: true}
  121. ],
  122. focus: ':input:first',
  123. defaultButton: 1,
  124. submit: function (e, v, m, f) {
  125. e.preventDefault();
  126. if (!v) {
  127. reject('cancelled');
  128. dialog.close();
  129. return;
  130. }
  131. let preziUrl = f.preziUrl;
  132. if (!preziUrl) {
  133. return;
  134. }
  135. let urlValue = encodeURI(UIUtil.escapeHtml(preziUrl));
  136. if (!isPreziLink(urlValue)) {
  137. dialog.goToState('state1');
  138. return false;
  139. }
  140. resolve(urlValue);
  141. dialog.close();
  142. }
  143. },
  144. state1: {
  145. html: `<h2>${title}</h2> ${linkError}`,
  146. persistent: false,
  147. buttons: [
  148. {title: cancelButton, value: false},
  149. {title: backButton, value: true}
  150. ],
  151. focus: ':input:first',
  152. defaultButton: 1,
  153. submit: function (e, v, m, f) {
  154. e.preventDefault();
  155. if (v === 0) {
  156. reject();
  157. dialog.close();
  158. } else {
  159. dialog.goToState('state0');
  160. }
  161. }
  162. }
  163. });
  164. });
  165. }
  166. export const PreziContainerType = "prezi";
  167. /**
  168. * Container for Prezi iframe.
  169. */
  170. class PreziContainer extends LargeContainer {
  171. constructor ({preziId, isMy, slide, onSlideChanged}) {
  172. super();
  173. this.reloadBtn = $('#reloadPresentation');
  174. let preziPlayer = new PreziPlayer(
  175. 'presentation', {
  176. preziId,
  177. width: DEFAULT_WIDTH,
  178. height: DEFAULT_HEIGHT,
  179. controls: isMy,
  180. debug: true
  181. }
  182. );
  183. this.preziPlayer = preziPlayer;
  184. this.$iframe = $(preziPlayer.iframe);
  185. this.$iframe.attr('id', preziId);
  186. preziPlayer.on(PreziPlayer.EVENT_STATUS, function({value}) {
  187. console.log("prezi status", value);
  188. if (value == PreziPlayer.STATUS_CONTENT_READY && !isMy) {
  189. preziPlayer.flyToStep(slide);
  190. }
  191. });
  192. preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function({value}) {
  193. console.log("event value", value);
  194. onSlideChanged(value);
  195. });
  196. }
  197. /**
  198. * Change Prezi slide.
  199. * @param {number} slide slide to show
  200. */
  201. goToSlide (slide) {
  202. if (this.preziPlayer.getCurrentStep() === slide) {
  203. return;
  204. }
  205. this.preziPlayer.flyToStep(slide);
  206. let animationStepsArray = this.preziPlayer.getAnimationCountOnSteps();
  207. if (!animationStepsArray) {
  208. return;
  209. }
  210. for (var i = 0; i < parseInt(animationStepsArray[slide]); i += 1) {
  211. this.preziPlayer.flyToStep(slide, i);
  212. }
  213. }
  214. /**
  215. * Show or hide "reload presentation" button.
  216. * @param {boolean} show
  217. */
  218. showReloadBtn (show) {
  219. this.reloadBtn.css('display', show ? 'inline-block' : 'none');
  220. }
  221. show () {
  222. return new Promise(resolve => {
  223. this.$iframe.fadeIn(300, () => {
  224. this.$iframe.css({opacity: 1});
  225. ToolbarToggler.dockToolbar(true);
  226. resolve();
  227. });
  228. });
  229. }
  230. hide () {
  231. return new Promise(resolve => {
  232. this.$iframe.fadeOut(300, () => {
  233. this.$iframe.css({opacity: 0});
  234. this.showReloadBtn(false);
  235. ToolbarToggler.dockToolbar(false);
  236. resolve();
  237. });
  238. });
  239. }
  240. onHoverIn () {
  241. let rightOffset = window.innerWidth - this.$iframe.offset().left - this.$iframe.width();
  242. this.showReloadBtn(true);
  243. this.reloadBtn.css('right', rightOffset);
  244. }
  245. onHoverOut (event) {
  246. let e = event.toElement || event.relatedTarget;
  247. if (e && e.id != 'reloadPresentation' && e.id != 'header') {
  248. this.showReloadBtn(false);
  249. }
  250. }
  251. resize (containerWidth, containerHeight) {
  252. let height = containerHeight - FilmStrip.getFilmStripHeight();
  253. let width = containerWidth;
  254. if (height < width / aspectRatio) {
  255. width = Math.floor(height * aspectRatio);
  256. }
  257. this.$iframe.width(width).height(height);
  258. }
  259. /**
  260. * Close Prezi frame.
  261. */
  262. close () {
  263. this.showReloadBtn(false);
  264. this.preziPlayer.destroy();
  265. this.$iframe.remove();
  266. }
  267. }
  268. /**
  269. * Manager of Prezi frames.
  270. */
  271. export default class PreziManager {
  272. constructor (emitter) {
  273. this.emitter = emitter;
  274. this.userId = null;
  275. this.url = null;
  276. this.prezi = null;
  277. $("#reloadPresentationLink").click(this.reloadPresentation.bind(this));
  278. }
  279. get isPresenting () {
  280. return !!this.userId;
  281. }
  282. get isMyPrezi () {
  283. return this.userId === APP.conference.localId;
  284. }
  285. /**
  286. * Check if user is currently sharing.
  287. * @param {string} id user id to check for
  288. */
  289. isSharing (id) {
  290. return this.userId === id;
  291. }
  292. handlePreziButtonClicked () {
  293. if (!this.isPresenting) {
  294. requestPreziLink().then(
  295. url => this.emitter.emit(UIEvents.SHARE_PREZI, url, 0),
  296. err => console.error('PREZI CANCELED', err)
  297. );
  298. return;
  299. }
  300. if (this.isMyPrezi) {
  301. proposeToClosePrezi().then(() => this.emitter.emit(UIEvents.STOP_SHARING_PREZI));
  302. } else {
  303. notifyOtherIsSharingPrezi();
  304. }
  305. }
  306. /**
  307. * Reload current Prezi frame.
  308. */
  309. reloadPresentation () {
  310. if (!this.prezi) {
  311. return;
  312. }
  313. let iframe = this.prezi.$iframe[0];
  314. iframe.src = iframe.src;
  315. }
  316. /**
  317. * Show Prezi. Create new Prezi if there is no Prezi yet.
  318. * @param {string} id owner id
  319. * @param {string} url Prezi url
  320. * @param {number} slide slide to show
  321. */
  322. showPrezi (id, url, slide) {
  323. if (!this.isPresenting) {
  324. this.createPrezi(id, url, slide);
  325. }
  326. if (this.userId === id && this.url === url) {
  327. this.prezi.goToSlide(slide);
  328. } else {
  329. console.error(this.userId, id);
  330. console.error(this.url, url);
  331. throw new Error("unexpected presentation change");
  332. }
  333. }
  334. /**
  335. * Create new Prezi frame..
  336. * @param {string} id owner id
  337. * @param {string} url Prezi url
  338. * @param {number} slide slide to show
  339. */
  340. createPrezi (id, url, slide) {
  341. console.log("presentation added", url);
  342. this.userId = id;
  343. this.url = url;
  344. let preziId = getPresentationId(url);
  345. let elementId = `participant_${id}_${preziId}`;
  346. this.$thumb = $(VideoLayout.addRemoteVideoContainer(elementId));
  347. VideoLayout.resizeThumbnails();
  348. this.$thumb.css({
  349. 'background-image': 'url(../images/avatarprezi.png)'
  350. }).click(() => VideoLayout.showLargeVideoContainer(PreziContainerType, true));
  351. this.prezi = new PreziContainer({
  352. preziId,
  353. isMy: this.isMyPrezi,
  354. slide,
  355. onSlideChanged: newSlide => {
  356. if (this.isMyPrezi) {
  357. this.emitter.emit(UIEvents.SHARE_PREZI, url, newSlide);
  358. }
  359. }
  360. });
  361. VideoLayout.addLargeVideoContainer(PreziContainerType, this.prezi);
  362. VideoLayout.showLargeVideoContainer(PreziContainerType, true);
  363. }
  364. /**
  365. * Close Prezi.
  366. * @param {string} id owner id
  367. */
  368. removePrezi (id) {
  369. if (this.userId !== id) {
  370. throw new Error(`cannot close presentation from ${this.userId} instead of ${id}`);
  371. }
  372. this.$thumb.remove();
  373. this.$thumb = null;
  374. // wait until Prezi is hidden, then remove it
  375. VideoLayout.showLargeVideoContainer(PreziContainerType, false).then(() => {
  376. console.log("presentation removed", this.url);
  377. VideoLayout.removeLargeVideoContainer(PreziContainerType);
  378. this.userId = null;
  379. this.url = null;
  380. this.prezi.close();
  381. this.prezi = null;
  382. });
  383. }
  384. }