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

View file

@ -172,50 +172,49 @@ FormSchema.pre('save', function (next) {
console.log('autogenerating form');
console.log(that.pdf.path);
try {
pdfFiller.generateFieldJson(that.pdf.path, function(_form_fields){
pdfFiller.generateFieldJson(that.pdf.path, function(err, _form_fields){
if(err){
next( new Error(err.message), null);
}
//Map PDF field names to FormField field names
for(var i = 0; i < _form_fields.length; i++){
var field = _form_fields[i];
//Map PDF field names to FormField field names
for(var i = 0; i < _form_fields.length; i++){
var field = _form_fields[i];
//Convert types from FDF to 'FormField' types
if(_typeConvMap[ field.fieldType+'' ]){
field.fieldType = _typeConvMap[ field.fieldType+'' ];
}
field.fieldValue = '';
field.created = Date.now();
field.required = true;
field.disabled = false;
// field = new Field(field);
// field.save(function(err) {
// if (err) {
// console.error(err.message);
// throw new Error(err.message);
// });
// } else {
// _form_fields[i] = that;
// }
// });
_form_fields[i] = field;
//Convert types from FDF to 'FormField' types
if(_typeConvMap[ field.fieldType+'' ]){
field.fieldType = _typeConvMap[ field.fieldType+'' ];
}
console.log('NEW FORM_FIELDS: ');
console.log(_form_fields);
field.fieldValue = '';
field.created = Date.now();
field.required = true;
field.disabled = false;
console.log('\n\nOLD FORM_FIELDS: ');
console.log(that.form_fields);
// field = new Field(field);
// field.save(function(err) {
// if (err) {
// console.error(err.message);
// throw new Error(err.message);
// });
// } else {
// _form_fields[i] = that;
// }
// });
_form_fields[i] = field;
}
that.form_fields = _form_fields;
callback();
});
} catch(err){
next( new Error(err.message) );
}
console.log('NEW FORM_FIELDS: ');
console.log(_form_fields);
console.log('\n\nOLD FORM_FIELDS: ');
console.log(that.form_fields);
that.form_fields = _form_fields;
callback(null, 'pdfFiller');
});
}
callback(null,null);
callback(null, that);
}
], function(err, results) {
if(err){
@ -223,10 +222,13 @@ FormSchema.pre('save', function (next) {
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
@ -297,7 +299,28 @@ FormSchema.pre('save', function (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'),
_values = _.pluck(this.form_fields, 'fieldValue');
@ -314,4 +337,5 @@ FormSchema.methods.convertToFDF = function (cb) {
return jsonObj;
};
mongoose.model('Form', FormSchema);

View file

@ -81,9 +81,10 @@ FormSubmissionSchema.pre('save', function (next) {
// debugger;
var fdfData, dest_filename, dest_path;
var that = this;
var _form = this.form;
Form.findById(that.form, function(err, _form){
if(err) next( new Error(err.mesasge) );
// Form.findById(that.form, function(err, _form){
// if(err) next( new Error(err.mesasge) );
that.title = _form.title;
// console.log(_form);
@ -96,7 +97,7 @@ FormSubmissionSchema.pre('save', function (next) {
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(that.fdfData);
@ -114,7 +115,7 @@ FormSubmissionSchema.pre('save', function (next) {
next();
}
});
// });
});

View file

@ -34,11 +34,17 @@
<!-- Fav 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-->
{% for cssFile in cssFiles %}
<link rel="stylesheet" href="{{cssFile}}">
{% endfor %}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<!-- end Application CSS Files-->
<!-- HTML5 Shim -->
@ -60,25 +66,31 @@
var user = {{ user | json | safe }};
</script>
<!--Bower JS dependencies-->
{% for bowerJSFile in bowerJSFiles %}
<script type="text/javascript" src="{{bowerJSFile}}"></script>
{% endfor %}
<!-- end Bower JS dependencies-->
<!--Application JavaScript Files-->
{% for jsFile in jsFiles %}
<script type="text/javascript" src="{{jsFile}}"></script>
{% endfor %}
<!-- end Application CSS dependencies-->
{% if process.env.NODE_ENV === 'development' %}
<!--Livereload script rendered -->
<script type="text/javascript" src="http://{{request.hostname}}:35729/livereload.js"></script>
{% endif %}
<!--[if lt IE 9]>
<!-- [if lt IE 9]>
<section class="browsehappy jumbotron hide">
<h1>Hello there!</h1>
<p>You are using an old browser which we unfortunately do not support.</p>
<p>Please <a href="http://browsehappy.com/">click here</a> to update your browser before using the website.</p>
<p><a href="http://browsehappy.com" class="btn btn-primary btn-lg" role="button">Yes, upgrade my browser!</a></p>
</section>
<![endif]-->
<![endif] -->
</body>

View file

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

View file

@ -4,7 +4,9 @@
* Module dependencies.
*/
var _ = require('lodash'),
glob = require('glob');
glob = require('glob'),
bowerFiles = require('main-bower-files'),
path = require('path');
/**
* Load app configurations
@ -53,11 +55,30 @@ module.exports.getGlobbedFiles = function(globPatterns, removeRoot) {
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
*/
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
if (includeTests) {
@ -71,6 +92,6 @@ module.exports.getJavaScriptAssets = function(includeTests) {
* Get the modules CSS files
*/
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;
};

34
config/env/all.js vendored
View file

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

View file

@ -39,6 +39,11 @@ module.exports = function(db) {
app.locals.description = config.app.description;
app.locals.keywords = config.app.keywords;
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.cssFiles = config.getCSSAssets();

View file

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

View file

@ -51,26 +51,28 @@
"then-fs": "^2.0.0"
},
"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-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",
"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-jasmine": "~0.2.1",
"karma-coverage": "~0.2.0",
"karma-chrome-launcher": "~0.1.2",
"karma-coverage": "~0.2.0",
"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
$scope.findAll = function() {
$scope.forms = Forms.query();
console.log($scope.forms);
};
// Find a specific Form