|
9 | 9 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
10 | 10 | */ |
11 | 11 |
|
12 | | -function postMessage(msg) { |
13 | | - document |
14 | | - .getElementById('collabora-online-viewer') |
15 | | - .contentWindow.postMessage(JSON.stringify(msg), '*'); |
16 | | -} |
| 12 | +(function (window) { |
17 | 13 |
|
18 | | -function postReady() { |
19 | | - postMessage({ MessageId: 'Host_PostmessageReady' }); |
20 | | - postMessage({ |
21 | | - MessageId: 'Hide_Button', |
22 | | - Values: { |
23 | | - id: 'renamedocument' |
| 14 | + const iframeDefaultAttributes = { |
| 15 | + id: 'collabora-online-viewer', |
| 16 | + name: 'collabora-online-viewer', |
| 17 | + class: 'cool-frame__iframe', |
| 18 | + allow: 'clipboard-read *; clipboard-write *', |
| 19 | + }; |
| 20 | + |
| 21 | + function createElement(tag, attributes) { |
| 22 | + const element = document.createElement(tag); |
| 23 | + for (const k in attributes) { |
| 24 | + element.setAttribute(k, attributes[k]); |
24 | 25 | } |
25 | | - }); |
26 | | -} |
| 26 | + return element; |
| 27 | + } |
27 | 28 |
|
28 | | -function receiveMessage(close_button_url, event) { |
29 | | - const msg = JSON.parse(event.data); |
30 | | - if (!msg) { |
31 | | - return; |
| 29 | + function buildAndSubmitForm(action, payload, target) { |
| 30 | + const form = createElement('form', { |
| 31 | + action, |
| 32 | + enctype: 'multipart/form-data', |
| 33 | + method: 'post', |
| 34 | + target, |
| 35 | + }); |
| 36 | + for (const name in payload) { |
| 37 | + const input = createElement('input', { |
| 38 | + type: 'hidden', |
| 39 | + name, |
| 40 | + value: payload[name], |
| 41 | + }); |
| 42 | + form.append(input); |
| 43 | + } |
| 44 | + document.body.append(form); |
| 45 | + form.submit(); |
| 46 | + form.remove(); |
32 | 47 | } |
33 | 48 |
|
34 | | - switch (msg.MessageId) { |
35 | | - case 'App_LoadingStatus': |
36 | | - if (msg.Values && msg.Values.Status === 'Document_Loaded') { |
37 | | - postReady(); |
38 | | - } |
39 | | - break; |
| 49 | + function postToEditorFrame(iframe, id, values) { |
| 50 | + iframe.contentWindow.postMessage(JSON.stringify({ |
| 51 | + MessageId: id, |
| 52 | + Values: values, |
| 53 | + }), '*'); |
| 54 | + } |
40 | 55 |
|
41 | | - case 'UI_Close': |
42 | | - if (close_button_url) { |
43 | | - if (msg.Values && msg.Values.EverModified) { |
44 | | - const reply = { MessageId: 'Action_Close' }; |
45 | | - postMessage(reply); |
46 | | - } |
47 | | - if (window.parent.location === window.location) { |
48 | | - // eslint-disable-next-line no-restricted-globals |
49 | | - document.location.href = close_button_url; |
50 | | - } else { |
51 | | - /* we send back the UI_Close message to the parent frame. */ |
52 | | - window.parent.postMessage(event.data); |
| 56 | + function receiveMessage(iframe, closeButtonUrl, event) { |
| 57 | + const msg = JSON.parse(event.data); |
| 58 | + if (!msg) { |
| 59 | + return; |
| 60 | + } |
| 61 | + const postToEditor = postToEditorFrame.bind(null, iframe); |
| 62 | + |
| 63 | + switch (msg.MessageId) { |
| 64 | + case 'App_LoadingStatus': |
| 65 | + if (msg.Values && msg.Values.Status === 'Document_Loaded') { |
| 66 | + postToEditor('Host_PostmessageReady'); |
| 67 | + postToEditor('Hide_Button', {id: 'renamedocument'}); |
53 | 68 | } |
54 | | - } |
55 | | - break; |
56 | | - } |
57 | | -} |
| 69 | + break; |
58 | 70 |
|
59 | | -function loadDocument(wopiClient, wopiSrc, options = {}) { |
60 | | - let wopiUrl = `${wopiClient}WOPISrc=${wopiSrc}`; |
61 | | - if (options.close_button_url) { |
62 | | - wopiUrl += '&closebutton=true'; |
| 71 | + case 'UI_Close': |
| 72 | + if (closeButtonUrl) { |
| 73 | + if (msg.Values && msg.Values.EverModified) { |
| 74 | + postToEditor('Action_Close'); |
| 75 | + } |
| 76 | + if (window.parent.location === window.location) { |
| 77 | + // eslint-disable-next-line no-restricted-globals |
| 78 | + document.location.href = closeButtonUrl; |
| 79 | + } |
| 80 | + else { |
| 81 | + /* we send back the UI_Close message to the parent frame. */ |
| 82 | + window.parent.postMessage(event.data); |
| 83 | + } |
| 84 | + } |
| 85 | + break; |
| 86 | + } |
63 | 87 | } |
64 | 88 |
|
65 | | - window.addEventListener( |
66 | | - 'message', |
67 | | - receiveMessage.bind(null, options.close_button_url), |
68 | | - false, |
69 | | - ); |
| 89 | + document.addEventListener('DOMContentLoaded', function () { |
| 90 | + // Only one editor per page/frame is supported, because the iframe has an |
| 91 | + // id attribute that would clash otherwise. |
| 92 | + const placeholder_element = document.querySelector('[data-collabora-online-editor]'); |
| 93 | + if (!placeholder_element) { |
| 94 | + return; |
| 95 | + } |
| 96 | + const json = placeholder_element.getAttribute('data-collabora-online-editor'); |
| 97 | + const data = JSON.parse(json); |
| 98 | + const iframe = createElement('iframe', { |
| 99 | + ...iframeDefaultAttributes, |
| 100 | + ...data.iframe_attributes, |
| 101 | + }); |
| 102 | + const div = createElement('div', {class: 'cool-frame'}); |
| 103 | + div.appendChild(iframe); |
| 104 | + placeholder_element.after(div); |
| 105 | + placeholder_element.remove(); |
70 | 106 |
|
71 | | - const formElem = document.getElementById('collabora-submit-form'); |
| 107 | + window.addEventListener('message', receiveMessage.bind(null, iframe, data.close_button_url)); |
72 | 108 |
|
73 | | - if (!formElem) { |
74 | | - console.log('error: submit form not found'); |
75 | | - return; |
76 | | - } |
| 109 | + buildAndSubmitForm(data.action, data.payload, iframe.id); |
| 110 | + }); |
77 | 111 |
|
78 | | - formElem.action = wopiUrl; |
79 | | - formElem.submit(); |
80 | | -} |
| 112 | +})(window); |
0 commit comments