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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 id="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. defaultButton: 1,
  86. submit: function(e,v,m,f){
  87. e.preventDefault();
  88. if(v)
  89. {
  90. var preziUrl = document.getElementById('preziUrl');
  91. if (preziUrl.value)
  92. {
  93. var urlValue
  94. = encodeURI(UIUtil.escapeHtml(preziUrl.value));
  95. if (urlValue.indexOf('http://prezi.com/') != 0
  96. && urlValue.indexOf('https://prezi.com/') != 0)
  97. {
  98. $.prompt.goToState('state1');
  99. return false;
  100. }
  101. else {
  102. var presIdTmp = urlValue.substring(
  103. urlValue.indexOf("prezi.com/") + 10);
  104. if (!isAlphanumeric(presIdTmp)
  105. || presIdTmp.indexOf('/') < 2) {
  106. $.prompt.goToState('state1');
  107. return false;
  108. }
  109. else {
  110. APP.xmpp.addToPresence("prezi", urlValue);
  111. $.prompt.close();
  112. }
  113. }
  114. }
  115. }
  116. else
  117. $.prompt.close();
  118. }
  119. },
  120. state1: {
  121. html: '<h2>' + html + '</h2>' +
  122. linkError,
  123. persistent: false,
  124. buttons: buttons1,
  125. defaultButton: 1,
  126. submit:function(e,v,m,f) {
  127. e.preventDefault();
  128. if(v==0)
  129. $.prompt.close();
  130. else
  131. $.prompt.goToState('state0');
  132. }
  133. }
  134. };
  135. var focusPreziUrl = function(e) {
  136. document.getElementById('preziUrl').focus();
  137. };
  138. messageHandler.openDialogWithStates(openPreziState, focusPreziUrl, focusPreziUrl);
  139. }
  140. }
  141. };
  142. /**
  143. * A new presentation has been added.
  144. *
  145. * @param event the event indicating the add of a presentation
  146. * @param jid the jid from which the presentation was added
  147. * @param presUrl url of the presentation
  148. * @param currentSlide the current slide to which we should move
  149. */
  150. function presentationAdded(event, jid, presUrl, currentSlide) {
  151. console.log("presentation added", presUrl);
  152. var presId = getPresentationId(presUrl);
  153. var elementId = 'participant_'
  154. + Strophe.getResourceFromJid(jid)
  155. + '_' + presId;
  156. // We explicitly don't specify the peer jid here, because we don't want
  157. // this video to be dealt with as a peer related one (for example we
  158. // don't want to show a mute/kick menu for this one, etc.).
  159. VideoLayout.addRemoteVideoContainer(null, elementId);
  160. VideoLayout.resizeThumbnails();
  161. var controlsEnabled = false;
  162. if (jid === APP.xmpp.myJid())
  163. controlsEnabled = true;
  164. setPresentationVisible(true);
  165. $('#largeVideoContainer').hover(
  166. function (event) {
  167. if (Prezi.isPresentationVisible()) {
  168. var reloadButtonRight = window.innerWidth
  169. - $('#presentation>iframe').offset().left
  170. - $('#presentation>iframe').width();
  171. $('#reloadPresentation').css({ right: reloadButtonRight,
  172. display:'inline-block'});
  173. }
  174. },
  175. function (event) {
  176. if (!Prezi.isPresentationVisible())
  177. $('#reloadPresentation').css({display:'none'});
  178. else {
  179. var e = event.toElement || event.relatedTarget;
  180. if (e && e.id != 'reloadPresentation' && e.id != 'header')
  181. $('#reloadPresentation').css({display:'none'});
  182. }
  183. });
  184. preziPlayer = new PreziPlayer(
  185. 'presentation',
  186. {preziId: presId,
  187. width: getPresentationWidth(),
  188. height: getPresentationHeihgt(),
  189. controls: controlsEnabled,
  190. debug: true
  191. });
  192. $('#presentation>iframe').attr('id', preziPlayer.options.preziId);
  193. preziPlayer.on(PreziPlayer.EVENT_STATUS, function(event) {
  194. console.log("prezi status", event.value);
  195. if (event.value == PreziPlayer.STATUS_CONTENT_READY) {
  196. if (jid != APP.xmpp.myJid())
  197. preziPlayer.flyToStep(currentSlide);
  198. }
  199. });
  200. preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function(event) {
  201. console.log("event value", event.value);
  202. APP.xmpp.addToPresence("preziSlide", event.value);
  203. });
  204. $("#" + elementId).css( 'background-image',
  205. 'url(../images/avatarprezi.png)');
  206. $("#" + elementId).click(
  207. function () {
  208. setPresentationVisible(true);
  209. }
  210. );
  211. };
  212. /**
  213. * A presentation has been removed.
  214. *
  215. * @param event the event indicating the remove of a presentation
  216. * @param jid the jid for which the presentation was removed
  217. * @param the url of the presentation
  218. */
  219. function presentationRemoved(event, jid, presUrl) {
  220. console.log('presentation removed', presUrl);
  221. var presId = getPresentationId(presUrl);
  222. setPresentationVisible(false);
  223. $('#participant_'
  224. + Strophe.getResourceFromJid(jid)
  225. + '_' + presId).remove();
  226. $('#presentation>iframe').remove();
  227. if (preziPlayer != null) {
  228. preziPlayer.destroy();
  229. preziPlayer = null;
  230. }
  231. };
  232. /**
  233. * Indicates if the given string is an alphanumeric string.
  234. * Note that some special characters are also allowed (-, _ , /, &, ?, =, ;) for the
  235. * purpose of checking URIs.
  236. */
  237. function isAlphanumeric(unsafeText) {
  238. var regex = /^[a-z0-9-_\/&\?=;]+$/i;
  239. return regex.test(unsafeText);
  240. }
  241. /**
  242. * Returns the presentation id from the given url.
  243. */
  244. function getPresentationId (presUrl) {
  245. var presIdTmp = presUrl.substring(presUrl.indexOf("prezi.com/") + 10);
  246. return presIdTmp.substring(0, presIdTmp.indexOf('/'));
  247. }
  248. /**
  249. * Returns the presentation width.
  250. */
  251. function getPresentationWidth() {
  252. var availableWidth = UIUtil.getAvailableVideoWidth();
  253. var availableHeight = getPresentationHeihgt();
  254. var aspectRatio = 16.0 / 9.0;
  255. if (availableHeight < availableWidth / aspectRatio) {
  256. availableWidth = Math.floor(availableHeight * aspectRatio);
  257. }
  258. return availableWidth;
  259. }
  260. /**
  261. * Returns the presentation height.
  262. */
  263. function getPresentationHeihgt() {
  264. var remoteVideos = $('#remoteVideos');
  265. return window.innerHeight - remoteVideos.outerHeight();
  266. }
  267. /**
  268. * Resizes the presentation iframe.
  269. */
  270. function resize() {
  271. if ($('#presentation>iframe')) {
  272. $('#presentation>iframe').width(getPresentationWidth());
  273. $('#presentation>iframe').height(getPresentationHeihgt());
  274. }
  275. }
  276. /**
  277. * Shows/hides a presentation.
  278. */
  279. function setPresentationVisible(visible) {
  280. var prezi = $('#presentation>iframe');
  281. if (visible) {
  282. // Trigger the video.selected event to indicate a change in the
  283. // large video.
  284. $(document).trigger("video.selected", [true]);
  285. $('#largeVideo').fadeOut(300);
  286. prezi.fadeIn(300, function() {
  287. prezi.css({opacity:'1'});
  288. ToolbarToggler.dockToolbar(true);
  289. VideoLayout.setLargeVideoVisible(false);
  290. });
  291. $('#activeSpeaker').css('visibility', 'hidden');
  292. }
  293. else {
  294. if (prezi.css('opacity') == '1') {
  295. prezi.fadeOut(300, function () {
  296. prezi.css({opacity:'0'});
  297. $('#reloadPresentation').css({display:'none'});
  298. $('#largeVideo').fadeIn(300, function() {
  299. VideoLayout.setLargeVideoVisible(true);
  300. ToolbarToggler.dockToolbar(false);
  301. });
  302. });
  303. }
  304. }
  305. }
  306. /**
  307. * Presentation has been removed.
  308. */
  309. $(document).bind('presentationremoved.muc', presentationRemoved);
  310. /**
  311. * Presentation has been added.
  312. */
  313. $(document).bind('presentationadded.muc', presentationAdded);
  314. /*
  315. * Indicates presentation slide change.
  316. */
  317. $(document).bind('gotoslide.muc', function (event, jid, presUrl, current) {
  318. if (preziPlayer && preziPlayer.getCurrentStep() != current) {
  319. preziPlayer.flyToStep(current);
  320. var animationStepsArray = preziPlayer.getAnimationCountOnSteps();
  321. for (var i = 0; i < parseInt(animationStepsArray[current]); i++) {
  322. preziPlayer.flyToStep(current, i);
  323. }
  324. }
  325. });
  326. /**
  327. * On video selected event.
  328. */
  329. $(document).bind('video.selected', function (event, isPresentation) {
  330. if (!isPresentation && $('#presentation>iframe')) {
  331. setPresentationVisible(false);
  332. }
  333. });
  334. $(window).resize(function () {
  335. resize();
  336. });
  337. module.exports = Prezi;