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 { Grid, Sort, Filter, Page, RowSelectEventArgs, Resize, ColumnChooser, Reorder } from "@syncfusion/ej2-grids" import { Query, DataManager, UrlAdaptor } from "@syncfusion/ej2-data"; import * as Models from "../../Common/Models"; import * as Utility from "../../Core/Utility"; import { setValue, getValue } from "@syncfusion/ej2-base"; import { createElement } from "@syncfusion/ej2-base"; import { DropDownList } from "@syncfusion/ej2-dropdowns"; import { Dialog } from "@syncfusion/ej2-popups"; import VoucherCampaignListItem = Models.VoucherCampaignListItem; import PaymentType = Models.PaymentType; import PaymentStatus = Models.PaymentStatus; import GeoMode = Models.GeoMode; import VoucherCampaignStatus = Models.VoucherCampaignStatus; import VoucherCampaignType = Models.VoucherCampaignType; class VoucherCampaignsModule { private _moduleName = "VoucherCampaignsModule"; private _component = "#voucherCampaignsContainer"; private _global: Global; private _eventManager: EventManager; private _uiManager: Ui; private _localizationMananger: LocalizationManager; private _data: any; private _grid: Grid; private _dataManager: DataManager; private _downloadDialog: Dialog; private _campaignTypeList; private _filterCampaignType: DropDownList; private _campaignStatusList; private _filterCampaignStatus: DropDownList; private _paymentTypeList; private _filterPaymentType: DropDownList; private _paymentStatusList; private _filterPaymentStatus: DropDownList; private _geoModeList; private _filterGeoMode: DropDownList; constructor() { this._global = Global.getInstance(); this._eventManager = EventManager.getInstance(); this._uiManager = Ui.getInstance(); this._localizationMananger = LocalizationManager.getInstance(); Grid.Inject(Sort, Filter, Page, Resize, ColumnChooser, Reorder); this._downloadDialog = new Dialog({ isModal: true, visible: false, target: "body", width: 500 }); $(this.prefix("#downloadDialog")).css("display", ""); } /** * 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}`; } /** * Einstellungen für die Liste machen */ private setupList(): void { var self = this; self._campaignTypeList = [ { value: VoucherCampaignType.SingleVoucher, text: self._localizationMananger.get("VoucherCampaignType_SingleVoucher")}, { value: VoucherCampaignType.MultiVoucher, text: self._localizationMananger.get("VoucherCampaignType_MultiVoucher") }]; self._campaignStatusList = [ //{ value: BannerStatus.Draft, text: self._localizationMananger.get("BannerStatus_Draft")}, { value: VoucherCampaignStatus.ToPay, text: self._localizationMananger.get("VoucherCampaignStatus_ToPay")}, { value: VoucherCampaignStatus.Booked, text: self._localizationMananger.get("VoucherCampaignStatus_Booked") }, { value: VoucherCampaignStatus.Running, text: self._localizationMananger.get("VoucherCampaignStatus_Running") }, { value: VoucherCampaignStatus.Ended, text: self._localizationMananger.get("VoucherCampaignStatus_Ended") }, { value: VoucherCampaignStatus.Cancelled, text: self._localizationMananger.get("VoucherCampaignStatus_Cancelled") }]; self._paymentTypeList = [ { value: PaymentType.None, text: self._localizationMananger.get("PaymentType_None")}, { value: PaymentType.Invoice, text: self._localizationMananger.get("PaymentType_Invoice")}, { value: PaymentType.Klarna, text: self._localizationMananger.get("PaymentType_Klarna")}, { value: PaymentType.PayPal, text: self._localizationMananger.get("PaymentType_PayPal") }]; self._paymentStatusList = [ { value: PaymentStatus.None, text: self._localizationMananger.get("PaymentStatus_None")}, { value: PaymentStatus.Pending, text: self._localizationMananger.get("PaymentStatus_Pending")}, { value: PaymentStatus.Authorized, text: self._localizationMananger.get("PaymentStatus_Authorized")}, { value: PaymentStatus.Paid, text: self._localizationMananger.get("PaymentStatus_Paid") }, //{ value: PaymentStatus.PartiallyRefunded, text: self._localizationMananger.get("PaymentStatus_PartiallyRefunded") }, { value: PaymentStatus.Refunded, text: self._localizationMananger.get("PaymentStatus_Refunded") }, { value: PaymentStatus.Voided, text: self._localizationMananger.get("PaymentStatus_Voided") }]; self._geoModeList = [ { value: GeoMode.None, text: self._localizationMananger.get("GeoMode_None")}, { value: GeoMode.Country, text: self._localizationMananger.get("GeoMode_Country")}, { value: GeoMode.CountryAndState, text: self._localizationMananger.get("GeoMode_CountryAndState")}, { value: GeoMode.Location, text: self._localizationMananger.get("GeoMode_Location") }]; self._dataManager = new DataManager({ url: $(self.prefix("#jsGetUrl")).val(), adaptor: new UrlAdaptor(), crossDomain: true, headers: [{ "X-Requested-With": "XmlHttpRequest" }] }); self._uiManager.blockUi(self.prefix("#listContainer")); var query = new Query().addParams("searchValue", $(self.prefix("#search")).valForSearch()); self._grid = new Grid({ dataSource: self._dataManager, query: query, height: 450, enablePersistence: true, allowSorting: true, allowFiltering: true, allowPaging: true, allowResizing: true, allowReordering: true, showColumnChooser: true, gridLines: "Vertical", filterSettings: { type: "Menu" }, sortSettings: { columns: [{ field: "startDate", direction: "Descending" }] }, selectionSettings: { checkboxMode: "ResetOnRowClick", checkboxOnly: false }, columns: [ { field: "select", type: "checkbox", width: 50, allowFiltering: false, allowSorting: false, showInColumnChooser: false, allowReordering: false }, { field: "id", type: "string", visible: false, showInColumnChooser: false }, { field: "campaignType", headerText: self._localizationMananger.get("VoucherCampaign_CampaignType"), type: "number", template: "${campaignTypeText}", filter: { ui: { create: (args: { target: Element, column: Object }) => { let flValInput: HTMLElement = createElement("input", { className: "flm-input" }); args.target.appendChild(flValInput); self._filterCampaignType = new DropDownList({ dataSource: new DataManager(self._campaignTypeList), fields: { text: "text", value: "value" }, placeholder: self._localizationMananger.get("Common_Select"), popupHeight: "200px" }); self._filterCampaignType.appendTo(flValInput); }, write: (args: { column: Object, target: Element, parent: any, filteredValue: number | string }) => { self._filterCampaignType.value = args.filteredValue; }, read: (args: { target: Element, column: any, operator: string, fltrObj: Filter }) => { args.fltrObj.filterByColumn(args.column.field, args.operator, self._filterCampaignType.value); } } } }, { field: "status", headerText: self._localizationMananger.get("VoucherCampaign_Status"), type: "number", template: "${statusText}", filter: { ui: { create: (args: { target: Element, column: Object }) => { let flValInput: HTMLElement = createElement("input", { className: "flm-input" }); args.target.appendChild(flValInput); self._filterCampaignStatus = new DropDownList({ dataSource: new DataManager(self._campaignStatusList), fields: { text: "text", value: "value" }, placeholder: self._localizationMananger.get("Common_Select"), popupHeight: "200px" }); self._filterCampaignStatus.appendTo(flValInput); }, write: (args: { column: Object, target: Element, parent: any, filteredValue: number | string }) => { self._filterCampaignStatus.value = args.filteredValue; }, read: (args: { target: Element, column: any, operator: string, fltrObj: Filter }) => { args.fltrObj.filterByColumn(args.column.field, args.operator, self._filterCampaignStatus.value); } } } }, { field: "customerName", headerText: self._localizationMananger.get("VoucherCampaign_CustomerId"), type: "string" }, { field: "prefix", headerText: self._localizationMananger.get("VoucherCampaign_Prefix"), type: "string" }, { field: "title", headerText: self._localizationMananger.get("VoucherCampaign_Title"), type: "string" }, { field: "startDate", headerText: self._localizationMananger.get("VoucherCampaign_StartDate"), type: "datetime", format: { type: "date", skeleton: "medium" } }, { field: "endDate", headerText: self._localizationMananger.get("VoucherCampaign_EndDate"), type: "datetime", format: { type: "date", skeleton: "medium" } }, { field: "budget", headerText: self._localizationMananger.get("VoucherCampaign_Budget"), type: "number", format: "n4", textAlign: "right" }, { field: "budgetUsed", headerText: self._localizationMananger.get("VoucherCampaign_BudgetUsed"), type: "number", format: "n4", textAlign: "right" }, { field: "voucherAmmount", headerText: self._localizationMananger.get("VoucherCampaign_VoucherAmmount"), type: "number", format: "n4", textAlign: "right" }, { field: "numberOfVouchers", headerText: self._localizationMananger.get("VoucherCampaign_NumberOfVouchers"), type: "number", format: "n0", textAlign: "right" }, { field: "vouchersUsed", headerText: self._localizationMananger.get("VoucherCampaign_VouchersUsed"), type: "number", format: "n0", textAlign: "right" }, { field: "maxUsagePerUser", headerText: self._localizationMananger.get("VoucherCampaign_MaxUsagePerUser"), type: "number", format: "n0", textAlign: "right" }, { field: "paymentStatus", headerText: self._localizationMananger.get("Common_PaymentStatus"), type: "number", template: "${paymentStatusText}", filter: { ui: { create: (args: { target: Element, column: Object }) => { let flValInput: HTMLElement = createElement("input", { className: "flm-input" }); args.target.appendChild(flValInput); self._filterPaymentStatus = new DropDownList({ dataSource: new DataManager(self._paymentStatusList), fields: { text: "text", value: "value" }, placeholder: self._localizationMananger.get("Common_Select"), popupHeight: "200px" }); self._filterPaymentStatus.appendTo(flValInput); }, write: (args: { column: Object, target: Element, parent: any, filteredValue: number | string }) => { self._filterPaymentStatus.value = args.filteredValue; }, read: (args: { target: Element, column: any, operator: string, fltrObj: Filter }) => { args.fltrObj.filterByColumn(args.column.field, args.operator, self._filterPaymentStatus.value); } } } }, { field: "paymentType", headerText: self._localizationMananger.get("Common_PaymentType"), type: "number", template: "${paymentTypeText}", filter: { ui: { create: (args: { target: Element, column: Object }) => { let flValInput: HTMLElement = createElement("input", { className: "flm-input" }); args.target.appendChild(flValInput); self._filterPaymentType = new DropDownList({ dataSource: new DataManager(self._paymentTypeList), fields: { text: "text", value: "value" }, placeholder: self._localizationMananger.get("Common_Select"), popupHeight: "200px" }); self._filterPaymentType.appendTo(flValInput); }, write: (args: { column: Object, target: Element, parent: any, filteredValue: number | string }) => { self._filterPaymentType.value = args.filteredValue; }, read: (args: { target: Element, column: any, operator: string, fltrObj: Filter }) => { args.fltrObj.filterByColumn(args.column.field, args.operator, self._filterPaymentType.value); } } } }, { field: "geoMode", headerText: self._localizationMananger.get("Common_GeoMode"), type: "number", template: "${geoModeText}", filter: { ui: { create: (args: { target: Element, column: Object }) => { let flValInput: HTMLElement = createElement("input", { className: "flm-input" }); args.target.appendChild(flValInput); self._filterGeoMode = new DropDownList({ dataSource: new DataManager(self._geoModeList), fields: { text: "text", value: "value" }, placeholder: self._localizationMananger.get("Common_Select"), popupHeight: "200px" }); self._filterGeoMode.appendTo(flValInput); }, write: (args: { column: Object, target: Element, parent: any, filteredValue: number | string }) => { self._filterGeoMode.value = args.filteredValue; }, read: (args: { target: Element, column: any, operator: string, fltrObj: Filter }) => { args.fltrObj.filterByColumn(args.column.field, args.operator, self._filterGeoMode.value); } } } }, { field: "created", headerText: self._localizationMananger.get("Common_Created"), type: "datetime", format: { type: "date", skeleton: "medium" } } ], pageSettings: { pageSizes: [5, 10, 25, 50, 100], pageSize: 50 }, rowSelected: function (args: RowSelectEventArgs) { self.handleSelection(); }, rowDeselected: function (args: any) { self.handleSelection(); }, recordDoubleClick: function (args: any) { self._grid.clearSelection(); if (args.rowIndex !== undefined) { self._grid.selectRow(args.rowIndex); self.edit(); } }, headerCellInfo: function (args) { if (args.cell.column.field !== "select") { const toolcontent = args.cell.column.headerText; args.node.setAttribute("title", toolcontent); args.node.setAttribute("data-placement", "top"); args.node.setAttribute("data-toggle", "tooltip"); } }, dataBound: function () { const cloned = getValue("addOnPersist", self._grid); setValue("addOnPersist", function (key) { key = key.filter(item => item !== "selectedRowIndex"); return cloned.call(this, key); }, self._grid); setTimeout(() => { $(".tooltip").remove(); $('[data-toggle="tooltip"]').tooltip(); }, 500); }, actionComplete: function (args) { if (args.requestType === "filterafteropen") { if (args.filterModel.dlgDiv.querySelector(".e-autocomplete")) { var autoObject = args.filterModel.dlgDiv.querySelector(".e-autocomplete").ej2_instances[0]; autoObject.autofill = false; autoObject.minLength = 20; autoObject.open = args => { args.popup.element.classList.add("d-none"); }; } } } }); self._grid.appendTo(self.prefix("#gridVoucherCampaigns")); self._uiManager.unblockUi(self.prefix("#listContainer")); } /** * Bei Auswahl oder abwahl einer Row... */ private handleSelection(): void { var self = this; let rows = self._grid.getSelectedRecords() as Array; if (rows.length === 0) { $(self.prefix("#btnEdit")).prop("disabled", true); $(self.prefix("#btnDelete")).prop("disabled", true); $(self.prefix("#btnExport")).prop("disabled", true); } else { if (rows.length === 1) { $(self.prefix("#btnEdit")).prop("disabled", false); $(self.prefix("#btnExport")).prop("disabled", false); } else { $(self.prefix("#btnEdit")).prop("disabled", true); $(self.prefix("#btnExport")).prop("disabled", true); } $(self.prefix("#btnDelete")).prop("disabled", false); } } /** * Laden der Einträge */ private loadItems(): void { var self = this; self._grid.clearSelection(); self._grid.refresh(); self.handleSelection(); } /** * Behandeln der Suche via Feld */ private _oldSearch = ""; private handleSearch(): void { var self = this; if ($(self.prefix("#search")).doSearch()) { var search = $(self.prefix("#search")).valForSearch(); if (self._oldSearch !== search) { self._oldSearch = search; self._grid.clearSelection(); self._grid.query = new Query().addParams("searchValue", $(self.prefix("#search")).valForSearch()); self.handleSelection(); } } } /** * Anlegen */ private create(): void { var self = this; self._uiManager.blockMainUi(); $.ajax({ url: $(self.prefix("#jsCreateUrl")).val(), data: {}, dataType: "HTML", type: "GET", success(data) { $(self.prefix("#detail")).html(data); $(self.prefix("#list")).fadeOut("slow", () => { $(self.prefix("#detail")).fadeIn("slow"); }); }, error() { }, complete() { self._uiManager.unblockMainUi(); } }); } /** * Bearbeiten */ private edit(): void { var self = this; self._uiManager.blockMainUi(); $.ajax({ url: $(self.prefix("#jsEditUrl")).val(), data: { id: (self._grid.getSelectedRecords()[0]).id }, dataType: "HTML", type: "GET", success(data) { $(self.prefix("#detail")).html(data); $(self.prefix("#list")).fadeOut("slow", () => { $(self.prefix("#detail")).fadeIn("slow"); }); }, error() { }, complete() { self._uiManager.unblockMainUi(); } }); } /** * Löschen */ private delete(): void { var self = voucherCampaignsModule; //Hier so weil Callback! self._uiManager.blockMainUi(); let rows = self._grid.getSelectedRecords() as Array; if (rows.length > 0) { $.ajax({ url: $(self.prefix("#jsDeleteUrl")).val(), data: { ids: rows.map(({ id }) => id) }, dataType: "json", type: "POST", success(response) { if (response.success) self._uiManager.showSuccessMessage(self._localizationMananger.get("Common_Delete_Success")); else self._uiManager.showWarningMessage(response.errorMessage); self.loadItems(); }, error(e) { self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Delete_NoSuccess")); }, complete() { self._uiManager.unblockMainUi(); } }); } } /** * Exportieren */ private export(): void { var self = voucherCampaignsModule; //Hier so weil Callback! self._uiManager.blockMainUi(); $.ajax({ url: $(self.prefix("#jsExportUrl")).val(), data: { voucherCampaignId: (self._grid.getSelectedRecords()[0]).id }, dataType: "json", type: "POST", success(response) { if (response.success) if (response.data && response.data.length > 0) { $(self.prefix("#downloadDialogDownload")).prop("href", response.data); self._downloadDialog.show(false); } else self._uiManager.showWarningMessage(response.errorMessage); self.loadItems(); }, error(e) { self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Export_NoSuccess")); }, complete() { self._uiManager.unblockMainUi(); } }); } /** * Bindungen erstellen */ private setupBindings(): void { var self = this; $(self.prefix("#btnColumns")).off("click").on("click", function () { self._grid.columnChooserModule.openColumnChooser(); }); $(self.prefix("#btnAdd")).off("click").on("click", function () { self.create(); }); $(self.prefix("#btnEdit")).off("click").on("click", function () { self.edit(); }); $(self.prefix("#btnExport")).off("click").on("click", function () { self.export(); }); $(self.prefix("#btnDelete")).off("click").on("click", function () { if (self._grid.getSelectedRows().length === 1) { self._uiManager.showModal(self._localizationMananger.get("Common_Delete_Question"), true, self.delete); } else if (self._grid.getSelectedRows().length > 1) { self._uiManager.showModal(self._localizationMananger.get("Common_Delete_Question_Batch"), true, self.delete); } }); $(self.prefix("#search")).on("keyup change", Utility.debounce(() => { self.handleSearch(); }, 500)); $(self.prefix("#downloadDialogDownload")).off("click").on("click", function () { self._downloadDialog.hide(); }); $(self.prefix("#downloadDialogClose")).off("click").on("click", function () { self._downloadDialog.hide(); }); self._downloadDialog.appendTo(self.prefix("#downloadDialog")); self._eventManager.subscribe(Events.VoucherCampaign.createCancel, self._moduleName, function () { $(self.prefix("#detail")).fadeOut("slow", () => { $(self.prefix("#list")).fadeIn(); $(self.prefix("#detail")).html(""); }); }); self._eventManager.subscribe(Events.VoucherCampaign.createSuccess, self._moduleName, function () { self.loadItems(); $(self.prefix("#detail")).fadeOut("slow", () => { $(self.prefix("#list")).fadeIn(); $(self.prefix("#detail")).html(""); }); }); self._eventManager.subscribe(Events.VoucherCampaign.editCancel, self._moduleName, function () { $(self.prefix("#detail")).fadeOut("slow", () => { $(self.prefix("#list")).fadeIn(); $(self.prefix("#detail")).html(""); }); }); self._eventManager.subscribe(Events.VoucherCampaign.editSuccess, self._moduleName, function () { self.loadItems(); $(self.prefix("#detail")).fadeOut("slow", () => { $(self.prefix("#list")).fadeIn(); $(self.prefix("#detail")).html(""); }); }); } /** * Aktivieren des Moduls */ public activate(): void { var self = this; $.setupValidator(); $.reinitializeValidator(); this.setupBindings(); self.setupList(); $.randomAutocompleteValue(); } /** * Initialisieren des Moduls */ public init(): void { if (this._global.appInitialized) { voucherCampaignsModule.activate(); } else { setTimeout(() => { this.init(); }, 10); } } } let voucherCampaignsModule = new VoucherCampaignsModule(); voucherCampaignsModule.init();