-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension-dev-toolkit.html
executable file
·209 lines (198 loc) · 8.6 KB
/
extension-dev-toolkit.html
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
<html>
<head>
<script src="https://unpkg.com/post-robot/dist/post-robot.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<style>
.form-wrap {
padding: 20px 20px 20px 40px;
border-right: dashed #4CAF50;
text-align: center;
}
.extensionWrapper {
}
.title {
text-align: center;
margin: 10px 0 20px;
border-bottom: solid gray 1px;
}
.submitButton {
margin: 20px;
}
.submitButton button {
margin: 5px;
}
.footer {
width: 100%;
height: 40px;
line-height: 40px;
color: #4CAF50;
background-color: #f5f5f5;
text-align: right;
}
</style>
</head>
<body>
<div class="title">
<h1>Acoustic Content Authoring UI extension development toolkit</h1>
</div>
<div class="row">
<div class='form-wrap col-6'>
<div class='input' id='valueWrapper'>
<div class="contentLabel"><label for='value' >Value</label></div>
<textarea id='value' type='email' name='mail' autocomplete='off' rows="10" cols="50">{
"elementType": "text",
"value": "Some text goes here"
}</textarea>
</div>
<div class='input' id='typeWrapper'>
<div class="contentLabel"><label for='type' class="contentLabel">Definition</label></div>
<textarea id='type' autocomplete='off' rows="10" cols="50" >{}</textarea>
</div>
<div class='input' id='statusWrapper'>
<div class="contentLabel"><label for='status' class="contentLabel">Element is Valid:</label></div>
<select id="status" style="font-size: 20px;">
<option value="true">true</option>
<option value="false">false</option>
</select>
</div>
</div>
<div class="extensionWrapper col-6">
<div class="extensionInputWrapper">
<label for="extensionURL">Extension URL: </label><input type="text" id="extensionURL" size="50" value="https://pages.github.ibm.com/DX/ui-extensions-dev-toolkit/simplest-extension.html"/>
<div class="submitButton">
<button class="loadButton" onclick="createExtensionWithURL()">Init custom extension</button>
<button class="loadButton" onclick="createExtensionWithURL('https://pages.github.ibm.com/DX/ui-extensions-dev-toolkit/simplest-extension.html')">Load Simple Extension Inspector</button>
<button class="loadButton" onclick="createExtensionWithURL('https://pages.github.ibm.com/DX/sample-ui-extension-map/content-artifacts/assets/dxauth/ui-extension-map/index.html')">Load Google Map Sample</button>
<button class="loadButton" onclick="createExtensionWithURL('https://pages.github.ibm.com/DX/sample-ui-extension-email/content-artifacts/assets/dxauth/ui-extension-email/index.html')">Load Email Sample</button>
<button class="loadButton" onclick="createExtensionWithURL('https://pages.github.ibm.com/DX/sample-ui-extension-rte/content-artifacts/assets/dxauth/ui-extension-rte/')">Load RTE Sample</button>
</div>
</div>
<div class="extensionIFrameWrapper" id="extensionIFrameWrapper">
</div>
</div>
</div>
<div class="footer">
Powered by [email protected]
</div>
<script>
'use strict';
var valueInput = document.querySelector('#value');
var typeInput = document.querySelector('#type');
var statusInput = document.querySelector('#status');
var extensionURLInput = document.querySelector('#extensionURL');
var extensionIFrameWrapper = document.querySelector('#extensionIFrameWrapper');
var extensionIFrame;
var statusMessage = document.querySelector('#statusMessage');
var extensionListener;
var listenerArray = new Array();
function initExtension() {
setTimeout(function () {
extensionIFrame = document.querySelector('#extension-frame');
if (extensionIFrame) {
console.log('Extension iFrame exist now', extensionIFrame)
// let listener = postRobot.listener({ window: extensionIFrame});
extensionListener = postRobot.listener({window: extensionIFrame.contentWindow});
listenerArray.push(extensionListener.on('updateValue', event => {
console.log('UI Extension: receive updateValue, get: ', event.data);
var textValue;
try {
textValue = JSON.stringify(event.data.value, null, 4)
} catch (e) {
alert('Error parsing the JSON sent from the extension')
}
valueInput.value = textValue;
}));
listenerArray.push(extensionListener.on('getValue', event => {
console.log('UI Extension: receive getValue, return: ', valueInput.value);
var jsonValue;
try {
jsonValue = JSON.parse(valueInput.value)
} catch (e) {
alert('Error parsing the element JSON.')
}
console.log('UI Extension: receive getValue, return: ', jsonValue);
return jsonValue;
}));
listenerArray.push(extensionListener.on('getDefinition', event => {
console.log('UI Extension: receive getDefinition, return: ', typeInput.value);
var jsonValue;
try {
jsonValue = JSON.parse(typeInput.value)
} catch (e) {
alert('Error parsing the definition JSON.')
}
return jsonValue;
}));
listenerArray.push(extensionListener.on('updateHeight', event => {
if (event && event.data && event.data.height) {
console.log('UI Extension: receive updateHeight: ', event.data.height);
extensionIFrame.setAttribute('height', event.data.height);
} else {
console.log('UI Extension: receive updateHeight with invalid data format: ', event);
}
}));
listenerArray.push(extensionListener.on('updateValidStatus', event => {
if (event && event.data && event.data.valid != null) {
console.log('UI Extension: receive updateValidStatus: ', event.data.valid);
statusInput.value = event.data.valid;
if (event.data.valid === false && event.data.showValidateMessage) {
if (event.data.customMessage && event.data.customMessage.trim() !== '') {
statusMessage.innerHTML = event.data.customMessage;
}
}
} else {
console.log('UI Extension: receive updateValidStatus with invalid data format: ', event);
}
}));
// // Validate text element for ready
// $scope.$on('valueValidate', (angularEvent: ng.IAngularEvent) => {
// postRobot.send(extensionIFrame.contentWindow, 'valueValidate', {}).then(event => {
// if (event && event.data && event.data.valid != null) {
// statusInput.value = event.data.valid;
// if (event.data.valid === false && event.data.showValidateMessage) {
// if (event.data.customMessage && event.data.customMessage.trim() !== '') {
// statusMessage.innerHTML = event.data.customMessage;
// }
// }
// }
// });
// });
} else {
console.log('Extension iFrame not exist yet, try again')
initExtension();
}
}, 100);
}
function createExtensionWithURL(url) {
var rawUrl = url;
if (rawUrl == null) {
rawUrl = extensionURLInput.value;
}
if (rawUrl.trim() === '' || rawUrl.indexOf('http') !== 0) {
alert('invalid extension url')
return;
}
extensionIFrameWrapper.innerHTML = '';
if (extensionListener) {
listenerArray.forEach(function(listener) {
listener.cancel();
listener = null;
});
listenerArray = new Array();
extensionListener = null;
}
var queryParamChar = !rawUrl.includes('?') ? '?' : '&';
var uniqueUrl = rawUrl + queryParamChar + 'unique=' + getNumberFromMilliseconds(4);
var iframeInnerHTML = `<iframe iframe width='100%' height='300px' frameborder='0' src=${uniqueUrl} id='extension-frame' name='extension-frame'></iframe>`;
extensionIFrameWrapper.innerHTML = iframeInnerHTML;
initExtension();
}
function getNumberFromMilliseconds(digits = 4) {
let time = new Date().getTime().toString();
if (time.length >= digits) {
time = time.substr(time.length - digits);
}
return time;
}
</script></body>
</html>