3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * Created by onvelocity on 1/27/16.
21 * This class provides utility methods for interrogating an instance of model uiState object.
26 import _
from 'lodash'
27 import guid
from './../guid'
28 import changeCase
from 'change-case'
29 import InstanceCounter
from './../InstanceCounter'
30 import DescriptorModelFields
from './DescriptorModelFields'
31 import DescriptorTemplateFactory
from './DescriptorTemplateFactory'
32 import utils
from '../utils'
35 isBoolean(property
= {}) {
36 return (typeof(property
['data-type']) == 'string') && (property
['data-type'].toLowerCase() == 'boolean')
38 isLeaf(property
= {}) {
39 return /leaf|choice/.test(property
.type
);
41 isList(property
= {}) {
42 return /list|leaf_list/.test(property
.type
);
44 isLeafList(property
= {}) {
45 return property
.type
=== 'leaf_list';
47 isLeafRef(property
= {}) {
48 const type
= property
['data-type'] || {};
49 return type
.hasOwnProperty('leafref');
51 isArray(property
= {}) {
52 // give '1' or '0..N' or '0..1' or '0..5' determine if represents an array
53 // '0..1' is not an array
56 const cardinality
= String(property
.cardinality
).toUpperCase();
57 const pos
= cardinality
.lastIndexOf('.') + 1;
58 const val
= cardinality
.substr(pos
);
59 return val
=== 'N' || parseInt(val
, 10) > 1;
61 isEnumeration(property
= {}) {
62 const type
= property
['data-type'] || {};
63 return type
.hasOwnProperty('enumeration');
65 isRequired(property
= {}) {
66 return /^1/.test(property
.cardinality
);
68 isObject(property
= {}) {
69 return !/^(leaf|leaf_list)$/.test(property
.type
);
71 isSimpleList(property
= {}) {
72 return _
.includes(DescriptorModelFields
.simpleList
, property
.name
);
74 isPrimativeDataType(property
= {}) {
75 const Property
= this;
76 return /string|int/.test(property
['data-type']) || Property
.isEnumeration(property
) || Property
.isGuid(property
);
78 defaultValue(property
= {}) {
79 if (property
.defaultValue
) {
80 return property
.defaultValue
;
82 if (this.isObject(property
)) {
87 getContainerMethod(property
, container
, methodName
) {
88 const name
= changeCase
.camel(methodName
+ '-' + property
.name
);
89 if (typeof container
[name
] === 'function') {
90 return container
[name
].bind(container
);
93 getContainerCreateMethod(property
, container
) {
94 const name
= changeCase
.camel('create-' + property
.name
);
95 if (typeof container
[name
] === 'function') {
96 return container
[name
].bind(container
);
99 containerHasCreateMethod(container
, property
= {}) {
100 const find
= changeCase
.camel('create-' + property
.name
);
101 return typeof container
[find
] === 'function';
103 getEnumeration(property
= {}, value
) {
104 const enumeration
= property
['data-type'].enumeration
.enum;
105 if (typeof enumeration
=== 'string') {
106 return [{name
: enumeration
, value
: enumeration
, isSelected
: String(value
) === enumeration
}];
108 return Object
.keys(enumeration
).map(enumName
=> {
109 let enumValue
= enumName
;
110 // warn we only support named enums and systematically ignore enum values
111 //const enumObj = enumeration[enumName];
113 // enumValue = enumObj.value || enumName;
115 return {name
: enumName
, value
: enumValue
, isSelected
: String(enumValue
) === String(value
)};
118 getLeafRef(property
= {}, path
, value
, fullFieldKey
, transientCatalogs
, container
) {
119 const leafRefPath
= property
['data-type']['leafref']['path'];
121 const transientCatalogHash
= {};
123 transientCatalogs
.map((catalog
) => {
124 transientCatalogHash
[catalog
.type
+ '-catalog'] = {};
125 transientCatalogHash
[catalog
.type
+ '-catalog'][catalog
.type
] = catalog
['descriptors'];
128 let leafRefPathValues
= utils
.resolveLeafRefPath(transientCatalogHash
, leafRefPath
, fullFieldKey
, path
, container
);
130 let leafRefObjects
= [];
132 leafRefPathValues
&& leafRefPathValues
.map((leafRefPathValue
) => {
133 leafRefObjects
.push({
134 name
: leafRefPathValue
,
135 value
: leafRefPathValue
,
136 isSelected
: String(leafRefPathValue
) === String(value
)
140 return leafRefObjects
;
142 isGuid(property
= {}) {
143 const type
= property
['data-type'];
144 if (typeof type
=== 'object' && type
.leafref
&& type
.leafref
.path
) {
145 return /\bid$/.test(type
.leafref
.path
);
147 return /uuid/.test(property
['data-type']);
149 createModelInstance(property
) {
150 const Property
= this;
151 const defaultValue
= Property
.defaultValue
.bind(this);
152 function createModel(uiState
, parentMeta
) {
154 if (Property
.isLeaf(uiState
)) {
155 if (uiState
.name
=== 'name') {
156 return changeCase
.param(parentMeta
.name
) + '-' + InstanceCounter
.count(parentMeta
[':qualified-type']);
158 if (_
.isArray(parentMeta
.key
) && _
.includes(parentMeta
.key
, uiState
.name
)) {
159 if (/uuid/.test(uiState
['data-type'])) {
162 if (uiState
['data-type'] === 'string') {
163 const prefix
= uiState
.name
.replace('id', '');
164 return (prefix
? changeCase
.param(prefix
) + '-' : '') + guid(5);
166 if (/int/.test(uiState
['data-type'])) {
167 return InstanceCounter
.count(uiState
[':qualified-type']);
170 return defaultValue(uiState
);
171 } else if (Property
.isList(uiState
)) {
174 uiState
.properties
.forEach(p
=> {
175 model
[p
.name
] = createModel(p
, uiState
);
181 if (Property
.isPrimativeDataType(property
)) {
182 return defaultValue(property
);
184 if (property
.type
=== 'leaf') {
185 return defaultValue(property
);
187 if (/list/.test(property
.type
)) {
188 property
.type
= 'container';
190 const modelInstance
= createModel(property
, property
);
191 modelInstance
.uiState
= {type
: property
.name
};
192 const modelFragment
= DescriptorTemplateFactory
.createModelForType(property
[':qualified-type'] || property
.name
) || {};
193 Object
.assign(modelInstance
, modelFragment
);
194 return modelInstance
;