open-vault/ui/app/lib/kv-object.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-04-03 14:16:57 +00:00
import Ember from 'ember';
const { typeOf, guidFor } = Ember;
2018-04-03 14:16:57 +00:00
export default Ember.ArrayProxy.extend({
fromJSON(json) {
if (json && typeOf(json) !== 'object') {
throw new Error('Vault expects data to be formatted as an JSON object.');
}
let contents = Object.keys(json || []).map(key => {
2018-04-03 14:16:57 +00:00
let obj = {
name: key,
value: json[key],
};
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;
}
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() {
return this.any(item => typeof item.value !== 'string');
},
});