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.
18 import Alt
from 'widgets/skyquake_container/skyquakeAltInstance';
19 import yang
from '../yang/leaf-utils';
24 } from 'source/schema'
29 import adminActions
from '../adminActions'
31 function pullOutWantedProperty(path
, result
) {
35 const nodes
= path
.split('/');
36 if (nodes
[0] === 'project') {
37 nodes
.shift(); // get rid of top level as it was not sent back
38 result
= nodes
.reduce((data
, node
) => data
[node
], result
);
43 function findItemIndex(list
, key
, keyValue
) {
44 const keyPath
= Array
.isArray(keyValue
) ? keyValue
: key
.length
> 1 ? JSON
.parse(keyValue
) : [keyValue
];
46 return list
.findIndex(item
=> {
47 return key
.every((k
, i
) => item
[k
] === keyPath
[i
]);
51 const match
= keyPath
[0];
52 return list
.findIndex(item
=> {
53 return item
[leaf
] === match
;
58 function getItemFromList(list
, key
, keyValue
) {
59 return list
[findItemIndex(list
, key
, keyValue
)];
62 function getElement(path
, store
) {
63 return path
.reduce((parent
, node
, index
) => {
64 const element
= Object
.assign({}, parent
);
65 if (parent
.type
=== 'list') {
66 element
.type
= 'list-entry'
67 element
.value
= getItemFromList(parent
.value
, parent
.schema
.key
, node
);
69 element
.schema
= parent
.schema
.properties
.find(property
=> property
.name
=== node
)
70 element
.type
= element
.schema
.type
;
71 element
.value
= (parent
.value
&& parent
.value
[node
]) || (parent
.value
[node
] = {});
76 schema
: store
.schema
[store
.path
],
77 type
: store
.schema
[store
.path
].type
81 function massageData(data
, schema
){
82 return Object
.keys(data
).reduce((o
, name
) => {
83 let input
= data
[name
];
85 if (yang
.isLeafEmpty(input
.property
)) {
86 output
= {type
: 'leaf_empty', data
: input
.value
.length
? 'set' : ''}
87 } else if (yang
.isLeafList(input
.property
)) {
88 const newList
= Array
.isArray(input
.value
) ? input
.value
: input
.value
.split(',');
89 const oldList
= input
.currentValue
? (Array
.isArray(input
.currentValue
) ? input
.currentValue
: input
.currentValue
.split(',')) : [];
90 const addList
= newList
.reduce((list
, v
) => {
93 const i
= oldList
.indexOf(v
);
102 output
= {type
: 'leaf_list', data
: {}};
103 addList
.length
&& (output
.data
.add
= addList
);
104 oldList
.length
&& (output
.data
.remove
= oldList
);
106 output
= input
.value
;
119 this.bindActions(adminActions
);
120 this.registerAsync(modelSource
);
121 this.bindActions(modelActions
);
122 this.registerAsync(schemaSource
);
123 this.bindActions(schemaActions
);
124 this.exportPublicMethods({
128 'delete': this.remove
134 new Promise((resolve
, reject
) => {
135 this.getInstance().loadSchema(this.state
.path
)
136 .then(result
=> resolve(result
))
137 .catch(error
=> reject(error
))
139 new Promise((resolve
, reject
) => {
140 const result
= this.getInstance().loadModel(this.state
.path
)
141 .then(result
=> resolve(pullOutWantedProperty(this.state
.path
, result
)))
142 .catch(error
=> reject(error
))
144 ]).then((results
) => {
147 path
: this.state
.path
,
148 schema
: results
[0][this.state
.path
],
149 data
: results
[1] || {}
151 }).catch((errors
) => {
161 path
: this.state
.path
164 update
= (path
, obj
) => {
165 const e
= getElement(path
, this.state
);
166 obj
= massageData(obj
, e
.schema
);
167 this.getInstance().updateModel(path
, obj
)
168 .then((response
) => {
170 const target
= getElement(path
, this.state
);
171 for (const name
in response
.result
) {
172 if (response
.result
[name
].success
) {
173 target
.value
[name
] = response
.result
[name
].value
175 errors
.push(response
.result
[name
]);
180 data
: this.state
.data
182 errors
&& this.setState({
187 }).catch((errors
) => {
200 create
= (path
, obj
) => {
201 const e
= getElement(path
, this.state
);
202 obj
= massageData(obj
, e
.schema
);
203 this.getInstance().createModel(path
, obj
)
204 .then((response
) => {
205 const createList
= () => {
206 const parentPath
= path
.slice();
207 const name
= parentPath
.pop();
209 const parent
= getElement(parentPath
, this.state
)
210 if (parent
.value
) parent
.value
[name
] = [];
211 return parent
.value
[name
];
214 const target
= getElement(path
, this.state
);
215 const list
= target
.value
|| createList();
216 list
.unshift(response
.data
);
219 data
: this.state
.data
221 errors
&& this.setState({
226 }).catch((errors
) => {
239 remove
= (path
, obj
) => {
240 this.getInstance().deleteModel(path
)
241 .then((response
) => {
243 const id
= path
.pop();
244 const list
= getElement(path
, this.state
);
245 const index
= findItemIndex(list
.value
, list
.schema
.key
, id
);
246 list
.value
.splice(index
, 1);
249 data
: this.state
.data
251 }).catch((errors
) => {
265 export default ModelStore