UNPKG

ferngully-aurelia-tools

Version:

Ferngully Tools for Aurelia

301 lines 13.6 kB
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'; var FormSchemaCollectionController = (function () { function FormSchemaCollectionController(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; } Object.defineProperty(FormSchemaCollectionController.prototype, "makingLocalChanges", { get: function () { return this._makingLocalChanges; }, set: function (value) { var _this = this; if (value) { this._makingLocalChanges = true; } else { setTimeout(function () { _this._makingLocalChanges = false; }, 0); } }, enumerable: true, configurable: true }); FormSchemaCollectionController.prototype.clearMakingLocalChanges = function (delay) { var _this = this; if (delay === void 0) { delay = 0; } return new Promise(function (resolve, reject) { setTimeout(function () { _this._makingLocalChanges = false; resolve(); }, delay); }); }; FormSchemaCollectionController.prototype.load = function (andFetch) { var _this = this; if (andFetch === void 0) { andFetch = true; } if (this.promiseToBeLoaded) { return this.promiseToBeLoaded; } var eventName; if (!this._loaded) { var promises = []; if (andFetch) { promises.push(this.getInstances()); } promises.push(this.getFormSchema()); eventName = "loaded"; this.promiseToBeLoaded = Promise.all(promises) .then(function (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(function (instances) { _this.promiseToBeLoaded = undefined; _this._loaded = true; _this.publish(eventName); return _this.instances; }); }; FormSchemaCollectionController.prototype.getInstances = function () { var _this = this; return this.formSchemaCrudService.getAll().then(function (data) { _this.instances = data; }); }; FormSchemaCollectionController.prototype.getFormSchema = function () { var _this = this; return this.formSchemaService.getFormSchema() .then(function (schema) { _this.className = schema.name; _this.key = schema.key; return _this.formSchema = schema; }); }; FormSchemaCollectionController.prototype.preValidate = function (instance, propertyName, newValue, oldValue) { }; FormSchemaCollectionController.prototype._preValidate = function (instance, propertyName, newValue, oldValue) { this.makingLocalChanges = true; this.preValidate(instance, propertyName, newValue, oldValue); return this.clearMakingLocalChanges(); }; FormSchemaCollectionController.prototype.validate = function (instance, propertyName, newValue, oldValue) { var _this = this; if (this.validationService) { return this._preValidate(instance, propertyName, newValue, oldValue) .then(function () { return _this.validationService.isValid(instance); }); } else return Promise.resolve(false); }; FormSchemaCollectionController.prototype.registerInstances = function (instances) { if (this.validationService) { this.validationService.registerInstances(instances, this.rulesGenerator.validationRules); } }; FormSchemaCollectionController.prototype.unRegisterInstances = function (instances) { if (this.validationService) { this.validationService.unRegisterInstances(instances); } }; FormSchemaCollectionController.prototype.revalidateAll = function () { if (this.validationService) { return this.validationService.validate(); } else return Promise.resolve([]); }; FormSchemaCollectionController.prototype.handleUpdate = function (instance, propertyName, newValue, oldValue) { var _this = this; if (this.makingLocalChanges) { return Promise.resolve(false); } if (this.validationService) { return this.validate(instance, propertyName, newValue, oldValue) .then(function (isValid) { if (isValid) { return _this.executeUpdate(instance, propertyName, newValue, oldValue); } else { return Promise.resolve(false); } }); } else { return this.executeUpdate(instance, propertyName, newValue, oldValue); } }; FormSchemaCollectionController.prototype.executeUpdate = function (instance, propertyName, newValue, oldValue) { var _this = this; var dlgTitle = this.i18n.tr('Update') + " " + this.className; var recordIdentication = this.recordIdentication(instance); return this.formSchemaCrudService.update(instance[this.key], instance, propertyName, newValue, oldValue) .then(function (result) { if (result.Success) { var 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(function () { _this.makingLocalChanges = true; instance[propertyName] = oldValue; _this.makingLocalChanges = false; return false; }); } }); }; FormSchemaCollectionController.prototype.handleCreate = function () { return this.formSchemaCrudService.create(); }; FormSchemaCollectionController.prototype.handleNew = function (instance) { var _this = this; var dlgTitle = this.i18n.tr('CreateNew') + " " + this.className; return this.formSchemaCrudService.insert(instance).then(function (result) { if (!result.Success) { return _this.commonDialogs.prompt({ title: dlgTitle, text: _this.i18n.tr('formSchemaNewError', { 'className': _this.className, 'ErrorMessage': result.ErrorMessage }), ok: true }) .then(function () { return null; }); } else { var newInstance = result.Data; _this.notificationService .success("A new " + _this.className + " was added"); _this.publish("changed"); _this.publish("entered", { instance: newInstance }); return newInstance; } }); }; FormSchemaCollectionController.prototype.handleDelete = function (instance) { var _this = this; var dlgTitle = this.i18n.tr('Delete') + " " + this.className; var id = instance[this.key]; var recordIdentication = this.recordIdentication(instance); return this.commonDialogs.prompt({ title: dlgTitle, text: this.i18n.tr('formSchemaConfirmDelete', { 'recordIdentication': recordIdentication }), yesNo: true }) .then(function (response) { if (!response.wasCancelled) { if (id === 0) { return Promise.resolve(true); } return _this.formSchemaCrudService.delete(id).then(function (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(function () { return false; }); } }); } else { return Promise.resolve(false); } }); }; FormSchemaCollectionController.prototype.fetchInstance = function (instance) { var key = instance[this.formSchema.key]; return this.formSchemaCrudService.get(key).then(function (newInstance) { return newInstance; }); }; FormSchemaCollectionController.prototype.recordIdentication = function (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); return FormSchemaCollectionController; }()); export { FormSchemaCollectionController }; export { ValidateResult } from "aurelia-validation"; //# sourceMappingURL=form-schema-collection-controller.js.map