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

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