I FUCKING DID IT
Added a helper component for managing a list of strings. This component could, in theory, also be turned into a generic one for any type of data
This commit is contained in:
parent
c7af216ce3
commit
e802027236
23 changed files with 1042 additions and 121 deletions
57
frontend-reactive/app/components/util/string-array.ts
Normal file
57
frontend-reactive/app/components/util/string-array.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import MutableArray from '@ember/array/mutable'
|
||||
import { action } from '@ember/object'
|
||||
import Component from '@glimmer/component'
|
||||
import { tracked } from '@glimmer/tracking'
|
||||
|
||||
export interface UtilStringArraySignature {
|
||||
// The arguments accepted by the component
|
||||
Args: {
|
||||
list: MutableArray<{ value: string }>
|
||||
prefix: string
|
||||
}
|
||||
// Any blocks yielded by the component
|
||||
Blocks: {
|
||||
default: []
|
||||
}
|
||||
// The element to which `...attributes` is applied in the component template
|
||||
Element: null
|
||||
}
|
||||
|
||||
export default class UtilStringArray extends Component<UtilStringArraySignature> {
|
||||
@action addElement() {
|
||||
MutableArray.apply(this.args.list)
|
||||
this.args.list.pushObject({ value: '' })
|
||||
}
|
||||
|
||||
@action removeElement(event: MouseEvent) {
|
||||
MutableArray.apply(this.args.list)
|
||||
const target = event.target as HTMLDivElement
|
||||
const splits = target.id.split('-', 2)
|
||||
if (splits.length != 2) return
|
||||
const indexStr = splits[1]
|
||||
//console.log('Content: ', indexStr)
|
||||
if (!indexStr) return
|
||||
//let index = this.args.list.find((elem) => elem == content)
|
||||
//let index = this.listCopy.findIndex((d) => d == content)
|
||||
this.args.list.removeAt(Number(indexStr))
|
||||
}
|
||||
|
||||
transformArrayIntoUsable(arr: Array<string>): { [key: number]: string } {
|
||||
const out: { [key: number]: string } = {}
|
||||
const tmp = arr.map((elem: string, index: number) => {
|
||||
out[index] = elem
|
||||
return elem
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
countElemsInObj(obj: any): number {
|
||||
let count = 0
|
||||
|
||||
for (var prop in obj) {
|
||||
if (obj.hasOwnProperty(prop)) ++count
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue