337 lines
12 KiB
TypeScript
337 lines
12 KiB
TypeScript
import { Global } from "../../Core/Globals"
|
|
import { EventManager } from "../../Core/Events"
|
|
import * as Utility from "../../Core/Utility"
|
|
import { Ui } from "../../Core/Ui"
|
|
import * as Events from "../../Core/EventDefinitions"
|
|
import { LocalizationManager } from "../../Core/Localization"
|
|
import { MultiSelect } from "@syncfusion/ej2-dropdowns";
|
|
import { Tab } from "@syncfusion/ej2-navigations";
|
|
import * as Models from "../../Common/Models";
|
|
import * as Forms from "../../Core/Forms";
|
|
import FormValidation = Forms.FormValidation;
|
|
import { Query, DataManager, UrlAdaptor } from "@syncfusion/ej2-data";
|
|
import { AutoComplete } from '@syncfusion/ej2-dropdowns';
|
|
import { Uppload, Local, xhrUploader, Crop, Rotate, Flip, Blur, Brightness, Contrast, Grayscale, Saturate } from "uppload";
|
|
import * as UpploadHelper from "../../Common/UpploadLanguages"
|
|
|
|
|
|
class CreateUserModule {
|
|
private _moduleName = "CreateUserModule";
|
|
private _component = "#userCreateContainer";
|
|
private _global: Global;
|
|
private _eventManager: EventManager;
|
|
private _uiManager: Ui;
|
|
private _localizationMananger: LocalizationManager;
|
|
private _tab: Tab;
|
|
private _selectedRole: string;
|
|
private _customerAutoComplete: AutoComplete;
|
|
private _appUserAutoComplete: AutoComplete;
|
|
private _dogWalkerAutoComplete: AutoComplete;
|
|
private _uppload: Uppload;
|
|
|
|
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("#frmCreateUser")).attr("action");
|
|
var data = $(self.prefix("#frmCreateUser")).serialize();
|
|
$.post(action, data)
|
|
.done(result => {
|
|
if (result.success) {
|
|
self._uiManager.pageTitleClear(self._component);
|
|
self._eventManager.unSubscribeAll(self._moduleName);
|
|
self._eventManager.publish(Events.User.createSuccess, result.data);
|
|
}
|
|
else {
|
|
$(self._component).replaceWith(result.html);
|
|
$.highlightErrors();
|
|
Utility.showErrorTab();
|
|
self.activate();
|
|
}
|
|
})
|
|
.fail(result => {
|
|
self._uiManager.showErrorMessage(self._localizationMananger.get("Common_Save_NoSuccess"));
|
|
})
|
|
.always(result => {
|
|
self._uiManager.unblockMainUi();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Enfernt Berechtigungen die mit einer Rolle verbunden sind
|
|
*/
|
|
private removePermissions(): void {
|
|
var self = createUserModule; //Hier so weil Callback!
|
|
$.ajax({
|
|
url: <string>$(self.prefix("#jsGetPermissionsForRole")).val(),
|
|
data: <any>{ role: self._selectedRole },
|
|
dataType: "json",
|
|
type: "POST",
|
|
success(data) {
|
|
for (let permission of data) {
|
|
$(`[data-permission="${permission}"`).prop("checked", false);
|
|
}
|
|
},
|
|
error(e) {
|
|
self._uiManager.showErrorMessage(self._localizationMananger.get("User_Permissions_NotLoaded"));
|
|
},
|
|
complete() {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Fügt Berechtigungen die mit einer Rolle verbunden sind hinzu
|
|
*/
|
|
private addPermissions(): void {
|
|
var self = createUserModule; //Hier so weil Callback!
|
|
$.ajax({
|
|
url: <string>$(self.prefix("#jsGetPermissionsForRole")).val(),
|
|
data: <any>{ role: self._selectedRole },
|
|
dataType: "json",
|
|
type: "POST",
|
|
success(data) {
|
|
for (let permission of data) {
|
|
$(`[data-permission="${permission}"`).prop("checked", true);
|
|
}
|
|
},
|
|
error(e) {
|
|
self._uiManager.showErrorMessage(self._localizationMananger.get("User_Permissions_NotLoaded"));
|
|
},
|
|
complete() {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Löschen des Temporären Files
|
|
* @param filename
|
|
*/
|
|
private remove(filename: string): void {
|
|
var self = this;
|
|
$.ajax({
|
|
url: <string>$(self.prefix("#jsRemoveUrl")).val(),
|
|
data: <any>{ filename },
|
|
dataType: "json",
|
|
type: "POST",
|
|
success(success) {
|
|
if (success)
|
|
self._uiManager.showSuccessMessage(self._localizationMananger.get("Common_Delete_Success"));
|
|
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("#btnCreateUser")).off("click").on("click", function () {
|
|
FormValidation.getInstance().validateForm(self.prefix("#frmCreateUser"), () => {
|
|
self.save();
|
|
}, () => {
|
|
$.highlightErrors();
|
|
Utility.showErrorTab();
|
|
});
|
|
});
|
|
|
|
$(self.prefix("#btnCreateUserCancel")).off("click").on("click", function () {
|
|
self._uiManager.pageTitleClear(self._component);
|
|
self._eventManager.unSubscribeAll(self._moduleName);
|
|
self._eventManager.publish(Events.User.createCancel);
|
|
});
|
|
|
|
self._tab = new Tab({
|
|
animation: { previous: { effect: "FadeIn", duration: 100 }, next: { effect: "FadeIn", duration: 100 } },
|
|
selecting: (arsg: any) => {
|
|
if (arsg.isSwiped) {
|
|
arsg.cancel = true;
|
|
}
|
|
}
|
|
});
|
|
self._tab.appendTo(self.prefix("#tabContainer"));
|
|
|
|
const multiSelect = new MultiSelect({
|
|
hideSelectedItem: false,
|
|
mode: "Box",
|
|
placeholder: self._localizationMananger.get("Common_Select"),
|
|
removed: (e) => {
|
|
self._selectedRole = e.itemData.value;
|
|
self._uiManager.showModal(self._localizationMananger.get("User_Permissions_Remove_Question"), true, self.removePermissions);
|
|
},
|
|
select: (e) => {
|
|
self._selectedRole = e.itemData.value;
|
|
self._uiManager.showModal(self._localizationMananger.get("User_Permissions_Add_Question"), true, self.addPermissions);
|
|
}
|
|
});
|
|
multiSelect.appendTo(self.prefix("#Roles"));
|
|
|
|
self._customerAutoComplete = new AutoComplete({
|
|
dataSource: new DataManager({
|
|
url: <string>$(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: <string>$(self.prefix("#CustomerName")).val(),
|
|
change: (e: any) => {
|
|
if (e.itemData === null) {
|
|
$(self.prefix("#CustomerId")).val("");
|
|
} else if (e.itemData.id === e.itemData.name) {
|
|
//nichts tun
|
|
} else {
|
|
$(self.prefix("#CustomerId")).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._customerAutoComplete.appendTo(self.prefix("#CustomerName"));
|
|
|
|
self._appUserAutoComplete = new AutoComplete({
|
|
dataSource: new DataManager({
|
|
url: <string>$(self.prefix("#jsLookupAppUsersUrl")).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("#AppUserName")).val(),
|
|
change: (e: any) => {
|
|
if (e.itemData === null) {
|
|
$(self.prefix("#AppUserId")).val("");
|
|
} else if (e.itemData.id === e.itemData.name) {
|
|
//nichts tun
|
|
} else {
|
|
$(self.prefix("#AppUserId")).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._appUserAutoComplete.appendTo(self.prefix("#AppUserName"));
|
|
|
|
$(self.prefix("#btnUploadImage")).off("click").on("click", function () {
|
|
self._uppload.open();
|
|
});
|
|
|
|
$(self.prefix("#btnDeleteImage")).off("click").on("click", function () {
|
|
if ($(self.prefix("#Photo")).val())
|
|
self.remove(<string>$(self.prefix("#Photo")).val());
|
|
$(self.prefix("#Photo")).val("");
|
|
$(self.prefix("#photoImage")).removeAttr("src");
|
|
$(self.prefix("#btnDeleteImage")).hide();
|
|
});
|
|
|
|
var currentLanguage = $("html").attr("lang");
|
|
self._uppload = new Uppload({
|
|
lang: UpploadHelper.getLanguage(currentLanguage),
|
|
uploader: xhrUploader({
|
|
endpoint: <string>$(self.prefix("#jsUploadUrl")).val(),
|
|
responseFunction: response => {
|
|
var responseJson = JSON.parse(response);
|
|
$(self.prefix("#Photo")).val(responseJson.filename);
|
|
return `/file/temp/${responseJson.filename}`;
|
|
}
|
|
}),
|
|
maxSize: [400, 400]
|
|
});
|
|
self._uppload.use(new Local());
|
|
self._uppload.use(new Crop({ aspectRatio: 1, }));
|
|
self._uppload.use(new Rotate());
|
|
self._uppload.use(new Flip());
|
|
self._uppload.use(<any>[new Blur(), new Brightness(), new Contrast(), new Grayscale(), new Saturate()]);
|
|
|
|
self._uppload.on("upload", (url: string) => {
|
|
$(self.prefix("#photoImage")).attr("src", url);
|
|
$(self.prefix("#btnDeleteImage")).show();
|
|
});
|
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
}
|
|
|
|
/**
|
|
* Aktivieren des Moduls
|
|
*/
|
|
public activate(): void {
|
|
var self = this;
|
|
$.setupValidator();
|
|
$.reinitializeValidator();
|
|
this.setupBindings();
|
|
$.randomAutocompleteValue();
|
|
self._uiManager.pageTitleAdd(self._component, self.prefix("#pageTitleAdd"));
|
|
}
|
|
|
|
/**
|
|
* Initialisieren des Moduls
|
|
*/
|
|
public init(): void {
|
|
if (this._global.appInitialized) {
|
|
createUserModule.activate();
|
|
} else {
|
|
setTimeout(() => { this.init() }, 10);
|
|
}
|
|
}
|
|
}
|
|
|
|
let createUserModule = new CreateUserModule();
|
|
createUserModule.init(); |