2015-02-27 20:50:19 +00:00
|
|
|
'use strict';
|
2015-02-27 14:20:01 +00:00
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var async = require('async');
|
|
|
|
var path = require('path');
|
|
|
|
var assets = require('../lib/node-ff-assets');
|
|
|
|
|
|
|
|
var assetsConfig = {
|
|
|
|
// Map from uri paths to file systems paths.
|
|
|
|
linkMapping : {
|
|
|
|
'/static/module/bootstrap/': 'node_modules/bootstrap/dist/',
|
|
|
|
'/static/module/flot/': 'node_modules/flot/',
|
|
|
|
'/static/': 'www_static/',
|
|
|
|
},
|
|
|
|
linkSourcesCss : [
|
|
|
|
'/static/module/bootstrap/css/bootstrap.css',
|
|
|
|
],
|
|
|
|
linkSourcesJs : [
|
|
|
|
'/static/js/lib/jquery-2.1.3/jquery.js@http://code.jquery.com/jquery-2.1.3.js',
|
|
|
|
'/static/module/bootstrap/js/bootstrap.js',
|
|
|
|
'/static/module/flot/jquery.flot.js',
|
|
|
|
'/static/module/flot/jquery.flot.resize.js',
|
|
|
|
'/static/module/flot/jquery.flot.pie.js',
|
|
|
|
'/static/js/lib/angularjs-1.4.0-b4/angular.js@https://code.angularjs.org/1.4.0-beta.4/angular.js',
|
|
|
|
'/static/js/lib/angularjs-1.4.0-b4/angular-route.js@https://code.angularjs.org/1.4.0-beta.4/angular-route.js',
|
|
|
|
'/static/js/lib/angularjs-1.4.0-b4/angular-resource.js@https://code.angularjs.org/1.4.0-beta.4/angular-resource.js',
|
|
|
|
'/static/js/lib/angularjs-1.4.0-b4/angular-touch.js@https://code.angularjs.org/1.4.0-beta.4/angular-touch.js',
|
|
|
|
],
|
2015-02-27 20:50:19 +00:00
|
|
|
};
|
2015-02-27 14:20:01 +00:00
|
|
|
|
|
|
|
//callback helper to return an assembler config per asset type
|
|
|
|
function createBuildConfig(type) {
|
|
|
|
return function(callback) {
|
2015-02-27 20:50:19 +00:00
|
|
|
var singleResult = 'false' !== process.env.DEV_ASSETS_SINGLE_RESULT;
|
2015-02-27 14:20:01 +00:00
|
|
|
console.info('Asset single page result: '+singleResult+' for: '+type);
|
2015-02-27 20:50:19 +00:00
|
|
|
if (type === 'css') {
|
2015-02-27 14:20:01 +00:00
|
|
|
callback(null,{
|
|
|
|
linkMapping: assetsConfig.linkMapping,
|
|
|
|
linkTargetSingleResult: singleResult,
|
|
|
|
linkTarget: '/static/css/lib/assets.css',
|
|
|
|
linkSources: assetsConfig.linkSourcesCss,
|
|
|
|
assetType: type,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null, {
|
|
|
|
linkTargetSingleResult: singleResult,
|
|
|
|
linkMapping: assetsConfig.linkMapping,
|
|
|
|
linkTarget: '/static/js/lib/assets.js',
|
|
|
|
linkSources: assetsConfig.linkSourcesJs,
|
|
|
|
assetType: type,
|
|
|
|
});
|
|
|
|
}
|
2015-02-27 20:50:19 +00:00
|
|
|
};
|
2015-02-27 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildAssets(server,callbackDone) {
|
|
|
|
assets.build({
|
|
|
|
assets: {
|
|
|
|
js: {
|
|
|
|
configCreate: createBuildConfig('js'),
|
|
|
|
configFill: function (config, callback) {
|
|
|
|
async.series([
|
|
|
|
assets.factory.lib.async.pushLinkSources(config, '/static/js/', 'www_static/js/'),
|
|
|
|
assets.factory.lib.async.pushLinkSources(config, '/static/js/controller/', 'www_static/js/controller/'),
|
|
|
|
],callback);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
css: {
|
|
|
|
configCreate: createBuildConfig('css'),
|
|
|
|
configFill: function (config, callback) {
|
|
|
|
async.series([
|
|
|
|
assets.factory.lib.async.pushLinkSources(config, '/static/css/', 'www_static/css/'),
|
|
|
|
],callback);
|
|
|
|
|
|
|
|
},
|
|
|
|
assemblerCreate: function (assemblerConfig, callback) {
|
|
|
|
callback(null,new assets.AssetAssembler(assemblerConfig,assets.factory.assembler.constructor.readFileMinify()));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assemblerFill: function (assembler, callback) {
|
|
|
|
var serverResultKey = 'ff_assets_'+assembler.config.assetType;
|
|
|
|
assembler.on ('log', assets.factory.assembler.event.log.console(assembler.config.assetType));
|
|
|
|
assembler.on ('result', assets.factory.assembler.event.result.objectSet(server,serverResultKey));
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
},callbackDone);
|
|
|
|
}
|
|
|
|
|
2015-02-27 20:50:19 +00:00
|
|
|
function renderTemplate() {
|
|
|
|
return function (req, res) {
|
2015-02-27 14:20:01 +00:00
|
|
|
if (req.params.sub) {
|
2015-03-09 19:13:03 +00:00
|
|
|
res.render('thtml/' + req.params.page + '/' + req.params.sub);
|
2015-02-27 14:20:01 +00:00
|
|
|
} else {
|
2015-03-09 19:13:03 +00:00
|
|
|
res.render('thtml/' + req.params.page);
|
2015-02-27 14:20:01 +00:00
|
|
|
}
|
2015-02-27 20:50:19 +00:00
|
|
|
};
|
2015-02-27 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderPage(server) {
|
2015-02-27 20:50:19 +00:00
|
|
|
return function (req, res) {
|
2015-02-27 14:20:01 +00:00
|
|
|
res.render('index', {
|
2015-03-09 19:31:31 +00:00
|
|
|
pageTitle: 'node-ff-assets example',
|
|
|
|
pageKeywords: 'node,ff,assets,example,ui',
|
|
|
|
pageCssFiles: server.get('ff_assets_css'),
|
|
|
|
pageJsFiles: server.get('ff_assets_js'),
|
2015-02-27 14:20:01 +00:00
|
|
|
});
|
2015-02-27 20:50:19 +00:00
|
|
|
};
|
2015-02-27 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendRedirect() {
|
2015-02-27 20:50:19 +00:00
|
|
|
return function (req, res) {
|
2015-02-27 14:20:01 +00:00
|
|
|
res.redirect('/example-ui');
|
2015-02-27 20:50:19 +00:00
|
|
|
};
|
2015-02-27 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var server = express();
|
|
|
|
buildAssets(server,function(err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
console.info('Server assets done.');
|
|
|
|
|
2015-03-09 19:31:31 +00:00
|
|
|
server.set('view engine', 'ejs');
|
2015-02-27 14:20:01 +00:00
|
|
|
server.set('views', path.join(__dirname, 'www_views'));
|
|
|
|
server.use('/static',express.static(path.join(__dirname,'www_static')));
|
|
|
|
server.use('/static/module/bootstrap',express.static(path.join(__dirname,'node_modules/bootstrap/dist')));
|
|
|
|
server.use('/static/module/flot',express.static(path.join(__dirname,'node_modules/flot')));
|
|
|
|
|
2015-03-09 19:13:03 +00:00
|
|
|
server.get('/example-ui/thtml/:page', renderTemplate());
|
|
|
|
server.get('/example-ui/thtml/:page/:sub', renderTemplate());
|
2015-02-27 14:20:01 +00:00
|
|
|
server.get('/', sendRedirect());
|
|
|
|
server.get('/example-ui', renderPage(server));
|
|
|
|
server.get('/example-ui/*', renderPage(server)); // must be last; for HTML5 history
|
|
|
|
console.info('Server config done.');
|
|
|
|
|
|
|
|
server.listen(8080);
|
|
|
|
console.info('Server started on port 8080');
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
version: 'example'
|
|
|
|
};
|