341 lines
14 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 { FormValidation } from "../../Core/Forms";
import * as Utility from "../../Core/Utility";
import { Query, DataManager, UrlAdaptor } from "@syncfusion/ej2-data";
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
import { CartChangedResult } from "../../Common/Models";
class ShopCheckoutAddressModule {
private _moduleName = "ShopCheckoutAddressModule";
private _component = "#checkoutAddressContainer";
private _global: Global;
private _eventManager: EventManager;
private _uiManager: Ui;
private _localizationMananger: LocalizationManager;
private _data: any;
private _billingCountryAutoComplete: AutoComplete;
private _billingStateAutoComplete: AutoComplete;
private _deliveryCountryAutoComplete: AutoComplete;
private _deliveryStateAutoComplete: AutoComplete;
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("#frmCheckoutAddress")).attr("action");
var data = $(self.prefix("#frmCheckoutAddress")).serialize();
$.post(action, data)
.done(result => {
if (result.success) {
//Auf Ergebnis prüfen
if (result.data && result.data.length > 0) {
var changedResult = <CartChangedResult>JSON.parse(result.data);
if (changedResult.itemCount === 0) {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Events.Shop.showOverview);
} else {
if (changedResult.recalculationResult.totalChanged || changedResult.recalculationResult.shipmentChanged) {
self._uiManager.showInfoMessage(self._localizationMananger.get("Common_CartTotal_Changed"));
}
if (changedResult.validationList.length > 0) {
for (var validation of changedResult.validationList) {
self._uiManager.showWarningMessage(validation);
}
}
}
}
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Events.Shop.showPayment);
}
else {
$(self._component).replaceWith(result.html);
$.highlightErrors();
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("#btnBack")).off("click").on("click", function() {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Events.Shop.showOverview);
});
$(self.prefix("#btnNext")).off("click").on("click", function() {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Events.Shop.showPayment);
});
$(self.prefix("#btnNextForm")).off("click").on("click", function() {
FormValidation.getInstance().validateForm(self.prefix("#frmCheckoutAddress"), () => {
self.save();
}, () => {
$.highlightErrors();
Utility.showErrorTab();
});
});
$(self.prefix("#btnEditAddress")).off("click").on("click", function () {
$(self.prefix("#ShowForm")).val("True");
$(self.prefix("#info")).addClass("d-none");
$(self.prefix("#form")).removeClass("d-none");
$(self.prefix("#btnNext")).addClass("d-none");
$(self.prefix("#btnNextForm")).removeClass("d-none");
});
//#region Länder autocompletes
self._billingCountryAutoComplete = 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("#BillingCountryName")).val(),
change: (e: any) => {
var resetState = false;
if (e.itemData === null) {
$(self.prefix("#BillingAddress_CountryCode")).val("");
resetState = true;
}
else if (e.itemData.id === e.itemData.name) {
//nichts tun
resetState = false;
} else {
$(self.prefix("#BillingAddress_CountryCode")).val(e.itemData.id);
resetState = true;
}
if (resetState) {
self._billingStateAutoComplete.value = null;
self._billingStateAutoComplete.text = null;
self._billingStateAutoComplete.dataSource = new DataManager({ url: <string>$(self.prefix("#jsLookupStatesUrl")).val() + `?countryIso=${$(self.prefix("#BillingAddress_CountryCode")).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._billingCountryAutoComplete.appendTo(self.prefix("#BillingCountryName"));
self._billingStateAutoComplete = new AutoComplete({
dataSource: new DataManager({
url: <string>$(self.prefix("#jsLookupStatesUrl")).val() + `?countryIso=${$(self.prefix("#BillingAddress_CountryCode")).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("#BillingStateName")).val(),
change: (e: any) => {
if (e.itemData === null) {
$(self.prefix("#BillingAddress_State")).val("");
}
else if (e.itemData.id === e.itemData.name) {
//nichts tun
} else {
$(self.prefix("#BillingAddress_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._billingStateAutoComplete.appendTo(self.prefix("#BillingStateName"));
self._deliveryCountryAutoComplete = 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("#DeliveryCountryName")).val(),
change: (e: any) => {
var resetState = false;
if (e.itemData === null) {
$(self.prefix("#DeliveryAddress_CountryCode")).val("");
resetState = true;
}
else if (e.itemData.id === e.itemData.name) {
//nichts tun
resetState = false;
} else {
$(self.prefix("#DeliveryAddress_CountryCode")).val(e.itemData.id);
resetState = true;
}
if (resetState) {
self._deliveryStateAutoComplete.value = null;
self._deliveryStateAutoComplete.text = null;
self._deliveryStateAutoComplete.dataSource = new DataManager({ url: <string>$(self.prefix("#jsLookupStatesUrl")).val() + `?countryIso=${$(self.prefix("#DeliveryAddress_CountryCode")).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._deliveryCountryAutoComplete.appendTo(self.prefix("#DeliveryCountryName"));
self._deliveryStateAutoComplete = new AutoComplete({
dataSource: new DataManager({
url: <string>$(self.prefix("#jsLookupStatesUrl")).val() + `?countryIso=${$(self.prefix("#DeliveryAddress_CountryCode")).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("#DeliveryStateName")).val(),
change: (e: any) => {
if (e.itemData === null) {
$(self.prefix("#DeliveryAddress_State")).val("");
}
else if (e.itemData.id === e.itemData.name) {
//nichts tun
} else {
$(self.prefix("#DeliveryAddress_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._deliveryStateAutoComplete.appendTo(self.prefix("#DeliveryStateName"));
//#endregion
$(self.prefix("#DeliveryAddressIsBilling")).off("change").on("change", function () {
if ($(this).is(":checked")) {
$(self.prefix("#DeliveryAddressIsBilling")).val("true");
$(self.prefix("#BillingAddress_DeliveryAddressIsBilling")).val("true");
$(self.prefix("#deliveryAddress")).addClass("d-none");
} else {
$(self.prefix("#DeliveryAddressIsBilling")).val("false");
$(self.prefix("#BillingAddress_DeliveryAddressIsBilling")).val("false");
$(self.prefix("#deliveryAddress")).removeClass("d-none");
}
});
$('[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) {
shopCheckoutAddressModule.activate();
} else {
setTimeout(() => { this.init(); }, 10);
}
}
}
let shopCheckoutAddressModule = new ShopCheckoutAddressModule();
shopCheckoutAddressModule.init();