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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var ToolbarToggler = require("../toolbars/ToolbarToggler");
  17. var UIUtil = require("../util/UIUtil");
  18. var VideoLayout = require("../videolayout/VideoLayout");
  19. var messageHandler = require("../util/MessageHandler");
  20. var PreziPlayer = require("./PreziPlayer");
  21. var preziPlayer = null;
  22. var Prezi = {
  23. /**
  24. * Reloads the current presentation.
  25. */
  26. reloadPresentation: function() {
  27. var iframe = document.getElementById(preziPlayer.options.preziId);
  28. iframe.src = iframe.src;
  29. },
  30. /**
  31. * Returns <tt>true</tt> if the presentation is visible, <tt>false</tt> -
  32. * otherwise.
  33. */
  34. isPresentationVisible: function () {
  35. return ($('#presentation>iframe') != null
  36. && $('#presentation>iframe').css('opacity') == 1);
  37. },
  38. /**
  39. * Opens the Prezi dialog, from which the user could choose a presentation
  40. * to load.
  41. */
  42. openPreziDialog: function() {
  43. var myprezi = APP.xmpp.getPrezi();
  44. if (myprezi) {
  45. messageHandler.openTwoButtonDialog("dialog.removePreziTitle",
  46. null,
  47. "dialog.removePreziMsg",
  48. null,
  49. false,
  50. "dialog.Remove",
  51. function(e,v,m,f) {
  52. if(v) {
  53. APP.xmpp.removePreziFromPresence();
  54. }
  55. }
  56. );
  57. }
  58. else if (preziPlayer != null) {
  59. messageHandler.openTwoButtonDialog("dialog.sharePreziTitle",
  60. null, "dialog.sharePreziMsg",
  61. null,
  62. false,
  63. "dialog.Ok",
  64. function(e,v,m,f) {
  65. $.prompt.close();
  66. }
  67. );
  68. }
  69. else {
  70. var html = APP.translation.generateTranslatonHTML(
  71. "dialog.sharePreziTitle");
  72. var cancelButton = APP.translation.generateTranslatonHTML(
  73. "dialog.Cancel");
  74. var shareButton = APP.translation.generateTranslatonHTML(
  75. "dialog.Share");
  76. var backButton = APP.translation.generateTranslatonHTML(
  77. "dialog.Back");
  78. var buttons = [];
  79. var buttons1 = [];
  80. // Cancel button to both states
  81. buttons.push({title: cancelButton, value: false});
  82. buttons1.push({title: cancelButton, value: false});
  83. // Share button
  84. buttons.push({title: shareButton, value: true});
  85. // Back button
  86. buttons1.push({title: backButton, value: true});
  87. var linkError = APP.translation.generateTranslatonHTML(
  88. "dialog.preziLinkError");
  89. var defaultUrl = APP.translation.translateString("defaultPreziLink",
  90. {url: "http://prezi.com/wz7vhjycl7e6/my-prezi"});
  91. var openPreziState = {
  92. state0: {
  93. html: '<h2>' + html + '</h2>' +
  94. '<input name="preziUrl" type="text" ' +
  95. 'data-i18n="[placeholder]defaultPreziLink" data-i18n-options=\'' +
  96. JSON.stringify({"url": "http://prezi.com/wz7vhjycl7e6/my-prezi"}) +
  97. '\' placeholder="' + defaultUrl + '" autofocus>',
  98. persistent: false,
  99. buttons: buttons,
  100. focus: ':input:first',
  101. defaultButton: 0,
  102. submit: function (e, v, m, f) {
  103. e.preventDefault();
  104. if(v)
  105. {
  106. var preziUrl = f.preziUrl;
  107. if (preziUrl)
  108. {
  109. var urlValue
  110. = encodeURI(UIUtil.escapeHtml(preziUrl));
  111. if (urlValue.indexOf('http://prezi.com/') != 0
  112. && urlValue.indexOf('https://prezi.com/') != 0)
  113. {
  114. $.prompt.goToState('state1');
  115. return false;
  116. }
  117. else {
  118. var presIdTmp = urlValue.substring(
  119. urlValue.indexOf("prezi.com/") + 10);
  120. if (!isAlphanumeric(presIdTmp)
  121. || presIdTmp.indexOf('/') < 2) {
  122. $.prompt.goToState('state1');
  123. return false;
  124. }
  125. else {
  126. APP.xmpp.addToPresence("prezi", urlValue);
  127. $.prompt.close();
  128. }
  129. }
  130. }
  131. }
  132. else
  133. $.prompt.close();
  134. }
  135. },
  136. state1: {
  137. html: '<h2>' + html + '</h2>' +
  138. linkError,
  139. persistent: false,
  140. buttons: buttons1,
  141. focus: ':input:first',
  142. defaultButton: 1,
  143. submit: function (e, v, m, f) {
  144. e.preventDefault();
  145. if (v === 0)
  146. $.prompt.close();
  147. else
  148. $.prompt.goToState('state0');
  149. }
  150. }
  151. };
  152. messageHandler.openDialogWithStates(openPreziState);
  153. }
  154. }
  155. };
  156. /**
  157. * A new presentation has been added.
  158. *
  159. * @param event the event indicating the add of a presentation
  160. * @param jid the jid from which the presentation was added
  161. * @param presUrl url of the presentation
  162. * @param currentSlide the current slide to which we should move
  163. */
  164. function presentationAdded(event, jid, presUrl, currentSlide) {
  165. console.log("presentation added", presUrl);
  166. var presId = getPresentationId(presUrl);
  167. var elementId = 'participant_'
  168. + Strophe.getResourceFromJid(jid)
  169. + '_' + presId;
  170. // We explicitly don't specify the peer jid here, because we don't want
  171. // this video to be dealt with as a peer related one (for example we
  172. // don't want to show a mute/kick menu for this one, etc.).
  173. VideoLayout.addRemoteVideoContainer(null, elementId);
  174. VideoLayout.resizeThumbnails();
  175. var controlsEnabled = false;
  176. if (jid === APP.xmpp.myJid())
  177. controlsEnabled = true;
  178. setPresentationVisible(true);
  179. $('#largeVideoContainer').hover(
  180. function (event) {
  181. if (Prezi.isPresentationVisible()) {
  182. var reloadButtonRight = window.innerWidth
  183. - $('#presentation>iframe').offset().left
  184. - $('#presentation>iframe').width();
  185. $('#reloadPresentation').css({ right: reloadButtonRight,
  186. display:'inline-block'});
  187. }
  188. },
  189. function (event) {
  190. if (!Prezi.isPresentationVisible())
  191. $('#reloadPresentation').css({display:'none'});
  192. else {
  193. var e = event.toElement || event.relatedTarget;
  194. if (e && e.id != 'reloadPresentation' && e.id != 'header')
  195. $('#reloadPresentation').css({display:'none'});
  196. }
  197. });
  198. preziPlayer = new PreziPlayer(
  199. 'presentation',
  200. {preziId: presId,
  201. width: getPresentationWidth(),
  202. height: getPresentationHeihgt(),
  203. controls: controlsEnabled,
  204. debug: true
  205. });
  206. $('#presentation>iframe').attr('id', preziPlayer.options.preziId);
  207. preziPlayer.on(PreziPlayer.EVENT_STATUS, function(event) {
  208. console.log("prezi status", event.value);
  209. if (event.value == PreziPlayer.STATUS_CONTENT_READY) {
  210. if (jid != APP.xmpp.myJid())
  211. preziPlayer.flyToStep(currentSlide);
  212. }
  213. });
  214. preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function(event) {
  215. console.log("event value", event.value);
  216. APP.xmpp.addToPresence("preziSlide", event.value);
  217. });
  218. $("#" + elementId).css( 'background-image',
  219. 'url(../images/avatarprezi.png)');
  220. $("#" + elementId).click(
  221. function () {
  222. setPresentationVisible(true);
  223. }
  224. );
  225. };
  226. /**
  227. * A presentation has been removed.
  228. *
  229. * @param event the event indicating the remove of a presentation
  230. * @param jid the jid for which the presentation was removed
  231. * @param the url of the presentation
  232. */
  233. function presentationRemoved(event, jid, presUrl) {
  234. console.log('presentation removed', presUrl);
  235. var presId = getPresentationId(presUrl);
  236. setPresentationVisible(false);
  237. $('#participant_'
  238. + Strophe.getResourceFromJid(jid)
  239. + '_' + presId).remove();
  240. $('#presentation>iframe').remove();
  241. if (preziPlayer != null) {
  242. preziPlayer.destroy();
  243. preziPlayer = null;
  244. }
  245. };
  246. /**
  247. * Indicates if the given string is an alphanumeric string.
  248. * Note that some special characters are also allowed (-, _ , /, &, ?, =, ;) for the
  249. * purpose of checking URIs.
  250. */
  251. function isAlphanumeric(unsafeText) {
  252. var regex = /^[a-z0-9-_\/&\?=;]+$/i;
  253. return regex.test(unsafeText);
  254. }
  255. /**
  256. * Returns the presentation id from the given url.
  257. */
  258. function getPresentationId (presUrl) {
  259. var presIdTmp = presUrl.substring(presUrl.indexOf("prezi.com/") + 10);
  260. return presIdTmp.substring(0, presIdTmp.indexOf('/'));
  261. }
  262. /**
  263. * Returns the presentation width.
  264. */
  265. function getPresentationWidth() {
  266. var availableWidth = UIUtil.getAvailableVideoWidth();
  267. var availableHeight = getPresentationHeihgt();
  268. var aspectRatio = 16.0 / 9.0;
  269. if (availableHeight < availableWidth / aspectRatio) {
  270. availableWidth = Math.floor(availableHeight * aspectRatio);
  271. }
  272. return availableWidth;
  273. }
  274. /**
  275. * Returns the presentation height.
  276. */
  277. function getPresentationHeihgt() {
  278. var remoteVideos = $('#remoteVideos');
  279. return window.innerHeight - remoteVideos.outerHeight();
  280. }
  281. /**
  282. * Resizes the presentation iframe.
  283. */
  284. function resize() {
  285. if ($('#presentation>iframe')) {
  286. $('#presentation>iframe').width(getPresentationWidth());
  287. $('#presentation>iframe').height(getPresentationHeihgt());
  288. }
  289. }
  290. /**
  291. * Shows/hides a presentation.
  292. */
  293. function setPresentationVisible(visible) {
  294. var prezi = $('#presentation>iframe');
  295. if (visible) {
  296. // Trigger the video.selected event to indicate a change in the
  297. // large video.
  298. $(document).trigger("video.selected", [true]);
  299. $('#largeVideo').fadeOut(300);
  300. prezi.fadeIn(300, function() {
  301. prezi.css({opacity:'1'});
  302. ToolbarToggler.dockToolbar(true);
  303. VideoLayout.setLargeVideoVisible(false);
  304. });
  305. $('#activeSpeaker').css('visibility', 'hidden');
  306. }
  307. else {
  308. if (prezi.css('opacity') == '1') {
  309. prezi.fadeOut(300, function () {
  310. prezi.css({opacity:'0'});
  311. $('#reloadPresentation').css({display:'none'});
  312. $('#largeVideo').fadeIn(300, function() {
  313. VideoLayout.setLargeVideoVisible(true);
  314. ToolbarToggler.dockToolbar(false);
  315. });
  316. });
  317. }
  318. }
  319. }
  320. /**
  321. * Presentation has been removed.
  322. */
  323. $(document).bind('presentationremoved.muc', presentationRemoved);
  324. /**
  325. * Presentation has been added.
  326. */
  327. $(document).bind('presentationadded.muc', presentationAdded);
  328. /*
  329. * Indicates presentation slide change.
  330. */
  331. $(document).bind('gotoslide.muc', function (event, jid, presUrl, current) {
  332. if (preziPlayer && preziPlayer.getCurrentStep() != current) {
  333. preziPlayer.flyToStep(current);
  334. var animationStepsArray = preziPlayer.getAnimationCountOnSteps();
  335. for (var i = 0; i < parseInt(animationStepsArray[current]); i++) {
  336. preziPlayer.flyToStep(current, i);
  337. }
  338. }
  339. });
  340. /**
  341. * On video selected event.
  342. */
  343. $(document).bind('video.selected', function (event, isPresentation) {
  344. if (!isPresentation && $('#presentation>iframe')) {
  345. setPresentationVisible(false);
  346. }
  347. });
  348. $(window).resize(function () {
  349. resize();
  350. });
  351. module.exports = Prezi;