-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
261 lines (228 loc) · 9.14 KB
/
app.js
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
$(function () {
$("#tabs").tabs();
});
function LiquidJsProcess() {
let data = $("#liquidjs-data").val();
let template = $("#liquidjs-template").val();
try {
var dataJson = JSON.parse(data);
} catch (e) {
alert("Failed to JSON parse the data!\n" + JSON.stringify(e));
console.error("Failed to JSON parse the data!", e);
return;
}
let liquid = new liquidjs.Liquid();
try {
let result = liquid.parseAndRenderSync(template, dataJson);
$("#liquidjs-result").text(result);
} catch (e) {
alert("Failed to process Liquid template!\n" + JSON.stringify(e));
console.error("Failed to process Liquid template!", e);
return;
}
}
async function WordFillerProcess() {
let apiKey = $("#wordfiller-api").val();
let data = $("#wordfiller-data").val();
let file = $('#wordfiller-file').prop('files')[0];
let targeturl;
if(document.getElementById("wordfiller-target").value == "prod") {
targeturl = "https://word.connectors.talxis.com/api/FillWordTemplate";
} else {
targeturl = "http://localhost:" + document.getElementById("wordfiller-port-value").value + "/api/FillWordTemplate";
}
const reader = new FileReader();
reader.addEventListener("load", async function () {
var fileBase64 = reader.result;
var filePost = fileBase64.split(',')[1];
var result = await fetch(`${targeturl}?code=${apiKey}`, {
method: 'POST',
mode: 'cors',
body: JSON.stringify({
"document": filePost,
"$properties": JSON.parse(data)
})
});
if (!result.ok) {
alert("Something went wrong with processing the file!");
return;
} else {
var responseFile = await result.text();
downloadBase64File("application/vnd.openxmlformats-officedocument.wordprocessingml.document", responseFile, "filled_document.docx");
}
}, false);
reader.readAsDataURL(file);
}
async function Word2PdfProcess() {
let apiKey = $("#word2pdf-api").val();
let file = $('#word2pdf-file').prop('files')[0];
let targeturl;
if(document.getElementById("wordtopdf-target").value == "prod") {
targeturl = "https://word.connectors.talxis.com/api/WordToPDF";
} else {
targeturl = "http://localhost:" + document.getElementById("wordtopdf-port-value").value + "/api/WordToPDF";
}
const reader = new FileReader();
reader.addEventListener("load", async function () {
var fileBase64 = reader.result;
var filePost = fileBase64.split(',')[1];
var result = await fetch(`${targeturl}?code=${apiKey}`, {
method: 'POST',
mode: 'cors',
body: JSON.stringify({
"document": filePost
})
});
if (!result.ok) {
alert("Something went wrong with processing the file!");
return;
} else {
var responseFile = await result.text();
downloadBase64File("application/pdf", responseFile, "converted_document.pdf");
}
}, false);
reader.readAsDataURL(file);
}
function datafeedOnChangeTarget() {
if(document.getElementById("datafeed-target").value === "local") {
document.getElementById("datafeed-port").style.display = 'inline-block'
document.getElementById("datafeed-frame").setAttribute("src", "https://localhost:5001/index.html");
} else {
document.getElementById("datafeed-port").style.display = 'none'
document.getElementById("datafeed-frame").setAttribute("src", "https://datafeed.connectors.talxis.com/index.html");
}
}
datafeedOnChangeTarget(); // call it for the first time
function parserOnchangeSolutionParser() {
if(document.getElementById("solutionparser-target").value === "local") {
document.getElementById("solutionparser-port").style.display = 'inline-block'
} else {
document.getElementById("solutionparser-port").style.display = 'none'
}
}
function parserOnchangeWordFiller() {
if(document.getElementById("wordfiller-target").value === "local") {
document.getElementById("wordfiller-port").style.display = 'inline-block'
} else {
document.getElementById("wordfiller-port").style.display = 'none'
}
}
function parserOnchangeWordToPDF() {
if(document.getElementById("wordtopdf-target").value === "local") {
document.getElementById("wordtopdf-port").style.display = 'inline-block'
} else {
document.getElementById("wordtopdf-port").style.display = 'none'
}
}
async function ParseSolution() {
var fileInput = document.getElementById("solutionparser-file");
var targeturl;
fileList = [];
for (let i = 0; i < fileInput.files.length; i++) {
fileList.push((await toBase64(fileInput.files[i])).split(',')[1]);
}
if(document.getElementById("solutionparser-target").value == "prod") {
targeturl = "https://metadata.services.talxis.com/api/parseSolution/";
} else {
targeturl = "http://localhost:" + document.getElementById("solutionparser-port-value").value + "/api/parseSolution/";
}
var result = await fetch(targeturl + document.getElementById("solutionparser-option").value, {
method: 'POST',
mode: 'cors',
body: JSON.stringify(fileList)
});
if (!result.ok) {
alert("Something went wrong with parsing the solutions!");
return;
} else {
var responseFile = await result.text();
var languageCode;
switch (document.getElementById("solutionparser-option").value) {
case "dbml":
case "sql":
languageCode = "sql"
break;
case "edmx":
case "ribbon":
languageCode = "xml"
responseFile = formatXml(responseFile);
break;
default:
break;
}
document.getElementById('solutionparser-result').innerHTML = "";
let editor = monaco.editor.create(document.getElementById('solutionparser-result'), {
value: responseFile,
language: languageCode
});
editor = monaco.editor.colorizeElement(document.getElementById('solutionparser-result'));
}
}
function downloadBase64File(contentType, base64Data, fileName) {
const linkSource = `data:${contentType};base64,${base64Data}`;
const downloadLink = document.createElement("a");
downloadLink.href = linkSource;
downloadLink.download = fileName;
downloadLink.click();
}
function formatXml(xml) {
const PADDING = ' '.repeat(2);
const reg = /(>)(<)(\/*)/g;
let pad = 0;
xml = xml.replace(reg, '$1\r\n$2$3');
return xml.split('\r\n').map((node, index) => {
let indent = 0;
if (node.match(/.+<\/\w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^<\/\w/) && pad > 0) {
pad -= 1;
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
pad += indent;
return PADDING.repeat(pad - indent) + node;
}).join('\r\n');
}
function loadEditor() {
// Based on https://jsfiddle.net/developit/bwgkr6uq/ which just works but is based on unpkg.com.
// Provided by loader.min.js.
require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' } });
window.MonacoEnvironment = { getWorkerUrl: () => proxy };
let proxy = URL.createObjectURL(new Blob([`
self.MonacoEnvironment = {
baseUrl: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min'
};
importScripts('https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs/base/worker/workerMain.min.js');
`], { type: 'text/javascript' }));
require(["vs/editor/editor.main"], function () {
});
} loadEditor();
const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
var prettifyXml = function (sourceXml) {
var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
var xsltDoc = new DOMParser().parseFromString([
// describes how we want to modify the XML - indent everything
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
' <xsl:strip-space elements="*"/>',
' <xsl:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes
' <xsl:value-of select="normalize-space(.)"/>',
' </xsl:template>',
' <xsl:template match="node()|@*">',
' <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
' </xsl:template>',
' <xsl:output indent="yes"/>',
'</xsl:stylesheet>',
].join('\n'), 'application/xml');
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
var resultXml = new XMLSerializer().serializeToString(resultDoc);
return resultXml;
};