added automatic bower dependency injection

This commit is contained in:
David Baldwynn 2015-07-03 12:25:02 -07:00
parent 7c8495cd64
commit fe574995b4
12 changed files with 242 additions and 133 deletions

View file

@ -1,3 +1,4 @@
{ {
"directory": "public/lib" "directory": "public/lib",
"analytics": false
} }

View file

@ -71,7 +71,8 @@ exports.createSubmission = function(req, res) {
var submission = new FormSubmission(), var submission = new FormSubmission(),
form = req.form, form = req.form,
fdfData, fdfData,
fdfTemplate; fdfTemplate,
that = this;
submission.form = form; submission.form = form;
submission.admin = req.user; submission.admin = req.user;
@ -81,31 +82,53 @@ exports.createSubmission = function(req, res) {
console.log(req.body); console.log(req.body);
// submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress; // submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if(form.autofillPDFs){
if (form.isGenerated){ if (form.isGenerated){
fdfTemplate = form.convertToFDF(); fdfTemplate = form.generateFDFTemplate();
} else { } else {
try { try {
fdfTemplate = pdfFiller.mapForm2PDF(form.convertToFDF(), form.pdfFieldMap); fdfTemplate = pdfFiller.mapForm2PDF(form.generateFDFTemplate(), form.pdfFieldMap);
} catch(err){ } catch(err){
throw new Error(err.message); res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} }
} }
if(form.autofillPDFs){
fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null); fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null);
submission.fdfData = fdfData; submission.fdfData = fdfData;
} }
async.series([
function(callback){
submission.save(function(err){ submission.save(function(err){
if (err) { if (err) {
console.error(err); callback(err);
} else {
callback(null);
}
});
},
function(callback){
//Add submission to Form.submissionns
form.submissions.push(submission);
form.save(function(err){
if (err) {
callback(err);
} else {
callback(null);
}
});
},
], function(err, results) {
if(err){
res.status(400).send({ res.status(400).send({
message: errorHandler.getErrorMessage(err) message: errorHandler.getErrorMessage(err)
}); });
} else {
console.log('Form Submission CREATED');
res.status(200).send('Form submission successfully saved');
} }
console.log(results);
console.log(that.form_fields);
res.status(200).send('Form submission successfully saved');
}); });
}; };
@ -115,17 +138,21 @@ exports.createSubmission = function(req, res) {
exports.listSubmissions = function(req, res) { exports.listSubmissions = function(req, res) {
var _form = req.form; var _form = req.form;
if(_form.submissions.length){
res.json(_form.submissions);
}else{
FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) { FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) {
if (err) { if (err) {
console.log(err); console.log(err);
res.status(500).send({ res.status(400).send({
message: errorHandler.getErrorMessage(err) message: errorHandler.getErrorMessage(err)
}); });
} else { } else {
console.log('hello'); console.log('retrieved submissions for form');
res.json(submissions); res.json(submissions);
} }
}); });
}
}; };
/** /**
@ -188,7 +215,7 @@ exports.delete = function(req, res) {
Form.remove({_id: form._id}, function(err) { Form.remove({_id: form._id}, function(err) {
if (err) { if (err) {
res.status(500).send({ res.status(500).send({
message: err.message message: errorHandler.getErrorMessage(err)
}); });
} else { } else {
console.log('Form successfully deleted'); console.log('Form successfully deleted');

View file

@ -172,8 +172,10 @@ FormSchema.pre('save', function (next) {
console.log('autogenerating form'); console.log('autogenerating form');
console.log(that.pdf.path); console.log(that.pdf.path);
try { pdfFiller.generateFieldJson(that.pdf.path, function(err, _form_fields){
pdfFiller.generateFieldJson(that.pdf.path, function(_form_fields){ if(err){
next( new Error(err.message), null);
}
//Map PDF field names to FormField field names //Map PDF field names to FormField field names
for(var i = 0; i < _form_fields.length; i++){ for(var i = 0; i < _form_fields.length; i++){
@ -209,13 +211,10 @@ FormSchema.pre('save', function (next) {
console.log(that.form_fields); console.log(that.form_fields);
that.form_fields = _form_fields; that.form_fields = _form_fields;
callback(); callback(null, 'pdfFiller');
}); });
} catch(err){
next( new Error(err.message) );
} }
} callback(null, that);
callback(null,null);
} }
], function(err, results) { ], function(err, results) {
if(err){ if(err){
@ -223,10 +222,13 @@ FormSchema.pre('save', function (next) {
message: err.message message: err.message
})); }));
} }
next(); console.log(results);
console.log(that.form_fields);
next(results[1]);
}); });
} }else{
next(); next();
}
}); });
//Autogenerate Form_fields from PDF if 'isGenerated' flag is set //Autogenerate Form_fields from PDF if 'isGenerated' flag is set
@ -297,7 +299,28 @@ FormSchema.pre('save', function (next) {
// next(); // next();
// }); // });
FormSchema.methods.convertToFDF = function (cb) { // FormSchema.methods.generateSubmissionsCSV = function (cb) {
// if(this.submissions.length){
// submissions = this.submissions
// }else{
// submissions =
// }
// _values.forEach(function(val){
// if(val === true){
// val = 'Yes';
// }else if(val === false) {
// val = 'Off';
// }
// });
// var jsonObj = _.zipObject(_keys, _values);
// return jsonObj;
// };
FormSchema.methods.generateFDFTemplate = function (cb) {
var _keys = _.pluck(this.form_fields, 'title'), var _keys = _.pluck(this.form_fields, 'title'),
_values = _.pluck(this.form_fields, 'fieldValue'); _values = _.pluck(this.form_fields, 'fieldValue');
@ -314,4 +337,5 @@ FormSchema.methods.convertToFDF = function (cb) {
return jsonObj; return jsonObj;
}; };
mongoose.model('Form', FormSchema); mongoose.model('Form', FormSchema);

View file

@ -81,9 +81,10 @@ FormSubmissionSchema.pre('save', function (next) {
// debugger; // debugger;
var fdfData, dest_filename, dest_path; var fdfData, dest_filename, dest_path;
var that = this; var that = this;
var _form = this.form;
Form.findById(that.form, function(err, _form){ // Form.findById(that.form, function(err, _form){
if(err) next( new Error(err.mesasge) ); // if(err) next( new Error(err.mesasge) );
that.title = _form.title; that.title = _form.title;
// console.log(_form); // console.log(_form);
@ -96,7 +97,7 @@ FormSubmissionSchema.pre('save', function (next) {
this.pdfFilePath = dest_path; this.pdfFilePath = dest_path;
pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){ pdfFiller.fillForm(_form.pdf.path, dest_path, that.fdfData, function(err){
console.log('fdfData: \n'); console.log('fdfData: \n');
console.log(that.fdfData); console.log(that.fdfData);
@ -114,7 +115,7 @@ FormSubmissionSchema.pre('save', function (next) {
next(); next();
} }
}); // });
}); });

View file

@ -34,11 +34,17 @@
<!-- Fav Icon --> <!-- Fav Icon -->
<link href="/modules/core/img/brand/favicon.ico" rel="shortcut icon" type="image/x-icon"> <link href="/modules/core/img/brand/favicon.ico" rel="shortcut icon" type="image/x-icon">
<!--Bower CSS dependencies-->
{% for bowerCssFile in bowerCssFiles %}
<link rel="stylesheet" href="{{bowerCssFile}}">
{% endfor %}
<!-- end Bower CSS dependencies-->
<!--Application CSS Files--> <!--Application CSS Files-->
{% for cssFile in cssFiles %} {% for cssFile in cssFiles %}
<link rel="stylesheet" href="{{cssFile}}"> <link rel="stylesheet" href="{{cssFile}}">
{% endfor %} {% endfor %}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <!-- end Application CSS Files-->
<!-- HTML5 Shim --> <!-- HTML5 Shim -->
@ -60,11 +66,17 @@
var user = {{ user | json | safe }}; var user = {{ user | json | safe }};
</script> </script>
<!--Bower JS dependencies-->
{% for bowerJSFile in bowerJSFiles %}
<script type="text/javascript" src="{{bowerJSFile}}"></script>
{% endfor %}
<!-- end Bower JS dependencies-->
<!--Application JavaScript Files--> <!--Application JavaScript Files-->
{% for jsFile in jsFiles %} {% for jsFile in jsFiles %}
<script type="text/javascript" src="{{jsFile}}"></script> <script type="text/javascript" src="{{jsFile}}"></script>
{% endfor %} {% endfor %}
<!-- end Application CSS dependencies-->
{% if process.env.NODE_ENV === 'development' %} {% if process.env.NODE_ENV === 'development' %}
<!--Livereload script rendered --> <!--Livereload script rendered -->

View file

@ -1,7 +1,14 @@
{ {
"name": "meanjs", "name": "MedForm",
"version": "0.3.2", "description": "PDF generated form builder",
"description": "Fullstack JavaScript with MongoDB, Express, AngularJS, and Node.js.", "version": "0.0.2",
"homepage": "https://github.com/whitef0x0/medform",
"authors": [
"David Baldwynn <polydaic@gmail.com>"
],
"license": "MIT",
"private": true,
"appPath": "public/modules",
"dependencies": { "dependencies": {
"bootstrap": "~3", "bootstrap": "~3",
"angular": "~1.2", "angular": "~1.2",
@ -12,11 +19,14 @@
"angular-ui-utils": "~0.1.1", "angular-ui-utils": "~0.1.1",
"angular-ui-router": "~0.2.11", "angular-ui-router": "~0.2.11",
"angular-strap": "~2.2.1", "angular-strap": "~2.2.1",
"angular-permission": "~0.3.0" "angular-permission": "~0.3.0",
"restangular": "~1.5.1",
"fontawesome": "~4.3.0",
"ng-file-upload": "~5.0.9"
}, },
"resolutions": { "resolutions": {
"angular": "^1.2.21", "angular": "^1.2.21",
"angular-resource": "~1.2", "angular-resource": "~1.2",
"ng-file-upload": "~4.1.0" "ng-file-upload": "~5.0.9"
} }
} }

View file

@ -4,7 +4,9 @@
* Module dependencies. * Module dependencies.
*/ */
var _ = require('lodash'), var _ = require('lodash'),
glob = require('glob'); glob = require('glob'),
bowerFiles = require('main-bower-files'),
path = require('path');
/** /**
* Load app configurations * Load app configurations
@ -53,11 +55,30 @@ module.exports.getGlobbedFiles = function(globPatterns, removeRoot) {
return output; return output;
}; };
module.exports.removeRootDir = function(files, root) {
return files.map(function(file) {
return file.replace(path.join(process.cwd(),root), '');
});
};
/**
* Get the app's bower dependencies
*/
module.exports.getBowerJSAssets = function() {
return this.removeRootDir(bowerFiles('**/**.js'), 'public/');
};
module.exports.getBowerCSSAssets = function() {
return this.removeRootDir(bowerFiles('**/**.css'), 'public/');
};
module.exports.getBowerOtherAssets = function() {
return this.removeRootDir(bowerFiles('**/!(*.js|*.css|*.less)'), 'public/');
};
/** /**
* Get the modules JavaScript files * Get the modules JavaScript files
*/ */
module.exports.getJavaScriptAssets = function(includeTests) { module.exports.getJavaScriptAssets = function(includeTests) {
var output = this.getGlobbedFiles(this.assets.lib.js.concat(this.assets.js), 'public/'); var output = this.getGlobbedFiles(this.assets.js, 'public/');
// To include tests // To include tests
if (includeTests) { if (includeTests) {
@ -71,6 +92,6 @@ module.exports.getJavaScriptAssets = function(includeTests) {
* Get the modules CSS files * Get the modules CSS files
*/ */
module.exports.getCSSAssets = function() { module.exports.getCSSAssets = function() {
var output = this.getGlobbedFiles(this.assets.lib.css.concat(this.assets.css), 'public/'); var output = this.getGlobbedFiles(this.assets.css, 'public/');
return output; return output;
}; };

34
config/env/all.js vendored
View file

@ -52,23 +52,23 @@ module.exports = {
} }
}, },
assets: { assets: {
lib: { // lib: {
css: [ // css: [
'public/lib/bootstrap/dist/css/bootstrap.css', // 'public/lib/bootstrap/dist/css/bootstrap.css',
'public/lib/bootstrap/dist/css/bootstrap-theme.css', // 'public/lib/bootstrap/dist/css/bootstrap-theme.css',
], // ],
js: [ // js: [
'public/lib/angular/angular.js', // 'public/lib/angular/angular.js',
'public/lib/angular-permission/dist/angular-permission.js', // 'public/lib/angular-permission/dist/angular-permission.js',
'public/lib/angular-resource/angular-resource.js', // 'public/lib/angular-resource/angular-resource.js',
'public/lib/angular-animate/angular-animate.js', // 'public/lib/angular-animate/angular-animate.js',
'public/lib/angular-ui-router/release/angular-ui-router.js', // 'public/lib/angular-ui-router/release/angular-ui-router.js',
'public/lib/angular-ui-utils/ui-utils.js', // 'public/lib/angular-ui-utils/ui-utils.js',
'public/lib/angular-bootstrap/ui-bootstrap-tpls.js', // 'public/lib/angular-bootstrap/ui-bootstrap-tpls.js',
'public/lib/ng-file-upload/ng-file-upload-all.js', // 'public/lib/ng-file-upload/ng-file-upload-all.js',
'public/lib/angular-cookies/angular-cookies.js', // 'public/lib/angular-cookies/angular-cookies.js',
] // ]
}, // },
css: [ css: [
'public/modules/**/css/*.css' 'public/modules/**/css/*.css'
], ],

View file

@ -39,6 +39,11 @@ module.exports = function(db) {
app.locals.description = config.app.description; app.locals.description = config.app.description;
app.locals.keywords = config.app.keywords; app.locals.keywords = config.app.keywords;
app.locals.facebookAppId = config.facebook.clientID; app.locals.facebookAppId = config.facebook.clientID;
app.locals.bowerJSFiles = config.getBowerJSAssets();
app.locals.bowerCssFiles = config.getBowerCSSAssets();
app.locals.bowerOtherFiles = config.getBowerOtherAssets();
app.locals.jsFiles = config.getJavaScriptAssets(); app.locals.jsFiles = config.getJavaScriptAssets();
app.locals.cssFiles = config.getCSSAssets(); app.locals.cssFiles = config.getCSSAssets();

View file

@ -18,39 +18,44 @@ module.exports = function(grunt) {
serverViews: { serverViews: {
files: watchFiles.serverViews, files: watchFiles.serverViews,
options: { options: {
livereload: true livereload: true,
spawn: false
} }
}, },
serverJS: { serverJS: {
files: watchFiles.serverJS, files: watchFiles.serverJS,
tasks: ['jshint'], tasks: ['newer:jshint'],
options: { options: {
livereload: true livereload: true,
spawn: false
} }
}, },
clientViews: { clientViews: {
files: watchFiles.clientViews, files: watchFiles.clientViews,
options: { options: {
livereload: true livereload: true,
spawn: false
} }
}, },
clientJS: { clientJS: {
files: watchFiles.clientJS, files: watchFiles.clientJS,
tasks: ['jshint'], tasks: ['newer:jshint'],
options: { options: {
livereload: true livereload: true,
spawn: false
} }
}, },
clientCSS: { clientCSS: {
files: watchFiles.clientCSS, files: watchFiles.clientCSS,
tasks: ['csslint'], tasks: ['newer:csslint'],
options: { options: {
livereload: true livereload: true,
spawn: false
} }
}, },
mochaTests: { mochaTests: {
files: watchFiles.mochaTests, files: watchFiles.mochaTests,
tasks: ['test:server'], tasks: ['newer:test:server'],
} }
}, },
jshint: { jshint: {
@ -171,7 +176,7 @@ module.exports = function(grunt) {
grunt.registerTask('secure', ['env:secure', 'lint', 'concurrent:default']); grunt.registerTask('secure', ['env:secure', 'lint', 'concurrent:default']);
// Lint task(s). // Lint task(s).
grunt.registerTask('lint', ['jshint', 'csslint']); grunt.registerTask('lint', ['newer:jshint', 'newer:csslint']);
// Build task(s). // Build task(s).
grunt.registerTask('build', ['lint', 'loadConfig', 'ngAnnotate', 'uglify', 'cssmin']); grunt.registerTask('build', ['lint', 'loadConfig', 'ngAnnotate', 'uglify', 'cssmin']);

View file

@ -51,26 +51,28 @@
"then-fs": "^2.0.0" "then-fs": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"supertest": "~0.14.0",
"should": "~4.1.0",
"grunt-env": "~0.4.1",
"grunt-node-inspector": "~0.1.3",
"grunt-contrib-watch": "~0.6.1",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-csslint": "^0.3.1",
"grunt-ng-annotate": "~0.4.0",
"grunt-contrib-uglify": "~0.6.0",
"grunt-contrib-cssmin": "~0.10.0",
"grunt-nodemon": "~0.3.0",
"grunt-concurrent": "~1.0.0", "grunt-concurrent": "~1.0.0",
"grunt-mocha-test": "~0.12.1", "grunt-contrib-csslint": "^0.3.1",
"grunt-contrib-cssmin": "~0.10.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-uglify": "~0.6.0",
"grunt-contrib-watch": "~0.6.1",
"grunt-env": "~0.4.1",
"grunt-karma": "~0.9.0", "grunt-karma": "~0.9.0",
"load-grunt-tasks": "~1.0.0", "grunt-mocha-test": "~0.12.1",
"grunt-newer": "^1.1.1",
"grunt-ng-annotate": "~0.4.0",
"grunt-node-inspector": "~0.1.3",
"grunt-nodemon": "~0.3.0",
"karma": "~0.12.0", "karma": "~0.12.0",
"karma-jasmine": "~0.2.1",
"karma-coverage": "~0.2.0",
"karma-chrome-launcher": "~0.1.2", "karma-chrome-launcher": "~0.1.2",
"karma-coverage": "~0.2.0",
"karma-firefox-launcher": "~0.1.3", "karma-firefox-launcher": "~0.1.3",
"karma-phantomjs-launcher": "~0.1.2" "karma-jasmine": "~0.2.1",
"karma-phantomjs-launcher": "~0.1.2",
"load-grunt-tasks": "~1.0.0",
"main-bower-files": "^2.8.2",
"should": "~4.1.0",
"supertest": "~0.14.0"
} }
} }

View file

@ -44,6 +44,7 @@ angular.module('forms').controller('ViewFormController', ['$scope', '$stateParam
// Return all user's Forms // Return all user's Forms
$scope.findAll = function() { $scope.findAll = function() {
$scope.forms = Forms.query(); $scope.forms = Forms.query();
console.log($scope.forms);
}; };
// Find a specific Form // Find a specific Form