@senx/discovery-code
Version:
Discovery Code Editor
182 lines (179 loc) • 6.59 kB
JavaScript
/*
* Copyright 2023 SenX S.A.S.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Utils {
static mergeDeep(...sources) {
// Variables
const extended = {};
const deep = true;
let i = 0;
// Merge the object into the extended object
// Loop through each object and conduct a merge
for (; i < sources.length; i++) {
const obj = sources[i];
Utils.merge(obj, extended, deep);
}
return extended;
}
static unsescape(str) {
return str.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/"/g, '"')
.replace(/'/g, '\'')
.replace(/&/g, '&');
}
static merge(obj, extended, deep) {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
// If property is an object, merge properties
if (deep && Object.prototype.toString.call(obj[prop]) === '[object Object]') {
extended[prop] = Utils.mergeDeep(extended[prop], obj[prop]);
}
else {
extended[prop] = obj[prop];
}
}
}
}
static toArray(obj) {
const arr = [];
Object.keys(obj).forEach(k => arr.push(obj[k]));
return arr;
}
static httpPost(theUrl, payload, headers) {
return new Promise((resolve, reject) => {
const xmlHttp = new XMLHttpRequest();
const resHeaders = {};
xmlHttp.onreadystatechange = () => {
xmlHttp.getAllResponseHeaders().split('\n').forEach(header => {
if (header.trim() !== '') {
const h = header.split(':');
resHeaders[h[0].trim()] = h[1].trim().replace('\r', '');
}
});
if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
resolve({
data: xmlHttp.responseText,
headers: resHeaders,
status: {
ops: parseInt(resHeaders['x-warp10-ops'], 10),
elapsed: parseInt(resHeaders['x-warp10-elapsed'], 10),
fetched: parseInt(resHeaders['x-warp10-fetched'], 10),
},
});
}
else if (xmlHttp.readyState === 4 && xmlHttp.status >= 400) {
reject({
statusText: xmlHttp.statusText,
status: xmlHttp.status,
url: theUrl,
message: `line #${resHeaders['x-warp10-error-line']}: ${resHeaders['x-warp10-error-message']}`,
detail: {
mess: resHeaders['x-warp10-error-message'],
line: resHeaders['x-warp10-error-line'],
},
});
}
else if (xmlHttp.readyState === 4 && xmlHttp.status === 0) {
reject({
statusText: theUrl + ' is unreachable',
status: 404,
url: theUrl,
message: theUrl + ' is unreachable',
detail: {
mess: theUrl + ' is unreachable',
line: -1
},
});
}
};
xmlHttp.open('POST', theUrl, true); // true for asynchronous
xmlHttp.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
Object.keys(headers || {})
.filter(h => h.toLowerCase() !== 'accept' && h.toLowerCase() !== 'content-type')
.forEach(h => xmlHttp.setRequestHeader(h, headers[h]));
xmlHttp.send(payload);
});
}
static isArray(value) {
return value && typeof value === 'object' && value instanceof Array && typeof value.length === 'number'
&& typeof value.splice === 'function' && !(value.propertyIsEnumerable('length'));
}
}
/*
* Copyright 2020 SenX S.A.S.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Config {
constructor() {
this.buttons = {
class: ''
};
this.execButton = {
class: '',
label: 'Execute'
};
this.datavizButton = {
class: '',
label: 'Visualize'
};
this.hover = true;
this.readOnly = false;
this.messageClass = '';
this.errorClass = '';
this.addLocalHeader = false;
this.editor = {
quickSuggestionsDelay: 10,
quickSuggestions: true,
tabSize: 2,
minLineNumber: 10,
enableDebug: false,
rawResultsReadOnly: false,
};
this.codeReview = {
enabled: false,
readonly: false,
cancelButton: {
class: '',
label: 'Cancel'
},
addButton: {
class: '',
label: 'Add comment'
},
replyButton: {
label: 'Reply'
},
removeButton: {
label: 'Remove'
},
editButton: {
label: 'Edit'
}
};
}
}
export { Config as C, Utils as U };