2
0
Fork 0
tcrud/lib/plugin/ui/angular/ui-angular.js

129 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2017-06-10 14:53:29 +00:00
var debug = require('debug')('ff:tcrud:ui:angular:boot');
module.exports = (function () {
var renderCrudController = function (tview, thtmlPrefix, tapiPrefix, tapiPrefix2) {
if (tview.tmeta.tmodel.tkeys.length === 0) {
throw Error('no model keys in: '+tview.tid);
}
var keySlug = '';
for (var i = 0; i < tview.tmeta.tmodel.tkeys.length; i++) {
var key = tview.tmeta.tmodel.tkeys[i];
keySlug += '$routeParams.'+key;
if (i < (tview.tmeta.tmodel.tkeys.length - 1)) {
keySlug += '/';
}
}
var tviewCode = '';
var slugParts = tview.tslug.split('/');
for (var i = 0; i < slugParts.length; i++) {
var part = slugParts[i];
part = part.replace(/-/g,''); // TODO use tcode which is already cleaned ?
tviewCode += part.substring(0,1).toUpperCase()+part.substring(1);
}
//debug('tviewCode: $s',tviewCode);
return function (req, res, next) {
res.set('Content-Type', 'text/javascript');
res.render('node-ff-tcrud/angular/js/crud/controller',{
tview: tview,
tviewCode: tviewCode,
thtmlPrefix: thtmlPrefix,
tapiPrefix: tapiPrefix,
tapiPrefix2: tapiPrefix2,
ejsRouteParams: keySlug
});
};
}
var renderCrudTemplate = function (tview,paction) {
var ejsKeyRow = '';
var ejsKeyData = '';
for (var i = 0; i < tview.tmeta.tmodel.tkeys.length; i++) {
var key = tview.tmeta.tmodel.tkeys[i];
ejsKeyRow += '{{row.'+key+'}}';
ejsKeyData += '{{data.'+key+'}}';
if (i < (tview.tmeta.tmodel.tkeys.length - 1)) {
ejsKeyRow += '/';
ejsKeyData += '/';
}
}
return function (req, res, next) {
res.render('node-ff-tcrud/angular/'+paction+'/'+req.params.action,{
tview: tview,
ejsKeyRow: ejsKeyRow,
ejsKeyData: ejsKeyData
});
};
}
return function UIAngularCorePlugin() {
this.configPlugin = function (ctx) {
ctx.key = 'angular';
ctx.description = 'Exports angular ui data.';
ctx.localDir = __dirname;
ctx.localConfigTemplate = 'ui-angular.json';
ctx.dependencies.push('uiLibAngular');
ctx.dependencies.push('uiLibFFSpaLoader');
//ctx.dependencies.push('uiLibTopcoat');
ctx.dependencies.push('serverConfigTMenu');
ctx.dependencies.push('uiLibFontFaceOnLoad');
};
this.configServer = function(ctx) {
ctx.hostTemplateJS('js/application');
ctx.hostTemplateJS('js/application-font');
ctx.hostTemplateJS('js/application-controller')
ctx.hostTemplateJS('js/navigation-service');
ctx.hostTemplateHTML('thtml/application-view',true);;
ctx.hostTemplateHTML('thtml/application-top',true);
ctx.hostTemplateHTML('thtml/application-top-action',true);
ctx.hostTemplateHTML('thtml/application-top-tabs',true);
var uiPath = ctx.troot.tmeta.tplugin.angular.tbase;
debug('Exported uiPath: %s',uiPath);
ctx.server.get('/', ctx.renderRedirect(uiPath));
ctx.server.get(uiPath, ctx.renderTemplate('index','text/html'));
ctx.server.get(uiPath+'/*', ctx.renderTemplate('index','text/html')); // must be last; for HTML5 history
ctx.registrateMenu({
name: 'Server',
icon: 'fa fa-server'
},'server'); // move ?
};
this.configApi = function(ctx) {
var uriPrefix = ctx.createSlugApiTEntityBase();
var thtmlPrefix = uriPrefix + '/' + ctx.tview.tmeta.tplugin.angular.thtml;
var tapiPrefix = ctx.createSlugApiTEntityBasePlugin('formatJSON'); // TODO: move to tview ?
var tapiPrefix2 = ctx.createSlugApiTEntityBasePlugin('serverConfigTView'); // TODO: move to tview ?
ctx.server.get(uriPrefix + '/thtml/crud/:action', renderCrudTemplate(ctx.tview,'thtml/crud/'));
ctx.server.get(uriPrefix + '/crud/controller.js', renderCrudController(ctx.tview,thtmlPrefix,tapiPrefix,tapiPrefix2));
//ctx.server.get(uriPrefix + '/service.js', renderCrudService(tview))
ctx.registrateClientJSResource(uriPrefix + '/crud/controller.js');
if (ctx.tview.tmeta.tmenu.tenable && ctx.tview.tmeta.tmenu.tkey !== null && ctx.tview.tmeta.tmenu.titem) {
ctx.registrateMenuItem({
name: ctx.tview.tmeta.tmenu.tname,
link: ctx.tview.tmeta.tplugin.angular.tbase+'/'+ctx.tview.tslug+'/'+ctx.tview.tlist.tplugin.angular.tslug,
enable: ctx.tview.tlist.tenable,
roles: ctx.tview.tlist.troles,
icon: ctx.tview.tmeta.tmenu.ticon
},ctx.tview.tmeta.tmenu.tkey);
}
};
};
})();