155 lines
5.7 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";
class ShopCheckoutOrderModule {
private _moduleName = "ShopCheckoutOrderModule";
private _component = "#checkoutOrderContainer";
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}`;
}
/**
* Speichern
*/
private save(): void {
var self = this;
self._uiManager.blockMainUi();
var action = $(self.prefix("#frmCheckoutOrder")).attr("action");
var data = $(self.prefix("#frmCheckoutOrder")).serialize();
$.post(action, data)
.done(result => {
if (result.success) {
if (result.data.length > 0) {
if (result.data === "klarna") {
//$(self.prefix("#klarnaContainer")).html(result.html);
$("html,body").animate(<any>{
scrollTop: 0
},
1000);
$(self.prefix("#paymentSelection")).fadeOut("slow",
() => {
$(self.prefix("#klarnaContainer")).fadeIn();
self.renderKlarna(result.html, "klarnaContainer");
});
} else
location.href = result.data;
} else {
self._uiManager.pageTitleClear(self._component);
self._eventManager.unSubscribeAll(self._moduleName);
self._eventManager.publish(Events.Shop.showConfirmation);
}
}
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();
});
}
private renderKlarna(htmlSnippet:string, containerName:string): void {
var confirmationContainer = document.getElementById(containerName);
confirmationContainer.innerHTML = htmlSnippet;
var scriptsTags = confirmationContainer.getElementsByTagName("script");
// This is necessary otherwise the scripts tags are not going to be evaluated
for (var i = 0; i < scriptsTags.length; i++) {
var parentNode = scriptsTags[i].parentNode;
var newScriptTag = document.createElement("script");
newScriptTag.type = "text/javascript";
newScriptTag.text = scriptsTags[i].text;
parentNode.removeChild(scriptsTags[i]);
parentNode.appendChild(newScriptTag);
}
}
/**
* 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.showPayment);
});
$(self.prefix("#btnOrder")).off("click").on("click", function () {
FormValidation.getInstance().validateForm(self.prefix("#frmCheckoutOrder"), () => {
if ($(self.prefix("#AcceptTermsAndConditions")).prop("checked") === true) {
if ($(self.prefix("#AcceptGdpr")).prop("checked") === true) {
self.save();
} else {
self._uiManager.showErrorMessage(self._localizationMananger.get("Shop_Gdpr_NotAccepted"));
}
} else {
self._uiManager.showErrorMessage(self._localizationMananger.get("Shop_TermsAndConditions_NotAccepted"));
}
}, () => {
$.highlightErrors();
});
});
$('[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) {
shopCheckoutOrderModule.activate();
} else {
setTimeout(() => { this.init(); }, 10);
}
}
}
let shopCheckoutOrderModule = new ShopCheckoutOrderModule();
shopCheckoutOrderModule.init();