update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / admin / src / yang / leaf-utils.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 export default {
25 isString(property = {}) {
26 return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'string')
27 },
28 isBoolean(property = {}) {
29 return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'boolean')
30 },
31 isLeafEmpty(property = {}) {
32 return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'empty')
33 },
34 isLeaf(property = {}) {
35 return property.type === 'leaf';
36 },
37 isLeafOrLeafList(property = {}) {
38 return property.type === 'leaf' || property.type === 'leaf_list';
39 },
40 isLeafList(property = {}) {
41 return property.type === 'leaf_list';
42 },
43 isList(property = {}) {
44 return property.type === 'list';
45 },
46 isLeafRef(property = {}) {
47 const type = property['data-type'] || {};
48 return type.hasOwnProperty('leafref');
49 },
50 isEnumeration(property = {}) {
51 const type = property['data-type'] || {};
52 return type.hasOwnProperty('enumeration');
53 },
54 isRequired(property = {}) {
55 return property.mandatory === 'true';
56 },
57 isKey(property = {}) {
58 return property['is-key'] === 'true';
59 },
60 isJSON(property = {}) {
61 property.name.match(/(vnfd|nsd):meta$/)
62 },
63 getDefaultValue(property = {}) {
64 if (property['default-value']) {
65 return property['default-value'];
66 }
67 return null;
68 },
69 getEnumeration(property = {}, value) {
70 const enumeration = property['data-type'].enumeration.enum;
71 if (typeof enumeration === 'string') {
72 return [{name: enumeration, value: enumeration, isSelected: String(value) === enumeration}];
73 }
74 return Object.keys(enumeration).map(enumName => {
75 let enumValue = enumName;
76 // warn we only support named enums and systematically ignore enum values
77 //const enumObj = enumeration[enumName];
78 //if (enumObj) {
79 // enumValue = enumObj.value || enumName;
80 //}
81 return {name: enumName, value: enumValue, isSelected: String(enumValue) === String(value)};
82 });
83 },
84 getDisplayValue(property = {}, rawValue) {
85 if (this.isLeafEmpty(property) && rawValue !== undefined && rawValue !== null) {
86 return "Enabled";
87 } if (this.isLeafList(property) && Array.isArray(rawValue)) {
88 return rawValue.join(", ");
89 }
90 return rawValue;
91 },
92
93 isValueSet(property = {}, rawValue) {
94 return !(
95 (typeof rawValue === 'undefined')
96 || (rawValue == null)
97 || (typeof rawValue === 'string' && rawValue.length === 0)
98 );
99 }
100 }