From fe574995b497188dd69fe44bfeb21e4b764bd2cb Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Fri, 3 Jul 2015 12:25:02 -0700 Subject: [PATCH] added automatic bower dependency injection --- .bowerrc | 3 +- app/controllers/forms.server.controller.js | 93 ++++++++++------ app/models/form.server.model.js | 104 +++++++++++------- app/models/form_submission.server.model.js | 9 +- app/views/layout.server.view.html | 20 +++- bower.json | 20 +++- config/config.js | 27 ++++- config/env/all.js | 34 +++--- config/express.js | 5 + gruntfile.js | 25 +++-- package.json | 34 +++--- .../view-form.client.controller.js | 1 + 12 files changed, 242 insertions(+), 133 deletions(-) diff --git a/.bowerrc b/.bowerrc index 1b842779..6a060253 100755 --- a/.bowerrc +++ b/.bowerrc @@ -1,3 +1,4 @@ { - "directory": "public/lib" + "directory": "public/lib", + "analytics": false } diff --git a/app/controllers/forms.server.controller.js b/app/controllers/forms.server.controller.js index f95d43d9..15d6bdef 100644 --- a/app/controllers/forms.server.controller.js +++ b/app/controllers/forms.server.controller.js @@ -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'); diff --git a/app/models/form.server.model.js b/app/models/form.server.model.js index ad637fff..1fdf7e53 100644 --- a/app/models/form.server.model.js +++ b/app/models/form.server.model.js @@ -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); diff --git a/app/models/form_submission.server.model.js b/app/models/form_submission.server.model.js index ee71e2f0..fc97280b 100644 --- a/app/models/form_submission.server.model.js +++ b/app/models/form_submission.server.model.js @@ -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(); } - }); + // }); }); diff --git a/app/views/layout.server.view.html b/app/views/layout.server.view.html index ced993bd..4e3779fe 100755 --- a/app/views/layout.server.view.html +++ b/app/views/layout.server.view.html @@ -34,11 +34,17 @@ + + {% for bowerCssFile in bowerCssFiles %} + + {% endfor %} + + {% for cssFile in cssFiles %} {% endfor %} - + @@ -60,25 +66,31 @@ var user = {{ user | json | safe }}; - + + {% for bowerJSFile in bowerJSFiles %} + + {% endfor %} + + {% for jsFile in jsFiles %} {% endfor %} + {% if process.env.NODE_ENV === 'development' %} {% endif %} - + diff --git a/bower.json b/bower.json index 22af2af4..3d054eb3 100755 --- a/bower.json +++ b/bower.json @@ -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 " + ], + "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" } } diff --git a/config/config.js b/config/config.js index ef4a4c2c..c7ca3447 100755 --- a/config/config.js +++ b/config/config.js @@ -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; }; diff --git a/config/env/all.js b/config/env/all.js index c4a5d4cf..92ec2efd 100755 --- a/config/env/all.js +++ b/config/env/all.js @@ -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' ], diff --git a/config/express.js b/config/express.js index 19325d91..bbf0ad3b 100755 --- a/config/express.js +++ b/config/express.js @@ -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(); diff --git a/gruntfile.js b/gruntfile.js index b45a9cd7..dc5b6bcf 100755 --- a/gruntfile.js +++ b/gruntfile.js @@ -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']); diff --git a/package.json b/package.json index e8d6c8d6..cd832282 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/public/modules/forms/controllers/view-form.client.controller.js b/public/modules/forms/controllers/view-form.client.controller.js index 09bdb17f..46108a08 100644 --- a/public/modules/forms/controllers/view-form.client.controller.js +++ b/public/modules/forms/controllers/view-form.client.controller.js @@ -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