jiti-meet/modules/UI/prezi/Prezi.js

449 lines
12 KiB
JavaScript
Raw Normal View History

2015-12-14 12:26:50 +00:00
/* global $, APP */
/* jshint -W101 */
2015-12-25 16:55:45 +00:00
2015-12-14 12:26:50 +00:00
import VideoLayout from "../videolayout/VideoLayout";
2015-12-25 16:55:45 +00:00
import LargeContainer from '../videolayout/LargeContainer';
import PreziPlayer from './PreziPlayer';
import UIUtil from '../util/UIUtil';
import UIEvents from '../../../service/UI/UIEvents';
import messageHandler from '../util/MessageHandler';
import ToolbarToggler from "../toolbars/ToolbarToggler";
import SidePanelToggler from "../side_pannels/SidePanelToggler";
2015-12-30 10:55:51 +00:00
import BottomToolbar from '../toolbars/BottomToolbar';
2015-12-14 12:26:50 +00:00
2016-01-15 14:59:35 +00:00
/**
* Example of Prezi link.
*/
2015-12-25 16:55:45 +00:00
const defaultPreziLink = "http://prezi.com/wz7vhjycl7e6/my-prezi";
const alphanumRegex = /^[a-z0-9-_\/&\?=;]+$/i;
2016-01-15 14:59:35 +00:00
/**
* Default aspect ratio for Prezi frame.
*/
2015-12-25 16:55:45 +00:00
const aspectRatio = 16.0 / 9.0;
2015-01-07 14:54:03 +00:00
2016-01-15 14:59:35 +00:00
/**
* Default Prezi frame width.
*/
2015-12-25 16:55:45 +00:00
const DEFAULT_WIDTH = 640;
2016-01-15 14:59:35 +00:00
/**
* Default Prezi frame height.
*/
2015-12-25 16:55:45 +00:00
const DEFAULT_HEIGHT = 480;
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
/**
* Indicates if the given string is an alphanumeric string.
* Note that some special characters are also allowed (-, _ , /, &, ?, =, ;) for the
* purpose of checking URIs.
2016-01-15 14:59:35 +00:00
* @param {string} unsafeText string to check
* @returns {boolean}
2015-12-25 16:55:45 +00:00
*/
function isAlphanumeric(unsafeText) {
return alphanumRegex.test(unsafeText);
}
/**
2015-12-25 16:55:45 +00:00
* Returns the presentation id from the given url.
2016-01-15 14:59:35 +00:00
* @param {string} url Prezi link
* @returns {string} presentation id
*/
2015-12-25 16:55:45 +00:00
function getPresentationId (url) {
let presId = url.substring(url.indexOf("prezi.com/") + 10);
return presId.substring(0, presId.indexOf('/'));
}
2016-01-15 14:59:35 +00:00
/**
* Checks if given string is Prezi url.
* @param {string} url string to check.
* @returns {boolean}
*/
2015-12-25 16:55:45 +00:00
function isPreziLink(url) {
if (url.indexOf('http://prezi.com/') !== 0 && url.indexOf('https://prezi.com/') !== 0) {
return false;
}
2015-12-25 16:55:45 +00:00
let presId = url.substring(url.indexOf("prezi.com/") + 10);
if (!isAlphanumeric(presId) || presId.indexOf('/') < 2) {
return false;
}
2015-12-25 16:55:45 +00:00
return true;
}
2015-01-07 14:54:03 +00:00
2016-01-15 14:59:35 +00:00
/**
* Notify user that other user if already sharing Prezi.
*/
2015-12-25 16:55:45 +00:00
function notifyOtherIsSharingPrezi() {
messageHandler.openMessageDialog(
"dialog.sharePreziTitle",
"dialog.sharePreziMsg"
);
}
2015-01-07 14:54:03 +00:00
2016-01-15 14:59:35 +00:00
/**
* Ask user if he want to close Prezi he's sharing.
*/
2015-12-25 16:55:45 +00:00
function proposeToClosePrezi() {
return new Promise(function (resolve, reject) {
messageHandler.openTwoButtonDialog(
"dialog.removePreziTitle",
null,
"dialog.removePreziMsg",
null,
false,
"dialog.Remove",
function(e,v,m,f) {
if (v) {
resolve();
} else {
reject();
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
}
);
});
}
2016-01-15 14:59:35 +00:00
/**
* Ask user for Prezi url to share with others.
* Dialog validates client input to allow only Prezi urls.
*/
2015-12-25 16:55:45 +00:00
function requestPreziLink() {
const title = APP.translation.generateTranslationHTML("dialog.sharePreziTitle");
const cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel");
const shareButton = APP.translation.generateTranslationHTML("dialog.Share");
const backButton = APP.translation.generateTranslationHTML("dialog.Back");
const linkError = APP.translation.generateTranslationHTML("dialog.preziLinkError");
const i18nOptions = {url: defaultPreziLink};
const defaultUrl = APP.translation.translateString(
"defaultPreziLink", i18nOptions
);
return new Promise(function (resolve, reject) {
let dialog = messageHandler.openDialogWithStates({
state0: {
html: `
<h2>${title}</h2>
<input name="preziUrl" type="text"
data-i18n="[placeholder]defaultPreziLink"
data-i18n-options="${JSON.stringify(i18nOptions)}"
placeholder="${defaultUrl}" autofocus>`,
persistent: false,
buttons: [
{title: cancelButton, value: false},
{title: shareButton, value: true}
],
focus: ':input:first',
defaultButton: 1,
submit: function (e, v, m, f) {
e.preventDefault();
if (!v) {
reject('cancelled');
dialog.close();
return;
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
let preziUrl = f.preziUrl;
if (!preziUrl) {
return;
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
let urlValue = encodeURI(UIUtil.escapeHtml(preziUrl));
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
if (!isPreziLink(urlValue)) {
dialog.goToState('state1');
return false;
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
resolve(urlValue);
dialog.close();
}
},
state1: {
html: `<h2>${title}</h2> ${linkError}`,
persistent: false,
buttons: [
{title: cancelButton, value: false},
{title: backButton, value: true}
],
focus: ':input:first',
defaultButton: 1,
submit: function (e, v, m, f) {
e.preventDefault();
if (v === 0) {
reject();
dialog.close();
} else {
dialog.goToState('state0');
}
}
}
});
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
});
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
export const PreziContainerType = "prezi";
2015-01-07 14:54:03 +00:00
2016-01-15 14:59:35 +00:00
/**
* Container for Prezi iframe.
*/
2015-12-25 16:55:45 +00:00
class PreziContainer extends LargeContainer {
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
constructor ({preziId, isMy, slide, onSlideChanged}) {
super();
this.reloadBtn = $('#reloadPresentation');
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
let preziPlayer = new PreziPlayer(
'presentation', {
preziId,
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT,
controls: isMy,
debug: true
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
);
this.preziPlayer = preziPlayer;
this.$iframe = $(preziPlayer.iframe);
this.$iframe.attr('id', preziId);
preziPlayer.on(PreziPlayer.EVENT_STATUS, function({value}) {
console.log("prezi status", value);
if (value == PreziPlayer.STATUS_CONTENT_READY && !isMy) {
preziPlayer.flyToStep(slide);
2015-01-07 14:54:03 +00:00
}
});
2015-12-25 16:55:45 +00:00
preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function({value}) {
console.log("event value", value);
onSlideChanged(value);
2015-01-07 14:54:03 +00:00
});
2015-12-25 16:55:45 +00:00
}
2016-01-15 14:59:35 +00:00
/**
* Change Prezi slide.
* @param {number} slide slide to show
*/
2015-12-25 16:55:45 +00:00
goToSlide (slide) {
if (this.preziPlayer.getCurrentStep() === slide) {
return;
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
this.preziPlayer.flyToStep(slide);
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
let animationStepsArray = this.preziPlayer.getAnimationCountOnSteps();
if (!animationStepsArray) {
return;
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
for (var i = 0; i < parseInt(animationStepsArray[slide]); i += 1) {
this.preziPlayer.flyToStep(slide, i);
}
}
2016-01-15 14:59:35 +00:00
/**
* Show or hide "reload presentation" button.
* @param {boolean} show
*/
2015-12-25 16:55:45 +00:00
showReloadBtn (show) {
this.reloadBtn.css('display', show ? 'inline-block' : 'none');
}
show () {
return new Promise(resolve => {
this.$iframe.fadeIn(300, () => {
this.$iframe.css({opacity: 1});
ToolbarToggler.dockToolbar(true);
resolve();
});
});
}
hide () {
return new Promise(resolve => {
this.$iframe.fadeOut(300, () => {
this.$iframe.css({opacity: 0});
this.showReloadBtn(false);
ToolbarToggler.dockToolbar(false);
resolve();
});
});
}
onHoverIn () {
let rightOffset = window.innerWidth - this.$iframe.offset().left - this.$iframe.width();
this.showReloadBtn(true);
this.reloadBtn.css('right', rightOffset);
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
onHoverOut (event) {
let e = event.toElement || event.relatedTarget;
if (e && e.id != 'reloadPresentation' && e.id != 'header') {
this.showReloadBtn(false);
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
resize (containerWidth, containerHeight) {
2015-12-30 10:55:51 +00:00
let height = containerHeight - BottomToolbar.getFilmStripHeight();
2015-12-25 16:55:45 +00:00
let width = containerWidth;
if (height < width / aspectRatio) {
width = Math.floor(height * aspectRatio);
}
this.$iframe.width(width).height(height);
2015-01-07 14:54:03 +00:00
}
2016-01-15 14:59:35 +00:00
/**
* Close Prezi frame.
*/
2015-12-25 16:55:45 +00:00
close () {
this.showReloadBtn(false);
this.preziPlayer.destroy();
this.$iframe.remove();
}
2015-01-07 14:54:03 +00:00
}
2016-01-15 14:59:35 +00:00
/**
* Manager of Prezi frames.
*/
2015-12-25 16:55:45 +00:00
export default class PreziManager {
constructor (emitter) {
this.emitter = emitter;
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
this.userId = null;
this.url = null;
this.prezi = null;
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
$("#reloadPresentationLink").click(this.reloadPresentation.bind(this));
2015-01-07 14:54:03 +00:00
}
2015-12-25 16:55:45 +00:00
get isPresenting () {
return !!this.userId;
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
get isMyPrezi () {
return this.userId === APP.conference.localId;
2015-01-07 14:54:03 +00:00
}
2016-01-15 14:59:35 +00:00
/**
* Check if user is currently sharing.
* @param {string} id user id to check for
*/
2015-12-25 16:55:45 +00:00
isSharing (id) {
return this.userId === id;
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
handlePreziButtonClicked () {
if (!this.isPresenting) {
requestPreziLink().then(
url => this.emitter.emit(UIEvents.SHARE_PREZI, url, 0),
err => console.error('PREZI CANCELED', err)
);
return;
}
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
if (this.isMyPrezi) {
proposeToClosePrezi().then(() => this.emitter.emit(UIEvents.STOP_SHARING_PREZI));
} else {
notifyOtherIsSharingPrezi();
}
}
2015-01-07 14:54:03 +00:00
2016-01-15 14:59:35 +00:00
/**
* Reload current Prezi frame.
*/
2015-12-25 16:55:45 +00:00
reloadPresentation () {
if (!this.prezi) {
return;
}
let iframe = this.prezi.$iframe[0];
iframe.src = iframe.src;
}
2016-01-15 14:59:35 +00:00
/**
* Show Prezi. Create new Prezi if there is no Prezi yet.
* @param {string} id owner id
* @param {string} url Prezi url
* @param {number} slide slide to show
*/
2015-12-25 16:55:45 +00:00
showPrezi (id, url, slide) {
if (!this.isPresenting) {
this.createPrezi(id, url, slide);
}
if (this.userId === id && this.url === url) {
this.prezi.goToSlide(slide);
} else {
console.error(this.userId, id);
console.error(this.url, url);
throw new Error("unexpected presentation change");
2015-01-07 14:54:03 +00:00
}
}
2016-01-15 14:59:35 +00:00
/**
* Create new Prezi frame..
* @param {string} id owner id
* @param {string} url Prezi url
* @param {number} slide slide to show
*/
2015-12-25 16:55:45 +00:00
createPrezi (id, url, slide) {
console.log("presentation added", url);
this.userId = id;
this.url = url;
let preziId = getPresentationId(url);
let elementId = `participant_${id}_${preziId}`;
2015-01-07 14:54:03 +00:00
2015-12-25 16:55:45 +00:00
this.$thumb = $(VideoLayout.addRemoteVideoContainer(elementId));
VideoLayout.resizeThumbnails();
this.$thumb.css({
'background-image': 'url(../images/avatarprezi.png)'
}).click(() => VideoLayout.showLargeVideoContainer(PreziContainerType, true));
this.prezi = new PreziContainer({
preziId,
isMy: this.isMyPrezi,
slide,
onSlideChanged: newSlide => {
if (this.isMyPrezi) {
this.emitter.emit(UIEvents.SHARE_PREZI, url, newSlide);
}
}
});
VideoLayout.addLargeVideoContainer(PreziContainerType, this.prezi);
VideoLayout.showLargeVideoContainer(PreziContainerType, true);
}
2016-01-15 14:59:35 +00:00
/**
* Close Prezi.
* @param {string} id owner id
*/
2015-12-25 16:55:45 +00:00
removePrezi (id) {
if (this.userId !== id) {
throw new Error(`cannot close presentation from ${this.userId} instead of ${id}`);
}
this.$thumb.remove();
this.$thumb = null;
// wait until Prezi is hidden, then remove it
VideoLayout.showLargeVideoContainer(PreziContainerType, false).then(() => {
console.log("presentation removed", this.url);
VideoLayout.removeLargeVideoContainer(PreziContainerType);
this.userId = null;
this.url = null;
this.prezi.close();
this.prezi = null;
});
}
}