summaryrefslogtreecommitdiffstats
path: root/browser/html/framed.html
blob: ef8721c6565f386d610b317fdb4e26253ef84daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>

<!-- Proof of concept of running cool.html in an iframe. Also
     shows how to, from outside the iframe, invoke Python scripting in
     the underlying LibreOffice instance that manipulates the document
     being edited.

     The top part of this page has several forms. The first one with
     three input fields: "x", "y", and "color", a submit button, and a
     "result" field used for output only.

     When the submit button is pressed, the input fields are passed as
     a postMessage to the iframe. The message also specifies what
     Python script to call. (In this demo case, the 'SetCellColor'
     script in scripting/examples/python/SetCellColor.py in
     LibreOffice core.) The parameters are then in our JavaScript
     passed on to the Python script that acts on the document open in
     the iframe. The Python script returns a value, which gets passed
     to cool in a unocommandresult: message, and passed on to the
     event listener on this page, which writes it to the output field.

     The other forms call other Python functions in other files. (In
     particular, in NamedRanges.py in the same directory.) The forms
     invoke Python functions that list, add, and delete named ranges
     in a Calc document being edited.

     To test this, do 'make run', and then in your browser open:
     https://localhost:9980/browser/dist/framed.html?file_path=/opt/libreoffice/online/test/data/hello-world.ods
-->

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Online Editor</title>

    <script>

      // These functions named call* are invoked when submitting the
      // forms defined in the body of this web page. They show how to
      // post a message to the iframe where Online is running, that
      // will cause a Python script in the underlying LibreOffice
      // instance to be invoked.

      function callSetCellColor() {
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'Host_PostmessageReady'}), '*');
        var x = document.forms[0].elements['x'].value;
        var y = document.forms[0].elements['y'].value;
        var color = document.forms[0].elements['color'].value;
        console.log('x=' + x + ' y=' + y + ' color=' + color);
        color = parseInt('0x' + color.substring(1));
        console.log('x=' + x + ' y=' + y + ' color=' + color);
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'CallPythonScript',
                                                     'SendTime': Date.now(),
                                                     'ScriptFile': 'SetCellColor.py',
                                                     'Function': 'SetCellColor',
                                                     'Values': {'x': {'type': 'long', 'value': x},
                                                                'y': {'type': 'long', 'value': y},
                                                                'color': {'type': 'long', 'value': color}
                                                                }
                                                     }),
                                     '*');
      }

      function callGetNamedRanges() {
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'Host_PostmessageReady'}), '*');
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'CallPythonScript',
                                                     'SendTime': Date.now(),
                                                     'ScriptFile': 'NamedRanges.py',
                                                     'Function': 'GetNamedRanges',
                                                     'Values': null
                                                     }),
                                     '*');
      }

      function callAddNamedRange() {
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'Host_PostmessageReady'}), '*');
        // Our form below takes input in A1 format, the Python script wants zero-based indexes and width and height
        var abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var name = document.forms['add-named-range-form'].elements['name'].value;
        var x0 = document.forms['add-named-range-form'].elements['x0'].value;
        var x1 = document.forms['add-named-range-form'].elements['x1'].value;
        var y0 = parseInt(document.forms['add-named-range-form'].elements['y0'].value);
        var y1 = parseInt(document.forms['add-named-range-form'].elements['y1'].value);
        console.log('x0=' + x0 + ', x1=' + x1)
        x0 = abc.indexOf(x0);
        x1 = abc.indexOf(x1);
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'CallPythonScript',
                                                     'SendTime': Date.now(),
                                                     'ScriptFile': 'NamedRanges.py',
                                                     'Function': 'DefineNamedRange',
                                                     'Values': {'sheet': {'type': 'string', 'value': 'Sheet1'},
								'x0': {'type': 'long', 'value': x0},
								'y0': {'type': 'long', 'value': y0-1},
								'width': {'type': 'long', 'value': x1-x0+1},
								'height': {'type': 'long', 'value': y1-y0+1},
								'name': {'type': 'string', 'value': name}
                                                               }
                                                     }),
                                     '*');
      }

      function callDeleteNamedRange() {
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'Host_PostmessageReady'}), '*');
        var name = document.forms['delete-named-range-form'].elements['name'].value;
        window.frames[0].postMessage(JSON.stringify({'MessageId': 'CallPythonScript',
                                                     'SendTime': Date.now(),
                                                     'ScriptFile': 'NamedRanges.py',
                                                     'Function': 'DeleteNamedRange',
                                                     'Values': {'name': {'type': 'string', 'value': name}
                                                               }
                                                     }),
                                     '*');
      }

      // This function is invoked when the iframe posts a message back.

      function receiveMessage(event) {
        var msg = JSON.parse(event.data);
        console.log('==== framed.html receiveMessage: ' + event.data);
        console.log('                                 ' + msg);

        // We are only interested in messages that are marked as being
        // a result from a Python script invoked by one of the call*
        // functions above.

        if (msg.hasOwnProperty('MessageId') &&
            msg.MessageId === 'CallPythonScript-Result' &&
            msg.hasOwnProperty('Values') &&
            msg.Values.hasOwnProperty('commandName') &&
	    msg.Values.hasOwnProperty('success') &&
	    msg.Values.success == 'true' &&
	    msg.Values.hasOwnProperty('result') &&
	    msg.Values.result.hasOwnProperty('value')) {
          // We are only interested in the result of the SetCellColor and GetNamedRanges functions
          if (msg.Values.commandName === 'vnd.sun.star.script:SetCellColor.py$SetCellColor?language=Python&location=share') {
	    document.forms['cell-colour-form'].elements['result'].readOnly = false;
	    document.forms['cell-colour-form'].elements['result'].value = msg.Values.result.value;
	    document.forms['cell-colour-form'].elements['result'].readOnly = true;
          }
          else if (msg.Values.commandName === 'vnd.sun.star.script:NamedRanges.py$GetNamedRanges?language=Python&location=share') {
	    document.forms['get-named-ranges-form'].elements['result'].readOnly = false;
            var index = 0;
            var result = '';
            while (msg.Values.result.value.hasOwnProperty(index.toString())) {
              if (msg.Values.result.value[index.toString()].value.hasOwnProperty('0') &&
                  msg.Values.result.value[index.toString()].value['0'].hasOwnProperty('value') &&
                  msg.Values.result.value[index.toString()].value.hasOwnProperty('1') &&
                  msg.Values.result.value[index.toString()].value['1'].hasOwnProperty('value'))
              result += msg.Values.result.value[index.toString()].value[0].value + ": " +
                        msg.Values.result.value[index.toString()].value[1].value + "\n";
              index++;
            }
	    document.forms['get-named-ranges-form'].elements['result'].value = result;
	    document.forms['get-named-ranges-form'].elements['result'].readOnly = true;
          }
        }
      }

      // 'main' code of this <script> block, run when page is being
      // rendered. Install the message listener.
      window.addEventListener("message", receiveMessage, false);

    </script>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>

  <body style="user-select: none;">
    <h3>Calc functions</h3>
    <p>
      <form id="cell-colour-form">
	Cell: (<input type="number" name="x" min="0" max="20" value="0">, <input type="number" name="y" min="0" max="20" value="0">),
	colour: <input type="text" name="color" value="#008000">
	<br>
	Click <button onclick="callSetCellColor(); return false;">here</button>
	to send message to iframe below. It returned <input type="text" name="result" value="" readonly>.
      </form>
    </p>

    <form id="get-named-ranges-form">
      Click <button onclick="callGetNamedRanges(); return false;">here</button> to get a list of named ranges in the document:
      <textarea name="result" value="" rows="10" cols="80"></textarea>
    </form>

    <form id="add-named-range-form">
      Click <button onclick="callAddNamedRange(); return false;">here</button> to add a new named range called
      <input type="text" name="name"> on Sheet1 from (A1 syntax)
      <input type="text" name="x0" value="A" size="1" pattern="[A-Z]{1}"> <input type="number" name="y0", min="1" max="999" value="1"> to
      <input type="text" name="x1" value="B" size="1" pattern="[A-Z]{1}"> <input type="number" name="y1", min="1" max="999" value="3">.
    </form>

    <form id="delete-named-range-form">
      Click <button onclick="callDeleteNamedRange(); return false;">here</button> to delete the named range called <input type="text" name="name">
    </form>

    <h3>Document frame</h3>

    <h4>If the frame fails to load click <a id="link">here and accept security bits</a></h4>
    <iframe id="frame" height="1000" width="1000"></iframe>

    <!-- 127.0.0.2 allows us to test CSS issues -->
    <script>
      var uriBase = document.location.protocol + "//127.0.0.2:9980/browser/42/cool.html" + window.location.search;
      var iframe = document.getElementById("frame");
      iframe.src = uriBase + "&NotWOPIButIframe=true";
      var msg = document.getElementById("link");
      link.href = uriBase;
    </script>
  </body>
</html>