ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
174 lines • 9.23 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var aurelia_framework_1 = require("aurelia-framework");
var ajax_1 = require("../ajax");
var aurelia_event_aggregator_1 = require("aurelia-event-aggregator");
var editable_inline_dropdown_checkbox_1 = require("../../resources/customElements/editable-inline-dropdown-checkbox/editable-inline-dropdown-checkbox");
var editable_inline_select_1 = require("../../resources/customElements/editable-inline-select/editable-inline-select");
var FormSchemaService = (function () {
function FormSchemaService(ajax, eventAggregator) {
this.ajax = ajax;
this.eventAggregator = eventAggregator;
}
FormSchemaService.prototype.getFormSchema = function () {
var _this = this;
return this.ajax.fetchJson(this.webServiceName + "/FormSchema")
.then(function (result) {
return _this.convertToFormSchema(result.Data);
});
};
FormSchemaService.prototype.convertToFormSchema = function (jsonSchema) {
var _this = this;
if (!jsonSchema) {
return Promise.resolve(null);
}
var promises = new Array();
var 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);
var _loop_1 = function () {
if (jsonSchema.properties.hasOwnProperty(key)) {
var schema_1 = (jsonSchema.properties)[key];
schema_1.description = schema_1.title || key;
schema_1.name = key;
schema_1.isKey = key === jsonSchema.id;
schema_1.hasValue = hasValue.bind(schema_1);
schema_1.required = (schema_1.type instanceof Array) ? (schema_1.type.indexOf("null") === -1) : true;
schema_1.type = (schema_1.type instanceof Array) ? schema_1.type[0] : schema_1.type;
schema_1.sortValueResolver = null;
schema_1.sortDataType = schema_1.type;
schema_1.sortTieBreakerColumnName = jsonSchema.id,
schema_1.sortTieBreakerDataType = (jsonSchema.properties)[jsonSchema.id].type;
if (schema_1.pattern) {
schema_1.matches = new RegExp(schema_1.pattern);
}
if (schema_1.isRecordDescriptionProperty !== undefined) {
formSchema.identifierPropertyName = schema_1.name;
}
if (schema_1.customType) {
var customPropertySchema = schema_1.customType;
var promise = void 0;
switch (customPropertySchema.Type) {
case "dropdown":
schema_1.dropdownConfiguration = customPropertySchema;
schema_1.type = "dropdown";
if (schema_1.dropdownConfiguration.ValueType === "integer")
schema_1.dropdownConfiguration.ValueType = "number";
if (schema_1.dropdownConfiguration.RefetchEventName) {
this_1.eventAggregator.subscribe(schema_1.dropdownConfiguration.RefetchEventName, function () { _this.getDropdownData(schema_1); });
}
promise = this_1.getDropdownData(schema_1);
promises.push(promise);
break;
case "dropdown-checkbox":
schema_1.dropdownCheckboxConfiguration = customPropertySchema;
schema_1.type = "dropdown-checkbox";
if (schema_1.dropdownCheckboxConfiguration.ValueType === "integer")
schema_1.dropdownCheckboxConfiguration.ValueType = "number";
if (schema_1.dropdownCheckboxConfiguration.RefetchEventName) {
this_1.eventAggregator.subscribe(schema_1.dropdownCheckboxConfiguration.RefetchEventName, function () { _this.getDropdownCheckboxData(schema_1); });
}
promise = this_1.getDropdownCheckboxData(schema_1);
promises.push(promise);
break;
case "MultiLineString":
schema_1.type = "multiLineString";
schema_1.multiLineStringConfiguration = customPropertySchema;
break;
case "password":
schema_1.type = "string";
schema_1.passwordRequirements = customPropertySchema;
break;
case "currency":
schema_1.type = "currency";
schema_1.format = "currency";
break;
case "calendar":
schema_1.type = "calendar";
schema_1.calendarConfiguration = customPropertySchema;
schema_1.format = "date";
break;
}
}
formSchema.properties.push(schema_1);
}
};
var this_1 = this;
for (var key in jsonSchema.properties) {
_loop_1();
}
return Promise.all(promises).then(function () {
return formSchema;
});
};
FormSchemaService.prototype.getDropdownData = function (schema) {
if (!schema.dropdownConfiguration) {
return Promise.resolve([]);
}
else {
var config_1 = (schema.dropdownConfiguration);
return this.ajax.fetchJson("" + config_1.DataUrl)
.then(function (result) {
var dropdowData = result.Data.map(function (datum) { return { text: datum.Text, value: datum.Value, selected: datum.Selected }; });
schema.sortValueResolver = {
fnGetRowValue: function (row, data) {
return editable_inline_select_1.EditableInlineSelect.dropdownTextFromValue(dropdowData, row[schema.name]);
}
};
schema.sortDataType = "string";
return config_1.DropdownData = dropdowData;
});
}
};
FormSchemaService.prototype.getDropdownCheckboxData = function (schema) {
if (!schema.dropdownCheckboxConfiguration) {
return Promise.resolve([]);
}
else {
var config_2 = (schema.dropdownCheckboxConfiguration);
return this.ajax.fetchJson("" + config_2.DataUrl)
.then(function (result) {
var dropdownData = result.Data.map(function (datum) { return { text: datum.Text, value: datum.Value, selected: datum.Selected }; });
schema.sortValueResolver = {
fnGetRowValue: function (instance, data) {
return editable_inline_dropdown_checkbox_1.EditableInlineDropdownCheckbox.computeSelectedItemsString(dropdownData, instance[schema.name]);
}
};
schema.sortDataType = "string";
return config_2.DropdownData = dropdownData;
});
}
};
FormSchemaService = __decorate([
aurelia_framework_1.autoinject,
aurelia_framework_1.transient(),
__metadata("design:paramtypes", [ajax_1.Ajax,
aurelia_event_aggregator_1.EventAggregator])
], FormSchemaService);
return FormSchemaService;
}());
exports.FormSchemaService = 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