Ancora sistemazioni Typescript... getters, mutations, actions... "@" alias.

This commit is contained in:
paolo
2018-11-17 20:32:28 +01:00
parent 7c49a97217
commit 794e7088e7
22 changed files with 184 additions and 165 deletions

View File

@@ -1,46 +1,45 @@
import { AlertsStore, LoginStore } from '@store';
import {Forms} from './FormController';
import {Forms} from './FormController'
import Router from '@router';
import Router from '@router'
export namespace AlertsElement {
type AlertType = "success" | "confirm" | "warning" | "error" | "info" | "form";
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
type AlertType = 'success' | 'confirm' | 'warning' | 'error' | 'info' | 'form'
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>
type formParam = {
form: Forms.Form,
validations?: {
[x:string]: any
[x: string]: any
},
submit: {
params?: {[x:string]: any},
params?: {[x: string]: any},
trigger: Function
}
}
export class Alert{
public type: AlertType;
public title: string;
public message?: string;
public strict?: boolean;
public actions: ActionsElements.Action[];
public formElement?: formParam;
export class Alert {
public type: AlertType
public title: string
public message?: string
public strict?: boolean
public actions: ActionsElements.Action[]
public formElement?: formParam
public onClose?: Function[]
constructor(fields?:{type: AlertType, title: string, message?: string, strict?: boolean, actions: ActionsElements.Action[], formElement?: formParam, onClose?: Function[]}) {
Object.assign(this, fields);
AlertsStore.actions.addAlert(this);
constructor(fields?: {type: AlertType, title: string, message?: string, strict?: boolean, actions: ActionsElements.Action[], formElement?: formParam, onClose?: Function[]}) {
Object.assign(this, fields)
AlertsStore.actions.addAlert(this)
}
async waitResponse() {
return AlertsStore.actions.addAlert(this);
return AlertsStore.actions.addAlert(this)
}
}
export class WarningAlert extends Alert {
constructor(fields?: {title: string, message?: string, strict?: boolean, actions?: ActionsElements.Action[], onClose?: Function[]}) {
const actions = fields.actions || [];
const actions = fields.actions || []
const confirmAction = (fields.actions && fields.actions.find(m => m.type == 'confirm')) ? undefined : new ActionsElements.ConfirmAction({})
console.log(fields.actions)
super({
@@ -53,13 +52,13 @@ export namespace AlertsElement {
...actions,
confirmAction
]
});
})
}
}
export class SuccessAlert extends Alert {
constructor(fields?: {title: string, message?: string, strict?: boolean, actions?: ActionsElements.Action[], onClose?: Function[]}) {
const actions = fields.actions || [];
const actions = fields.actions || []
const confirmAction = (fields.actions && fields.actions.find(m => m.type == 'confirm')) ? undefined : new ActionsElements.ConfirmAction({})
console.log(fields.actions)
super({
@@ -72,13 +71,13 @@ export namespace AlertsElement {
...actions,
confirmAction
]
});
})
}
}
export class ErrorAlert extends Alert {
constructor(fields?: {title: string, message?: string, strict?: boolean, actions?: ActionsElements.Action[], onClose?: Function[]}) {
const actions = fields.actions || [];
const actions = fields.actions || []
console.log(fields.actions)
const confirmAction = (fields.actions && fields.actions.find(m => m.type == 'confirm')) ? undefined : new ActionsElements.ConfirmAction({text: 'Fermer'})
super({
@@ -91,12 +90,12 @@ export namespace AlertsElement {
...actions,
confirmAction
]
});
})
}
}
export class FormAlert extends Alert {
constructor(fields?: {title: string, message?: string, strict?:boolean, formElement: formParam, onClose?: Function[]}) {
constructor(fields?: {title: string, message?: string, strict?: boolean, formElement: formParam, onClose?: Function[]}) {
const confirmAction = new ActionsElements.ConfirmAction({
text: 'Valider',
triggers: [
@@ -115,7 +114,7 @@ export namespace AlertsElement {
confirmAction,
new ActionsElements.CancelAction()
]
});
})
}
}
}
@@ -127,37 +126,37 @@ export namespace AlertsElement {
export namespace ActionsElements {
type ActionType = "confirm" | "action" | "cancel" | "link";
type ActionType = 'confirm' | 'action' | 'cancel' | 'link'
export class Action {
public type: ActionType;
public text: string;
public to?: {path: string} | {name: string, params?: {[x: string]: any}};
public trigger?: Function;
public triggers?: Function[];
public type: ActionType
public text: string
public to?: {path: string} | {name: string, params?: {[x: string]: any}}
public trigger?: Function
public triggers?: Function[]
constructor({type, text, trigger, triggers, to}: Action) {
this.text = text;
this.type = type;
this.trigger = trigger;
this.triggers = triggers;
this.to = to;
this.text = text
this.type = type
this.trigger = trigger
this.triggers = triggers
this.to = to
}
}
export class ConfirmAction extends Action {
constructor({text, triggers}: {text?: string, triggers?: Function[]}) {
super({
text: text || "Ça marche!",
type: "confirm",
});
text: text || 'Ça marche!',
type: 'confirm'
})
if (triggers) {
this.triggers = [
...triggers,
AlertsStore.mutations.confirmAlert,
];
AlertsStore.mutations.confirmAlert
]
} else {
this.trigger = this.trigger = AlertsStore.mutations.confirmAlert;
this.trigger = this.trigger = AlertsStore.mutations.confirmAlert
}
}
}
@@ -167,30 +166,30 @@ export namespace ActionsElements {
super({
text: text,
to: to,
type: "link",
});
type: 'link'
})
}
}
export class LoginAction extends Action {
constructor() {
super({
text: "Se connecter",
type: "action",
text: 'Se connecter',
type: 'action',
triggers: [
LoginStore.mutations.showLogin,
AlertsStore.actions.hideAlert
]
});
})
}
}
export class CancelAction extends Action {
constructor() {
super({
text: "Annuler",
type: "cancel",
text: 'Annuler',
type: 'cancel',
trigger: AlertsStore.mutations.cancelAlert
});
})
}
}