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 _includes
from 'lodash/includes'
27 import _isArray
from 'lodash/isArray'
28 import guid
from './../guid'
29 import changeCase
from 'change-case'
30 import InstanceCounter
from './../InstanceCounter'
31 import DescriptorModelFields
from './DescriptorModelFields'
32 import DescriptorTemplateFactory
from './DescriptorTemplateFactory'
33 import utils
from '../utils'
36 isBoolean(property
= {}) {
37 return (typeof(property
['data-type']) == 'string') && (property
['data-type'].toLowerCase() == 'boolean')
39 isLeaf(property
= {}) {
40 return /leaf|choice/.test(property
.type
);
42 isList(property
= {}) {
43 return /list|leaf_list/.test(property
.type
);
45 isLeafList(property
= {}) {
46 return property
.type
=== 'leaf_list';
48 isLeafRef(property
= {}) {
49 const type
= property
['data-type'] || {};
50 return type
.hasOwnProperty('leafref');
52 isArray(property
= {}) {
53 // give '1' or '0..N' or '0..1' or '0..5' determine if represents an array
54 // '0..1' is not an array
57 const cardinality
= String(property
.cardinality
).toUpperCase();
58 const pos
= cardinality
.lastIndexOf('.') + 1;
59 const val
= cardinality
.substr(pos
);
60 return val
=== 'N' || parseInt(val
, 10) > 1;
62 isEnumeration(property
= {}) {
63 const type
= property
['data-type'] || {};
64 return type
.hasOwnProperty('enumeration');
66 isRequired(property
= {}) {
67 return /^1/.test(property
.cardinality
);
69 isObject(property
= {}) {
70 return !/^(leaf|leaf_list)$/.test(property
.type
);
72 isSimpleList(property
= {}) {
73 return _includes(DescriptorModelFields
.simpleList
, property
.name
);
75 isPrimativeDataType(property
= {}) {
76 const Property
= this;
77 return /string|int/.test(property
['data-type']) || Property
.isEnumeration(property
) || Property
.isGuid(property
);
79 defaultValue(property
= {}) {
80 if (property
.defaultValue
) {
81 return property
.defaultValue
;
83 if (this.isObject(property
)) {
88 getContainerMethod(property
, container
, methodName
) {
89 const name
= changeCase
.camel(methodName
+ '-' + property
.name
);
90 if (typeof container
[name
] === 'function') {
91 return container
[name
].bind(container
);
94 getContainerCreateMethod(property
, container
) {
95 const name
= changeCase
.camel('create-' + property
.name
);
96 if (typeof container
[name
] === 'function') {
97 return container
[name
].bind(container
);
100 containerHasCreateMethod(container
, property
= {}) {
101 const find
= changeCase
.camel('create-' + property
.name
);
102 return typeof container
[find
] === 'function';
104 getEnumeration(property
= {}, value
) {
105 const enumeration
= property
['data-type'].enumeration
.enum;
106 if (typeof enumeration
=== 'string') {
107 return [{name
: enumeration
, value
: enumeration
, isSelected
: String(value
) === enumeration
}];
109 return Object
.keys(enumeration
).map(enumName
=> {
110 let enumValue
= enumName
;
111 // warn we only support named enums and systematically ignore enum values
112 //const enumObj = enumeration[enumName];
114 // enumValue = enumObj.value || enumName;
116 return {name
: enumName
, value
: enumValue
, isSelected
: String(enumValue
) === String(value
)};
119 getLeafRef(property
= {}, path
, value
, fullFieldKey
, transientCatalogs
, container
) {
120 const leafRefPath
= property
['data-type']['leafref']['path'];
122 const transientCatalogHash
= {};
124 transientCatalogs
.map((catalog
) => {
125 transientCatalogHash
[catalog
.type
+ '-catalog'] = {};
126 transientCatalogHash
[catalog
.type
+ '-catalog'][catalog
.type
] = catalog
['descriptors'];
129 let leafRefPathValues
= utils
.resolveLeafRefPath(transientCatalogHash
, leafRefPath
, fullFieldKey
, path
, container
);
131 let leafRefObjects
= [];
133 leafRefPathValues
&& leafRefPathValues
.map((leafRefPathValue
) => {
134 leafRefObjects
.push({
135 name
: leafRefPathValue
,
136 value
: leafRefPathValue
,
137 isSelected
: String(leafRefPathValue
) === String(value
)
141 return leafRefObjects
;
143 isGuid(property
= {}) {
144 const type
= property
['data-type'];
145 if (typeof type
=== 'object' && type
.leafref
&& type
.leafref
.path
) {
146 return /\bid$/.test(type
.leafref
.path
);
148 return /uuid/.test(property
['data-type']);
150 createModelInstance(property
) {
151 const Property
= this;
152 const defaultValue
= Property
.defaultValue
.bind(this);
153 function createModel(uiState
, parentMeta
) {
155 if (Property
.isLeaf(uiState
)) {
156 if (uiState
.name
=== 'name') {
157 return changeCase
.param(parentMeta
.name
) + '-' + InstanceCounter
.count(parentMeta
[':qualified-type']);
159 if (_isArray(parentMeta
.key
) && _includes(parentMeta
.key
, uiState
.name
)) {
160 if (/uuid/.test(uiState
['data-type'])) {
163 if (uiState
['data-type'] === 'string') {
164 const prefix
= uiState
.name
.replace('id', '');
165 return (prefix
? changeCase
.param(prefix
) + '-' : '') + guid(5);
167 if (/int/.test(uiState
['data-type'])) {
168 return InstanceCounter
.count(uiState
[':qualified-type']);
171 return defaultValue(uiState
);
172 } else if (Property
.isList(uiState
)) {
175 uiState
.properties
.forEach(p
=> {
176 model
[p
.name
] = createModel(p
, uiState
);
182 if (Property
.isPrimativeDataType(property
)) {
183 return defaultValue(property
);
185 if (property
.type
=== 'leaf') {
186 return defaultValue(property
);
188 if (/list/.test(property
.type
)) {
189 property
.type
= 'container';
191 const modelInstance
= createModel(property
, property
);
192 modelInstance
.uiState
= {type
: property
.name
};
193 const modelFragment
= DescriptorTemplateFactory
.createModelForType(property
[':qualified-type'] || property
.name
) || {};
194 Object
.assign(modelInstance
, modelFragment
);
195 return modelInstance
;