mirror of
https://github.com/dzeiocom/FormManager.git
synced 2025-04-25 12:22:15 +00:00
Cleaning & Fixing
This commit is contained in:
parent
b357183587
commit
66229c64c4
@ -1,5 +1,4 @@
|
||||
import FormManager from "./FormManager";
|
||||
import FMAttribute, { FMAttributeAssignment, FMAttributeListeners, TriggerCallback } from "./FMAttribute";
|
||||
import InputAbstract from "./modules/InputAbstract";
|
||||
import AttributeAbstract from "./attributes/AttributeAbstract";
|
||||
import AttributeIdentity from "./attributes/Interfaces/AttributeIdentity";
|
||||
|
@ -1,45 +0,0 @@
|
||||
import FMInput from "./FMInput";
|
||||
|
||||
|
||||
export default class FMAttribute {
|
||||
|
||||
public input: FMInput
|
||||
|
||||
public static listeners: FMAttributeListeners[] = []
|
||||
|
||||
constructor(input: FMInput) {
|
||||
this.input = input
|
||||
}
|
||||
|
||||
/**
|
||||
* Function launched on Listener trigger
|
||||
* it MUST be implemented and SHOULD return boolean
|
||||
*
|
||||
* @param ev Attribue triggered
|
||||
*/
|
||||
public trigger(ev: FMAttributeListeners, datas?: any): TriggerCallback|boolean|void {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// list of listeners
|
||||
export enum FMAttributeListeners {
|
||||
PRE_CLEAR, // Event run before clearing the form
|
||||
POST_CLEAR, // Event run after learing the form
|
||||
CHANGE, // Event runs on Form Change (datas wil be filled with the FMInput element)
|
||||
FORM_INIT, // Event runs on form init
|
||||
FORM_SUBMIT, // Event run before submitting (datas is filled with the datas that will be submitted, MUST return TriggerCallback)
|
||||
VERIFY, // Event run on element verification (return true or false only)
|
||||
FORM_FILL, // Event run after the form was filled
|
||||
}
|
||||
|
||||
|
||||
export interface FMAttributeAssignment {
|
||||
attribute: typeof FMAttribute,
|
||||
dataElement: string
|
||||
}
|
||||
|
||||
export interface TriggerCallback {
|
||||
result: boolean,
|
||||
datas?: any
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import AttributesManager from './AttributesManager';
|
||||
import { FMAttributeListeners } from './FMAttribute';
|
||||
import InputIdentity from './modules/Interfaces/InputIdentity';
|
||||
import DefaultInput from './modules/DefaultInput';
|
||||
import InputAbstract from './modules/InputAbstract';
|
||||
|
@ -21,7 +21,7 @@ export function evalF(str: string, callback?: (str: string) => void): boolean {
|
||||
* @param str the string to transform
|
||||
*/
|
||||
export function toNumber(str: any): number|undefined {
|
||||
if (str !== 0 && (str === "" || str === undefined)) return undefined
|
||||
if (str !== 0 && (str === "" || str === undefined || typeof(str) === "boolean")) return undefined
|
||||
// return undefined if it must be shown as string
|
||||
// console.log("toNumber", str)
|
||||
if ((str.startsWith("0") || str.startsWith("+")) && str.length > 1) return undefined
|
||||
|
@ -1,4 +1,3 @@
|
||||
import FMAttribute, { FMAttributeListeners, FMAttributeAssignment, TriggerCallback } from "../FMAttribute";
|
||||
import { evalF } from "../Functions";
|
||||
import AttributeListeners from "./AttributeListeners";
|
||||
import AttributeAbstract from "./AttributeAbstract";
|
||||
|
@ -1,4 +1,3 @@
|
||||
import FMAttribute, { FMAttributeListeners, FMAttributeAssignment, TriggerCallback } from "../FMAttribute";
|
||||
import AttributeAbstract from "./AttributeAbstract";
|
||||
import AttributeListeners from "./AttributeListeners";
|
||||
import AttributeIdentity from "./Interfaces/AttributeIdentity";
|
||||
|
@ -1,115 +0,0 @@
|
||||
import FormManager from '../FormManager';
|
||||
import DefaultInput from './DefaultInput';
|
||||
import InputIdentity from './Interfaces/InputIdentity';
|
||||
import { evalF } from '../Functions';
|
||||
|
||||
/**
|
||||
*
|
||||
* @class FMFileInput
|
||||
* @extends {FMInput}
|
||||
*/
|
||||
export default class FileInput extends DefaultInput {
|
||||
|
||||
isUploaded = false
|
||||
|
||||
type: number|string = 1
|
||||
|
||||
|
||||
|
||||
button: HTMLButtonElement|undefined
|
||||
|
||||
constructor(element: HTMLElement, form: FormManager) {
|
||||
super(element, form)
|
||||
|
||||
this.type = this.element.dataset.uploadType ? this.element.dataset.uploadType: 1
|
||||
|
||||
element.addEventListener("change", () => {
|
||||
console.log("pouet")
|
||||
let files = this.element.files
|
||||
if (files && element.parentElement && files.length > 0) {
|
||||
const name = element.parentElement.querySelector(".file-name")
|
||||
if (name) name.textContent = files[0].name
|
||||
}
|
||||
})
|
||||
|
||||
if (this.element.hasAttribute("data-button") && element.parentElement && element.dataset.button) {
|
||||
let btn = element.parentElement.querySelector(element.dataset.button)
|
||||
this.button = btn ? btn as HTMLButtonElement : undefined
|
||||
// this.button = element.parentElement.querySelector(element.dataset.button)
|
||||
}
|
||||
|
||||
if (this.button) {
|
||||
this.button.addEventListener("click", () => {
|
||||
if (!this.element.disabled) {
|
||||
console.log("pouet!")
|
||||
this.upload()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
upload() {
|
||||
// if (this.form.getJSON()["id"] == 0 || this.form.getJSON()["id"] == undefined) {
|
||||
// NotificationManager.getNotificationManager().add("Merci de sauvegarder l'offre au moins une fois !")
|
||||
// }
|
||||
let files = this.element.files
|
||||
if (files && files.length > 0) {
|
||||
const file = files[0]
|
||||
const ajax = new XMLHttpRequest
|
||||
let form = new FormData
|
||||
form.append(this.getName(), file, file.name)
|
||||
ajax.open("POST", `/api/file?upload&type=${this.type}`)
|
||||
ajax.addEventListener("loadstart", () => {
|
||||
if (this.button) this.button.classList.add("is-loading")
|
||||
if (!this.element.hasAttribute("disabled")) {
|
||||
this.element.setAttribute("disabled", "")
|
||||
this.element.setAttribute("data-uploading", "")
|
||||
}
|
||||
})
|
||||
ajax.addEventListener("loadend", () => {
|
||||
if (this.button) this.button.classList.remove("is-loading")
|
||||
if (this.element.hasAttribute("disabled") && this.element.hasAttribute("data-uploading")) {
|
||||
this.element.removeAttribute("disabled")
|
||||
this.element.removeAttribute("data-uploading")
|
||||
}
|
||||
if (this.button) this.button.innerText = "Uploaded!"
|
||||
this.element.dataset.id = JSON.parse(ajax.responseText).id
|
||||
if (this.element.hasAttribute("data-on-upload")) {
|
||||
evalF(this.element.dataset.onUpload)
|
||||
}
|
||||
})
|
||||
ajax.send(form)
|
||||
}
|
||||
}
|
||||
|
||||
setValue(value: string|number) {
|
||||
if (value == "") {
|
||||
this.element.dataset.id = value + ""
|
||||
if (this.element.parentElement) {
|
||||
const name = this.element.parentElement.querySelector(".file-name")
|
||||
if (name) name.textContent = ""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getValue(): number {
|
||||
return this.element.dataset.id ? parseInt(this.element.dataset.id):0
|
||||
}
|
||||
|
||||
verify() {
|
||||
if (this.element.hasAttribute("required")) {
|
||||
return this.isUploaded
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public static identity: InputIdentity = {
|
||||
input: FileInput,
|
||||
type: "file",
|
||||
tagName: "input"
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,4 @@
|
||||
import InputAbstract from "../InputAbstract";
|
||||
import FMDatalistInput from "../FMDatalistInput";
|
||||
import FMDateInput from "../FMDateInput";
|
||||
import DefaultInput from "../DefaultInput";
|
||||
import FormManager from "../../FormManager";
|
||||
|
||||
interface InputConstructor {
|
||||
|
@ -145,6 +145,15 @@ export default class RepeatInput extends DefaultInput {
|
||||
return values
|
||||
}
|
||||
|
||||
public verify(): boolean {
|
||||
for (const el of this.elements) {
|
||||
for (const i of el) {
|
||||
if (!i.verify()) return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public static identity: InputIdentity = {
|
||||
input: RepeatInput,
|
||||
classes: "fm-repeat",
|
||||
|
Loading…
x
Reference in New Issue
Block a user