165 lines
5.3 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"
class ShopCartOverviewModule {
private _moduleName = "ShopCartOverviewModule";
private _component = "#cartOverviewContainer";
private _global: Global;
private _eventManager: EventManager;
private _uiManager: Ui;
private _localizationMananger: LocalizationManager;
private _data: any;
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}`;
}
/**Prüfen der Min- und Max Bestellmenge je Produkt */
private checkMinMaxQuantity(): void {
var self = this;
$(self.prefix("[data-itemid]")).each(function() {
var itemId = $(this).data("itemid");
var minQuantity = $(this).data("minquantity");
var maxQuantity = $(this).data("maxquantity");
var quantity = $(this).data("quantity");
if (quantity <= minQuantity) {
$(this).find(".btn-minus").attr("disabled", "disabled");
} else {
$(this).find(".btn-minus").removeAttr("disabled");
}
if (quantity >= maxQuantity) {
$(this).find(".btn-plus").attr("disabled", "disabled");
} else {
$(this).find(".btn-plus").removeAttr("disabled");
}
});
}
/**
* Entfernen eines Artikels aus dem Warenkorb
* @param cartItemId Id des Artikels
*/
private removeItemFromCart(cartItemId: number): void {
var self = shopCartOverviewModule;
self._uiManager.blockMainUi();
$.ajax({
url: <string>$(self.prefix("#jsRemoveItemFromCartUrl")).val(),
data: <any>{ cartItemId: cartItemId },
dataType: "json",
type: "POST",
success(response) {
if (response.success)
location.reload();
else
self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Delete_NoSuccess"));
},
error(e) {
self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Delete_NoSuccess"));
},
complete() {
self._uiManager.unblockMainUi();
}
});
}
/**
* Leeren des gesamten Warenkorbs
*/
private clearCart(): void {
var self = shopCartOverviewModule;
self._uiManager.blockMainUi();
$.ajax({
url: <string>$(self.prefix("#jsClearCartUrl")).val(),
data: <any>{ },
dataType: "json",
type: "POST",
success(response) {
if (response.success)
location.reload();
else
self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Delete_NoSuccess"));
},
error(e) {
self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Delete_NoSuccess"));
},
complete() {
self._uiManager.unblockMainUi();
}
});
}
/**
* Bindungen erstellen
*/
private setupBindings(): void {
var self = this;
$(self.prefix("#btnNext")).off("click").on("click", function() {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Events.Shop.showAddress);
});
$(self.prefix(".btn-remove")).off("click").on("click", function() {
var cartItemId = $(this).parent().data("itemid");
var callback = self.removeItemFromCart.bind(null, cartItemId);
self._uiManager.showModal(self._localizationMananger.get("Cart_Remove_Question"), true, callback);
});
$(self.prefix("#btnClear")).off("click").on("click", function() {
self._uiManager.showModal(self._localizationMananger.get("Cart_Clear_Question"), true, self.clearCart);
});
self.checkMinMaxQuantity();
}
/**
* 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) {
shopCartOverviewModule.activate();
} else {
setTimeout(() => { this.init(); }, 10);
}
}
}
let shopCartOverviewModule = new ShopCartOverviewModule();
shopCartOverviewModule.init();