X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FUI.git;a=blobdiff_plain;f=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Flibraries%2Fmodel%2FDescriptorModelMetaProperty.js;h=2955e550372eafd12ca2816019cd75fbd9d519ff;hp=efd06c0ff833f5671853e6b7df63833b68f93723;hb=075200b023e659111da2ffd47129c88314140dd1;hpb=e29efc315df33d546237e270470916e26df391d6 diff --git a/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js b/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js index efd06c0ff..2955e5503 100644 --- a/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js +++ b/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js @@ -1,5 +1,5 @@ /* - * + * * Copyright 2016 RIFT.IO Inc * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,22 +23,31 @@ 'use strict'; -import _ from 'lodash' +import _includes from 'lodash/includes' +import _isArray from 'lodash/isArray' import guid from './../guid' import changeCase from 'change-case' import InstanceCounter from './../InstanceCounter' import DescriptorModelFields from './DescriptorModelFields' import DescriptorTemplateFactory from './DescriptorTemplateFactory' +import utils from '../utils' export default { + isBoolean(property = {}) { + return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'boolean') + }, isLeaf(property = {}) { return /leaf|choice/.test(property.type); }, isList(property = {}) { - return /list|leaf-list/.test(property.type); + return /list|leaf_list/.test(property.type); }, isLeafList(property = {}) { - return property.type === 'leaf-list'; + return property.type === 'leaf_list'; + }, + isLeafRef(property = {}) { + const type = property['data-type'] || {}; + return type.hasOwnProperty('leafref'); }, isArray(property = {}) { // give '1' or '0..N' or '0..1' or '0..5' determine if represents an array @@ -58,10 +67,10 @@ export default { return /^1/.test(property.cardinality); }, isObject(property = {}) { - return !/^(leaf|leaf-list)$/.test(property.type); + return !/^(leaf|leaf_list)$/.test(property.type); }, isSimpleList(property = {}) { - return _.contains(DescriptorModelFields.simpleList, property.name); + return _includes(DescriptorModelFields.simpleList, property.name); }, isPrimativeDataType(property = {}) { const Property = this; @@ -107,6 +116,30 @@ export default { return {name: enumName, value: enumValue, isSelected: String(enumValue) === String(value)}; }); }, + getLeafRef(property = {}, path, value, fullFieldKey, transientCatalogs, container) { + const leafRefPath = property['data-type']['leafref']['path']; + + const transientCatalogHash = {}; + + transientCatalogs.map((catalog) => { + transientCatalogHash[catalog.type + '-catalog'] = {}; + transientCatalogHash[catalog.type + '-catalog'][catalog.type] = catalog['descriptors']; + }); + + let leafRefPathValues = utils.resolveLeafRefPath(transientCatalogHash, leafRefPath, fullFieldKey, path, container); + + let leafRefObjects = []; + + leafRefPathValues && leafRefPathValues.map((leafRefPathValue) => { + leafRefObjects.push({ + name: leafRefPathValue, + value: leafRefPathValue, + isSelected: String(leafRefPathValue) === String(value) + }); + }); + + return leafRefObjects; + }, isGuid(property = {}) { const type = property['data-type']; if (typeof type === 'object' && type.leafref && type.leafref.path) { @@ -114,22 +147,36 @@ export default { } return /uuid/.test(property['data-type']); }, - createModelInstance(property) { + /** + * Create a new instance of the indicated property and, if relevent, use the given + * unique name for the instance's key (see generateItemUniqueName) + * + * @param {Object} typeOrPath - property definition + * @param {any} uniqueName + * @returns + */ + createModelInstance(property, uniqueName) { const Property = this; const defaultValue = Property.defaultValue.bind(this); - function createModel(uiState, parentMeta) { + function createModel(uiState, parentMeta, uniqueName) { const model = {}; if (Property.isLeaf(uiState)) { if (uiState.name === 'name') { - return changeCase.param(parentMeta.name) + '-' + InstanceCounter.count(parentMeta[':qualified-type']); + return uniqueName || (changeCase.param(parentMeta.name) + '-' + InstanceCounter.count(parentMeta[':qualified-type'])); } - if (_.isArray(parentMeta.key) && _.contains(parentMeta.key, uiState.name)) { + if (_isArray(parentMeta.key) && _includes(parentMeta.key, uiState.name)) { if (/uuid/.test(uiState['data-type'])) { return guid(); } if (uiState['data-type'] === 'string') { - const prefix = uiState.name.replace('id', ''); - return (prefix ? changeCase.param(prefix) + '-' : '') + guid(5); + // if there is only one key property and we were given a + // unique name (probably because creating a list entry + // property) then use the unique name otherwise make one up. + if (parentMeta.key.length > 1 || !uniqueName) { + const prefix = uiState.name.replace('id', ''); + uniqueName = (prefix ? changeCase.param(prefix) + '-' : '') + guid(5); + } + return uniqueName; } if (/int/.test(uiState['data-type'])) { return InstanceCounter.count(uiState[':qualified-type']); @@ -140,7 +187,7 @@ export default { return []; } else { uiState.properties.forEach(p => { - model[p.name] = createModel(p, uiState); + model[p.name] = createModel(p, uiState, uniqueName); }); } return model; @@ -155,7 +202,7 @@ export default { if (/list/.test(property.type)) { property.type = 'container'; } - const modelInstance = createModel(property, property); + const modelInstance = createModel(property, property, uniqueName); modelInstance.uiState = {type: property.name}; const modelFragment = DescriptorTemplateFactory.createModelForType(property[':qualified-type'] || property.name) || {}; Object.assign(modelInstance, modelFragment);