2013-03-26 09:07:59 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2013 KO GmbH <copyright@kogmbh.com>
|
|
|
|
|
*
|
|
|
|
|
* @licstart
|
|
|
|
|
* The JavaScript code in this page is free software: you can redistribute it
|
|
|
|
|
* and/or modify it under the terms of the GNU Affero General Public License
|
|
|
|
|
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
|
|
|
|
|
* the License, or (at your option) any later version. The code is distributed
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
|
|
|
|
|
*
|
|
|
|
|
* As additional permission under GNU AGPL version 3 section 7, you
|
|
|
|
|
* may distribute non-source (e.g., minimized or compacted) forms of
|
|
|
|
|
* that code without the copy of the GNU GPL normally required by
|
|
|
|
|
* section 4, provided you include this license notice and a URL
|
|
|
|
|
* through which recipients can access the Corresponding Source.
|
|
|
|
|
*
|
|
|
|
|
* As a special exception to the AGPL, any HTML file which merely makes function
|
|
|
|
|
* calls to this code, and for that purpose includes it by reference shall be
|
|
|
|
|
* deemed a separate work for copyright law purposes. In addition, the copyright
|
|
|
|
|
* holders of this code give you permission to combine this code with free
|
|
|
|
|
* software libraries that are released under the GNU LGPL. You may copy and
|
|
|
|
|
* distribute such a system following the terms of the GNU AGPL for this code
|
|
|
|
|
* and the LGPL for the libraries. If you modify this code, you may extend this
|
|
|
|
|
* exception to your version of the code, but you are not obligated to do so.
|
|
|
|
|
* If you do not wish to do so, delete this exception statement from your
|
|
|
|
|
* version.
|
|
|
|
|
*
|
|
|
|
|
* This license applies to this entire compilation.
|
|
|
|
|
* @licend
|
|
|
|
|
* @source: http://www.webodf.org/
|
|
|
|
|
* @source: http://gitorious.org/webodf/webodf/
|
|
|
|
|
*/
|
|
|
|
|
/*global runtime,define,document,odf,ops */
|
|
|
|
|
|
|
|
|
|
define("webodf/editor/Editor", [
|
|
|
|
|
"dojo/i18n!webodf/editor/nls/myResources",
|
|
|
|
|
"webodf/editor/EditorSession",
|
|
|
|
|
"webodf/editor/UserList",
|
|
|
|
|
"dijit/layout/BorderContainer",
|
|
|
|
|
"dijit/layout/ContentPane",
|
|
|
|
|
"webodf/editor/widgets"],
|
|
|
|
|
|
|
|
|
|
function (myResources,
|
|
|
|
|
EditorSession,
|
|
|
|
|
UserList,
|
|
|
|
|
BorderContainer,
|
|
|
|
|
ContentPane,
|
|
|
|
|
loadWidgets) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
/**
|
|
|
|
|
* @constructor
|
2013-04-08 16:49:47 +02:00
|
|
|
* @param {{networked:boolean=,
|
|
|
|
|
* memberid:string=,
|
2013-04-10 10:40:16 +02:00
|
|
|
* saveCallback:function()=,
|
|
|
|
|
* cursorAddedCallback:function(!string)=,
|
|
|
|
|
* cursorRemovedCallback:function(!string)=,
|
|
|
|
|
* registerCallbackForShutdown:function(!function())= }} args
|
2013-04-03 13:37:17 +02:00
|
|
|
*/
|
|
|
|
|
function Editor(args) {
|
2013-03-26 09:07:59 +01:00
|
|
|
|
|
|
|
|
var self = this,
|
|
|
|
|
// Private
|
2013-04-03 13:37:17 +02:00
|
|
|
userid,
|
|
|
|
|
memberid = args.memberid,
|
|
|
|
|
session,
|
2013-03-26 09:07:59 +01:00
|
|
|
editorSession,
|
|
|
|
|
userList,
|
2013-04-09 11:21:15 +10:00
|
|
|
networked = args.networked === true,
|
2013-03-26 09:07:59 +01:00
|
|
|
opRouter,
|
|
|
|
|
userModel,
|
2013-04-08 16:49:47 +02:00
|
|
|
saveOdtFile = args.saveCallback,
|
2013-04-10 10:40:16 +02:00
|
|
|
cursorAddedHandler = args.cursorAddedCallback,
|
|
|
|
|
cursorRemovedHandler = args.cursorRemovedCallback,
|
|
|
|
|
registerCallbackForShutdown = args.registerCallbackForShutdown,
|
2013-04-03 13:37:17 +02:00
|
|
|
documentUrl;
|
2013-03-26 09:07:59 +01:00
|
|
|
|
|
|
|
|
function translator(key, context) {
|
|
|
|
|
if (undefined === myResources[key]) {
|
|
|
|
|
return "translation missing: " + key;
|
|
|
|
|
}
|
|
|
|
|
return myResources[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runtime.currentDirectory = function () {
|
|
|
|
|
return "../../webodf/lib";
|
|
|
|
|
};
|
|
|
|
|
runtime.libraryPaths = function () {
|
|
|
|
|
return [ runtime.currentDirectory() ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-03 13:37:17 +02:00
|
|
|
* prepare all gui elements and load the given document.
|
|
|
|
|
* after loading is completed, the given callback is called.
|
|
|
|
|
* the caller still has to call editorSession.startEditing
|
|
|
|
|
* which will insert the the cursor.
|
|
|
|
|
*
|
|
|
|
|
* @param {!string} initialDocumentUrl
|
|
|
|
|
* @param {!function()} editorReadyCallback
|
2013-03-26 09:07:59 +01:00
|
|
|
*/
|
2013-04-03 13:37:17 +02:00
|
|
|
function initGuiAndDoc(initialDocumentUrl, editorReadyCallback) {
|
|
|
|
|
var odfElement, odfCanvas, mainContainer,
|
|
|
|
|
editorPane, peoplePane,
|
|
|
|
|
inviteButton,
|
2013-04-09 11:21:15 +10:00
|
|
|
viewOptions = {editInfoMarkersInitiallyVisible:networked, caretAvatarsInitiallyVisible:networked},
|
2013-04-03 13:37:17 +02:00
|
|
|
peopleListDiv = document.getElementById('peopleList');
|
2013-03-26 09:07:59 +01:00
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
if (networked) {
|
|
|
|
|
runtime.assert(peopleListDiv, "missing peopleList div in HTML");
|
|
|
|
|
}
|
2013-03-26 09:07:59 +01:00
|
|
|
|
|
|
|
|
runtime.loadClass('odf.OdfCanvas');
|
|
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
// we might need it later
|
|
|
|
|
documentUrl = initialDocumentUrl;
|
|
|
|
|
runtime.assert(documentUrl, "document should be defined here.");
|
2013-03-26 09:07:59 +01:00
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
runtime.assert(memberid !== undefined, "memberid should be defined here.");
|
2013-03-26 09:07:59 +01:00
|
|
|
|
|
|
|
|
odfElement = document.getElementById("canvas");
|
2013-04-03 13:37:17 +02:00
|
|
|
runtime.assert(odfElement, "initGuiAndDoc failed to get odf canvas from html");
|
2013-03-26 09:07:59 +01:00
|
|
|
odfCanvas = new odf.OdfCanvas(odfElement);
|
|
|
|
|
// make the canvas accessible to users of editor.js
|
|
|
|
|
self.odfCanvas = odfCanvas;
|
|
|
|
|
|
|
|
|
|
document.translator = translator;
|
|
|
|
|
|
|
|
|
|
function translateContent(node) {
|
|
|
|
|
var i,
|
|
|
|
|
element,
|
|
|
|
|
tag,
|
|
|
|
|
placeholder,
|
|
|
|
|
translatable = node.querySelectorAll("*[text-i18n]");
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < translatable.length; i += 1) {
|
|
|
|
|
element = translatable[i];
|
|
|
|
|
tag = element.localName;
|
|
|
|
|
placeholder = element.getAttribute('text-i18n');
|
|
|
|
|
if (tag === "label"
|
|
|
|
|
|| tag === "span"
|
|
|
|
|
|| /h\d/i.test(tag)) {
|
|
|
|
|
element.textContent = document.translator(placeholder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
document.translateContent = translateContent;
|
|
|
|
|
|
|
|
|
|
odfCanvas.addListener("statereadychange", function () {
|
|
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
if (!memberid) {
|
|
|
|
|
// legacy - memberid should be passed in the constructor
|
|
|
|
|
memberid = (userid||'undefined') + "___" + Date.now();
|
|
|
|
|
}
|
2013-03-26 09:07:59 +01:00
|
|
|
|
2013-03-26 14:15:02 +01:00
|
|
|
session = new ops.Session(odfCanvas);
|
2013-04-09 11:21:15 +10:00
|
|
|
editorSession = new EditorSession(session, memberid, {viewOptions:viewOptions});
|
2013-04-03 13:37:17 +02:00
|
|
|
|
2013-03-26 09:07:59 +01:00
|
|
|
if (peopleListDiv) {
|
|
|
|
|
userList = new UserList(editorSession, peopleListDiv);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 10:40:16 +02:00
|
|
|
if (registerCallbackForShutdown) {
|
|
|
|
|
registerCallbackForShutdown(editorSession.endEditing);
|
|
|
|
|
}
|
2013-03-26 09:07:59 +01:00
|
|
|
|
2013-04-08 16:49:47 +02:00
|
|
|
loadWidgets(editorSession, saveOdtFile);
|
2013-04-03 13:37:17 +02:00
|
|
|
editorReadyCallback();
|
|
|
|
|
|
2013-03-26 09:07:59 +01:00
|
|
|
});
|
2013-04-03 13:37:17 +02:00
|
|
|
odfCanvas.load(initialDocumentUrl);
|
2013-03-26 09:07:59 +01:00
|
|
|
odfCanvas.setEditable(false);
|
|
|
|
|
|
|
|
|
|
// App Widgets
|
|
|
|
|
mainContainer = new BorderContainer({}, 'mainContainer');
|
|
|
|
|
|
|
|
|
|
editorPane = new ContentPane({
|
|
|
|
|
region: 'center'
|
|
|
|
|
}, 'editor');
|
|
|
|
|
mainContainer.addChild(editorPane);
|
|
|
|
|
|
|
|
|
|
if (peopleListDiv) {
|
|
|
|
|
peoplePane = new ContentPane({
|
|
|
|
|
region: 'right',
|
|
|
|
|
title: translator("people")
|
|
|
|
|
}, 'people');
|
|
|
|
|
mainContainer.addChild(peoplePane);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mainContainer.startup();
|
|
|
|
|
|
|
|
|
|
if (window.inviteButtonProxy) {
|
|
|
|
|
inviteButton = document.getElementById('inviteButton');
|
|
|
|
|
if (inviteButton) {
|
|
|
|
|
inviteButton.innerText = translator("invitePeople");
|
|
|
|
|
inviteButton.style.display = "block";
|
|
|
|
|
inviteButton.onclick = window.inviteButtonProxy.clicked;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
/**
|
|
|
|
|
* create the editor, load the starting document,
|
|
|
|
|
* call editorReadyCallback once everything is done.
|
|
|
|
|
*
|
|
|
|
|
* @param {!string} docUrl
|
|
|
|
|
* @param {?function()} editorReadyCallback
|
2013-03-26 09:07:59 +01:00
|
|
|
*/
|
2013-04-03 13:37:17 +02:00
|
|
|
self.loadDocument = function (docUrl, editorReadyCallback) {
|
|
|
|
|
initGuiAndDoc(docUrl, function() {
|
|
|
|
|
editorSession.startEditing();
|
|
|
|
|
editorReadyCallback();
|
|
|
|
|
});
|
2013-03-26 09:07:59 +01:00
|
|
|
};
|
|
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
/**
|
|
|
|
|
* create the editor, load the starting document of an editing-session,
|
|
|
|
|
* request a replay of previous operations, call editorReadyCallback once
|
|
|
|
|
* everything is done.
|
|
|
|
|
*
|
|
|
|
|
* @param {!string} sessionId
|
|
|
|
|
* @param {?function()} editorReadyCallback
|
2013-03-26 09:07:59 +01:00
|
|
|
*/
|
2013-04-03 13:37:17 +02:00
|
|
|
self.loadSession = function (sessionId, editorReadyCallback) {
|
|
|
|
|
initGuiAndDoc("/session/"+sessionId+"/genesis", function () {
|
|
|
|
|
// use the nowjs op-router when connected
|
|
|
|
|
opRouter = opRouter || new ops.NowjsOperationRouter(sessionId, memberid);
|
|
|
|
|
session.setOperationRouter(opRouter);
|
2013-03-26 09:07:59 +01:00
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
userModel = userModel || new ops.NowjsUserModel();
|
|
|
|
|
session.setUserModel(userModel);
|
2013-03-26 09:07:59 +01:00
|
|
|
|
2013-04-03 13:37:17 +02:00
|
|
|
editorSession.sessionView.disableEditHighlighting();
|
|
|
|
|
opRouter.requestReplay(function done() {
|
2013-04-10 10:40:16 +02:00
|
|
|
var odtDocument = session.getOdtDocument();
|
|
|
|
|
if (cursorAddedHandler) {
|
|
|
|
|
odtDocument.subscribe(ops.OdtDocument.signalCursorAdded, function (cursor) {
|
|
|
|
|
cursorAddedHandler(cursor.getMemberId());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (cursorRemovedHandler) {
|
|
|
|
|
odtDocument.subscribe(ops.OdtDocument.signalCursorRemoved, function (memberId) {
|
|
|
|
|
cursorRemovedHandler(memberId);
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-04-03 13:37:17 +02:00
|
|
|
editorSession.sessionView.enableEditHighlighting();
|
|
|
|
|
|
|
|
|
|
// start editing: let the controller send the OpAddCursor
|
|
|
|
|
editorSession.startEditing();
|
|
|
|
|
editorReadyCallback();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// access to user model
|
|
|
|
|
self.getUserModel = function() {
|
|
|
|
|
return userModel;
|
2013-03-26 09:07:59 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return Editor;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// vim:expandtab
|