diff --git a/entity/Attribute_entity/recordcontainers/jdito/contentProcess.js b/entity/Attribute_entity/recordcontainers/jdito/contentProcess.js index 741ab92a4d39bc694a37e0ce0b913accdd1cc265..d7fc1bfc18e31d699109c9630c2e42c371e22c1f 100644 --- a/entity/Attribute_entity/recordcontainers/jdito/contentProcess.js +++ b/entity/Attribute_entity/recordcontainers/jdito/contentProcess.js @@ -127,20 +127,30 @@ attributes.forEach(function ([attributeId, parentId, simpleName, isActive, ]); }); +var sorter = new AttributeSorter(vars.get("$local.order")); + var resultTable = []; do { var oldSize = resultTable.length; + var layer = []; attributesById.forEach(function (row, id) { - var parentId = row[3]; + if (!row) + { + attributesById["delete"](id); + return; + } + var parentId = row[4]; //rows that are already in the result array are removed from the attributesById Map, so if the parentId is in that Map, //the parent has not been added yet if (!parentId || !attributesById.has(parentId)) { - resultTable.push(row); - attributesById["delete"](id); + layer.push(row); + attributesById.set(id, null); } }); + sorter.sort(layer); + resultTable = resultTable.concat(layer); } while (oldSize != resultTable.length); //stops the loop when no new items were added so that recursive relations between attributes don't cause an infinite loop result.object(resultTable); @@ -217,4 +227,42 @@ function AttributeUsageLoader () return ContextUtils.getTitle(usage, true); }).join(", "); } +} + +function AttributeSorter (pSortOrder) +{ + if (Utils.isNullOrEmpty(pSortOrder)) + { + this.sort = function (pArray) {return pArray;}; + } + else + { + var fieldIndexes = { + "ATTRIBUTE_NAME.value": 1, + "TRANSLATED_NAME.value": 2, + "ATTRIBUTE_TYPE.displayValue": 7, + "ATTRIBUTE_ACTIVE.value": 8, + "SORTING.value": 9, + "USAGELIST.value": 13 + }; + var sortOrder = Utils.objectEntries(vars.get("$local.order")); + if (sortOrder.length === 0) + { + sortOrder = [["ATTRIBUTE_NAME.value", "UP"]] + } + sortOrder = Utils.objectEntries(vars.get("$local.order")).reduce(function (orderMap, [field, direction]) + { + if (field in fieldIndexes) + { + orderMap.set(fieldIndexes[field], direction == "DOWN"); + } + return orderMap; + }, new Map()); + var sortFn = ArrayUtils.getMultiSortFn(sortOrder); + + this.sort = function (pArray) + { + return pArray.sort(sortFn); + } + } } \ No newline at end of file diff --git a/process/Util_lib/process.js b/process/Util_lib/process.js index ada0e3f465462c2959c61f639b5dc3d379cb38e1..0dc0f4e7797dc212ab0a9c9c79561a627e70fa23 100644 --- a/process/Util_lib/process.js +++ b/process/Util_lib/process.js @@ -981,23 +981,51 @@ ArrayUtils.sortArrayOfObjects = function(targetArray, key, sortAsc, isNumber) { /** * sorts an array with columns * -* @param {Array} targetArray the array with data -* @param {Array} sortOrder array with the format [columnIndex1, sortDescending1, columnIndex2, sortDescending2, ...], +* @param {Array} pTargetArray the array with data +* @param {Array} pSortOrder array with the format [columnIndex1, sortDescending1, columnIndex2, sortDescending2, ...], * the columnIndex must be an integer, sortDescending must be boolean (true -> descending, just like db.DESCENDING) * @example * ArrayUtils.sortMulti(rows, [1, true, 2, true, 5, false]); * * @return {void} */ -ArrayUtils.sortMulti = function(targetArray, sortOrder) { +ArrayUtils.sortMulti = function(pTargetArray, pSortOrder) +{ + var sortFn = ArrayUtils.getMultiSortFn(ArrayUtils.chunk(pSortOrder, 2)); + + pTargetArray.sort(sortFn); + return pTargetArray; +} + +/** +* makes a sorting function for an array with columns +* +* @param {Map|Array} pSortOrder Map with columnIndex as keys and direction as values, or +* array with the format [[columnIndex1, direction1], [columnIndex2, direction2], ...], +* the columnIndex must be an integer, direction must be boolean (true -> descending, just like db.DESCENDING) +* @example +* var sortFn = ArrayUtils.getMultiSortFn([[1, true], [2, true], [5, false]]); +* rows.sort(sortFn); +* +* @return {void} +*/ +ArrayUtils.getMultiSortFn = function (pSortOrder) +{ + if (Array.isArray(pSortOrder)) + { + pSortOrder = new Map(pSortOrder); + } + /* * @param {String} a req value 1, first compared element * @param {String} b req value 2, sencond compared element * * @return {Integer} -1 - set a below b, 0 - equal, 1 - set b below a */ - var sortFn = function(a, b) { - var stringComparison = function(a, b) { + return function(a, b) + { + var stringComparison = function(a, b) + { a = a.toLowerCase(); a = a.replace(/ä/g,"ae"); a = a.replace(/ö/g,"oe"); @@ -1015,28 +1043,31 @@ ArrayUtils.sortMulti = function(targetArray, sortOrder) { var swap = 0; - for (let i = 0, l = sortOrder.length; i < l; i += 2) + pSortOrder.forEach(function (sortDesc, colIndex) { - let colIndex = sortOrder[i]; - let sortDesc = sortOrder[i+1]; if (swap || colIndex == undefined || sortDesc == undefined) - return swap; + return; if (isNaN(a[colIndex] - b[colIndex])) + { if ((isNaN(a[colIndex])) && (isNaN(b[colIndex]))) - swap = stringComparison(a[colIndex], b[colIndex]); + { + swap = stringComparison(a[colIndex], b[colIndex]); + } else + { swap = (isNaN(a[colIndex]) ? 1 : -1); + } + } else + { swap = (a[colIndex] - b[colIndex]); + } swap *= (sortDesc ? -1 : 1); - } + }); return swap; } - - targetArray.sort(sortFn); - return targetArray; } /**