diff --git a/app/static/js/Utils.js b/app/static/js/Utils.js deleted file mode 100644 index 0377bc56..00000000 --- a/app/static/js/Utils.js +++ /dev/null @@ -1,92 +0,0 @@ -class Utils { - static 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] + ';'; - }); - }; - - static 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; - }); -} - - static HTMLToElement(HTMLString) { - let templateElement = document.createElement('template'); - templateElement.innerHTML = HTMLString.trim(); - return templateElement.content.firstChild; - } - - static generateElementId(prefix='', suffix='') { - for (let i = 0; true; i++) { - if (document.querySelector(`#${prefix}${i}${suffix}`) !== null) {continue;} - return `${prefix}${i}${suffix}`; - } - } - - static isObject(object) { - return object !== null && typeof object === 'object' && !Array.isArray(object); - } - - static 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)); - } - -} diff --git a/app/static/js/App.js b/app/static/js/app/app.js similarity index 99% rename from app/static/js/App.js rename to app/static/js/app/app.js index cfcb3a05..92a5c546 100644 --- a/app/static/js/App.js +++ b/app/static/js/app/app.js @@ -1,4 +1,4 @@ -class App { +App.App = class App { constructor() { this.data = { promises: {getUser: {}, subscribeUser: {}}, @@ -101,4 +101,4 @@ class App { // Apply Patch jsonpatch.applyPatch(this.data, filteredPatch); } -} +}; diff --git a/app/static/js/app/index.js b/app/static/js/app/index.js new file mode 100644 index 00000000..8a7ef152 --- /dev/null +++ b/app/static/js/app/index.js @@ -0,0 +1 @@ +App = {}; diff --git a/app/static/js/utils/index.js b/app/static/js/utils/index.js new file mode 100644 index 00000000..39d693f5 --- /dev/null +++ b/app/static/js/utils/index.js @@ -0,0 +1 @@ +Utils = {}; diff --git a/app/static/js/utils/utils.js b/app/static/js/utils/utils.js new file mode 100644 index 00000000..17dcde05 --- /dev/null +++ b/app/static/js/utils/utils.js @@ -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)); +}; diff --git a/app/static/js/XMLtoObject.js b/app/static/js/utils/xml-to-object.js similarity index 100% rename from app/static/js/XMLtoObject.js rename to app/static/js/utils/xml-to-object.js diff --git a/app/templates/_scripts.html.j2 b/app/templates/_scripts.html.j2 index 8e8dd343..1fd32f7e 100644 --- a/app/templates/_scripts.html.j2 +++ b/app/templates/_scripts.html.j2 @@ -7,9 +7,17 @@ {%- assets filters='rjsmin', output='gen/app.%(version)s.js', - 'js/App.js', - 'js/Utils.js', - 'js/XMLtoObject.js' + 'js/app/index.js', + 'js/app/app.js' +%} + +{%- endassets %} + +{%- assets + filters='rjsmin', + output='gen/utils.%(version)s.js', + 'js/utils/index.js', + 'js/utils/utils.js' %} {%- endassets %} @@ -110,7 +118,7 @@