ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
165 lines • 8.42 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 { autoinject, transient } from "aurelia-framework";
import { Ajax } from "../ajax";
import { EventAggregator } from 'aurelia-event-aggregator';
import { EditableInlineDropdownCheckbox } from "../../resources/customElements/editable-inline-dropdown-checkbox/editable-inline-dropdown-checkbox";
import { EditableInlineSelect } from "../../resources/customElements/editable-inline-select/editable-inline-select";
let FormSchemaService = class FormSchemaService {
constructor(ajax, eventAggregator) {
this.ajax = ajax;
this.eventAggregator = eventAggregator;
}
getFormSchema() {
return this.ajax.fetchJson(`${this.webServiceName}/FormSchema`)
.then((result) => {
return this.convertToFormSchema(result.Data);
});
}
convertToFormSchema(jsonSchema) {
if (!jsonSchema) {
return Promise.resolve(null);
}
const promises = new Array();
const formSchema = {};
formSchema.key = jsonSchema.id;
formSchema.name = jsonSchema.title;
formSchema.properties = new Array();
formSchema.identifierPropertyName = jsonSchema.properties.hasOwnProperty("Name") ? "Name" : (jsonSchema.properties.hasOwnProperty("Description") ? "Description" : null);
for (var key in jsonSchema.properties) {
if (jsonSchema.properties.hasOwnProperty(key)) {
let schema = (jsonSchema.properties)[key];
schema.description = schema.title || key;
schema.name = key;
schema.isKey = key === jsonSchema.id;
schema.hasValue = hasValue.bind(schema);
schema.required = (schema.type instanceof Array) ? (schema.type.indexOf("null") === -1) : true;
schema.type = (schema.type instanceof Array) ? schema.type[0] : schema.type;
schema.sortValueResolver = null;
schema.sortDataType = schema.type;
schema.sortTieBreakerColumnName = jsonSchema.id,
schema.sortTieBreakerDataType = (jsonSchema.properties)[jsonSchema.id].type;
if (schema.pattern) {
schema.matches = new RegExp(schema.pattern);
}
if (schema.isRecordDescriptionProperty !== undefined) {
formSchema.identifierPropertyName = schema.name;
}
if (schema.customType) {
let customPropertySchema = schema.customType;
let promise;
switch (customPropertySchema.Type) {
case "dropdown":
schema.dropdownConfiguration = customPropertySchema;
schema.type = "dropdown";
if (schema.dropdownConfiguration.ValueType === "integer")
schema.dropdownConfiguration.ValueType = "number";
if (schema.dropdownConfiguration.RefetchEventName) {
this.eventAggregator.subscribe(schema.dropdownConfiguration.RefetchEventName, () => { this.getDropdownData(schema); });
}
promise = this.getDropdownData(schema);
promises.push(promise);
break;
case "dropdown-checkbox":
schema.dropdownCheckboxConfiguration = customPropertySchema;
schema.type = "dropdown-checkbox";
if (schema.dropdownCheckboxConfiguration.ValueType === "integer")
schema.dropdownCheckboxConfiguration.ValueType = "number";
if (schema.dropdownCheckboxConfiguration.RefetchEventName) {
this.eventAggregator.subscribe(schema.dropdownCheckboxConfiguration.RefetchEventName, () => { this.getDropdownCheckboxData(schema); });
}
promise = this.getDropdownCheckboxData(schema);
promises.push(promise);
break;
case "MultiLineString":
schema.type = "multiLineString";
schema.multiLineStringConfiguration = customPropertySchema;
break;
case "password":
schema.type = "string";
schema.passwordRequirements = customPropertySchema;
break;
case "currency":
schema.type = "currency";
schema.format = "currency";
break;
case "calendar":
schema.type = "calendar";
schema.calendarConfiguration = customPropertySchema;
schema.format = "date";
break;
}
}
formSchema.properties.push(schema);
}
}
return Promise.all(promises).then(() => {
return formSchema;
});
}
getDropdownData(schema) {
if (!schema.dropdownConfiguration) {
return Promise.resolve([]);
}
else {
let config = (schema.dropdownConfiguration);
return this.ajax.fetchJson(`${config.DataUrl}`)
.then((result) => {
let dropdowData = result.Data.map((datum) => { return { text: datum.Text, value: datum.Value, selected: datum.Selected }; });
schema.sortValueResolver = {
fnGetRowValue: (row, data) => {
return EditableInlineSelect.dropdownTextFromValue(dropdowData, row[schema.name]);
}
};
schema.sortDataType = "string";
return config.DropdownData = dropdowData;
});
}
}
getDropdownCheckboxData(schema) {
if (!schema.dropdownCheckboxConfiguration) {
return Promise.resolve([]);
}
else {
let config = (schema.dropdownCheckboxConfiguration);
return this.ajax.fetchJson(`${config.DataUrl}`)
.then((result) => {
let dropdownData = result.Data.map((datum) => { return { text: datum.Text, value: datum.Value, selected: datum.Selected }; });
schema.sortValueResolver = {
fnGetRowValue: (instance, data) => {
return EditableInlineDropdownCheckbox.computeSelectedItemsString(dropdownData, instance[schema.name]);
}
};
schema.sortDataType = "string";
return config.DropdownData = dropdownData;
});
}
}
};
FormSchemaService = __decorate([
autoinject,
transient(),
__metadata("design:paramtypes", [Ajax,
EventAggregator])
], FormSchemaService);
export { FormSchemaService };
var hasValue = function (instance) {
var val = instance[this.name];
if ((val === undefined) || (val === null)) {
return false;
}
else if ((this.type === 'string') && (val === '')) {
return false;
}
else {
return true;
}
};
//# sourceMappingURL=form-schema-service.js.map