web-apps/apps/api/wopi/editor-wopi.ejs
2021-05-11 11:28:38 +03:00

278 lines
9 KiB
Plaintext

<!DOCTYPE html>
<html>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<!--
*
* (c) Copyright Ascensio System SIA 2021
*
* 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.
*
-->
<title>ONLYOFFICE</title>
<style type="text/css">
html {
height: 100%;
width: 100%;
}
body {
background: #fff;
color: #333;
font-family: Arial, Tahoma,sans-serif;
font-size: 12px;
font-weight: normal;
height: 100%;
margin: 0;
overflow-y: hidden;
padding: 0;
text-decoration: none;
}
.form {
height: 100%;
}
div {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div class="form">
<div id="iframeEditor">
</div>
</div>
<script type="text/javascript" src="../../web-apps/apps/api/documents/api.js"></script>
<script type="text/javascript" language="javascript">
var docEditor;
var postMessageOrigin;
var lang = "en-US";
var startTime;
var documentChanged;
var sendNotificationTimer;
var innerAlert = function (message) {
if (console && console.log)
console.log(message);
};
var commandMap = {
'Blur_Focus': function (data) {
innerAlert('Blur_Focus');
docEditor.blurFocus();
},
'Grab_Focus': function (data) {
innerAlert('Grab_Focus');
docEditor.grabFocus();
},
'Host_PostmessageReady': function (data) {
innerAlert('Host_PostmessageReady');
}
};
var _postMessage = function(msgId, msgData) {
if (window.parent && window.JSON) {
msgData = msgData || {};
msgData["ui-language"] = lang;
msgData["wdUserSession"] = ""; // ???
var msg = {
"MessageId": msgId,
"SendTime": Date.now(),
"Values": msgData
};
window.parent.postMessage(window.JSON.stringify(msg), postMessageOrigin);
}
};
var _onMessage = function(msg) {
if (msg.origin !== postMessageOrigin) return;
var data = msg.data;
if (Object.prototype.toString.apply(data) !== '[object String]' || !window.JSON) {
return;
}
var cmd, handler;
try {
cmd = window.JSON.parse(data)
} catch(e) {
cmd = '';
}
if (cmd) {
handler = commandMap[cmd.MessageId];
if (handler) {
handler.call(this, cmd);
}
}
};
var fn = function(e) { _onMessage(e); };
if (window.attachEvent) {
window.attachEvent('onmessage', fn);
} else {
window.addEventListener('message', fn, false);
}
var onAppReady = function () {
_postMessage('App_LoadingStatus', {
"DocumentLoadedTime": Date.now() - startTime
});
innerAlert("App ready");
};
var sendEditNotification = function () {
documentChanged && _postMessage('Edit_Notification', {});
documentChanged = false;
};
var onDocumentStateChange = function (event) {
if (event.data) {
documentChanged = true;
if (sendNotificationTimer===undefined) {
sendNotificationTimer = setInterval(sendEditNotification, 30000);
sendEditNotification();
}
}
};
var onRequestRename = function (event) {
if (event.data) {
_postMessage('File_Rename', {
"NewName": event.data
});
}
};
var onRequestClose = function () {
_postMessage('UI_Close', {});
};
var onRequestEditRights = function () {
_postMessage('UI_Edit', {});
};
var requestSharingSettings = function (event) {
_postMessage('UI_Sharing', {});
};
var onError = function (event) {
if (event)
innerAlert(event.data);
};
var connectEditor = function () {
var key = "<%- key %>";
var fileInfo = <%- JSON.stringify(fileInfo) %>;
var userAuth = <%- JSON.stringify(userAuth) %>;
var token = "<%- token %>";
var queryParams = <%- JSON.stringify(queryParams) %>;
var fileType = fileInfo.BaseFileName ? fileInfo.BaseFileName.substr(fileInfo.BaseFileName.lastIndexOf('.') + 1) : "";
var config = {
"width": "100%",
"height": "100%",
"type": "desktop",
"documentType": queryParams.documenttype,
"token": token,
"document": {
"title": fileInfo.BreadcrumbDocName || fileInfo.BaseFileName,
"url": userAuth.wopiSrc,
"fileType": fileInfo.FileExtension ? fileInfo.FileExtension.substr(1) : fileType,
"key": key,
"info": {
"folder": fileInfo.BreadcrumbFolderName
},
"permissions": {
"edit": !fileInfo.ReadOnly && fileInfo.UserCanWrite,
"review": (fileInfo.SupportsReviewing===false) ? false : (fileInfo.UserCanReview===false ? false : fileInfo.UserCanReview),
"copy": fileInfo.CopyPasteRestrictions!=="CurrentDocumentOnly" && fileInfo.CopyPasteRestrictions!=="BlockAll",
"print": !fileInfo.DisablePrint
}
},
"editorConfig": {
"mode": queryParams.mode,
"lang": queryParams.lang || "en-US",
"callbackUrl": JSON.stringify(userAuth),
"sharingSettingsUrl": fileInfo.FileSharingUrl,
"user": {
"id": fileInfo.UserId,
"name": fileInfo.IsAnonymousUser ? "" : fileInfo.UserFriendlyName
},
"customization": {
"about": true,
"goback": {
"url": fileInfo.BreadcrumbFolderUrl
},
"customer": {
"name": fileInfo.BreadcrumbBrandName,
"www": fileInfo.BreadcrumbBrandUrl
}
},
"coEditing": {
"mode": "fast",
"change": false
}
},
"events": {
"onAppReady": onAppReady,
"onDocumentStateChange": fileInfo.EditNotificationPostMessage ? onDocumentStateChange : undefined,
'onRequestEditRights': fileInfo.EditModePostMessage ? onRequestEditRights : undefined,
"onError": onError,
"onRequestClose": fileInfo.ClosePostMessage ? onRequestClose : undefined,
"onRequestRename": fileInfo.SupportsRename && fileInfo.UserCanRename ? onRequestRename : undefined,
"onRequestSharingSettings": fileInfo.FileSharingPostMessage ? requestSharingSettings : undefined
}
};
postMessageOrigin = fileInfo.PostMessageOrigin;
if (postMessageOrigin && (typeof postMessageOrigin === 'string') && postMessageOrigin.charAt(postMessageOrigin.length-1)=='/')
postMessageOrigin = postMessageOrigin.substring(0, postMessageOrigin.length - 1);
lang = config.editorConfig.lang;
startTime = Date.now();
docEditor = new DocsAPI.DocEditor("iframeEditor", config);
fixSize();
};
var fixSize = function () {
var wrapEl = document.getElementsByClassName("form");
if (wrapEl.length) {
wrapEl[0].style.height = screen.availHeight + "px";
window.scrollTo(0, -1);
wrapEl[0].style.height = window.innerHeight + "px";
}
};
if (window.addEventListener) {
window.addEventListener("load", connectEditor);
window.addEventListener("resize", fixSize);
} else if (window.attachEvent) {
window.attachEvent("onload", connectEditor);
window.attachEvent("onresize", fixSize);
}
</script>
</body>
</html>