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

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