2024-10-25 14:54:48 +00:00
|
|
|
import MutableArray from '@ember/array/mutable';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
2024-10-24 14:15:08 +00:00
|
|
|
|
|
|
|
export interface UtilStringArraySignature {
|
|
|
|
// The arguments accepted by the component
|
|
|
|
Args: {
|
2024-10-25 14:54:48 +00:00
|
|
|
list: MutableArray<{ value: string }>;
|
|
|
|
prefix: string;
|
|
|
|
onNewElement: (index: number) => void;
|
|
|
|
onDeleteElement: (index: number) => void;
|
|
|
|
};
|
2024-10-24 14:15:08 +00:00
|
|
|
// Any blocks yielded by the component
|
|
|
|
Blocks: {
|
2024-10-25 14:54:48 +00:00
|
|
|
default: [];
|
|
|
|
};
|
2024-10-24 14:15:08 +00:00
|
|
|
// The element to which `...attributes` is applied in the component template
|
2024-10-25 14:54:48 +00:00
|
|
|
Element: null;
|
2024-10-24 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class UtilStringArray extends Component<UtilStringArraySignature> {
|
|
|
|
@action addElement() {
|
2024-10-25 14:54:48 +00:00
|
|
|
MutableArray.apply(this.args.list);
|
|
|
|
this.args.list.pushObject({ value: '' });
|
|
|
|
if (this.args.onNewElement)
|
|
|
|
this.args.onNewElement(this.args.list.length - 1);
|
2024-10-24 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 14:54:48 +00:00
|
|
|
@action removeElement(index: number) {
|
|
|
|
MutableArray.apply(this.args.list);
|
2024-10-24 14:15:08 +00:00
|
|
|
//let index = this.args.list.find((elem) => elem == content)
|
|
|
|
//let index = this.listCopy.findIndex((d) => d == content)
|
2024-10-25 14:54:48 +00:00
|
|
|
this.args.list.removeAt(index);
|
|
|
|
if (this.args.onDeleteElement) this.args.onDeleteElement(index);
|
2024-10-24 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transformArrayIntoUsable(arr: Array<string>): { [key: number]: string } {
|
2024-10-25 14:54:48 +00:00
|
|
|
const out: { [key: number]: string } = {};
|
2024-10-24 14:15:08 +00:00
|
|
|
const tmp = arr.map((elem: string, index: number) => {
|
2024-10-25 14:54:48 +00:00
|
|
|
out[index] = elem;
|
|
|
|
return elem;
|
|
|
|
});
|
|
|
|
return out;
|
2024-10-24 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
countElemsInObj(obj: any): number {
|
2024-10-25 14:54:48 +00:00
|
|
|
let count = 0;
|
2024-10-24 14:15:08 +00:00
|
|
|
|
2024-10-25 14:54:48 +00:00
|
|
|
for (const prop in obj) {
|
|
|
|
if (obj.hasOwnProperty(prop)) ++count;
|
2024-10-24 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 14:54:48 +00:00
|
|
|
return count;
|
2024-10-24 14:15:08 +00:00
|
|
|
}
|
|
|
|
}
|