META-INF.resources.scripts.lightadmin-serializer.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lightadmin Show documentation
Show all versions of lightadmin Show documentation
Pluggable data management solution for Java web applications developed in a "Rapid" manner
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function ($) {
$.fn.serializeFormJSON = function (resourceName, usePlaceholders) {
function resolveObjectHref(attrVal, attrMetadata) {
if (attrVal == '') {
if (usePlaceholders) {
attrVal = 'NULL';
} else {
return null;
}
}
var rest_link_template = attrMetadata['rest_link']['href'];
return decodeURIComponent(rest_link_template).replace('{idPlaceholder}', attrVal);
}
var json = {};
$.each(this.serializeArray(), function () {
var attrVal = this.value || '';
var property = ConfigurationMetadataService.getProperty(resourceName, this.name, 'formView');
var propertyType = property['type'];
if (propertyType.indexOf('ASSOC') == 0) {
var href = resolveObjectHref(attrVal, property);
if (propertyType == 'ASSOC_MULTI') {
if (!json[this.name]) {
json[this.name] = [];
}
json[this.name].push(href);
} else {
json[this.name] = href;
}
} else {
json[this.name] = attrVal;
}
});
$.each(ConfigurationMetadataService.getDynamicProperties(resourceName, "formView"), function (attrName, attrMetadata) {
var attrVal = json[attrName];
if (attrVal != undefined && attrVal != '') {
return;
}
switch (attrMetadata.type) {
case 'ASSOC_MULTI':
json[attrName] = [];
break;
case 'BOOL':
json[attrName] = false;
break;
case 'DATE':
if (usePlaceholders) {
json[attrName] = -377743392000001;
}
break;
}
});
return json;
};
})(jQuery);