ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
284 lines • 11.9 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { transient, inject } from "aurelia-framework";
import { DialogService } from "aurelia-dialog";
import { CommonDialogs } from "../dialog-service";
import { FormSchemaService } from "./form-schema-service";
import { FormSchemaCrudService } from "./form-schema-crud-service";
import { includeEventsIn } from "aurelia-event-aggregator";
import { NotificationService } from "../notification-service/notification-service";
import { SortColumnService } from "../sort-column-service";
import { I18N } from 'aurelia-i18n';
let FormSchemaCollectionController = class FormSchemaCollectionController {
constructor(commonDialogs, sortColumnService, dialogService, notificationService, formSchemaService, formSchemaCrudService, i18n, webServiceName) {
this.commonDialogs = commonDialogs;
this.sortColumnService = sortColumnService;
this.dialogService = dialogService;
this.notificationService = notificationService;
this.formSchemaService = formSchemaService;
this.formSchemaCrudService = formSchemaCrudService;
this.i18n = i18n;
this.showing = true;
this.disableNew = false;
this.disableDelete = false;
this.showKeyColumn = false;
this._makingLocalChanges = false;
this._loaded = false;
this.insertOnNew = false;
this.refreshDatumOnUpdate = false;
includeEventsIn(this);
formSchemaService.webServiceName =
formSchemaCrudService.webServiceName = webServiceName;
}
get makingLocalChanges() {
return this._makingLocalChanges;
}
set makingLocalChanges(value) {
if (value) {
this._makingLocalChanges = true;
}
else {
setTimeout(() => { this._makingLocalChanges = false; }, 0);
}
}
clearMakingLocalChanges(delay = 0) {
return new Promise((resolve, reject) => {
setTimeout(() => {
this._makingLocalChanges = false;
resolve();
}, delay);
});
}
load(andFetch = true) {
if (this.promiseToBeLoaded) {
return this.promiseToBeLoaded;
}
let eventName;
if (!this._loaded) {
var promises = [];
if (andFetch) {
promises.push(this.getInstances());
}
promises.push(this.getFormSchema());
eventName = "loaded";
this.promiseToBeLoaded = Promise.all(promises)
.then((instances) => {
if (this.rulesGenerator) {
this.rulesGenerator.generateValidationRules(this.formSchema, this.showKeyColumn);
}
return this.instances;
});
}
else {
eventName = "refetched";
this.promiseToBeLoaded = this.getInstances();
}
return this.promiseToBeLoaded.then((instances) => {
this.promiseToBeLoaded = undefined;
this._loaded = true;
this.publish(eventName);
return this.instances;
});
}
getInstances() {
return this.formSchemaCrudService.getAll().then((data) => { this.instances = data; });
}
getFormSchema() {
return this.formSchemaService.getFormSchema()
.then((schema) => {
this.className = schema.name;
this.key = schema.key;
return this.formSchema = schema;
});
}
preValidate(instance, propertyName, newValue, oldValue) { }
_preValidate(instance, propertyName, newValue, oldValue) {
this.makingLocalChanges = true;
this.preValidate(instance, propertyName, newValue, oldValue);
return this.clearMakingLocalChanges();
}
validate(instance, propertyName, newValue, oldValue) {
if (this.validationService) {
return this._preValidate(instance, propertyName, newValue, oldValue)
.then(() => {
return this.validationService.isValid(instance);
});
}
else
return Promise.resolve(false);
}
registerInstances(instances) {
if (this.validationService) {
this.validationService.registerInstances(instances, this.rulesGenerator.validationRules);
}
}
unRegisterInstances(instances) {
if (this.validationService) {
this.validationService.unRegisterInstances(instances);
}
}
revalidateAll() {
if (this.validationService) {
return this.validationService.validate();
}
else
return Promise.resolve([]);
}
handleUpdate(instance, propertyName, newValue, oldValue) {
if (this.makingLocalChanges) {
return Promise.resolve(false);
}
if (this.validationService) {
return this.validate(instance, propertyName, newValue, oldValue)
.then((isValid) => {
if (isValid) {
return this.executeUpdate(instance, propertyName, newValue, oldValue);
}
else {
return Promise.resolve(false);
}
});
}
else {
return this.executeUpdate(instance, propertyName, newValue, oldValue);
}
}
executeUpdate(instance, propertyName, newValue, oldValue) {
let dlgTitle = `${this.i18n.tr('Update')} ${this.className}`;
let recordIdentication = this.recordIdentication(instance);
return this.formSchemaCrudService.update(instance[this.key], instance, propertyName, newValue, oldValue)
.then((result) => {
if (result.Success) {
let newInstance = result.Data;
if (this.refreshDatumOnUpdate) {
this.makingLocalChanges = true;
Object.assign(instance, newInstance);
this.makingLocalChanges = false;
}
this.notificationService
.success(`${recordIdentication} was updated`);
this.publish("changed");
this.publish("modified", { instance: instance, propertyName: propertyName, newValue: newValue, oldValue: oldValue });
return Promise.resolve(true);
}
else {
return this.commonDialogs.prompt({
title: dlgTitle,
text: this.i18n.tr('formSchemaUpdateError', {
'className': this.className,
'propertyName': propertyName,
'ErrorMessage': result.ErrorMessage
}),
ok: true
})
.then(() => {
this.makingLocalChanges = true;
instance[propertyName] = oldValue;
this.makingLocalChanges = false;
return false;
});
}
});
}
handleCreate() {
return this.formSchemaCrudService.create();
}
handleNew(instance) {
let dlgTitle = `${this.i18n.tr('CreateNew')} ${this.className}`;
return this.formSchemaCrudService.insert(instance).then((result) => {
if (!result.Success) {
return this.commonDialogs.prompt({
title: dlgTitle,
text: this.i18n.tr('formSchemaNewError', {
'className': this.className,
'ErrorMessage': result.ErrorMessage
}),
ok: true
})
.then(() => {
return null;
});
}
else {
let newInstance = result.Data;
this.notificationService
.success(`A new ${this.className} was added`);
this.publish("changed");
this.publish("entered", { instance: newInstance });
return newInstance;
}
});
}
handleDelete(instance) {
let dlgTitle = `${this.i18n.tr('Delete')} ${this.className}`;
let id = instance[this.key];
let recordIdentication = this.recordIdentication(instance);
return this.commonDialogs.prompt({
title: dlgTitle,
text: this.i18n.tr('formSchemaConfirmDelete', {
'recordIdentication': recordIdentication
}),
yesNo: true
})
.then((response) => {
if (!response.wasCancelled) {
if (id === 0) {
return Promise.resolve(true);
}
return this.formSchemaCrudService.delete(id).then((result) => {
if (result.Success) {
this.notificationService
.success(`${recordIdentication} was deleted`);
this.publish("changed");
this.publish("exited", { instance: instance });
return true;
}
else {
return this.commonDialogs.prompt({
title: dlgTitle,
text: this.i18n.tr('formSchemaConfirmDelete', {
'recordIdentication': recordIdentication,
'ErrorMessage': result.ErrorMessage
}),
ok: true
})
.then(() => {
return false;
});
}
});
}
else {
return Promise.resolve(false);
}
});
}
fetchInstance(instance) {
let key = instance[this.formSchema.key];
return this.formSchemaCrudService.get(key).then((newInstance) => { return newInstance; });
}
recordIdentication(instance) {
if (this.formSchema.identifierPropertyName) {
return `'${instance[this.formSchema.identifierPropertyName]}'`;
}
else {
return this.i18n.tr("theData");
}
}
};
FormSchemaCollectionController = __decorate([
inject(CommonDialogs, SortColumnService, DialogService, NotificationService, FormSchemaService, FormSchemaCrudService, I18N),
transient(),
__metadata("design:paramtypes", [CommonDialogs, Object, DialogService,
NotificationService, Object, Object, I18N, String])
], FormSchemaCollectionController);
export { FormSchemaCollectionController };
export { ValidateResult } from "aurelia-validation";
//# sourceMappingURL=form-schema-collection-controller.js.map