-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
357 lines (353 loc) · 13.4 KB
/
Copy patheditor.js
File metadata and controls
357 lines (353 loc) · 13.4 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const headers = {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8",
"Prefer": "odata.include-annotations=\"*\""
}
const notificationType = {
info: 'info',
error: 'error',
success: 'success'
}
const webresourceType = {
html: 1,
css: 2,
javascript: 3,
xml: 4,
png: 5,
jpg: 6,
gif: 7,
xap: 8,
xsl: 9,
ico: 10,
svg: 11,
resx: 12
}
var notification;
var button = document.getElementById("search");
var spinner = document.getElementById("spinner");
async function search() {
var webresources = [];
var searchText = document.getElementById("searchInput").value;
spinnerVisibility(true);
// Get all list items except the header
const listItems = document.querySelectorAll("#webresourceslist li:not(#header)");
// Remove existing li
listItems.forEach(element => element.remove());
var response = await fetch(window.location.origin + "/api/data/v9.2/webresourceset?$select=displayname,webresourceid,webresourcetype,modifiedon,name&$filter=contains(name,'" + searchText + "') or contains(displayname,'" + searchText + "')&$orderby=name", {
method: "GET",
headers: headers
})
webresources = await response.json();
webresources = webresources.value;
const ul = document.getElementById("webresourceslist");
if (webresources.length > 0) {
document.getElementById("header").style.display = "inline";
}
else {
var li = document.getElementById("header");
li.style.display = "none";
li.innerHTML = "";
showNotification(searchText + " not found. Please check and try again.", notificationType.error, 5);
}
const webresource = webresources.map((each) => {
const li = document.createElement("li");
const a = document.createElement("a");
a.id = each.webresourceid;
a.href = "#";
a.onclick = selectWebresource;
const nameDiv = document.createElement("div");
nameDiv.className = "name";
nameDiv.textContent = each.name;
const displaynameDiv = document.createElement("div");
displaynameDiv.className = "displayname";
displaynameDiv.textContent = each.displayname;
const typeDiv = document.createElement("div");
typeDiv.className = "type";
typeDiv.textContent = getWebResourceTypeString(each.webresourcetype);
const modifiedonDiv = document.createElement("div");
modifiedonDiv.className = "modifiedon";
modifiedonDiv.textContent = each.modifiedon;
a.appendChild(nameDiv);
a.appendChild(displaynameDiv);
a.appendChild(typeDiv);
a.appendChild(modifiedonDiv);
li.appendChild(a);
return li;
});
// Append list items to ul
ul.append(...webresource);
spinnerVisibility(false);
}
async function selectWebresource(event) {
let target = event.target;
while (target.tagName !== 'A') {
target = target.parentElement;
}
document.getElementById("searchContainer").remove();
document.getElementById("editorContainer").style.display = "block";
var language;
var webResource = await getWebresource(target.id);
switch (webResource.webresourcetype) {
case webresourceType.html: language = 'html';
break;
case webresourceType.css: language = 'css';
break;
case webresourceType.javascript: language = 'javascript';
break;
default: language = 'text'
}
// Configure the loader to use the minified version of the editor
self.MonacoEnvironment = {
getWorkerUrl: function (workerId, label) {
return `data:text/javascript;charset=utf-8,${encodeURIComponent(`
self.MonacoEnvironment = {
baseUrl: 'https://unpkg.com/monaco-editor@0.37.1/min/'
};
importScripts('https://unpkg.com/monaco-editor@0.37.1/min/vs/base/worker/workerMain.js');`
)}`;
}
};
// Load the editor and create an instance
require.config({ paths: { 'vs': 'https://unpkg.com/monaco-editor@0.37.1/min/vs' } });
require(['vs/editor/editor.main'], function () {
var editor = monaco.editor.create(document.getElementById('editorContainer'), {
value: webResource.content,
language: language,
theme: 'vs-dark',
automaticLayout: true
});
// Define the command to execute when Ctrl+S is pressed (Save the webresource)
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, function () {
updateWebresource(editor.getValue(), webResource.webresourceid);
})
// Define the command to execute when Ctrl+P is pressed (Publish the webresource)
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyP, function () {
publishWebResource(webResource.webresourceid);
})
// Define the command to execute when Ctrl+O is pressed (Open the webresource)
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyO, function () {
openWebResource(webResource.name);
})
// Define the command to execute when Ctrl+Shift+P is pressed (Update and Publish the webresource)
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyP, function () {
updateAndPublish(editor.getValue(), webResource.webresourceid);
})
})
}
function getWebResourceTypeString(typeNumber) {
switch (typeNumber) {
case webresourceType.html:
return 'html';
case webresourceType.css:
return 'css';
case webresourceType.javascript:
return 'javascript';
case webresourceType.xml:
return 'xml';
case webresourceType.png:
return 'png';
case webresourceType.jpg:
return 'jpg';
case webresourceType.gif:
return 'gif';
case webresourceType.xap:
return 'xap';
case webresourceType.xsl:
return 'xsl';
case webresourceType.ico:
return 'ico';
case webresourceType.svg:
return 'svg';
case webresourceType.resx:
return 'resx';
default:
return 'unknown format';
}
}
function showNotification(text, type, duration) {
notification.innerText = text;
notification.style.display = 'block'
notification.className = type
duration && setTimeout(hideNotification, duration * 1000);
}
function hideNotification() {
notification.style.display = 'none'
}
async function onLoad() {
notification = document.getElementById("notification");
document.getElementById("searchInput").focus();
// Add event listener to search input text box to detect Enter key press
document.getElementById("searchInput").addEventListener("keyup", function (event) {
if (event.key === 'Enter') {
// Call search function when user presses Enter key
search();
}
});
document.getElementById("editorContainer").style.display = "none";
createCustomAPI();
}
async function getWebresource(webresourceid) {
try {
var webResource = {
name: null,
webresourceid: webresourceid,
content: null,
webresourcetype: null
}
showNotification("Retrieving webresource, please wait...", notificationType.info)
var response = await fetch(window.location.origin + "/api/data/v9.2/webresourceset(" + webresourceid + ")?$select=content,name,webresourceid,webresourcetype", {
method: "GET",
headers: headers
})
const data = await response.json();
const encodedContent = data["content"];
webResource.content = atob(encodedContent);
webResource.webresourceid = data["webresourceid"];
webResource.webresourcetype = data["webresourcetype"];
webResource.name = data["name"]
showNotification("Webresource retrieved successfully. Use CTRL+S to save, CTRL+P to publish or CTRL+SHIFT+P to save and publish to D365.", notificationType.success, 10);
document.title = webResource.name;
return webResource;
}
catch (ex) {
showNotification(ex.message, notificationType.error);
}
}
async function updateWebresource(content, webresourceid) {
try {
var body = JSON.stringify(
{
"content": btoa(content),
"webresourceid": webresourceid
}
)
showNotification("Saving webresource...", notificationType.info);
await fetch(window.location.origin + "/api/data/v9.2/web_update", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: body
})
showNotification("Webresource saved successfully. Press CTRL+P to publish it.", notificationType.success, 5);
}
catch (ex) {
showNotification(ex.message, notificationType.error)
}
}
async function publishWebResource(webresourceid) {
try {
var data = {
ParameterXml: "<importexportxml><webresources><webresource>{" + webresourceid + "}</webresource></webresources></importexportxml>"
};
showNotification("Publishing webresource...", notificationType.info);
var response = await fetch(window.location.origin + "/api/data/v9.2/PublishXml", {
method: "POST",
headers: headers,
body: JSON.stringify(data)
});
if (response.ok) {
showNotification("Web resource published successfully.", notificationType.success, 5);
} else {
showNotification(response.statusText, notificationType.error);
}
} catch (error) {
showNotification(error.message, notificationType.error);
}
}
async function updateAndPublish(content, webresourceid) {
await updateWebresource(content, webresourceid);
await publishWebResource(webresourceid);
}
function openWebResource(name) {
const url = window.location.origin + '/WebResources/' + name;
window.open(url, "_blank");
}
async function createCustomAPI() {
try {
var response = await fetch(window.location.origin + "/api/data/v9.2/customapis?$filter=uniquename eq 'web_update' &$select=customapiid")
const data = await response.json();
if (data.value.length == 0) {
// API doesn't exist so create it (in default solution)
body = {
"allowedcustomprocessingsteptype": 0,
"boundentitylogicalname": null,
"uniquename": "web_update",
"displayname": "Update Webresource",
"bindingtype": 0,
"executeprivilegename": null,
"isfunction": false,
"isprivate": false,
"name": "UpdateWebresource",
"description": "UpdateWebresource",
"iscustomizable": {
"Value": true
},
"CustomAPIRequestParameters": [
{
"type": 10,
"isoptional": false,
"displayname": "UpdateWebresource.content",
"name": "UpdateWebresource.content",
"uniquename": "content",
"logicalentityname": null,
"description": "UpdateWebresource.content",
"iscustomizable": {
"Value": true
}
},
{
"type": 10,
"isoptional": false,
"displayname": "UpdateWebresource.webresourceid",
"name": "UpdateWebresource.webresourceid",
"uniquename": "webresourceid",
"logicalentityname": null,
"description": "UpdateWebresource.webresourceid",
"iscustomizable": {
"Value": true
}
}
],
"CustomAPIResponseProperties": [
{
"type": 10,
"name": "UpdateWebresource.response",
"logicalentityname": null,
"displayname": "UpdateWebresource.response",
"uniquename": "response",
"description": "UpdateWebresource.response",
"iscustomizable": {
"Value": true
}
}
],
"PluginTypeId@odata.bind": "plugintypes(a8de3897-c9de-480c-8007-9e618c9d2ea8)"
}
await fetch(window.location.origin + "/api/data/v9.2/customapis", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'MSCRM.SolutionUniqueName': 'Default'
},
body: JSON.stringify(body)
})
}
}
catch (error) {
showNotification("Error in createCustomAPI: " + error.message);
}
}
function spinnerVisibility(visible) {
if (visible) {
// show spinner
button.disabled = true;
spinner.style.display = "inline-block";
}
else {
spinner.style.display = "none";
button.disabled = false;
}
}