open-vault/ui/app/components/pgp-list.js
Matthew Irish 07613fc4a6
pgp list fix (#7542)
* add data selectors

* add tests for pgp-list component and refactor so initialization works

* fix comment

* fix linting

* add else statement
2019-10-09 21:09:25 -05:00

57 lines
1.4 KiB
JavaScript

import Component from '@ember/component';
export default Component.extend({
onDataUpdate: () => {},
listLength: 0,
listData: null,
init() {
this._super(...arguments);
let num = this.listLength;
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: '' }));
},
actions: {
setKey(index, key) {
let { listData } = this;
listData.splice(index, 1, key);
this.onDataUpdate(listData.compact().map(k => k.value));
},
},
});