Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Prezi.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* global $, APP */
  2. /* jshint -W101 */
  3. import VideoLayout from "../videolayout/VideoLayout";
  4. import LargeContainer from '../videolayout/LargeContainer';
  5. import PreziPlayer from './PreziPlayer';
  6. import UIUtil from '../util/UIUtil';
  7. import UIEvents from '../../../service/UI/UIEvents';
  8. import messageHandler from '../util/MessageHandler';
  9. import ToolbarToggler from "../toolbars/ToolbarToggler";
  10. import SidePanelToggler from "../side_pannels/SidePanelToggler";
  11. const defaultPreziLink = "http://prezi.com/wz7vhjycl7e6/my-prezi";
  12. const alphanumRegex = /^[a-z0-9-_\/&\?=;]+$/i;
  13. const aspectRatio = 16.0 / 9.0;
  14. const DEFAULT_WIDTH = 640;
  15. const DEFAULT_HEIGHT = 480;
  16. /**
  17. * Indicates if the given string is an alphanumeric string.
  18. * Note that some special characters are also allowed (-, _ , /, &, ?, =, ;) for the
  19. * purpose of checking URIs.
  20. */
  21. function isAlphanumeric(unsafeText) {
  22. return alphanumRegex.test(unsafeText);
  23. }
  24. /**
  25. * Returns the presentation id from the given url.
  26. */
  27. function getPresentationId (url) {
  28. let presId = url.substring(url.indexOf("prezi.com/") + 10);
  29. return presId.substring(0, presId.indexOf('/'));
  30. }
  31. function isPreziLink(url) {
  32. if (url.indexOf('http://prezi.com/') !== 0 && url.indexOf('https://prezi.com/') !== 0) {
  33. return false;
  34. }
  35. let presId = url.substring(url.indexOf("prezi.com/") + 10);
  36. if (!isAlphanumeric(presId) || presId.indexOf('/') < 2) {
  37. return false;
  38. }
  39. return true;
  40. }
  41. function notifyOtherIsSharingPrezi() {
  42. messageHandler.openMessageDialog(
  43. "dialog.sharePreziTitle",
  44. "dialog.sharePreziMsg"
  45. );
  46. }
  47. function proposeToClosePrezi() {
  48. return new Promise(function (resolve, reject) {
  49. messageHandler.openTwoButtonDialog(
  50. "dialog.removePreziTitle",
  51. null,
  52. "dialog.removePreziMsg",
  53. null,
  54. false,
  55. "dialog.Remove",
  56. function(e,v,m,f) {
  57. if (v) {
  58. resolve();
  59. } else {
  60. reject();
  61. }
  62. }
  63. );
  64. });
  65. }
  66. function requestPreziLink() {
  67. const title = APP.translation.generateTranslationHTML("dialog.sharePreziTitle");
  68. const cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel");
  69. const shareButton = APP.translation.generateTranslationHTML("dialog.Share");
  70. const backButton = APP.translation.generateTranslationHTML("dialog.Back");
  71. const linkError = APP.translation.generateTranslationHTML("dialog.preziLinkError");
  72. const i18nOptions = {url: defaultPreziLink};
  73. const defaultUrl = APP.translation.translateString(
  74. "defaultPreziLink", i18nOptions
  75. );
  76. return new Promise(function (resolve, reject) {
  77. let dialog = messageHandler.openDialogWithStates({
  78. state0: {
  79. html: `
  80. <h2>${title}</h2>
  81. <input name="preziUrl" type="text"
  82. data-i18n="[placeholder]defaultPreziLink"
  83. data-i18n-options="${JSON.stringify(i18nOptions)}"
  84. placeholder="${defaultUrl}" autofocus>`,
  85. persistent: false,
  86. buttons: [
  87. {title: cancelButton, value: false},
  88. {title: shareButton, value: true}
  89. ],
  90. focus: ':input:first',
  91. defaultButton: 1,
  92. submit: function (e, v, m, f) {
  93. e.preventDefault();
  94. if (!v) {
  95. reject('cancelled');
  96. dialog.close();
  97. return;
  98. }
  99. let preziUrl = f.preziUrl;
  100. if (!preziUrl) {
  101. return;
  102. }
  103. let urlValue = encodeURI(UIUtil.escapeHtml(preziUrl));
  104. if (!isPreziLink(urlValue)) {
  105. dialog.goToState('state1');
  106. return false;
  107. }
  108. resolve(urlValue);
  109. dialog.close();
  110. }
  111. },
  112. state1: {
  113. html: `<h2>${title}</h2> ${linkError}`,
  114. persistent: false,
  115. buttons: [
  116. {title: cancelButton, value: false},
  117. {title: backButton, value: true}
  118. ],
  119. focus: ':input:first',
  120. defaultButton: 1,
  121. submit: function (e, v, m, f) {
  122. e.preventDefault();
  123. if (v === 0) {
  124. reject();
  125. dialog.close();
  126. } else {
  127. dialog.goToState('state0');
  128. }
  129. }
  130. }
  131. });
  132. });
  133. }
  134. export const PreziContainerType = "prezi";
  135. class PreziContainer extends LargeContainer {
  136. constructor ({preziId, isMy, slide, onSlideChanged}) {
  137. super();
  138. this.reloadBtn = $('#reloadPresentation');
  139. let preziPlayer = new PreziPlayer(
  140. 'presentation', {
  141. preziId,
  142. width: DEFAULT_WIDTH,
  143. height: DEFAULT_HEIGHT,
  144. controls: isMy,
  145. debug: true
  146. }
  147. );
  148. this.preziPlayer = preziPlayer;
  149. this.$iframe = $(preziPlayer.iframe);
  150. this.$iframe.attr('id', preziId);
  151. preziPlayer.on(PreziPlayer.EVENT_STATUS, function({value}) {
  152. console.log("prezi status", value);
  153. if (value == PreziPlayer.STATUS_CONTENT_READY && !isMy) {
  154. preziPlayer.flyToStep(slide);
  155. }
  156. });
  157. preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function({value}) {
  158. console.log("event value", value);
  159. onSlideChanged(value);
  160. });
  161. }
  162. goToSlide (slide) {
  163. if (this.preziPlayer.getCurrentStep() === slide) {
  164. return;
  165. }
  166. this.preziPlayer.flyToStep(slide);
  167. let animationStepsArray = this.preziPlayer.getAnimationCountOnSteps();
  168. if (!animationStepsArray) {
  169. return;
  170. }
  171. for (var i = 0; i < parseInt(animationStepsArray[slide]); i += 1) {
  172. this.preziPlayer.flyToStep(slide, i);
  173. }
  174. }
  175. showReloadBtn (show) {
  176. this.reloadBtn.css('display', show ? 'inline-block' : 'none');
  177. }
  178. show () {
  179. return new Promise(resolve => {
  180. this.$iframe.fadeIn(300, () => {
  181. this.$iframe.css({opacity: 1});
  182. ToolbarToggler.dockToolbar(true);
  183. resolve();
  184. });
  185. });
  186. }
  187. hide () {
  188. return new Promise(resolve => {
  189. this.$iframe.fadeOut(300, () => {
  190. this.$iframe.css({opacity: 0});
  191. this.showReloadBtn(false);
  192. ToolbarToggler.dockToolbar(false);
  193. resolve();
  194. });
  195. });
  196. }
  197. onHoverIn () {
  198. let rightOffset = window.innerWidth - this.$iframe.offset().left - this.$iframe.width();
  199. this.showReloadBtn(true);
  200. this.reloadBtn.css('right', rightOffset);
  201. }
  202. onHoverOut (event) {
  203. let e = event.toElement || event.relatedTarget;
  204. if (e && e.id != 'reloadPresentation' && e.id != 'header') {
  205. this.showReloadBtn(false);
  206. }
  207. }
  208. resize (containerWidth, containerHeight) {
  209. let remoteVideos = $('#remoteVideos');
  210. let height = containerHeight - remoteVideos.outerHeight();
  211. let width = containerWidth;
  212. if (height < width / aspectRatio) {
  213. width = Math.floor(height * aspectRatio);
  214. }
  215. this.$iframe.width(width).height(height);
  216. }
  217. close () {
  218. this.showReloadBtn(false);
  219. this.preziPlayer.destroy();
  220. this.$iframe.remove();
  221. }
  222. }
  223. export default class PreziManager {
  224. constructor (emitter) {
  225. this.emitter = emitter;
  226. this.userId = null;
  227. this.url = null;
  228. this.prezi = null;
  229. $("#reloadPresentationLink").click(this.reloadPresentation.bind(this));
  230. }
  231. get isPresenting () {
  232. return !!this.userId;
  233. }
  234. get isMyPrezi () {
  235. return this.userId === APP.conference.localId;
  236. }
  237. isSharing (id) {
  238. return this.userId === id;
  239. }
  240. handlePreziButtonClicked () {
  241. if (!this.isPresenting) {
  242. requestPreziLink().then(
  243. url => this.emitter.emit(UIEvents.SHARE_PREZI, url, 0),
  244. err => console.error('PREZI CANCELED', err)
  245. );
  246. return;
  247. }
  248. if (this.isMyPrezi) {
  249. proposeToClosePrezi().then(() => this.emitter.emit(UIEvents.STOP_SHARING_PREZI));
  250. } else {
  251. notifyOtherIsSharingPrezi();
  252. }
  253. }
  254. reloadPresentation () {
  255. if (!this.prezi) {
  256. return;
  257. }
  258. let iframe = this.prezi.$iframe[0];
  259. iframe.src = iframe.src;
  260. }
  261. showPrezi (id, url, slide) {
  262. if (!this.isPresenting) {
  263. this.createPrezi(id, url, slide);
  264. }
  265. if (this.userId === id && this.url === url) {
  266. this.prezi.goToSlide(slide);
  267. } else {
  268. console.error(this.userId, id);
  269. console.error(this.url, url);
  270. throw new Error("unexpected presentation change");
  271. }
  272. }
  273. createPrezi (id, url, slide) {
  274. console.log("presentation added", url);
  275. this.userId = id;
  276. this.url = url;
  277. let preziId = getPresentationId(url);
  278. let elementId = `participant_${id}_${preziId}`;
  279. this.$thumb = $(VideoLayout.addRemoteVideoContainer(elementId));
  280. VideoLayout.resizeThumbnails();
  281. this.$thumb.css({
  282. 'background-image': 'url(../images/avatarprezi.png)'
  283. }).click(() => VideoLayout.showLargeVideoContainer(PreziContainerType, true));
  284. this.prezi = new PreziContainer({
  285. preziId,
  286. isMy: this.isMyPrezi,
  287. slide,
  288. onSlideChanged: newSlide => {
  289. if (this.isMyPrezi) {
  290. this.emitter.emit(UIEvents.SHARE_PREZI, url, newSlide);
  291. }
  292. }
  293. });
  294. VideoLayout.addLargeVideoContainer(PreziContainerType, this.prezi);
  295. VideoLayout.showLargeVideoContainer(PreziContainerType, true);
  296. }
  297. removePrezi (id) {
  298. if (this.userId !== id) {
  299. throw new Error(`cannot close presentation from ${this.userId} instead of ${id}`);
  300. }
  301. this.$thumb.remove();
  302. this.$thumb = null;
  303. // wait until Prezi is hidden, then remove it
  304. VideoLayout.showLargeVideoContainer(PreziContainerType, false).then(() => {
  305. console.log("presentation removed", this.url);
  306. VideoLayout.removeLargeVideoContainer(PreziContainerType);
  307. this.userId = null;
  308. this.url = null;
  309. this.prezi.close();
  310. this.prezi = null;
  311. });
  312. }
  313. }