2018-09-25 16:28:26 +00:00
|
|
|
import Component from '@ember/component';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend({
|
2018-04-03 14:16:57 +00:00
|
|
|
onDataUpdate: () => {},
|
2019-10-10 02:09:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-11-09 23:15:31 +00:00
|
|
|
const list = this.newList(num);
|
2019-10-10 02:09:25 +00:00
|
|
|
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
|
2021-11-04 20:25:15 +00:00
|
|
|
list = [...this.listData, ...this.newList(this.listLength - this.listData.length)];
|
2019-10-10 02:09:25 +00:00
|
|
|
}
|
|
|
|
this.set('listData', list || this.listData);
|
2021-12-17 03:44:29 +00:00
|
|
|
this.onDataUpdate((list || this.listData).compact().map((k) => k.value));
|
2019-10-10 02:09:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
newList(length) {
|
|
|
|
return Array(length || 0)
|
2018-10-12 19:03:01 +00:00
|
|
|
.fill(null)
|
|
|
|
.map(() => ({ value: '' }));
|
2019-10-10 02:09:25 +00:00
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
actions: {
|
|
|
|
setKey(index, key) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const { listData } = this;
|
2019-10-10 02:09:25 +00:00
|
|
|
listData.splice(index, 1, key);
|
2021-12-17 03:44:29 +00:00
|
|
|
this.onDataUpdate(listData.compact().map((k) => k.value));
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|