mirror of
https://github.com/dzeiocom/FormManager.git
synced 2025-04-22 19:02:15 +00:00
Added Draggable attribute on ReapeatInput
Signed-off-by: Florian Bouillon <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
c2d2899a8c
commit
0520ec952f
@ -77,6 +77,48 @@ export default class RepeatInput extends DefaultInput {
|
|||||||
let node = this.element.children.item(this.element.childElementCount-2) as HTMLElement
|
let node = this.element.children.item(this.element.childElementCount-2) as HTMLElement
|
||||||
node.classList.add("fmr-element")
|
node.classList.add("fmr-element")
|
||||||
node.style.display = ""
|
node.style.display = ""
|
||||||
|
if ('draggable' in this.element.dataset) {
|
||||||
|
node.draggable = true
|
||||||
|
node.addEventListener('dragstart', (ev) => {
|
||||||
|
ev.dataTransfer?.setData('text', JSON.stringify({index: getNodePosition(this.element, node), name: this.getName()}))
|
||||||
|
})
|
||||||
|
node.addEventListener('drop', (ev) => {
|
||||||
|
ev.preventDefault()
|
||||||
|
|
||||||
|
if (!ev.dataTransfer?.getData('text')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const transferData = JSON.parse(ev.dataTransfer.getData('text'))
|
||||||
|
|
||||||
|
if (transferData.name !== this.getName()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const otherItemPos = transferData.index
|
||||||
|
const currentItemPos = getNodePosition(this.element, node)
|
||||||
|
|
||||||
|
const val = this.getValue()
|
||||||
|
const item = val[otherItemPos]
|
||||||
|
|
||||||
|
// First Part slice
|
||||||
|
const firstPart = val.slice(undefined, currentItemPos)
|
||||||
|
const secondPart = val.slice(currentItemPos)
|
||||||
|
|
||||||
|
// Second part remove item
|
||||||
|
const frstpIndex = firstPart.indexOf(item)
|
||||||
|
const scndPIndex = secondPart.indexOf(item)
|
||||||
|
if (frstpIndex !== -1) {
|
||||||
|
firstPart.splice(frstpIndex, 1)
|
||||||
|
} else if (scndPIndex !== -1) {
|
||||||
|
secondPart.splice(scndPIndex, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
//third part merge all
|
||||||
|
this.form.setValue(this.getName(), firstPart.concat(item, ...secondPart))
|
||||||
|
})
|
||||||
|
node.addEventListener('dragover', (ev) => ev.preventDefault())
|
||||||
|
}
|
||||||
|
|
||||||
// loop through inputs ot init them
|
// loop through inputs ot init them
|
||||||
let sub: InputAbstract[] = [];
|
let sub: InputAbstract[] = [];
|
||||||
@ -90,9 +132,11 @@ export default class RepeatInput extends DefaultInput {
|
|||||||
}
|
}
|
||||||
sub.push(input)
|
sub.push(input)
|
||||||
|
|
||||||
|
const name = input.getName()
|
||||||
|
|
||||||
// if values is a named array
|
// if values is a named array
|
||||||
if (values != undefined && values[input.getName()] != undefined) {
|
if (values !== undefined && typeof values === 'object' && name in values) {
|
||||||
input.setValue(values[input.getName()])
|
input.setValue(values[name])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,25 +160,23 @@ export default class RepeatInput extends DefaultInput {
|
|||||||
//remove every elements
|
//remove every elements
|
||||||
this.element.querySelectorAll(".fmr-element").forEach(el => {
|
this.element.querySelectorAll(".fmr-element").forEach(el => {
|
||||||
el.remove()
|
el.remove()
|
||||||
this.elements = []
|
|
||||||
})
|
})
|
||||||
|
this.elements = []
|
||||||
//ef string finish function
|
//ef string finish function
|
||||||
if (typeof(value) == "string") return
|
if (typeof(value) == "string") return
|
||||||
|
|
||||||
//create each line
|
//create each line
|
||||||
for (const indexStr in value) {
|
for (const indexStr in value) {
|
||||||
let index = parseInt(indexStr)
|
let index = parseInt(indexStr)
|
||||||
if (value.hasOwnProperty(index)) {
|
if (!Object.prototype.hasOwnProperty.call(value, index)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
const el = value[index];
|
const el = value[index];
|
||||||
|
|
||||||
if (this.element.querySelectorAll(".fmr-element").length <= index) {
|
if (this.element.querySelectorAll(".fmr-element").length <= index) {
|
||||||
//add element
|
|
||||||
if (!el) continue
|
|
||||||
this.addLine(el)
|
this.addLine(el)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// update element
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +188,6 @@ export default class RepeatInput extends DefaultInput {
|
|||||||
for (let i = 0; i < line.length; i++) {
|
for (let i = 0; i < line.length; i++) {
|
||||||
const col = line[i];
|
const col = line[i];
|
||||||
if (!col.element.hasAttribute('data-ignore')) {
|
if (!col.element.hasAttribute('data-ignore')) {
|
||||||
console.log(col)
|
|
||||||
realLine.push(col)
|
realLine.push(col)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,7 +209,6 @@ export default class RepeatInput extends DefaultInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public verify(): boolean {
|
public verify(): boolean {
|
||||||
console.log(this.elements.length)
|
|
||||||
if (this.element.hasAttribute('required') && this.elements.length === 0) {
|
if (this.element.hasAttribute('required') && this.elements.length === 0) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -186,3 +226,16 @@ export default class RepeatInput extends DefaultInput {
|
|||||||
tagName: "div"
|
tagName: "div"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getNodePosition(parent: HTMLElement, node: HTMLElement) {
|
||||||
|
for (const index in parent.children) {
|
||||||
|
if (!Object.prototype.hasOwnProperty.call(parent.children, index)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const child = parent.children[index];
|
||||||
|
if (child === node) {
|
||||||
|
return parseInt(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user