2
0
Fork 0
tcrud/lib/plugin/auto/auto-tname.js

51 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2017-06-10 14:53:29 +00:00
module.exports = (function () {
var createName = function (fieldKey,fieldName) {
if (fieldKey === undefined) {
throw new Error('no fieldKey');
}
if (fieldName && fieldName.length !== 0) {
return fieldName;
}
var result = '';
var names = fieldKey.split('_');
for (var i in names) {
var name = names[i];
if (name.length > 1) {
name = name.substring(0,1).toUpperCase() + name.substring(1);
}
result = result + ' ' + name;
}
return result.substring(1); // remove first space
};
var filterName = function(ctx,value) {
return ctx.filterValue(value,['_','-','.',','],' ', function (part) {
return part.substring(0,1).toUpperCase()+part.substring(1).toLowerCase();
});
};
return function AutoTNamePlugin() {
this.configPlugin = function (ctx) {
ctx.key='autoTName';
ctx.description='Auto fills and checks the tname fields.';
};
this.fillTField = function(ctx) {
if (ctx.tfield.tname === null) {
ctx.tfield.tname = createName(ctx.tfield.tid);
} else {
//ctx.tfield.tname = filterName(ctx,ctx.tfield.tname);
}
};
this.fillTEntity = function(ctx) {
if (ctx.tentity.tname === null) {
ctx.tentity.tname = ctx.tentity.tid;//
}
};
};
})();