2018-09-25 16:28:26 +00:00
|
|
|
import ArrayProxy from '@ember/array/proxy';
|
|
|
|
import { typeOf } from '@ember/utils';
|
|
|
|
import { guidFor } from '@ember/object/internals';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default ArrayProxy.extend({
|
2018-04-03 14:16:57 +00:00
|
|
|
fromJSON(json) {
|
2018-09-05 16:07:39 +00:00
|
|
|
if (json && typeOf(json) !== 'object') {
|
|
|
|
throw new Error('Vault expects data to be formatted as an JSON object.');
|
|
|
|
}
|
2021-12-17 03:44:29 +00:00
|
|
|
let contents = Object.keys(json || []).map((key) => {
|
2018-04-03 14:16:57 +00:00
|
|
|
let obj = {
|
|
|
|
name: key,
|
|
|
|
value: json[key],
|
|
|
|
};
|
2018-09-05 16:07:39 +00:00
|
|
|
guidFor(obj);
|
2018-04-03 14:16:57 +00:00
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
this.setObjects(
|
|
|
|
contents.sort((a, b) => {
|
|
|
|
if (a.name === '') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (b.name === '') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
fromJSONString(jsonString) {
|
|
|
|
return this.fromJSON(JSON.parse(jsonString));
|
|
|
|
},
|
|
|
|
|
|
|
|
toJSON(includeBlanks = false) {
|
|
|
|
return this.reduce((obj, item) => {
|
|
|
|
if (!includeBlanks && item.value === '' && item.name === '') {
|
|
|
|
return obj;
|
|
|
|
}
|
2018-07-23 21:57:35 +00:00
|
|
|
let val = typeof item.value === 'undefined' ? '' : item.value;
|
|
|
|
obj[item.name || ''] = val;
|
2018-04-03 14:16:57 +00:00
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
},
|
|
|
|
|
|
|
|
toJSONString(includeBlanks) {
|
|
|
|
return JSON.stringify(this.toJSON(includeBlanks), null, 2);
|
|
|
|
},
|
|
|
|
|
|
|
|
isAdvanced() {
|
2021-12-17 03:44:29 +00:00
|
|
|
return this.any((item) => typeof item.value !== 'string');
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
});
|