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

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