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

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