open-vault/ui/app/components/pgp-list.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
2018-04-03 14:16:57 +00:00
export default Component.extend({
2018-04-03 14:16:57 +00:00
onDataUpdate: () => {},
listLength: 0,
listData: null,
init() {
this._super(...arguments);
let num = this.listLength;
2018-04-03 14:16:57 +00:00
if (num) {
num = parseInt(num, 10);
}
let list = this.newList(num);
this.set('listData', list);
},
didReceiveAttrs() {
this._super(...arguments);
let list;
if (!this.listLength) {
this.set('listData', []);
return;
}
// no update needed
if (this.listData.length === this.listLength) {
return;
}
if (this.listLength < this.listData.length) {
// shorten the current list
list = this.listData.slice(0, this.listLength);
} else if (this.listLength > this.listData.length) {
// add to the current list by creating a new list and copying over existing list
list = this.newList(this.listLength);
if (this.listData.length) {
list.splice(0, this.listData.length, ...this.listData);
}
}
this.set('listData', list || this.listData);
this.onDataUpdate((list || this.listData).compact().map(k => k.value));
},
newList(length) {
return Array(length || 0)
.fill(null)
.map(() => ({ value: '' }));
},
2018-04-03 14:16:57 +00:00
actions: {
setKey(index, key) {
let { listData } = this;
listData.splice(index, 1, key);
this.onDataUpdate(listData.compact().map(k => k.value));
2018-04-03 14:16:57 +00:00
},
},
});