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"; class ImpersionateModule { private _moduleName = "ImpersionateModule"; private _component = "#impersionateContainer"; private _global: Global; private _eventManager: EventManager; private _uiManager: Ui; private _localizationMananger: LocalizationManager; private _customerAutoComplete: AutoComplete; private _instructorAutoComplete: AutoComplete; private _studentAutoComplete: 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("#frmImpersionate")).attr("action"); var data = $(self.prefix("#frmImpersionate")).serialize(); $.post(action, data) .done(result => { if (result.success) { self._uiManager.pageTitleClear(self._component); self._eventManager.unSubscribeAll(self._moduleName); self._uiManager.hideModule(self._moduleName); var data = JSON.parse(result.data); if ($(".nav-link.active") && $(".nav-link.active").data("post-options")) { if ($(".nav-link.active").data("post-options")) { var postOptions = $(".nav-link.active").data("post-options"); if (postOptions.customerId) { postOptions.customerId = data.customerId; $(".nav-link.active").data("post-options", postOptions); } if (postOptions.tenantId) { postOptions.tenantId = data.tenantId; $(".nav-link.active").data("post-options", postOptions); } } $(".nav-link.active").trigger("click"); } else { location.reload(); } } 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("#btnSave")).off("click").on("click", function () { FormValidation.getInstance().validateForm(self.prefix("#frmImpersionate"), () => { self.save(); }, () => { $.highlightErrors(); }); }); $(self.prefix("#btnCancel")).off("click").on("click", function () { self._uiManager.pageTitleClear(self._component); self._eventManager.unSubscribeAll(self._moduleName); self._uiManager.hideModule(self._moduleName); }); self._customerAutoComplete = new AutoComplete({ dataSource: new DataManager({ url: $(self.prefix("#jsLookupCustomersUrl")).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: $(self.prefix("#CustomerName")).val(), change: (e: any) => { if (e.itemData === null) { $(self.prefix("#CustomerId")).val(""); self._studentAutoComplete.enabled = false; } else if (e.itemData.id === e.itemData.name) { //nichts tun } else { $(self.prefix("#CustomerId")).val(e.itemData.id); self._studentAutoComplete.enabled = true; } self._studentAutoComplete.value = null; self._studentAutoComplete.text = null; self._studentAutoComplete.index = null; self._studentAutoComplete.dataSource = new DataManager({ url: $(self.prefix("#jsLookupStudentsUrl")).val() + `&customerId=${$(self.prefix("#CustomerId")).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._customerAutoComplete.appendTo(self.prefix("#CustomerName")); self._instructorAutoComplete = new AutoComplete({ dataSource: new DataManager({ url: $(self.prefix("#jsLookupInstructorsUrl")).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: $(self.prefix("#InstructorName")).val(), change: (e: any) => { if (e.itemData === null) { $(self.prefix("#InstructorId")).val(""); }else if (e.itemData.id === e.itemData.name) { //nichts tun } else { $(self.prefix("#InstructorId")).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._instructorAutoComplete.appendTo(self.prefix("#InstructorName")); self._studentAutoComplete = new AutoComplete({ dataSource: new DataManager({ url: $(self.prefix("#jsLookupStudentsUrl")).val() + `&customerId=${$(self.prefix("#CustomerId")).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: $(self.prefix("#StudentName")).val(), change: (e: any) => { if (e.itemData === null) { $(self.prefix("#StudentId")).val(""); } else if (e.itemData.id === e.itemData.name) { //nichts tun } else { $(self.prefix("#StudentId")).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._studentAutoComplete.appendTo(self.prefix("#StudentName")); if (self._customerAutoComplete.value === null) { self._studentAutoComplete.enabled = false; } $('[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) { impersionateModule.activate(); } else { setTimeout(() => { this.init() }, 10); } } } let impersionateModule = new ImpersionateModule(); impersionateModule.init();