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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. var ToolbarToggler = require("../toolbars/ToolbarToggler");
  2. var UIUtil = require("../util/UIUtil");
  3. var VideoLayout = require("../videolayout/VideoLayout");
  4. var messageHandler = require("../util/MessageHandler");
  5. var PreziPlayer = require("./PreziPlayer");
  6. var preziPlayer = null;
  7. var Prezi = {
  8. /**
  9. * Reloads the current presentation.
  10. */
  11. reloadPresentation: function() {
  12. var iframe = document.getElementById(preziPlayer.options.preziId);
  13. iframe.src = iframe.src;
  14. },
  15. /**
  16. * Returns <tt>true</tt> if the presentation is visible, <tt>false</tt> -
  17. * otherwise.
  18. */
  19. isPresentationVisible: function () {
  20. return ($('#presentation>iframe') != null
  21. && $('#presentation>iframe').css('opacity') == 1);
  22. },
  23. /**
  24. * Opens the Prezi dialog, from which the user could choose a presentation
  25. * to load.
  26. */
  27. openPreziDialog: function() {
  28. var myprezi = APP.xmpp.getPrezi();
  29. if (myprezi) {
  30. messageHandler.openTwoButtonDialog("dialog.removePreziTitle",
  31. null,
  32. "dialog.removePreziMsg",
  33. null,
  34. false,
  35. "dialog.Remove",
  36. function(e,v,m,f) {
  37. if(v) {
  38. APP.xmpp.removePreziFromPresence();
  39. }
  40. }
  41. );
  42. }
  43. else if (preziPlayer != null) {
  44. messageHandler.openTwoButtonDialog("dialog.sharePreziTitle",
  45. null, "dialog.sharePreziMsg",
  46. null,
  47. false,
  48. "dialog.Ok",
  49. function(e,v,m,f) {
  50. $.prompt.close();
  51. }
  52. );
  53. }
  54. else {
  55. var html = APP.translation.generateTranslatonHTML(
  56. "dialog.sharePreziTitle");
  57. var cancelButton = APP.translation.generateTranslatonHTML(
  58. "dialog.Cancel");
  59. var shareButton = APP.translation.generateTranslatonHTML(
  60. "dialog.Share");
  61. var backButton = APP.translation.generateTranslatonHTML(
  62. "dialog.Back");
  63. var buttons = [];
  64. var buttons1 = [];
  65. // Cancel button to both states
  66. buttons.push({title: cancelButton, value: false});
  67. buttons1.push({title: cancelButton, value: false});
  68. // Share button
  69. buttons.push({title: shareButton, value: true});
  70. // Back button
  71. buttons1.push({title: backButton, value: true});
  72. var linkError = APP.translation.generateTranslatonHTML(
  73. "dialog.preziLinkError");
  74. var defaultUrl = APP.translation.translateString("defaultPreziLink",
  75. {url: "http://prezi.com/wz7vhjycl7e6/my-prezi"});
  76. var openPreziState = {
  77. state0: {
  78. html: '<h2>' + html + '</h2>' +
  79. '<input name="preziUrl" type="text" ' +
  80. 'data-i18n="[placeholder]defaultPreziLink" data-i18n-options=\'' +
  81. JSON.stringify({"url": "http://prezi.com/wz7vhjycl7e6/my-prezi"}) +
  82. '\' placeholder="' + defaultUrl + '" autofocus>',
  83. persistent: false,
  84. buttons: buttons,
  85. focus: ':input:first',
  86. defaultButton: 0,
  87. submit: function (e, v, m, f) {
  88. e.preventDefault();
  89. if(v)
  90. {
  91. var preziUrl = f.preziUrl;
  92. if (preziUrl)
  93. {
  94. var urlValue
  95. = encodeURI(UIUtil.escapeHtml(preziUrl));
  96. if (urlValue.indexOf('http://prezi.com/') != 0
  97. && urlValue.indexOf('https://prezi.com/') != 0)
  98. {
  99. $.prompt.goToState('state1');
  100. return false;
  101. }
  102. else {
  103. var presIdTmp = urlValue.substring(
  104. urlValue.indexOf("prezi.com/") + 10);
  105. if (!isAlphanumeric(presIdTmp)
  106. || presIdTmp.indexOf('/') < 2) {
  107. $.prompt.goToState('state1');
  108. return false;
  109. }
  110. else {
  111. APP.xmpp.addToPresence("prezi", urlValue);
  112. $.prompt.close();
  113. }
  114. }
  115. }
  116. }
  117. else
  118. $.prompt.close();
  119. }
  120. },
  121. state1: {
  122. html: '<h2>' + html + '</h2>' +
  123. linkError,
  124. persistent: false,
  125. buttons: buttons1,
  126. focus: ':input:first',
  127. defaultButton: 1,
  128. submit: function (e, v, m, f) {
  129. e.preventDefault();
  130. if (v === 0)
  131. $.prompt.close();
  132. else
  133. $.prompt.goToState('state0');
  134. }
  135. }
  136. };
  137. messageHandler.openDialogWithStates(openPreziState);
  138. }
  139. }
  140. };
  141. /**
  142. * A new presentation has been added.
  143. *
  144. * @param event the event indicating the add of a presentation
  145. * @param jid the jid from which the presentation was added
  146. * @param presUrl url of the presentation
  147. * @param currentSlide the current slide to which we should move
  148. */
  149. function presentationAdded(event, jid, presUrl, currentSlide) {
  150. console.log("presentation added", presUrl);
  151. var presId = getPresentationId(presUrl);
  152. var elementId = 'participant_'
  153. + Strophe.getResourceFromJid(jid)
  154. + '_' + presId;
  155. // We explicitly don't specify the peer jid here, because we don't want
  156. // this video to be dealt with as a peer related one (for example we
  157. // don't want to show a mute/kick menu for this one, etc.).
  158. VideoLayout.addRemoteVideoContainer(null, elementId);
  159. VideoLayout.resizeThumbnails();
  160. var controlsEnabled = false;
  161. if (jid === APP.xmpp.myJid())
  162. controlsEnabled = true;
  163. setPresentationVisible(true);
  164. $('#largeVideoContainer').hover(
  165. function (event) {
  166. if (Prezi.isPresentationVisible()) {
  167. var reloadButtonRight = window.innerWidth
  168. - $('#presentation>iframe').offset().left
  169. - $('#presentation>iframe').width();
  170. $('#reloadPresentation').css({ right: reloadButtonRight,
  171. display:'inline-block'});
  172. }
  173. },
  174. function (event) {
  175. if (!Prezi.isPresentationVisible())
  176. $('#reloadPresentation').css({display:'none'});
  177. else {
  178. var e = event.toElement || event.relatedTarget;
  179. if (e && e.id != 'reloadPresentation' && e.id != 'header')
  180. $('#reloadPresentation').css({display:'none'});
  181. }
  182. });
  183. preziPlayer = new PreziPlayer(
  184. 'presentation',
  185. {preziId: presId,
  186. width: getPresentationWidth(),
  187. height: getPresentationHeihgt(),
  188. controls: controlsEnabled,
  189. debug: true
  190. });
  191. $('#presentation>iframe').attr('id', preziPlayer.options.preziId);
  192. preziPlayer.on(PreziPlayer.EVENT_STATUS, function(event) {
  193. console.log("prezi status", event.value);
  194. if (event.value == PreziPlayer.STATUS_CONTENT_READY) {
  195. if (jid != APP.xmpp.myJid())
  196. preziPlayer.flyToStep(currentSlide);
  197. }
  198. });
  199. preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function(event) {
  200. console.log("event value", event.value);
  201. APP.xmpp.addToPresence("preziSlide", event.value);
  202. });
  203. $("#" + elementId).css( 'background-image',
  204. 'url(../images/avatarprezi.png)');
  205. $("#" + elementId).click(
  206. function () {
  207. setPresentationVisible(true);
  208. }
  209. );
  210. };
  211. /**
  212. * A presentation has been removed.
  213. *
  214. * @param event the event indicating the remove of a presentation
  215. * @param jid the jid for which the presentation was removed
  216. * @param the url of the presentation
  217. */
  218. function presentationRemoved(event, jid, presUrl) {
  219. console.log('presentation removed', presUrl);
  220. var presId = getPresentationId(presUrl);
  221. setPresentationVisible(false);
  222. $('#participant_'
  223. + Strophe.getResourceFromJid(jid)
  224. + '_' + presId).remove();
  225. $('#presentation>iframe').remove();
  226. if (preziPlayer != null) {
  227. preziPlayer.destroy();
  228. preziPlayer = null;
  229. }
  230. };
  231. /**
  232. * Indicates if the given string is an alphanumeric string.
  233. * Note that some special characters are also allowed (-, _ , /, &, ?, =, ;) for the
  234. * purpose of checking URIs.
  235. */
  236. function isAlphanumeric(unsafeText) {
  237. var regex = /^[a-z0-9-_\/&\?=;]+$/i;
  238. return regex.test(unsafeText);
  239. }
  240. /**
  241. * Returns the presentation id from the given url.
  242. */
  243. function getPresentationId (presUrl) {
  244. var presIdTmp = presUrl.substring(presUrl.indexOf("prezi.com/") + 10);
  245. return presIdTmp.substring(0, presIdTmp.indexOf('/'));
  246. }
  247. /**
  248. * Returns the presentation width.
  249. */
  250. function getPresentationWidth() {
  251. var availableWidth = UIUtil.getAvailableVideoWidth();
  252. var availableHeight = getPresentationHeihgt();
  253. var aspectRatio = 16.0 / 9.0;
  254. if (availableHeight < availableWidth / aspectRatio) {
  255. availableWidth = Math.floor(availableHeight * aspectRatio);
  256. }
  257. return availableWidth;
  258. }
  259. /**
  260. * Returns the presentation height.
  261. */
  262. function getPresentationHeihgt() {
  263. var remoteVideos = $('#remoteVideos');
  264. return window.innerHeight - remoteVideos.outerHeight();
  265. }
  266. /**
  267. * Resizes the presentation iframe.
  268. */
  269. function resize() {
  270. if ($('#presentation>iframe')) {
  271. $('#presentation>iframe').width(getPresentationWidth());
  272. $('#presentation>iframe').height(getPresentationHeihgt());
  273. }
  274. }
  275. /**
  276. * Shows/hides a presentation.
  277. */
  278. function setPresentationVisible(visible) {
  279. var prezi = $('#presentation>iframe');
  280. if (visible) {
  281. // Trigger the video.selected event to indicate a change in the
  282. // large video.
  283. $(document).trigger("video.selected", [true]);
  284. $('#largeVideo').fadeOut(300);
  285. prezi.fadeIn(300, function() {
  286. prezi.css({opacity:'1'});
  287. ToolbarToggler.dockToolbar(true);
  288. VideoLayout.setLargeVideoVisible(false);
  289. });
  290. $('#activeSpeaker').css('visibility', 'hidden');
  291. }
  292. else {
  293. if (prezi.css('opacity') == '1') {
  294. prezi.fadeOut(300, function () {
  295. prezi.css({opacity:'0'});
  296. $('#reloadPresentation').css({display:'none'});
  297. $('#largeVideo').fadeIn(300, function() {
  298. VideoLayout.setLargeVideoVisible(true);
  299. ToolbarToggler.dockToolbar(false);
  300. });
  301. });
  302. }
  303. }
  304. }
  305. /**
  306. * Presentation has been removed.
  307. */
  308. $(document).bind('presentationremoved.muc', presentationRemoved);
  309. /**
  310. * Presentation has been added.
  311. */
  312. $(document).bind('presentationadded.muc', presentationAdded);
  313. /*
  314. * Indicates presentation slide change.
  315. */
  316. $(document).bind('gotoslide.muc', function (event, jid, presUrl, current) {
  317. if (preziPlayer && preziPlayer.getCurrentStep() != current) {
  318. preziPlayer.flyToStep(current);
  319. var animationStepsArray = preziPlayer.getAnimationCountOnSteps();
  320. for (var i = 0; i < parseInt(animationStepsArray[current]); i++) {
  321. preziPlayer.flyToStep(current, i);
  322. }
  323. }
  324. });
  325. /**
  326. * On video selected event.
  327. */
  328. $(document).bind('video.selected', function (event, isPresentation) {
  329. if (!isPresentation && $('#presentation>iframe')) {
  330. setPresentationVisible(false);
  331. }
  332. });
  333. $(window).resize(function () {
  334. resize();
  335. });
  336. module.exports = Prezi;