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

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