191 lines
6.6 KiB
TypeScript

import { Global } from "../../Core/Globals"
import { EventManager } from "../../Core/Events"
import { Ui } from "../../Core/Ui"
import * as Events from "../../Core/EventDefinitions"
import { LocalizationManager } from "../../Core/Localization"
import { Tab } from "@syncfusion/ej2-navigations";
import * as Utility from "../../Core/Utility";
import { FormValidation } from "../../Core/Forms"
import { Switch } from '@syncfusion/ej2-buttons';
import Subscription = Events.Subscription;
import { NumericTextBox } from "@syncfusion/ej2-inputs";
import { DatePicker } from "@syncfusion/ej2-calendars";
class CreateSubscriptionModule {
private _moduleName = "CreateSubscriptionModule";
private _component = "#subscriptionCreateContainer";
private _global: Global;
private _eventManager: EventManager;
private _uiManager: Ui;
private _localizationMananger: LocalizationManager;
private _tab: Tab;
private _tabText: Tab;
private _priceNumeric: NumericTextBox;
private _gracePeriodNumeric: NumericTextBox;
private _maxRegisteredDateDatePicker: DatePicker;
constructor() {
this._global = Global.getInstance();
this._eventManager = EventManager.getInstance();
this._uiManager = Ui.getInstance();
this._localizationMananger = LocalizationManager.getInstance();
}
/**
* Stellt einen Prefix vor einen Selektor
* @param selector
*/
private prefix(selector: string): string {
const self = this;
if ($(`${self._component} ${selector}`).length > 0)
return `${self._component} ${selector}`;
return `html ${selector}`;
}
/**
* Speichern
*/
private save(): void {
var self = this;
self._uiManager.blockMainUi();
var action = $(self.prefix("#frmCreateSubscription")).attr("action");
var data = $(self.prefix("#frmCreateSubscription")).serialize();
$.post(action, data)
.done(result => {
if (result.success) {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Subscription.createSuccess, result.data);
}
else {
$(self._component).replaceWith(result.html);
$.highlightErrors();
Utility.showErrorTab();
self.activate();
if (result.errorMessage && result.errorMessage.length > 0)
self._uiManager.showErrorMessage(result.errorMessage);
}
})
.fail(result => {
self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Save_NoSuccess"));
})
.always(result => {
self._uiManager.unblockMainUi();
});
}
/**
* Bindungen erstellen
*/
private setupBindings(): void {
var self = this;
$(self.prefix("#btnSave")).off("click").on("click", function () {
FormValidation.getInstance().validateForm(self.prefix("#frmCreateSubscription"), () => {
self.save();
}, () => {
$.highlightErrors();
Utility.showErrorTab();
});
});
$(self.prefix("#btnCancel")).off("click").on("click", function () {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Subscription.createCancel);
});
self._tab = new Tab({
animation: { previous: { effect: "FadeIn", duration: 100 }, next: { effect: "FadeIn", duration: 100 } },
selecting: (arsg: any) => {
if (arsg.isSwiped) {
arsg.cancel = true;
}
}
});
self._tab.appendTo(self.prefix("#tabContainer"));
self._tabText = new Tab({
animation: { previous: { effect: "FadeIn", duration: 100 }, next: { effect: "FadeIn", duration: 100 } },
selecting: (arsg: any) => {
if (arsg.isSwiped) {
arsg.cancel = true;
}
},
selected: (args: any) => {
}
});
self._tabText.appendTo(self.prefix("#tabContainerText"));
let enabled = new Switch(
{ checked: $(self.prefix("#Enabled")).prop("checked") }
);
enabled.appendTo("#Enabled");
let hidden = new Switch(
{ checked: $(self.prefix("#Hidden")).prop("checked") }
);
hidden.appendTo("#Hidden");
self._priceNumeric = new NumericTextBox({
value: Globalize.parseNumber($(self.prefix("#Price")).val()),
decimals: 4,
step: 1,
min: 0,
max: 1000000,
format: "n4",
change: (e: any) => {
$(self.prefix("#Price")).val(Globalize.formatNumber(e.value, { minimumFractionDigits: 4 }));
}
});
self._priceNumeric.appendTo(self.prefix("#PriceHelper"));
self._gracePeriodNumeric = new NumericTextBox({
value: Globalize.parseNumber($(self.prefix("#GracePeriodInDays")).val()),
decimals: 0,
step: 1,
min: 0,
max: 1000000,
format: "n0",
change: (e: any) => {
$(self.prefix("#GracePeriodInDays")).val(Globalize.formatNumber(e.value, { minimumFractionDigits: 0 }));
}
});
self._gracePeriodNumeric.appendTo(self.prefix("#GracePeriodInDaysHelper"));
self._maxRegisteredDateDatePicker = new DatePicker({
value: new Date(Date.parse(<string>$(self.prefix("#MaxRegisteredDate")).val()))
});
self._maxRegisteredDateDatePicker.appendTo(self.prefix("#MaxRegisteredDate"));
$('[data-toggle="tooltip"]').tooltip();
}
/**
* Aktivieren des Moduls
*/
public activate(): void {
var self = this;
$.setupValidator();
$.reinitializeValidator();
this.setupBindings();
self._uiManager.pageTitleAdd(self._component, self.prefix("#pageTitleAdd"));
$.randomAutocompleteValue();
}
/**
* Initialisieren des Moduls
*/
public init(): void {
if (this._global.appInitialized) {
createSubscriptionModule.activate();
} else {
setTimeout(() => { this.init() }, 10);
}
}
}
let createSubscriptionModule = new CreateSubscriptionModule();
createSubscriptionModule.init();