open-vault/ui/app/mixins/key-mixin.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

import { computed } from '@ember/object';
import Mixin from '@ember/object/mixin';
import utils from 'vault/lib/key-utils';
2018-04-03 14:16:57 +00:00
export default Mixin.create({
// what attribute has the path for the key
// will.be 'path' for v2 or 'id' v1
pathAttr: 'id',
2018-04-03 14:16:57 +00:00
flags: null,
initialParentKey: null,
isCreating: computed('initialParentKey', function() {
2018-04-03 14:16:57 +00:00
return this.get('initialParentKey') != null;
}),
pathVal() {
return this.get(this.pathAttr);
},
// rather than using defineProperty for all of these,
// we're just going to hardcode the known keys for the path ('id' and 'path')
isFolder: computed('id', 'path', function() {
return utils.keyIsFolder(this.pathVal());
2018-04-03 14:16:57 +00:00
}),
keyParts: computed('id', 'path', function() {
return utils.keyPartsForKey(this.pathVal());
2018-04-03 14:16:57 +00:00
}),
parentKey: computed('id', 'path', 'isCreating', {
2018-04-03 14:16:57 +00:00
get: function() {
return this.isCreating ? this.initialParentKey : utils.parentKeyForKey(this.pathVal());
2018-04-03 14:16:57 +00:00
},
set: function(_, value) {
return value;
},
}),
keyWithoutParent: computed('id', 'path', 'parentKey', {
2018-04-03 14:16:57 +00:00
get: function() {
var key = this.pathVal();
return key ? key.replace(this.parentKey, '') : null;
2018-04-03 14:16:57 +00:00
},
set: function(_, value) {
if (value && value.trim()) {
this.set(this.pathAttr, this.parentKey + value);
2018-04-03 14:16:57 +00:00
} else {
this.set(this.pathAttr, null);
2018-04-03 14:16:57 +00:00
}
return value;
},
}),
});