X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FUI.git;a=blobdiff_plain;f=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Flibraries%2Futils.js;h=5a3684121846aa22bd633d06909657890a7f731b;hp=1d54a6f5be6f201e6a92412c38a6ec163c580123;hb=28412d0042632687d765d239cbb3ff0523a131b9;hpb=64b803dbd876e15a0a2d4ca46d8ce81ac18915f6 diff --git a/skyquake/plugins/composer/src/src/libraries/utils.js b/skyquake/plugins/composer/src/src/libraries/utils.js index 1d54a6f5b..5a3684121 100644 --- a/skyquake/plugins/composer/src/src/libraries/utils.js +++ b/skyquake/plugins/composer/src/src/libraries/utils.js @@ -18,7 +18,14 @@ 'use strict'; -import changeCase from 'change-case'; +import _cloneDeep from 'lodash/cloneDeep'; +import _isArray from 'lodash/isArray'; +import _map from 'lodash/map'; +import _flatten from 'lodash/flatten'; +import _find from 'lodash/find'; +import _toNumber from 'lodash/toNumber'; +import _isNumber from 'lodash/isNumber'; +import _clone from 'lodash/clone'; export default { addAuthorizationStub(xhr) { @@ -62,7 +69,7 @@ export default { // last item in path used to assign value on the resolved object const name = path.pop(); const resolvedObj = path.reduce((r, p, i) => { - if (typeof(r[p]) !== 'object') { + if (typeof (r[p]) !== 'object') { // look-ahead to see if next path item is a number const isArray = !isNaN(parseInt(pathCopy[i + 1], 10)); r[p] = isArray ? [] : {} @@ -71,6 +78,25 @@ export default { }, obj); resolvedObj[name] = value; }, + mergePathData(obj, path, data) { + path = path.split(/[\.\[\]]/).filter(d => d); + // enable look-ahead to determine if type is array or object + const pathCopy = path.slice(); + let resolvedObj = obj; + if (path.length) { + // last item in path used to assign value on the resolved object + const name = path.pop(); + resolvedObj = path.reduce((r, p, i) => { + if (typeof (r[p]) !== 'object') { + // look-ahead to see if next path item is a number + const isArray = !isNaN(parseInt(pathCopy[i + 1], 10)); + r[p] = isArray ? [] : {} + } + return r[p]; + }, obj)[name]; + } + Object.assign(resolvedObj, data); + }, updatePathValue(obj, path, value, isCase) { // todo: replace implementation of assignPathValue with this impl and // remove updatePathValue (only need one function, not both) @@ -85,21 +111,21 @@ export default { // look-ahead to see if next path item is a number const index = parseInt(pathCopy[i + 1], 10); const isArray = !isNaN(index); - if (typeof(r[p]) !== 'object') { + if (typeof (r[p]) !== 'object') { r[p] = isArray ? [] : {} } if (isRemove && ((i + 1) === path.length)) { if (isArray) { r[p] = r[p].filter((d, i) => i !== index); } else { - if(isCase) { + if (isCase) { delete r[name]; } else { delete r[p][name]; } } } - if(isCase) { + if (isCase) { return r; } else { return r[p]; @@ -116,7 +142,7 @@ export default { }, suffixAsInteger: (field) => { - return (obj) =>{ + return (obj) => { const str = String(obj[field]); const value = str.replace(str.replace(/[\d]+$/, ''), ''); return 1 + parseInt(value, 10) || 0; @@ -125,30 +151,30 @@ export default { toBiggestValue: (maxIndex, curIndex) => Math.max(maxIndex, curIndex), - isRelativePath (path) { + isRelativePath(path) { if (path.split('/')[0] == '..') { return true; } return false; }, - getResults (topLevelObject, pathArray) { - let objectCopy = _.cloneDeep(topLevelObject); + getResults(topLevelObject, pathArray) { + let objectCopy = _cloneDeep(topLevelObject); let i = pathArray.length; let results = []; - while(pathArray[pathArray.length - i]) { - if (_.isArray(objectCopy[pathArray[pathArray.length - i]])) { + while (pathArray[pathArray.length - i]) { + if (_isArray(objectCopy[pathArray[pathArray.length - i]])) { if (i == 2) { - results = _.map(objectCopy[pathArray[pathArray.length - i]], pathArray[pathArray.length - 1]); + results = _map(objectCopy[pathArray[pathArray.length - i]], pathArray[pathArray.length - 1]); } else { objectCopy = objectCopy[pathArray[pathArray.length - i]]; } - } else if (_.isArray(objectCopy)) { + } else if (_isArray(objectCopy)) { objectCopy.map((object) => { - if (_.isArray(object[pathArray[pathArray.length - i]])) { + if (_isArray(object[pathArray[pathArray.length - i]])) { if (i == 2) { - results = results.concat(_.map(object[pathArray[pathArray.length - i]], pathArray[pathArray.length - 1])); + results = results.concat(_map(object[pathArray[pathArray.length - i]], pathArray[pathArray.length - 1])); } } }) @@ -159,9 +185,9 @@ export default { return results; }, - getAbsoluteResults (topLevelObject, pathArray) { + getAbsoluteResults(topLevelObject, pathArray) { let i = pathArray.length; - let objectCopy = _.cloneDeep(topLevelObject); + let objectCopy = _cloneDeep(topLevelObject); let results = []; let fragment = pathArray[pathArray.length - i] @@ -169,9 +195,9 @@ export default { while (fragment) { if (i == 1) { // last fragment - if (_.isArray(objectCopy)) { + if (_isArray(objectCopy)) { // results will be obtained from a map - results = _.map(objectCopy, fragment); + results = _map(objectCopy, fragment); } else { // object if (fragment.match(/\[.*\]/g)) { @@ -180,20 +206,23 @@ export default { console.log('Something went wrong while resolving a leafref. Reached a leaf with predicate.'); } else { // contains no predicate + if (!objectCopy) { + break; + } results.push(objectCopy[fragment]); } } } else { - if (_.isArray(objectCopy)) { + if (_isArray(objectCopy)) { // is array - objectCopy = _.map(objectCopy, fragment); + objectCopy = _map(objectCopy, fragment); // If any of the deeper object is an array, flatten the entire list. // This would usually be a bad leafref going out of its scope. // Log it too for (let i = 0; i < objectCopy.length; i++) { - if (_.isArray(objectCopy[i])) { - objectCopy = _.flatten(objectCopy); + if (_isArray(objectCopy[i])) { + objectCopy = _flatten(objectCopy); console.log('This might be a bad leafref. Verify with backend team.') break; } @@ -212,13 +241,38 @@ export default { let key = fragment.split('[')[0]; let searchObject = {}; searchObject[predicateKey] = predicateValue; - objectCopy = _.find(objectCopy[key], searchObject); - if (!objectCopy) { - return []; + let found = _find(objectCopy[key], searchObject); + if (found) { + objectCopy = found; + } else { + // check for numerical value + if (predicateValue != "" && + predicateValue != null && + predicateValue != NaN && + predicateValue != Infinity && + predicateValue != -Infinity) { + let numericalPredicateValue = _toNumber(predicateValue); + if (_isNumber(numericalPredicateValue)) { + searchObject[predicateKey] = numericalPredicateValue; + found = _find(objectCopy[key], searchObject); + } + } + if (found) { + objectCopy = found; + } else { + return []; + } } } else { // contains no predicate + if (!objectCopy) { + break; + } objectCopy = objectCopy[fragment]; + if (!objectCopy) { + // contains no value + break; + } } } } @@ -229,7 +283,7 @@ export default { return results; }, - resolveCurrentPredicate (leafRefPath, container, pathCopy) { + resolveCurrentPredicate(leafRefPath, container, pathCopy) { if (leafRefPath.indexOf('current()') != -1) { // contains current @@ -254,10 +308,17 @@ export default { return leafRefPath; }, - resolveLeafRefPath (catalogs, leafRefPath, fieldKey, path, container) { - let pathCopy = _.clone(path); + cleanupFieldKeyArray (fieldKeyArray) { + fieldKeyArray.map((fieldKey, fieldKeyIndex) => { + fieldKeyArray[fieldKeyIndex] = fieldKey.replace(/.*\/(.*)/, '$1'); + }); + return fieldKeyArray; + }, + + resolveLeafRefPath(catalogs, leafRefPath, fieldKey, path, container) { + let pathCopy = _clone(path); // Strip any prefixes - let leafRefPathCopy = leafRefPath.replace(/[\w\d]*:/g, ''); + let leafRefPathCopy = leafRefPath.replace(/[-\w]*:/g, ''); // Strip any spaces leafRefPathCopy = leafRefPathCopy.replace(/\s/g, ''); @@ -266,7 +327,11 @@ export default { // Split on delimiter (/) const pathArray = leafRefPathCopy.split('/'); + let fieldKeyArray = fieldKey.split(':'); + + // strip prepending qualifiers from fieldKeys + fieldKeyArray = this.cleanupFieldKeyArray(fieldKeyArray); let results = []; // Check if relative path or not @@ -288,7 +353,9 @@ export default { if (fieldKeyArray.length == 1) { for (let key in catalogs) { for (let subKey in catalogs[key]) { - let found = _.find(catalogs[key][subKey], {id: fieldKeyArray[0]}); + let found = _find(catalogs[key][subKey], { + id: fieldKeyArray[0] + }); if (found) { results = this.getAbsoluteResults(found, pathArray.splice(-i, i)); return results; @@ -298,13 +365,51 @@ export default { } else if (fieldKeyArray.length == 2) { for (let key in catalogs) { for (let subKey in catalogs[key]) { - let found = _.find(catalogs[key][subKey], {id: fieldKeyArray[0]}); + console.log(key, subKey); + var found = _find(catalogs[key][subKey], { + id: fieldKeyArray[0] + }); + if (found) { + for (let foundKey in found) { + if (_isArray(found[foundKey])) { + let topLevel = _find(found[foundKey], { + id: fieldKeyArray[1] + }); + if (topLevel) { + results = this.getAbsoluteResults(topLevel, pathArray.splice(-i, i)); + return results; + } + } else { + if (foundKey == fieldKeyArray[1]) { + results = this.getAbsoluteResults(found[foundKey], pathArray.splice(-i, i)); + return results; + } + } + } + } + } + } + } else if (fieldKeyArray.length == 3) { + for (let key in catalogs) { + for (let subKey in catalogs[key]) { + let found = _find(catalogs[key][subKey], { + id: fieldKeyArray[0] + }); if (found) { for (let foundKey in found) { - let topLevel = _.find(found[foundKey], {id: fieldKeyArray[1]}); - if (topLevel) { - results = this.getAbsoluteResults(topLevel, pathArray.splice(-i, i)); - return results; + if (_isArray(found[foundKey])) { + let topLevel = _find(found[foundKey], { + id: fieldKeyArray[1] + }); + if (topLevel) { + results = this.getAbsoluteResults(topLevel, pathArray.splice(-i, i)); + return results; + } + } else { + if (foundKey == fieldKeyArray[1]) { + results = this.getAbsoluteResults(found[foundKey], pathArray.splice(-i, i)); + return results; + } } } }