222 lines
8.4 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 * as Utility from "../../Core/Utility";
import { FormValidation } from "../../Core/Forms"
import * as Models from "../../Common/Models";
import { Query, DataManager, UrlAdaptor } from "@syncfusion/ej2-data";
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
import { DatePicker } from "@syncfusion/ej2-calendars";
import { Switch } from '@syncfusion/ej2-buttons';
import SystemMessage = Events.SystemMessage;
class CreateSystemMessageModule {
private _moduleName = "CreateSystemMessageModule";
private _component = "#systemMessageCreateContainer";
private _global: Global;
private _eventManager: EventManager;
private _uiManager: Ui;
private _localizationMananger: LocalizationManager;
private _countryAutoComplete: AutoComplete;
private _stateAutoComplete: AutoComplete;
private _sendDatePicker: 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("#frmCreateSystemMessage")).attr("action");
var data = $(self.prefix("#frmCreateSystemMessage")).serialize();
$.post(action, data)
.done(result => {
if (result.success) {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(SystemMessage.createSuccess, result.data);
}
else {
$(self._component).replaceWith(result.html);
$.highlightErrors();
Utility.showErrorTab();
self.activate();
}
})
.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("#frmCreateSystemMessage"), () => {
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(SystemMessage.createCancel);
});
self._sendDatePicker = new DatePicker({
value: new Date(Date.parse(<string>$(self.prefix("#ToSendDate")).val()))
});
self._sendDatePicker.appendTo(self.prefix("#ToSendDate"));
self._countryAutoComplete = new AutoComplete({
dataSource: new DataManager({
url: <string>$(self.prefix("#jsLookupCountriesUrl")).val(),
adaptor: new UrlAdaptor(),
crossDomain: true,
headers: [{ "X-Requested-With": "XmlHttpRequest" }]
}),
showPopupButton: true,
placeholder: self._localizationMananger.get("Common_Select"),
fields: { value: "id", text: "name" },
value: <string>$(self.prefix("#CountryName")).val(),
change: (e: any) => {
var resetState = false;
if (e.itemData === null) {
$(self.prefix("#Country")).val("");
resetState = true;
}
else if (e.itemData.id === e.itemData.name) {
//nichts tun
resetState = false;
} else {
$(self.prefix("#Country")).val(e.itemData.id);
resetState = true;
}
if (resetState) {
self._stateAutoComplete.value = "";
self._stateAutoComplete.text = null;
self._stateAutoComplete.dataSource = new DataManager({ url: <string>$(self.prefix("#jsLookupStatesUrl")).val() + `&countryIso=${$(self.prefix("#Country")).val()}`, adaptor: new UrlAdaptor(), crossDomain: true, headers: [{ "X-Requested-With": "XmlHttpRequest" }] });
}
},
htmlAttributes: { openhelper: "off" },
open: function () {
if (this.value !== null && this.text === this.queryString) {
this.value = null;
this.htmlAttributes["openhelper"] = "on";
}
},
close: function () {
if (this.value === null && this.htmlAttributes["openhelper"] === "on") {
this.showPopup();
this.htmlAttributes["openhelper"] = "off";
}
}
});
self._countryAutoComplete.appendTo(self.prefix("#CountryName"));
self._stateAutoComplete = new AutoComplete({
dataSource: new DataManager({
url: <string>$(self.prefix("#jsLookupStatesUrl")).val() + `&countryIso=${$(self.prefix("#Country")).val()}`,
adaptor: new UrlAdaptor(),
crossDomain: true,
headers: [{ "X-Requested-With": "XmlHttpRequest" }]
}),
showPopupButton: true,
placeholder: self._localizationMananger.get("Common_Select"),
fields: { value: "id", text: "name" },
value: <string>$(self.prefix("#StateName")).val(),
change: (e: any) => {
if (e.itemData === null) {
$(self.prefix("#State")).val("");
}
else if (e.itemData.id === e.itemData.name) {
//nichts tun
} else {
$(self.prefix("#State")).val(e.itemData.id);
}
},
htmlAttributes: { openhelper: "off" },
open: function () {
if (this.value !== null && this.text === this.queryString) {
this.value = null;
this.htmlAttributes["openhelper"] = "on";
}
},
close: function () {
if (this.value === null && this.htmlAttributes["openhelper"] === "on") {
this.showPopup();
this.htmlAttributes["openhelper"] = "off";
}
}
});
self._stateAutoComplete.appendTo(self.prefix("#StateName"));
let SendPushNotification = new Switch(
{ checked: $(self.prefix("#SendPushNotification")).prop("checked") }
);
SendPushNotification.appendTo("#SendPushNotification");
let VerifiedOnly = new Switch(
{ checked: $(self.prefix("#VerifiedOnly")).prop("checked") }
);
VerifiedOnly.appendTo("#VerifiedOnly");
$('[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) {
createSystemMessageModule.activate();
} else {
setTimeout(() => { this.init() }, 10);
}
}
}
let createSystemMessageModule = new CreateSystemMessageModule();
createSystemMessageModule.init();