integrate all js files into assets

This commit is contained in:
Patrick Jentsch
2023-10-12 14:13:47 +02:00
parent c40e428eb2
commit bf249193af
8 changed files with 107 additions and 99 deletions

View File

@ -0,0 +1 @@
Utils = {};

View File

@ -0,0 +1,89 @@
Utils.escape = (text) => {
// https://codereview.stackexchange.com/a/126722
var table = {
'<': 'lt',
'>': 'gt',
'"': 'quot',
'\'': 'apos',
'&': 'amp',
'\r': '#10',
'\n': '#13'
};
return text.toString().replace(/[<>"'\r\n&]/g, (chr) => {
return '&' + table[chr] + ';';
});
};
Utils.unescape = (escapedText) => {
var table = {
'lt': '<',
'gt': '>',
'quot': '"',
'apos': "'",
'amp': '&',
'#10': '\r',
'#13': '\n'
};
return escapedText.replace(/&(#?\w+);/g, (match, entity) => {
if (table.hasOwnProperty(entity)) {
return table[entity];
}
return match;
});
};
Utils.HTMLToElement = (HTMLString) => {
let templateElement = document.createElement('template');
templateElement.innerHTML = HTMLString.trim();
return templateElement.content.firstChild;
};
Utils.generateElementId = (prefix='', suffix='') => {
for (let i = 0; true; i++) {
if (document.querySelector(`#${prefix}${i}${suffix}`) !== null) {continue;}
return `${prefix}${i}${suffix}`;
}
};
Utils.isObject = (object) => {
return object !== null && typeof object === 'object' && !Array.isArray(object);
};
Utils.mergeObjectsDeep = (...objects) => {
let mergedObject = {};
if (objects.length === 0) {
return mergedObject;
}
if (!Utils.isObject(objects[0])) {throw 'Cannot merge non-object';}
if (objects.length === 1) {
return Utils.mergeObjectsDeep(mergedObject, objects[0]);
}
if (!Utils.isObject(objects[1])) {throw 'Cannot merge non-object';}
for (let key in objects[0]) {
if (objects[0].hasOwnProperty(key)) {
if (objects[1].hasOwnProperty(key)) {
if (Utils.isObject(objects[0][key]) && Utils.isObject(objects[1][key])) {
mergedObject[key] = Utils.mergeObjectsDeep(objects[0][key], objects[1][key]);
} else {
mergedObject[key] = objects[1][key];
}
} else {
mergedObject[key] = objects[0][key];
}
}
}
for (let key in objects[1]) {
if (objects[1].hasOwnProperty(key)) {
if (!objects[0].hasOwnProperty(key)) {
mergedObject[key] = objects[1][key];
}
}
}
if (objects.length === 2) {
return mergedObject;
}
return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2));
};

View File

@ -0,0 +1,102 @@
/**
* XMLtoObject - Converts XML into a JavaScript value or object.
* GitHub: https://github.com/Pevtrick/XMLtoObject
* by Patrick Jentsch: https://github.com/Pevtrick
*/
/**
* The XMLDocument.toObject() method converts the XMLDocument into a JavaScript value or object.
* @param {String} [attributePrefix=] - A Prefix, which is added to all properties generated by XML attributes.
* @returns {Object} - The converted result.
*/
XMLDocument.prototype.toObject = function(attributePrefix='') {
let obj = {};
obj[this.documentElement.nodeName] = this.documentElement.toObject(attributePrefix);
return obj;
};
/**
* The Node.toObject() method converts the Node into a JavaScript value or object.
* @param {String} [attributePrefix=] - A Prefix, which is added to all properties generated by XML attributes.
* @returns {Object|String|null} - The converted result.
*/
Node.prototype.toObject = function(attributePrefix='') {
let obj = null;
switch (this.nodeType) {
case Node.ELEMENT_NODE:
let hasAttributes = this.attributes.length > 0;
let hasChildNodes = this.childNodes.length > 0;
/* Stop conversion if the Node doesn't contain any attributes or child nodes */
if (!(hasAttributes || hasChildNodes)) {
break;
}
obj = {};
/* Convert attributes */
for (let attribute of this.attributes) {
obj[`attributePrefix${attribute.name}`] = attribute.value;
}
/* Convert child nodes */
for (let childNode of this.childNodes) {
switch (childNode.nodeType) {
case Node.ELEMENT_NODE:
break;
case Node.TEXT_NODE:
/* Check whether the child text node is the only child of the current node. */
if (!hasAttributes && this.childNodes.length === 1) {
obj = childNode.toObject(attributePrefix);
continue;
}
if (childNode.data.trim() === '') {continue;}
break;
default:
/* This recursion leads to a console message. */
childNode.toObject(attributePrefix);
continue;
}
/**
* If the child node is the first of its type in this childset,
* process it and add it directly as a property to the return object.
* If not add it to an array which is set as a property of the return object.
*/
if (childNode.nodeName in obj) {
if (!Array.isArray(obj[childNode.nodeName])) {
obj[childNode.nodeName] = [obj[childNode.nodeName]];
}
obj[childNode.nodeName].push(childNode.toObject(attributePrefix));
} else {
obj[childNode.nodeName] = childNode.toObject(attributePrefix);
}
}
break;
case Node.TEXT_NODE:
if (this.data.trim() !== '') {obj = this.data;}
break;
case Node.COMMENT_NODE:
console.log('Skipping comment node:');
console.log(node);
break;
case Node.DOCUMENT_NODE:
obj = {};
obj[this.documentElement.nodeName] = this.documentElement.toObject(attributePrefix);
break;
default:
/**
* The following node types are not processed because they don't offer data, which has to be stored in the object:
* Node.PROCESSING_INSTRUCTION_NODE, Node.DOCUMENT_TYPE_NODE, Node.DOCUMENT_FRAGMENT_NODE
* The following node types are deprecated and therefore not supported by this function:
* Node.ATTRIBUTE_NODE, Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE, Node.ENTITY_NODE, Node.NOTATION_NODE
*/
console.log(`Node type: '${this.nodeType}' is not supported.`);
console.log(node);
break;
}
return obj;
}