From 69152f5e6af3e93bb39787455002fb7c5fe98b47 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Mon, 12 Dec 2022 13:18:22 +0100 Subject: [PATCH 1/2] Remove PublicCorporaList.js --- .../js/RessourceLists/PublicCorporaList.js | 70 ------------------- app/static/js/RessourceLists/RessourceList.js | 1 - app/templates/_scripts.html.j2 | 1 - 3 files changed, 72 deletions(-) delete mode 100644 app/static/js/RessourceLists/PublicCorporaList.js diff --git a/app/static/js/RessourceLists/PublicCorporaList.js b/app/static/js/RessourceLists/PublicCorporaList.js deleted file mode 100644 index 2171396e..00000000 --- a/app/static/js/RessourceLists/PublicCorporaList.js +++ /dev/null @@ -1,70 +0,0 @@ -class PublicCorporaList extends RessourceList { - static instances = []; - - static getInstance(elem) { - return PublicCorporaList.instances.find((instance) => { - return instance.listjs.list === elem; - }); - } - - static autoInit() { - for (let publicCorporaListElement of document.querySelectorAll('.public-corpora-list:not(.no-autoinit)')) { - new PublicCorporaList(publicCorporaListElement); - } - } - - static options = { - initialHtmlGenerator: (id) => { - return ` -
- search - - -
- - - - - - - - - - -
TitleDescription
- - `.trim(); - }, - item: ` - - book - - - - `.trim(), - ressourceMapper: (corpus) => { - return { - 'id': corpus.id, - 'creation-date': corpus.creation_date, - 'description': corpus.description, - 'title': corpus.title - }; - }, - sortArgs: ['creation-date', {order: 'desc'}], - valueNames: [ - {data: ['id']}, - {data: ['creation-date']}, - 'description', - 'title' - ] - }; - - constructor(listElement, options = {}) { - super(listElement, {...PublicCorporaList.options, ...options}); - PublicCorporaList.instances.push(this); - } - - init(user) { - this._init(user.corpora.is_public); - } -} diff --git a/app/static/js/RessourceLists/RessourceList.js b/app/static/js/RessourceLists/RessourceList.js index 5af7a231..871a1e2f 100644 --- a/app/static/js/RessourceLists/RessourceList.js +++ b/app/static/js/RessourceLists/RessourceList.js @@ -10,7 +10,6 @@ class RessourceList { JobList.autoInit(); JobInputList.autoInit(); JobResultList.autoInit(); - PublicCorporaList.autoInit(); SpaCyNLPPipelineModelList.autoInit(); TesseractOCRPipelineModelList.autoInit(); UserList.autoInit(); diff --git a/app/templates/_scripts.html.j2 b/app/templates/_scripts.html.j2 index 8ff90c33..7cc8a8f8 100644 --- a/app/templates/_scripts.html.j2 +++ b/app/templates/_scripts.html.j2 @@ -24,7 +24,6 @@ 'js/RessourceLists/JobList.js', 'js/RessourceLists/JobInputList.js', 'js/RessourceLists/JobResultList.js', - 'js/RessourceLists/PublicCorporaList.js', 'js/RessourceLists/SpacyNLPPipelineModelList.js', 'js/RessourceLists/TesseractOCRPipelineModelList.js', 'js/RessourceLists/UserList.js' From a413b41dfd54aac163b6dbc771b5864fa9305206 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Mon, 12 Dec 2022 15:37:33 +0100 Subject: [PATCH 2/2] Update News page --- app/static/js/RessourceLists/RessourceList.js | 4 +- app/static/js/XMLtoObject.js | 102 ++++++++++++++ app/templates/_scripts.html.j2 | 5 +- app/templates/corpora/corpora.html.j2 | 62 ++++++--- app/templates/main/news.html.j2 | 129 +++++++++++++++--- 5 files changed, 260 insertions(+), 42 deletions(-) create mode 100644 app/static/js/XMLtoObject.js diff --git a/app/static/js/RessourceLists/RessourceList.js b/app/static/js/RessourceLists/RessourceList.js index 871a1e2f..323f9d0e 100644 --- a/app/static/js/RessourceLists/RessourceList.js +++ b/app/static/js/RessourceLists/RessourceList.js @@ -30,11 +30,11 @@ class RessourceList { ...{pagination: {item: `
  • `}}, ...options } - if ('ressourceMapper' in options) { + if ('ressourceMapper' in options && typeof options.ressourceMapper === 'function') { this.ressourceMapper = options.ressourceMapper; delete options.ressourceMapper; } - if ('initialHtmlGenerator' in options) { + if ('initialHtmlGenerator' in options && typeof options.initialHtmlGenerator === 'function') { this.initialHtmlGenerator = options.initialHtmlGenerator; listElement.innerHTML = this.initialHtmlGenerator(listElement.id); delete options.initialHtmlGenerator; diff --git a/app/static/js/XMLtoObject.js b/app/static/js/XMLtoObject.js new file mode 100644 index 00000000..001376cb --- /dev/null +++ b/app/static/js/XMLtoObject.js @@ -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; +} diff --git a/app/templates/_scripts.html.j2 b/app/templates/_scripts.html.j2 index 7cc8a8f8..24d24d97 100644 --- a/app/templates/_scripts.html.j2 +++ b/app/templates/_scripts.html.j2 @@ -26,7 +26,8 @@ 'js/RessourceLists/JobResultList.js', 'js/RessourceLists/SpacyNLPPipelineModelList.js', 'js/RessourceLists/TesseractOCRPipelineModelList.js', - 'js/RessourceLists/UserList.js' + 'js/RessourceLists/UserList.js', + 'js/XMLtoObject.js' %} {%- endassets %} @@ -52,7 +53,7 @@ // Initialize components M.AutoInit(); - M.CharacterCounter.init(document.querySelectorAll('input[data-length][type="text"], input[data-length][type="email"], input[data-length][type="search"], input[data-length][type="password"], input[data-length][type="tel"], input[data-length][type="url"], textarea[data-length]')); + M.CharacterCounter.init(document.querySelectorAll('input[data-length], textarea[data-length]')); M.Dropdown.init( document.querySelectorAll('#nav-more-dropdown-trigger'), {alignment: 'right', constrainWidth: false, coverTrigger: false} diff --git a/app/templates/corpora/corpora.html.j2 b/app/templates/corpora/corpora.html.j2 index 8cc701f8..9be377f3 100644 --- a/app/templates/corpora/corpora.html.j2 +++ b/app/templates/corpora/corpora.html.j2 @@ -1,30 +1,46 @@ {% extends "base.html.j2" %} +{% block main_attribs %} class="service-scheme" data-service="corpus-analysis"{% endblock main_attribs %} + {% block page_content %} -
    -
    -
    -
    -
    -

    ICorpora

    -
    -
    -
    - search - +
    +
    +
    +
    +
    +
    +

    ICorpora

    +
    +
    +
    + search + + +
    -
    -
    -
    +
    + + + + + + + + + + +
    Title and DescriptionStatus
    +
      +
      @@ -36,9 +52,21 @@ {% block scripts %} {{ super() }} {% endblock scripts %} diff --git a/app/templates/main/news.html.j2 b/app/templates/main/news.html.j2 index b7204f07..c374237c 100644 --- a/app/templates/main/news.html.j2 +++ b/app/templates/main/news.html.j2 @@ -9,7 +9,7 @@
      -
      +
      @@ -132,29 +132,116 @@ {% block scripts %} {{ super() }} {% endblock scripts %}