The app which enables the users to edit office documents from Nextcloud using ONLYOFFICE Document Server, allows multiple users to collaborate in real time and to save back those changes to Nextcloud
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.
 
 
 
 
 
onlyoffice-nextcloud/js/editor.js

423 lines
15 KiB

/**
*
* (c) Copyright Ascensio System SIA 2020
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
(function ($, OCA) {
OCA.Onlyoffice = _.extend({
AppName: "onlyoffice",
inframe: false,
fileId: null,
shareToken: null,
insertImageType: null
}, OCA.Onlyoffice);
OCA.Onlyoffice.InitEditor = function () {
var displayError = function (error) {
OCP.Toast.error(error, {
timeout: -1
});
};
OCA.Onlyoffice.fileId = $("#iframeEditor").data("id");
OCA.Onlyoffice.shareToken = $("#iframeEditor").data("sharetoken");
OCA.Onlyoffice.version = $("#iframeEditor").data("version");
var directToken = $("#iframeEditor").data("directtoken");
OCA.Onlyoffice.inframe = !!$("#iframeEditor").data("inframe");
var guestName = localStorage.getItem("nick");
if (!OCA.Onlyoffice.fileId && !OCA.Onlyoffice.shareToken && !directToken) {
displayError(t(OCA.Onlyoffice.AppName, "FileId is empty"));
return;
}
if (typeof DocsAPI === "undefined") {
displayError(t(OCA.Onlyoffice.AppName, "ONLYOFFICE cannot be reached. Please contact admin"));
return;
}
var configUrl = OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/config/{fileId}",
{
fileId: OCA.Onlyoffice.fileId || 0
});
var params = [];
var filePath = $("#iframeEditor").data("path");
if (filePath) {
params.push("filePath=" + encodeURIComponent(filePath));
}
if (OCA.Onlyoffice.shareToken) {
params.push("shareToken=" + encodeURIComponent(OCA.Onlyoffice.shareToken));
}
if (directToken) {
$("html").addClass("onlyoffice-full-page");
params.push("directToken=" + encodeURIComponent(directToken));
}
if (OCA.Onlyoffice.version > 0) {
params.push("version=" + OCA.Onlyoffice.version);
}
if (guestName) {
params.push("guestName=" + encodeURIComponent(guestName));
}
if (OCA.Onlyoffice.inframe || directToken) {
var dsVersion = DocsAPI.DocEditor.version();
var versionArray = dsVersion.split(".");
if (versionArray[0] < 5 || versionArray[0] == 5 && versionArray[1] < 5) {
if (OCA.Onlyoffice.inframe
&& window.parent.OCA.Onlyoffice.ShowHeaderButton) {
window.parent.postMessage({
method: "editorShowHeaderButton"
},
"*");
}
params.push("inframe=2");
} else {
params.push("inframe=1");
}
}
if (OCA.Onlyoffice.Desktop) {
params.push("desktop=true");
}
if (params.length) {
configUrl += "?" + params.join("&");
}
$.ajax({
url: configUrl,
success: function onSuccess(config) {
if (config) {
if (config.error != null) {
displayError(config.error);
return;
}
if (config.redirectUrl) {
location.href = config.redirectUrl;
return;
}
var docIsChanged = null;
var docIsChangedTimeout = null;
var setPageTitle = function (event) {
clearTimeout(docIsChangedTimeout);
if (docIsChanged !== event.data) {
var titleChange = function () {
window.document.title = config.document.title + (event.data ? " *" : "") + " - " + oc_defaults.title;
docIsChanged = event.data;
};
if (event === false || event.data) {
titleChange();
} else {
docIsChangedTimeout = setTimeout(titleChange, 500);
}
}
};
setPageTitle(false);
OCA.Onlyoffice.documentType = config.documentType;
config.events = {
"onDocumentStateChange": setPageTitle,
"onRequestHistory": OCA.Onlyoffice.onRequestHistory,
"onRequestHistoryData": OCA.Onlyoffice.onRequestHistoryData,
"onDocumentReady": OCA.Onlyoffice.onDocumentReady,
};
if (!OCA.Onlyoffice.version) {
config.events.onRequestHistoryClose = OCA.Onlyoffice.onRequestHistoryClose;
}
if (config.editorConfig.tenant) {
config.events.onAppReady = function () {
OCA.Onlyoffice.docEditor.showMessage(t(OCA.Onlyoffice.AppName, "You are using public demo ONLYOFFICE Document Server. Please do not store private sensitive data."));
};
}
if (OCA.Onlyoffice.inframe && !OCA.Onlyoffice.shareToken
|| OC.currentUser) {
config.events.onRequestSaveAs = OCA.Onlyoffice.onRequestSaveAs;
config.events.onRequestInsertImage = OCA.Onlyoffice.onRequestInsertImage;
config.events.onRequestMailMergeRecipients = OCA.Onlyoffice.onRequestMailMergeRecipients;
config.events.onRequestCompareFile = OCA.Onlyoffice.onRequestCompareFile;
}
if (OCA.Onlyoffice.directEditor || OCA.Onlyoffice.inframe) {
config.events.onRequestClose = OCA.Onlyoffice.onRequestClose;
}
if (OCA.Onlyoffice.inframe
&& config._files_sharing && !OCA.Onlyoffice.shareToken
&& window.parent.OCA.Onlyoffice.context) {
config.events.onRequestSharingSettings = OCA.Onlyoffice.onRequestSharingSettings;
}
OCA.Onlyoffice.docEditor = new DocsAPI.DocEditor("iframeEditor", config);
if (OCA.Onlyoffice.directEditor) {
OCA.Onlyoffice.directEditor.loaded();
}
if (!OCA.Onlyoffice.directEditor
&& config.type === "mobile" && $("#app > iframe").css("position") === "fixed") {
$("#app > iframe").css("height", "calc(100% - 50px)");
}
}
}
});
};
OCA.Onlyoffice.onRequestHistory = function (version) {
$.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/history?fileId={fileId}&shareToken={shareToken}",
{
fileId: OCA.Onlyoffice.fileId || 0,
shareToken: OCA.Onlyoffice.shareToken || "",
}),
function onSuccess(response) {
if (response.error) {
var data = {error: response.error};
} else {
var currentVersion = 0;
$.each(response, function (i, fileVersion) {
if (fileVersion.version >= currentVersion) {
currentVersion = fileVersion.version;
}
fileVersion.created = moment(fileVersion.created * 1000).format("L LTS");
if (fileVersion.changes) {
$.each(fileVersion.changes, function (j, change) {
change.created = moment(change.created + "+00:00").format("L LTS");
});
}
});
if (version) {
currentVersion = Math.min(currentVersion, version);
}
data = {
currentVersion: currentVersion,
history: response,
};
}
OCA.Onlyoffice.docEditor.refreshHistory(data);
});
};
OCA.Onlyoffice.onRequestHistoryData = function (event) {
var version = event.data;
$.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/version?fileId={fileId}&version={version}&shareToken={shareToken}",
{
fileId: OCA.Onlyoffice.fileId || 0,
version: version,
shareToken: OCA.Onlyoffice.shareToken || "",
}),
function onSuccess(response) {
if (response.error) {
response = {
error: response.error,
version: version,
};
}
OCA.Onlyoffice.docEditor.setHistoryData(response);
});
};
OCA.Onlyoffice.onRequestHistoryClose = function () {
location.reload(true);
};
OCA.Onlyoffice.onDocumentReady = function() {
if (OCA.Onlyoffice.inframe) {
window.parent.postMessage({
method: "onDocumentReady",
param: OCA.Onlyoffice.documentType
},
"*");
}
if (OCA.Onlyoffice.version > 0) {
OCA.Onlyoffice.onRequestHistory(OCA.Onlyoffice.version);
}
};
OCA.Onlyoffice.onRequestSaveAs = function (event) {
var saveData = {
name: event.data.title,
url: event.data.url
};
if (OCA.Onlyoffice.inframe) {
window.parent.postMessage({
method: "editorRequestSaveAs",
param: saveData
},
"*");
} else {
OC.dialogs.filepicker(t(OCA.Onlyoffice.AppName, "Save as"),
function (fileDir) {
saveData.dir = fileDir;
OCA.Onlyoffice.editorSaveAs(saveData);
},
false,
"httpd/unix-directory");
}
};
OCA.Onlyoffice.editorSaveAs = function (saveData) {
$.post(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/save"),
saveData,
function onSuccess(response) {
if (response.error) {
OCP.Toast.error(response.error);
return;
}
OCP.Toast.success(t(OCA.Onlyoffice.AppName, "File saved") + " (" + response.name + ")");
});
};
OCA.Onlyoffice.onRequestInsertImage = function (event) {
var imageMimes = [
"image/bmp", "image/x-bmp", "image/x-bitmap", "application/bmp",
"image/gif",
"image/jpeg", "image/jpg", "application/jpg", "application/x-jpg",
"image/png", "image/x-png", "application/png", "application/x-png"
];
if (event.data) {
OCA.Onlyoffice.insertImageType = event.data.c;
}
if (OCA.Onlyoffice.inframe) {
window.parent.postMessage({
method: "editorRequestInsertImage",
param: imageMimes
},
"*");
} else {
OC.dialogs.filepicker(t(OCA.Onlyoffice.AppName, "Insert image"), OCA.Onlyoffice.editorInsertImage, false, imageMimes);
}
};
OCA.Onlyoffice.editorInsertImage = function (filePath) {
$.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/url?filePath={filePath}",
{
filePath: filePath
}),
function onSuccess(response) {
if (response.error) {
OCP.Toast.error(response.error);
return;
}
if (OCA.Onlyoffice.insertImageType) {
response.c = OCA.Onlyoffice.insertImageType;
}
OCA.Onlyoffice.docEditor.insertImage(response);
});
};
OCA.Onlyoffice.onRequestMailMergeRecipients = function () {
var recipientMimes = [
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
];
if (OCA.Onlyoffice.inframe) {
window.parent.postMessage({
method: "editorRequestMailMergeRecipients",
param: recipientMimes
},
"*");
} else {
OC.dialogs.filepicker(t(OCA.Onlyoffice.AppName, "Select recipients"), OCA.Onlyoffice.editorSetRecipient, false, recipientMimes);
}
};
OCA.Onlyoffice.editorSetRecipient = function (filePath) {
$.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/url?filePath={filePath}",
{
filePath: filePath
}),
function onSuccess(response) {
if (response.error) {
OCP.Toast.error(response.error);
return;
}
OCA.Onlyoffice.docEditor.setMailMergeRecipients(response);
});
};
OCA.Onlyoffice.onRequestClose = function () {
if (OCA.Onlyoffice.directEditor) {
OCA.Onlyoffice.directEditor.close();
return;
}
window.parent.postMessage({
method: "editorRequestClose"
},
"*");
};
OCA.Onlyoffice.onRequestSharingSettings = function () {
window.parent.postMessage({
method: "editorRequestSharingSettings"
},
"*");
};
OCA.Onlyoffice.onRequestCompareFile = function () {
var revisedMimes = [
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
];
if (OCA.Onlyoffice.inframe) {
window.parent.postMessage({
method: "editorRequestCompareFile",
param: revisedMimes
},
"*");
} else {
OC.dialogs.filepicker(t(OCA.Onlyoffice.AppName, "Select file to compare"), OCA.Onlyoffice.editorSetRevised, false, revisedMimes);
}
};
OCA.Onlyoffice.editorSetRevised = function (filePath) {
$.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/url?filePath={filePath}",
{
filePath: filePath
}),
function onSuccess(response) {
if (response.error) {
OCP.Toast.error(response.error);
return;
}
OCA.Onlyoffice.docEditor.setRevisedFile(response);
});
};
$(document).ready(OCA.Onlyoffice.InitEditor);
})(jQuery, OCA);