"use strict"; /// *** Core app scripts ***/// // ** Constants and variables definition ** // // Element name constants const elementId_mainContainer = 'content'; const elementId_bodyTitle = 'page-title'; const elementClassName_breadcrumb = 'ecl-breadcrumb__container'; const elementClassName_siteIdentity = 'ecl-page-header__title'; const elementName_title = 'title'; // Key contants const key_uri = 's-uri'; const key_mainAppTitle = 's-site-title'; const key_thisVersion = 's-thisversion'; const key_versionHistory = 's-versionhistory'; const key_collectionTitle = 's-collection-title'; const key_narrowerTitle = 's-narrower-title'; const key_errorFetch = 's-error-fetch'; const key_errorPrefix = 's-error-'; const key_marginVL = 'ecl-u-mv-l'; const key_marginT3xl = 'ecl-u-mt-3xl'; const key_genericError = 's-error-fetch'; const key_paginationPrevious = 's-pagination-previous'; const key_paginationNext = 's-pagination-next'; const key_otherFormats = 's-other-formats'; const key_insertDate = 's-insert-date'; const key_editDate = 's-edit-date'; // Value constants const val_itemTypeItem = 'item'; const val_itemTypeRegister = 'register'; const val_itemTypeRegistry = 'registry'; // HTML snippet constants const htmlSnippet_table = '{0}{1}
'; const htmlSnippet_th = '{0}'; const htmlSnippet_tr = '{0}'; const htmlSnippet_td = '{0}'; const htmlSnippet_ul = ''; const htmlSnippet_li = '
  • {0}
  • '; const htmlSnippet_href = '{1}'; const htmlSnippet_href_external_link = '{1} '; const htmlSnippet_href_format = '
  • {1}
  • '; const htmlSnippet_field = '
    {0}
    {1}
    '; const htmlSnippet_field_format = '
    {0}
    '; //const htmlSnippet_field = '
    {0}
    {1}
    '; const htmlSnippet_hx = '{2}'; const htmlSnippet_hr = '
    '; const htmlSnippet_breadcrumbLastElement = '
  • {0}
  • '; const htmlSnippet_breadcrumbLink = '
  • {1}
  • '; const htmlSnippet_dl = '
    {0}
    '; const htmlSnippet_paginationPreviousIcon = ' {0}'; const htmlSnippet_paginationNextIcon = '{0} '; // Event name constants // Regular expression constants // Global variables // ** Script body ** // /* * Fetch Re3gistry data * * @param {String} uri The uri of the item to retrieve * @param {String} lang The language of the data */ function fetchData(uri, lang) { if (uri === null || typeof uri === val_undefined || uri.length === 0) { uri = uriFromUrl; } if (lang === null || typeof lang === val_undefined || lang.length === 0) { lang = currentLanguage; } var url = new URL(uri); var status = url.searchParams.get("status"); uri = uri.split('?')[0]; // Show the loading overlay showLoadingOverlay(true); $.ajax({ // Base URL of the service taken from the configuration url: registryApp.dataServiceURL, data: {uri: uri, lang: lang, format: key_jsonc, status: status} }).done(function (responseData) { // Rendering the HTML renderData(responseData); // Hide the loading overlay showLoadingOverlay(false); }).fail(function (data) { // Error handler renderFetchError(data.responseJSON); // Hide the loading overlay showLoadingOverlay(false); }); } /* * Render the succesful response from the service * * @param {Json} data The Re3gistry json data */ function renderData(data) { // Getting the container element let mainContainer = $('#' + elementId_mainContainer); // Clearing the conatiner mainContainer.empty(); if (data.error) { // If there is an error on the service respose, rendering the error renderServiceError(data); } else { // Rendering the HMTL handleBreadcrumbs(data); renderSiteIdentity(data); mainContainer.append(renderProperties(data)); mainContainer.append(renderDate(data)); mainContainer.append(renderFormats(data)); // mainContainer.append(renderHr(key_marginVL)); let containedItems = data.containedItems; if (containedItems !== null && typeof containedItems !== val_undefined && containedItems.length > 0) { mainContainer.append(renderCollections(data)); } else { mainContainer.append(renderNarrowers(data)); } $('.ecl-table').DataTable({ 'dom': '<"top-tools ecl-container"lif>tp', 'scrollX': true, 'bAutoWidth': false, "oLanguage": { "sSearch": "Filter: " }, 'drawCallback': function () { enhanceTables(); } }); } } /* * Render the error response from the service * * @param {Json} data The Re3gistry json data */ function renderFetchError(data) { // Getting the container element let mainContainer = $('#' + elementId_mainContainer); // Clearing the conatiner mainContainer.empty(); if (data) { mainContainer.append(htmlSnippet_errorMessage.replace('{0}', i18n[key_errorPrefix + data.error.code])); } else { mainContainer.append(htmlSnippet_errorMessage.replace('{0}', i18n[key_genericError])); } // Initializing the ECL Message component after creating it let elt = document.querySelector('[' + key_dataEclMessage + ']'); let message = new ECL.Message(elt); message.init(); } /* * Render the error response from the service * * @param {Json} data The Re3gistry json data * @return {String} The rendered html of the error */ function renderServiceError(data) { let htmlOutput = val_emptyString; htmlOutput += htmlSnippet_errorMessage.replace('{0}', i18n[key_errorPrefix + data.error.code]); mainContainer.append(htmlOutput); // Initializing the ECL Message component after creating it let elt = document.querySelector('[' + key_dataEclMessage + ']'); let message = new ECL.Message(elt); message.init(); } /* * Render the URI field * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML string of the URI */ function renderUri(data) { return renderField(i18n[key_uri], renderHref(data.uri, data.uri)); } /* * Render the version(s) information of an element (including eventual history) * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML string of the version(s) */ function renderVersionInfo(data) { let htmlOutput = val_emptyString; // The version info are only available for elements of type 'item' if (data.type === val_itemTypeItem) { // Getting version info from the data let version = data.version; let versionHistory = data.versionHistory; // Rendering info of current version htmlOutput += renderField(i18n[key_thisVersion], renderHref(version.uri, version.uri)); // Checking if the version history info are available if (typeof versionHistory !== 'undefined' && versionHistory !== null && versionHistory.length > 0) { // Sorting the version history descending versionHistory.sort(function (a, b) { return sortArray(a.number, b.number, key_descOrdering); }); // Preparing the list of values let listValues = []; $.each(versionHistory, function (index, item) { // Creating the object to be processed by the // renderFieldListValues method let tmpObject = {}; tmpObject.value = item.uri; tmpObject.href = item.uri; listValues.push(tmpObject); }); // Rendering the field list htmlOutput += renderFieldListValues(i18n[key_versionHistory], listValues); } } return htmlOutput; } /* * Render the properties of the current element * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML string of the properties */ function renderProperties(data) { let htmlOutput = val_emptyString; // Sorting the properties (fields) by 'order' ascending data.properties.sort(function (a, b) { return sortArray(a.order, b.order, key_ascOrdering); }); // Rendering URI field htmlOutput += renderUri(data); // Rendering version info htmlOutput += renderVersionInfo(data); // Render each properties $.each(data.properties, function (index, item) { let values = item.values; let tmpHtml = val_emptyString; if (values.length > 1) { htmlOutput += renderFieldListValues(item.label, values); } else if (values.length == 1) { let value = values[0].value; let href = values[0].href; tmpHtml = (href !== null && href !== val_emptyString) ? renderHref(value, href) : value; if((item.id === "label" || item.id ==="definition") && data.language != item.lang){ let myJSON = getLanguageJSON(data.language); let finalJSON = JSON.parse(myJSON); let language = finalJSON.translatenotavailable; let finalLabel = item.label + "
    "+" [" + language + "] "; htmlOutput += renderField(finalLabel, tmpHtml); }else{ htmlOutput += renderField(item.label, tmpHtml); } // If the property is the title, updating the page title. if (item.istitle !== null && item.istitle === val_true) { updatePageTitle(tmpHtml); } } }); htmlOutput = renderDl(htmlOutput); // htmlOutput += renderHr(key_marginVL); return htmlOutput; } /* * Render the formats of the current element * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML string of the properties */ function renderFormats(data) { let href; let hrefRor; let htmlOutput = val_emptyString; if (data.latest == true) { href = data.uri + "/" + data.localid + "." + currentLanguage + "."; hrefRor = data.uri + "/" + data.localid + "."; } else { if (typeof data.version !== 'undefined') { href = data.uri + ":" + data.version.number + "/" + data.localid + "." + currentLanguage + "."; hrefRor = data.uri + ":" + data.version.number + "/" + data.localid + "."; } } if (typeof href !== 'undefined') { // href = encodeURIComponent(href); let htmlInner = val_emptyString; htmlInner += renderHrefFormat("XML Registry", href + "xml"); htmlInner += renderHrefFormat("XML ISO 19135", href + "iso19135xml"); htmlInner += renderHrefFormat("RDF/XML", href + "rdf"); htmlInner += renderHrefFormat("JSON", href + "json"); let containedItems = data.containedItems; let narrowerItems = data.narrower; if (containedItems !== null && typeof containedItems !== val_undefined && containedItems.length > 0) { htmlInner += renderHrefFormat("CSV", href + "csv"); } else if (narrowerItems !== null && typeof narrowerItems !== val_undefined && narrowerItems.length > 0) { htmlInner += renderHrefFormat("CSV", href + "csv"); } htmlInner += renderHrefFormat("ATOM", href + "atom"); htmlInner += renderHrefFormat("ROR", hrefRor + "ror"); //TODO to be completed htmlOutput += renderFieldFormat(i18n[key_otherFormats], htmlInner); } return htmlOutput; } /* * Render the date of the current element * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML string of the properties */ function renderDate(data) { let htmlOutput = val_emptyString; if (data.insertdate) { htmlOutput += renderField(i18n[key_insertDate], data.insertdate); } if (data.editdate) { htmlOutput += renderField(i18n[key_editDate], data.editdate); } return htmlOutput; } /* * Render the collection related to the current element * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML of the collections */ function renderCollections(data) { let htmlOutput = val_emptyString; let tHead = val_emptyString; let containedItems = data.containedItems; // If the current element has Available items if (containedItems !== null && typeof containedItems !== val_undefined && containedItems.length > 0) { // Rendering the header htmlOutput += renderHx(i18n[key_collectionTitle], 3, key_marginT3xl); // The table header is rendered with the first element (the fields are // the same for all the elements) let max = -Infinity; let index = -1; containedItems.forEach(function (a, i) { if (a.properties.length > max) { max = a.properties.length; index = i; } }); var headerProperties = []; $.each(containedItems[index].properties, function (index1, propertie) { if (propertie.tablevisible !== null && propertie.tablevisible === val_true) { headerProperties.push(propertie); } }); tHead = renderTableHeader(headerProperties); // The HTML of each contained item is rendered $.each(containedItems, function (index, item) { htmlOutput += renderTableProperties(item, headerProperties); }); // The HTML of the table is rendered htmlOutput = renderTable(htmlOutput, tHead); } return htmlOutput; } /* * Render the narrower related to the current element * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML of the narrowers */ function renderNarrowers(data) { let htmlOutput = val_emptyString; let tHead = val_emptyString; let narrowerItems = data.narrower; // If the current element has narrower items if (narrowerItems !== null && typeof narrowerItems !== val_undefined && narrowerItems.length > 0) { // Rendering the header htmlOutput += renderHx(i18n[key_narrowerTitle], 3, key_marginT3xl); // The table header is rendered with the first element (the fields are // the same for all the elements) let max = -Infinity; let index = -1; narrowerItems.forEach(function (a, i) { if (a.properties.length > max) { max = a.properties.length; index = i; } }); var headerProperties = []; $.each(narrowerItems[index].properties, function (index1, propertie) { if (propertie.tablevisible !== null && propertie.tablevisible === val_true) { headerProperties.push(propertie); } }); tHead = renderTableHeader(headerProperties); // The HTML of each narrower item is rendered $.each(narrowerItems, function (index, item) { htmlOutput += renderTableProperties(item, headerProperties); }); // The HTML of the table is rendered htmlOutput = renderTable(htmlOutput, tHead); } return htmlOutput; } /* * Render the label and value of the field in HTML * * @param {String} label The label of the field * @param {String} value The value of the field * @returns {String} The rendered HTML of the field */ function renderField(label, value) { if (label.toLowerCase() === 'label') { return htmlSnippet_field.replace('{0}', label).replace('{1}', value).replace('{2}', 'ecl-u-type-bold'); } else { return htmlSnippet_field.replace('{0}', label).replace('{1}', value).replace('{2}', ''); } } function renderFieldFormat(label, value) { return htmlSnippet_field_format.replace('{0}', label).replace('{1}', value).replace('{2}', ''); } /* * Render the label and values (list) of the field in HTML * * @param {String} label The label of the field * @param {String} values The values (list) of the field * @returns {String} The rendered HTML of the field */ function renderFieldListValues(label, values) { let htmlOutput = val_emptyString; // Render the HTML of each element of the values list htmlOutput = renderList(values); // Renred the field and the list values processed before return renderField(label, htmlOutput); } /* * Render the label and values (list) of the field in HTML * * @param {String} label The label of the field * @param {String} values The values (list) of the field * @returns {String} The rendered HTML of the field */ function renderList(values) { let htmlOutput = val_emptyString; // Render the HTML of each element of the values list $.each(values, function (index, item) { let value = item.value; let href = item.href; let tmpOutput = val_emptyString; tmpOutput = (href !== null && href !== val_emptyString) ? renderHref(value, href) : value; htmlOutput += htmlSnippet_li.replace('{0}', tmpOutput); }); // Render the list container return htmlSnippet_ul.replace('{0}', htmlOutput); } /* * Render the href of the field in HTML * * @param {String} value The value of the field * @param {String} href The href of the field * @returns {String} The rendered href HTML element of the field * */ function renderHref(value, href) { return htmlSnippet_href.replace('{0}', href).replace('{1}', value); } /* * Render the href of the field in HTML * * @param {String} value The value of the field * @param {String} href The href of the field * @returns {String} The rendered href HTML element of the field * */ function renderHrefExternalLink(value, href) { return htmlSnippet_href_external_link.replace('{0}', href).replace('{1}', value); } /* * Render the href of the field in HTML * * @param {String} value The value of the field * @param {String} href The href of the field * @returns {String} The rendered href HTML element of the field * */ function renderHrefFormat(value, href) { return htmlSnippet_href_format.replace('{0}', href).replace('{1}', value); } /* * Render table properties * * @param {Json} data The Re3gistry json data * @returns {String} The rendered HTML of the table properties */ function renderTableProperties(data, headerProperties) { let htmlOutput = val_emptyString; if (data.properties !== null && data.properties.length > 0) { // Sorting the fields of the current element data.properties.sort(function (a, b) { return sortArray(a.order, b.order, key_ascOrdering); }); $.each(headerProperties, function (index1, propertie) { if (propertie.tablevisible !== null && propertie.tablevisible === val_true) { // Rendering the HTML of each field for the table let valueAdded = false; $.each(data.properties, function (index, item) { if ((item.id === propertie.id) && !valueAdded) { if (item.tablevisible !== null && item.tablevisible === val_true) { let values = item.values; let value = val_emptyString; if (values.length > 1) { value = renderList(values); } else if (values.length == 1) { let tmpValue = values[0].value; let href = values[0].href; // In the table view, in case the field is a 'titlefield' the href // is added, in order to enable the link to the related element // directly from the table if (item.istitle !== null && item.istitle === val_true) { let windowlocation = window.location.href.replace("http://", "").replace("https://", ""); let indexSlash = windowlocation.indexOf("/"); let contain = windowlocation.substring(0, indexSlash); if (data.uri.includes(contain)) { if(data.properties[2].values[0].value.toLowerCase() === "valid"){ value = renderHref(tmpValue, data.uri); } else { value = renderHref(tmpValue, data.uri + "?status=" + data.properties[2].values[0].value.toLowerCase()); } } else { value = renderHrefExternalLink(tmpValue, data.uri); } } else { value = (href !== null && href !== val_emptyString) ? renderHref(tmpValue, href) : tmpValue; } } // Rendering the HTML of the td htmlOutput += renderTd(value, item.label); valueAdded = true; } } }); if (!valueAdded) { // Rendering the HTML of the td let value = val_emptyString; htmlOutput += renderTd(value, propertie.label); } } }); // Rendering the HTML of the tr htmlOutput = renderTr(htmlOutput); } return htmlOutput; } /* * Render the HTML of a td * * @param {String} value The value to be put inside the td * @returns {String} The rendered HTML of the td */ function renderTd(value, field) { return htmlSnippet_td.replace('{0}', value).replace('{1}', field); } /* * Render the HTML of a th * * @param {String} value The value to be put inside the th * @returns {String} The rendered HTML of the th */ function renderTh(value) { return htmlSnippet_th.replace('{0}', value); } /* * Render the HTML of a tr * * @param {String} value The value to be put inside the tr * @returns {String} The rendered HTML of the tr */ function renderTr(value) { return htmlSnippet_tr.replace('{0}', value); } /* * Render the HTML of the table header * * @param {Json} The Json object representing the properties * @returns {String} The rendered HTML of the table head */ function renderTableHeader(properties) { // Sorting the fields ascending properties.sort(function (a, b) { return sortArray(a.order, b.order, key_ascOrdering); }); // Rendering the table head let htmlOutput = val_emptyString; $.each(properties, function (index, item) { if (item.tablevisible !== null && item.tablevisible === val_true) { htmlOutput += renderTh(item.label, val_emptyString); } }); // Rendering the HTML of the tr return renderTr(htmlOutput); } /* * Render the HTML of a table * * @param {String} value The value to be put inside the table * @returns {String} The rendered HTML of the table */ function renderTable(value, thead) { return htmlSnippet_table.replace('{0}', thead).replace('{1}', value); } function enhanceTables() { /** Customize Pagination **/ let dtPaginate = $('.dataTables_paginate'); // Remove all classes dtPaginate.removeClass(); dtPaginate.addClass('ecl-u-mt-l ecl-pagination'); let paginationLi = $('
  • '); let paginationLiCurrent = $('
  • '); // Pagination previous let dtPagination_previous = $('.paginate_button.previous'); let prevClasses = dtPagination_previous.attr('class'); dtPagination_previous.removeClass(); dtPagination_previous.addClass('ecl-pagination__link ecl-link ecl-link--standalone ecl-link--icon ecl-link--icon-before'); dtPagination_previous.text(val_emptyString); dtPagination_previous.append(htmlSnippet_paginationPreviousIcon.replace('{0}', i18n[key_paginationPrevious])); if (prevClasses.indexOf('disabled') >= 0) { dtPagination_previous.hide(); } dtPagination_previous.wrap(paginationLi); // Pagination next let dtPagination_next = $('.paginate_button.next'); let nextClasses = dtPagination_next.attr('class'); dtPagination_next.removeClass(); dtPagination_next.addClass('ecl-pagination__link ecl-link ecl-link--standalone ecl-link--icon ecl-link--icon-after'); dtPagination_next.text(val_emptyString); dtPagination_next.append(htmlSnippet_paginationNextIcon.replace('{0}', i18n[key_paginationNext])); if (nextClasses.indexOf('disabled') >= 0) { dtPagination_next.hide(); } dtPagination_next.wrap(paginationLi); let pages = $('.ecl-pagination span a, .ecl-pagination span span'); pages.each(function (i) { let tmpEl = $(this); let classes = tmpEl.attr('class'); tmpEl.removeClass(); tmpEl.addClass('ecl-pagination__link ecl-link ecl-link--standalone'); if (classes.indexOf('current') >= 0) { tmpEl.wrap(paginationLiCurrent); } else { tmpEl.wrap(paginationLi); } }); let liPaginations = $('.ecl-pagination').find('li'); liPaginations.wrapAll('