ng2-json-editor
Version:
Angular2 component for editing large json documents
76 lines • 2.99 kB
JavaScript
/*
* This file is part of ng2-json-editor.
* Copyright (C) 2016 CERN.
*
* ng2-json-editor is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* ng2-json-editor is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ng2-json-editor; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
* In applying this license, CERN does not
* waive the privileges and immunities granted to it by virtue of its status
* as an Intergovernmental Organization or submit itself to any jurisdiction.
*/
import { Injectable } from '@angular/core';
import { fromJS } from 'immutable';
var EmptyValueService = (function () {
function EmptyValueService() {
}
EmptyValueService.prototype.generateEmptyValue = function (schema) {
var emptyValue = this.generateEmptyValueRecursively(schema);
if (typeof emptyValue === 'object') {
return fromJS(emptyValue);
}
else {
return emptyValue;
}
};
EmptyValueService.prototype.generateEmptyValueRecursively = function (schema) {
var _this = this;
if (schema.default) {
return schema.default;
}
if (schema.type === 'object') {
var emptyObject_1 = {};
Object.keys(schema.properties)
.filter(function (prop) {
var required = schema.required || [];
var alwaysShow = schema.alwaysShow || [];
return required.indexOf(prop) > -1 || alwaysShow.indexOf(prop) > -1;
}).forEach(function (prop) {
var propSchema = schema.properties[prop];
emptyObject_1[prop] = _this.generateEmptyValueRecursively(propSchema);
});
return emptyObject_1;
}
if (schema.type === 'array') {
var emptyArray = [];
var arrayElementSchema = schema.items;
if (schema.componentType !== 'complex-list') {
emptyArray.push(this.generateEmptyValueRecursively(arrayElementSchema));
}
return emptyArray;
}
return EmptyValueService.defaultValueLookup[schema.type];
};
return EmptyValueService;
}());
export { EmptyValueService };
EmptyValueService.defaultValueLookup = {
'string': '',
'boolean': false
};
EmptyValueService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
EmptyValueService.ctorParameters = function () { return []; };
//# sourceMappingURL=empty-value.service.js.map