Merge branch 'pkg_mgmt' into v1.1
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / model / DescriptorModelMetaProperty.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18 /**
19 * Created by onvelocity on 1/27/16.
20 *
21 * This class provides utility methods for interrogating an instance of model uiState object.
22 */
23
24 'use strict';
25
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'
33
34 export default {
35 isBoolean(property = {}) {
36 return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'boolean')
37 },
38 isLeaf(property = {}) {
39 return /leaf|choice/.test(property.type);
40 },
41 isList(property = {}) {
42 return /list|leaf_list/.test(property.type);
43 },
44 isLeafList(property = {}) {
45 return property.type === 'leaf_list';
46 },
47 isLeafRef(property = {}) {
48 const type = property['data-type'] || {};
49 return type.hasOwnProperty('leafref');
50 },
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
54 // '0..2' is an array
55 // '0..N' is 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;
60 },
61 isEnumeration(property = {}) {
62 const type = property['data-type'] || {};
63 return type.hasOwnProperty('enumeration');
64 },
65 isRequired(property = {}) {
66 return /^1/.test(property.cardinality);
67 },
68 isObject(property = {}) {
69 return !/^(leaf|leaf_list)$/.test(property.type);
70 },
71 isSimpleList(property = {}) {
72 return _.includes(DescriptorModelFields.simpleList, property.name);
73 },
74 isPrimativeDataType(property = {}) {
75 const Property = this;
76 return /string|int/.test(property['data-type']) || Property.isEnumeration(property) || Property.isGuid(property);
77 },
78 defaultValue(property = {}) {
79 if (property.defaultValue) {
80 return property.defaultValue;
81 }
82 if (this.isObject(property)) {
83 return {};
84 }
85 return '';
86 },
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);
91 }
92 },
93 getContainerCreateMethod(property, container) {
94 const name = changeCase.camel('create-' + property.name);
95 if (typeof container[name] === 'function') {
96 return container[name].bind(container);
97 }
98 },
99 containerHasCreateMethod(container, property = {}) {
100 const find = changeCase.camel('create-' + property.name);
101 return typeof container[find] === 'function';
102 },
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}];
107 }
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];
112 //if (enumObj) {
113 // enumValue = enumObj.value || enumName;
114 //}
115 return {name: enumName, value: enumValue, isSelected: String(enumValue) === String(value)};
116 });
117 },
118 getLeafRef(property = {}, path, value, fullFieldKey, transientCatalogs, container) {
119 const leafRefPath = property['data-type']['leafref']['path'];
120
121 const transientCatalogHash = {};
122
123 transientCatalogs.map((catalog) => {
124 transientCatalogHash[catalog.type + '-catalog'] = {};
125 transientCatalogHash[catalog.type + '-catalog'][catalog.type] = catalog['descriptors'];
126 });
127
128 let leafRefPathValues = utils.resolveLeafRefPath(transientCatalogHash, leafRefPath, fullFieldKey, path, container);
129
130 let leafRefObjects = [];
131
132 leafRefPathValues && leafRefPathValues.map((leafRefPathValue) => {
133 leafRefObjects.push({
134 name: leafRefPathValue,
135 value: leafRefPathValue,
136 isSelected: String(leafRefPathValue) === String(value)
137 });
138 });
139
140 return leafRefObjects;
141 },
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);
146 }
147 return /uuid/.test(property['data-type']);
148 },
149 createModelInstance(property) {
150 const Property = this;
151 const defaultValue = Property.defaultValue.bind(this);
152 function createModel(uiState, parentMeta) {
153 const model = {};
154 if (Property.isLeaf(uiState)) {
155 if (uiState.name === 'name') {
156 return changeCase.param(parentMeta.name) + '-' + InstanceCounter.count(parentMeta[':qualified-type']);
157 }
158 if (_.isArray(parentMeta.key) && _.includes(parentMeta.key, uiState.name)) {
159 if (/uuid/.test(uiState['data-type'])) {
160 return guid();
161 }
162 if (uiState['data-type'] === 'string') {
163 const prefix = uiState.name.replace('id', '');
164 return (prefix ? changeCase.param(prefix) + '-' : '') + guid(5);
165 }
166 if (/int/.test(uiState['data-type'])) {
167 return InstanceCounter.count(uiState[':qualified-type']);
168 }
169 }
170 return defaultValue(uiState);
171 } else if (Property.isList(uiState)) {
172 return [];
173 } else {
174 uiState.properties.forEach(p => {
175 model[p.name] = createModel(p, uiState);
176 });
177 }
178 return model;
179 }
180 if (property) {
181 if (Property.isPrimativeDataType(property)) {
182 return defaultValue(property);
183 }
184 if (property.type === 'leaf') {
185 return defaultValue(property);
186 }
187 if (/list/.test(property.type)) {
188 property.type = 'container';
189 }
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;
195 }
196 }
197 }