'use strict'; // Init the application configuration module for AngularJS application var ApplicationConfiguration = (function() { // Init module configuration options var applicationModuleName = 'medform'; var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.utils', 'ngRaven']; // Add a new vertical module var registerModule = function(moduleName, dependencies) { // Create angular module angular.module(moduleName, dependencies || []); // Add the module to the AngularJS configuration file angular.module(applicationModuleName).requires.push(moduleName); }; return { applicationModuleName: applicationModuleName, applicationModuleVendorDependencies: applicationModuleVendorDependencies, registerModule: registerModule }; })(); 'use strict'; //Start by defining the main module and adding the module dependencies angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies); // Setting HTML5 Location Mode angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', function($locationProvider) { $locationProvider.hashPrefix('!'); } ]); //Permission Constants angular.module(ApplicationConfiguration.applicationModuleName).constant('APP_PERMISSIONS', { viewAdminSettings: 'viewAdminSettings', editAdminSettings: 'editAdminSettings', editForm: 'editForm', viewPrivateForm: 'viewPrivateForm', }); //User Role constants angular.module(ApplicationConfiguration.applicationModuleName).constant('USER_ROLES', { admin: 'admin', normal: 'user', superuser: 'superuser', }); // angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', '$state', '$stateParams', // function($rootScope, Auth, $state, $stateParams) { // $rootScope.$state = $state; // $rootScope.$stateParams = $stateParams; // // add previous state property // $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) { // // console.log(fromState); // $state.previous = fromState; // //Redirect home to listForms if user is authenticated // if(toState.name === 'home'){ // if(Auth.isAuthenticated()){ // event.preventDefault(); // stop current execution // $state.go('listForms'); // go to login // } // } // }); // } // ]); angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Auth', 'User', 'Authorizer', '$state', '$stateParams', function($rootScope, Auth, User, Authorizer, $state, $stateParams) { $rootScope.$on('$stateChangeStart', function(event, next) { var authenticator, permissions, user; permissions = next && next.data ? next.data.permissions : null; Auth.ensureCurrentUser(User); user = Auth.currentUser; // if(user){} authenticator = new Authorizer(user); if( (permissions !== null) && !authenticator.canAccess(permissions) ){ event.preventDefault(); if (!user) { $state.go('sigin'); } else { $state.go('access_denied'); } } }); }]); //Then define the init function for starting up the application angular.element(document).ready(function() { //Fixing facebook bug with redirect if (window.location.hash === '#_=_') window.location.hash = '#!'; //Then init the app angular.bootstrap(document, [ApplicationConfiguration.applicationModuleName]); }); 'use strict'; // Use Application configuration module to register a new module ApplicationConfiguration.registerModule('core', ['users']); 'use strict'; // Use Application configuration module to register a new module ApplicationConfiguration.registerModule('forms', ['ngFileUpload', 'ui.date', 'users']); 'use strict'; // Use Application configuration module to register a new module ApplicationConfiguration.registerModule('users'); 'use strict'; // Setting up route angular.module('core').config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider, Authorization) { // Redirect to home view when route not found $urlRouterProvider.otherwise('/'); // Home state routing $stateProvider. state('home', { url: '/', templateUrl: 'modules/core/views/home.client.view.html' }); // $urlRouterProvider.otherwise( function($injector) { // var $state = $injector.get('$state'); // $state.go('home'); // }); } ]); 'use strict'; angular.module('core').controller('HeaderController', ['$rootScope','$scope','Menus', '$state', 'Auth', 'User', function ($rootScope, $scope, Menus, $state, Auth, User) { $scope.user = $rootScope.user = Auth.ensureHasCurrentUser(User); $scope.authentication = $rootScope.authentication = Auth; $rootScope.languages = $scope.languages = ['english', 'french', 'spanish']; $scope.isCollapsed = false; $scope.hideNav = false; $scope.menu = Menus.getMenu('topbar'); $scope.signout = function() { var promise = User.logout(); promise.then(function() { Auth.logout(); // Auth.ensureHasCurrentUser(null); $rootScope.user = null; $state.go('home'); }, function(reason) { console.log('Logout Failed: ' + reason); }); }; $scope.toggleCollapsibleMenu = function() { $scope.isCollapsed = !$scope.isCollapsed; }; // Collapsing the menu after navigation $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { $scope.isCollapsed = false; $scope.hideNav = false; if ( angular.isDefined( toState.data ) ) { if ( angular.isDefined( toState.data.hideNav ) ) { $scope.hideNav = toState.data.hideNav; } } }); // Principal.identity().then(function(user){ // $rootScope.user = user; // console.log('topbar') // console.log($scope.user); // }, // function(error){ // console.log(error); // }).then(function(){ // $scope.signout = function() { // $http.get('/auth/signout').success(function(response) { // $state.go('home'); // }).error(function(error) { // $scope.error = (error.message || error); // }); // Principal.signout().then( // function(result){ // $state.go('home'); // }, // function(error){ // $scope.error = (error.message || error); // } // ); // if( angular.isDefined(response_obj.error) ){ // $scope.error = response_obj.error; // } else{ // $state.go('home'); // } // }; // }); } ]); 'use strict'; angular.module('core').controller('HomeController', ['$rootScope', '$scope', 'User', 'Auth', '$state', function($rootScope, $scope, User, Auth, $state) { $scope = $rootScope; $scope.user = Auth.ensureHasCurrentUser(User); $scope.authentication = Auth; // if($scope.authentication.isAuthenticated()){ // $state.go('listForms'); // } } ]); // 'use strict'; // /** // * @ngdoc function // * @name medform.controller:IndexCtrl // * @description // * # IndexCtrl // * Controller of core // */ // angular.module('medform').controller('IndexCtrl', function ($scope, $rootScope, $location, User, Auth, $state) { // $rootScope.user = Auth.ensureHasCurrentUser(User); // // $rootScope.user = Auth.getUserState(User).user; // $rootScope.authentication = Auth; // $scope.signout = function() { // User.logout(function() { // Auth.logout(); // $rootScope.user = null; // $state.go('home'); // // $scope.$apply(); // }); // }; // }); 'use strict'; //Menu service used for managing menus angular.module('core').service('Menus', [ function() { // Define a set of default roles this.defaultRoles = ['*']; // Define the menus object this.menus = {}; // A private function for rendering decision var shouldRender = function(user) { if (user) { if (!!~this.roles.indexOf('*')) { return true; } else { for (var userRoleIndex in user.roles) { for (var roleIndex in this.roles) { console.log(this.roles[roleIndex]); console.log( this.roles[roleIndex] === user.roles[userRoleIndex]); if (this.roles[roleIndex] === user.roles[userRoleIndex]) { return true; } } } } } else { return this.isPublic; } return false; }; // Validate menu existance this.validateMenuExistance = function(menuId) { if (menuId && menuId.length) { if (this.menus[menuId]) { return true; } else { throw new Error('Menu does not exists'); } } else { throw new Error('MenuId was not provided'); } return false; }; // Get the menu object by menu id this.getMenu = function(menuId) { // Validate that the menu exists this.validateMenuExistance(menuId); // Return the menu object return this.menus[menuId]; }; // Add new menu object by menu id this.addMenu = function(menuId, isPublic, roles) { // Create the new menu this.menus[menuId] = { isPublic: isPublic || false, roles: roles || this.defaultRoles, items: [], shouldRender: shouldRender }; // Return the menu object return this.menus[menuId]; }; // Remove existing menu object by menu id this.removeMenu = function(menuId) { // Validate that the menu exists this.validateMenuExistance(menuId); // Return the menu object delete this.menus[menuId]; }; // Add menu item object this.addMenuItem = function(menuId, menuItemTitle, menuItemURL, menuItemType, menuItemUIRoute, isPublic, roles, position) { // Validate that the menu exists this.validateMenuExistance(menuId); // Push new menu item this.menus[menuId].items.push({ title: menuItemTitle, link: menuItemURL, menuItemType: menuItemType || 'item', menuItemClass: menuItemType, uiRoute: menuItemUIRoute || ('/' + menuItemURL), isPublic: ((isPublic === null || typeof isPublic === 'undefined') ? this.menus[menuId].isPublic : isPublic), roles: ((roles === null || typeof roles === 'undefined') ? this.menus[menuId].roles : roles), position: position || 0, items: [], shouldRender: shouldRender }); // Return the menu object return this.menus[menuId]; }; // Add submenu item object this.addSubMenuItem = function(menuId, rootMenuItemURL, menuItemTitle, menuItemURL, menuItemUIRoute, isPublic, roles, position) { // Validate that the menu exists this.validateMenuExistance(menuId); // Search for menu item for (var itemIndex in this.menus[menuId].items) { if (this.menus[menuId].items[itemIndex].link === rootMenuItemURL) { // Push new submenu item this.menus[menuId].items[itemIndex].items.push({ title: menuItemTitle, link: menuItemURL, uiRoute: menuItemUIRoute || ('/' + menuItemURL), isPublic: ((isPublic === null || typeof isPublic === 'undefined') ? this.menus[menuId].items[itemIndex].isPublic : isPublic), roles: ((roles === null || typeof roles === 'undefined') ? this.menus[menuId].items[itemIndex].roles : roles), position: position || 0, shouldRender: shouldRender }); } } // Return the menu object return this.menus[menuId]; }; // Remove existing menu object by menu id this.removeMenuItem = function(menuId, menuItemURL) { // Validate that the menu exists this.validateMenuExistance(menuId); // Search for menu item to remove for (var itemIndex in this.menus[menuId].items) { if (this.menus[menuId].items[itemIndex].link === menuItemURL) { this.menus[menuId].items.splice(itemIndex, 1); } } // Return the menu object return this.menus[menuId]; }; // Remove existing menu object by menu id this.removeSubMenuItem = function(menuId, submenuItemURL) { // Validate that the menu exists this.validateMenuExistance(menuId); // Search for menu item to remove for (var itemIndex in this.menus[menuId].items) { for (var subitemIndex in this.menus[menuId].items[itemIndex].items) { if (this.menus[menuId].items[itemIndex].items[subitemIndex].link === submenuItemURL) { this.menus[menuId].items[itemIndex].items.splice(subitemIndex, 1); } } } // Return the menu object return this.menus[menuId]; }; //Adding the topbar menu this.addMenu('topbar', false, ['*']); //Adding the bottombar menu for the Form-Footer view this.addMenu('bottombar', false, ['*']); } ]); 'use strict'; // Configuring the Articles module angular.module('forms').run(['Menus', function(Menus) { // Set top bar menu items Menus.addMenuItem('topbar', 'My Forms', 'forms', '', '/forms', false); } ]).filter('formValidity', function(){ return function(formObj){ //get keys var formKeys = Object.keys(formObj); //we only care about things that don't start with $ var fieldKeys = formKeys.filter(function(key){ return key[0] !== '$'; }); var fields = formObj.form_fields; // fieldKeys.map(function(key){ // return formObj[key]; // }); var valid_count = fields.filter(function(field){ if(typeof field === 'object'){ return !!(field.fieldValue); } }).length; return valid_count; }; }); 'use strict'; // Setting up route angular.module('forms').config(['$stateProvider', function($stateProvider) { // Forms state routing $stateProvider. state('listForms', { url: '/forms', templateUrl: 'modules/forms/views/list-forms.client.view.html', }). state('createForm', { url: '/forms/create', templateUrl: 'modules/forms/views/create-form.client.view.html', }). state('viewForm', { url: '/forms/:formId/admin', templateUrl: 'modules/forms/views/view-form.client.view.html', }). state('viewPublicForm', { url: '/forms/:formId', templateUrl: 'modules/forms/views/view-public-form.client.view.html', data: { hideNav: true, hideFooter: false }, }). state('editForm', { url: '/forms/:formId/edit', templateUrl: 'modules/forms/views/create-form.client.view.html', }); } ]); // 'use strict'; // angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', '$http', // function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location, $http) { // $scope.form = {}; // $scope.isNewForm = false; // $scope.log = ''; // $scope.pdfLoading = false; // var _current_upload = null; // // Get current form if it exists, or create new one // if($stateParams.formId){ // Forms.get({ formId: $stateParams.formId}, function(form){ // $scope.form = angular.fromJson(angular.toJson(form)); // console.log($scope.form); // }); // } else { // $scope.form.form_fields = []; // $scope.isNewForm = true; // } // //PDF Functions // $scope.cancelUpload = function(){ // _current_upload.abort(); // $scope.pdfLoading = false; // $scope.removePDF(); // }; // $scope.removePDF = function(){ // $scope.form.pdf = null; // $scope.form.isGenerated = false; // $scope.form.autofillPDFs = false; // console.log('form.pdf: '+$scope.form.pdf+' REMOVED'); // }; // $scope.uploadPDF = function(files) { // if (files && files.length) { // // for (var i = 0; i < files.length; i++) { // var file = files[0]; // _current_upload = Upload.upload({ // url: '/upload/pdf', // fields: { // 'user': $scope.user, // 'form': $scope.form // }, // file: file // }).progress(function (evt) { // var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); // $scope.log = 'progress: ' + progressPercentage + '% ' + // evt.config.file.name + '\n' + $scope.log; // $scope.pdfLoading = true; // }).success(function (data, status, headers, config) { // $scope.log = 'file ' + data.originalname + ' uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log; // console.log($scope.form.pdf); // $scope.form.pdf = angular.fromJson(angular.toJson(data)); // $scope.pdfLoading = false; // console.log($scope.log); // console.log('$scope.pdf: '+$scope.form.pdf.name); // if(!$scope.$$phase){ // $scope.$apply(); // } // }).error(function(err){ // $scope.pdfLoading = false; // console.log('Error occured during upload.\n'); // console.log(err); // }); // // } // } // }; // $rootScope.goToWithId = function(route, id) { // $state.go(route, {'formId': id}, {reload: true}); // }; // // Create new Form // $rootScope.createOrUpdate = function() { // if($scope.isNewForm){ // // Create new Form object // var form = new Forms($scope.form); // $http.post('/forms', {form: $scope.form}) // .success(function(data, status, headers){ // console.log('form created'); // // Clear form fields // $scope.form = {}; // // Redirect after save // $scope.goToWithId('viewForm', $scope.form._id); // }).error(function(errorResponse){ // console.log(errorResponse); // $scope.error = errorResponse; // }); // } else{ // $scope.update(function(err){ // console.log('done updating'); // }); // } // }; // // Update existing Form // $rootScope.update = function(cb) { // var form = new Forms($scope.form); // console.log('update form'); // console.log($scope.form); // $http.put('/forms/'+$scope.form._id, {form: $scope.form}) // .success(function(data, status, headers){ // console.log('form updated successfully'); // $scope.goToWithId('viewForm', $scope.form._id); // cb(null); // }).error(function(err){ // console.log('Error occured during form UPDATE.\n'); // console.log(err); // cb(err); // }); // }; // } // ]); 'use strict'; // Forms controller angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', function($scope, $stateParams, $state, Forms, CurrentForm) { $scope.form = Forms.get({ formId: $stateParams.formId }); CurrentForm.setForm($scope.form); } ]); 'use strict'; // submissions controller angular.module('forms').controller('ViewSubmissionController', ['$scope', '$stateParams', '$state', 'Submissions','$http', function($scope, $stateParams, $state, Submissions, $http) { $scope.submissionId = undefined; // Principal.identity().then(function(user){ // $scope.authentication.user = user; // }).then(function(){ // Return all form's submissions $scope.findAll = function() { $scope.submissions = Submissions.query({ formId: $stateParams.formId }); }; // Find a specific submission $scope.findOne = function() { $scope.submission = Submissions.get({ submissionId: $scope.submissionId, formId: $stateParams.formId }); }; // Remove existing submission $scope.remove = function(submission) { if (!submission) { submission = $scope.submission; } $http.delete('/forms/'+$stateParams.formId+'/submissions/'+submission._id). success(function(data, status, headers){ console.log('submission deleted successfully'); alert('submission deleted..'); }); }; // }); } ]); 'use strict'; // Forms controller angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http', function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) { $scope.myform = CurrentForm.getForm(); $scope.submissions = undefined; $scope.viewSubmissions = false; $scope.showCreateModal = false; $scope.table = { masterChecker: true, rows: [] }; // Return all user's Forms $scope.findAll = function() { $scope.myforms = Forms.query(); }; // Find a specific Form $scope.findOne = function() { $scope.myform = Forms.get({ formId: $stateParams.formId }); CurrentForm.setForm($scope.myform); }; $scope.goToWithId = function(route, id) { $state.go(route, {'formId': id}, {reload: true}); }; $scope.setForm = function (form) { $scope.myForm = form; }; //Modal functions $scope.openCreateModal = function(){ if(!$scope.showCreateModal){ $scope.showCreateModal = true; } }; $scope.closeCreateModal = function(){ if($scope.showCreateModal){ $scope.showCreateModal = false; } }; /* * Table Functions */ $scope.toggleAllCheckers = function(){ console.log('toggleAllCheckers'); for(var i=0; i<$scope.table.rows.length; i++){ $scope.table.rows[i].selected = $scope.table.masterChecker; } }; $scope.toggleObjSelection = function($event, description) { $event.stopPropagation(); console.log('checkbox clicked'); }; $scope.rowClicked = function(obj) { console.log('row clicked'); obj.selected = !obj.selected; }; //show submissions of Form $scope.showSubmissions = function(){ $scope.viewSubmissions = true; if(!$scope.table.rows.length){ $http.get('/forms/'+$scope.myform._id+'/submissions') .success(function(data, status, headers){ console.log(data); $scope.submissions = data; $scope.table.rows = data; console.log('form submissions successfully fetched'); }) .error(function(err){ console.log('Could not fetch form submissions.\nError: '+err); }); } else if(!$scope.submissions.length){ $http.get('/forms/'+$scope.myform._id+'/submissions') .success(function(data, status, headers){ $scope.submissions = data; $scope.table.rows = data; console.log($scope.table.rows); console.log('form submissions successfully fetched'); }) .error(function(err){ console.log('Could not fetch form submissions.\nError: '+err); }); } console.log($scope.submissions); }; //hide submissions of Form $scope.hideSubmissions = function(){ $scope.viewSubmissions = false; }; // Remove existing Form $scope.remove = function(form_id) { var form = {}; if(!form_id){ form = CurrentForm.getForm(); if(!form) form = $scope.myform; }else { form._id = form_id; } $http.delete('/forms/'+form._id) .success(function(data, status, headers){ console.log('form deleted successfully'); if(!form_id){ $state.go('listForms'); } if($scope.myforms.length > 0){ $scope.myforms = _.filter($scope.myforms, function(myform){ return myform._id !== form._id; }); } }).error(function(error){ console.log('ERROR: Form could not be deleted.'); console.error(error); }); }; // Create new Form $scope.createNew = function(){ var form = {}; form.title = $scope.myForm.name.$modelValue; form.language = $scope.myForm.language.$modelValue; console.log(form); $scope.showCreateModal = true; console.log($scope.myForm); if($scope.myForm.$valid && $scope.myForm.$dirty){ $http.post('/forms', {form: form}) .success(function(data, status, headers){ console.log('form created'); // Clear form fields $scope.myForm = {}; // Redirect after save $scope.goToWithId('viewForm', $scope.myform._id); }).error(function(errorResponse){ console.log(errorResponse); // $scope.error = errorResponse.data.message; }); } }; // Update existing Form $scope.saveInProgress = false; $scope.update = $rootScope.update = function(cb) { if(!$scope.saveInProgress){ $scope.saveInProgress = true; $rootScope.saveInProgress = true; console.log('begin updating form'); var err = null; $http.put('/forms/'+$scope.myform._id, {form: $scope.myform}) .then(function(response){ console.log('form updated successfully'); console.log(response.status); }).catch(function(response){ console.log('Error occured during form UPDATE.\n'); console.log(response.data); err = response.data; }).finally(function() { console.log('finished updating'); $scope.saveInProgress = false; cb(err); }); } }; $rootScope.resetForm = function(){ $scope.myform = Forms.get({ formId: $stateParams.formId }); }; } ]); 'use strict'; angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', function($rootScope, $timeout) { return { require: ['^form'], // scope: { // callback: '&autoSaveCallback' // }, link: function($scope, $element, $attrs, $ctrls) { if($rootScope.watchCount === undefined){ $rootScope.watchCount = 0; } var difference = function(array){ var rest = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1)); var containsEquals = function(obj, target) { if (obj == null) return false; return _.any(obj, function(value) { return _.isEqual(value, target); }); }; return _.filter(array, function(value){ return !containsEquals(rest, value); }); }; var $formCtrl = $ctrls[0]; var savePromise = null; $scope.finishedRender = false; var expression = $attrs.autoSaveForm || 'true'; $scope.$on('ngRepeatStarted', function(ngRepeatFinishedEvent) { // $scope.finishedRender = false; $rootScope.watchCount = 0; }); $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) { $scope.finishedRender = true; }); $scope.$watch('myform.form_fields', function(newValue, oldValue) { console.log('watchCount: '+$rootScope.watchCount); if(difference(oldValue,newValue).length === 0 || oldValue === undefined){ return; } console.log('\n\n----------\n$dirty: '+( $formCtrl.$dirty ) ); console.log('form_fields changed: '+difference(oldValue,newValue).length ); // console.log('$valid: '+$formCtrl.$valid); console.log('finishedRender: '+$scope.finishedRender); // console.log('saveInProgress: '+$scope.saveInProgress); if($scope.finishedRender && ($formCtrl.$dirty || difference(oldValue,newValue).length !== 0) ) { $rootScope.watchCount++; if($rootScope.watchCount === 1) { if(savePromise) { $timeout.cancel(savePromise); } savePromise = $timeout(function() { savePromise = null; $rootScope[$attrs.autoSaveCallback]( function(err){ if(!err){ // console.log('Form data persisted -- setting pristine flag'); console.log('\n\n---------\nUpdate form CLIENT'); console.log(Date.now()); $rootScope.watchCount = 0; $formCtrl.$setPristine(); }else{ console.log('Error form data NOT persisted'); console.log(err); } }); }); } } }, true); } }; }]); 'use strict'; angular.module('forms').directive('changeFocus', function() { return { scope:{ focusDownId: '@', focusUpId: '@', }, link: function(scope, elem, attrs) { // console.log('aoeuaoeuaoeuaou'); scope.focusUp = function(){ if(!scope.$first) { // console.log('aoeuaoeu'); elem[0].previousElementSibling.find('input').focus(); } scope.apply(); }; scope.focusDown = function(){ if(!scope.$last) { elem[0].nextElementSibling.focus(); } scope.apply(); }; //Bind 'focus-down' click event to given dom element angular.element('#' + scope.focusDownId).bind('click', function() { scope.focusDown(); }); //Bind 'focus-up' click event to given dom element angular.element('#' + scope.focusUpId).bind('click', function() { scope.focusUp(); }); } }; }); 'use strict'; angular.module('forms').directive('configureFormDirective', ['$rootScope','$http', '$timeout', 'timeCounter', 'Auth', 'FormFields', function ($rootScope, $http, $timeout, timeCounter, Auth, FormFields) { return { controller: function($scope){ $scope.log = ''; $scope.pdfLoading = false; $scope.languages = $rootScope.languages; var _current_upload = null; $scope.createOrUpdate = $rootScope.createOrUpdate; $scope.resetForm = $rootScope.resetForm; var _unbindedPdfFields = $scope.pdfFields; //DAVID: TODO: finish this so we can create a Form.pdfFieldMap // $scope.getUnbindedPdfFields = function(fieldType){ // _unbindedPdfFields = $scope.pdfFields // } //PDF Functions $scope.cancelUpload = function(){ _current_upload.abort(); $scope.pdfLoading = false; $scope.removePDF(); }; $scope.removePDF = function(){ $scope.myform.pdf = null; $scope.myform.isGenerated = false; $scope.myform.autofillPDFs = false; console.log('form.pdf: '+$scope.myform.pdf+' REMOVED'); }; $scope.uploadPDF = function(files) { if (files && files.length) { // for (var i = 0; i < files.length; i++) { var file = files[0]; _current_upload = Upload.upload({ url: '/upload/pdf', fields: { 'user': $scope.user, 'form': $scope.myform }, file: file }).progress(function (evt) { var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); $scope.log = 'progress: ' + progressPercentage + '% ' + evt.config.file.name + '\n' + $scope.log; $scope.pdfLoading = true; }).success(function (data, status, headers, config) { $scope.log = 'file ' + data.originalname + ' uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log; console.log($scope.myform.pdf); $scope.myform.pdf = angular.fromJson(angular.toJson(data)); $scope.pdfLoading = false; console.log($scope.log); console.log('$scope.pdf: '+$scope.myform.pdf.name); if(!$scope.$$phase){ $scope.$apply(); } }).error(function(err){ $scope.pdfLoading = false; console.log('Error occured during upload.\n'); console.log(err); }); // } } }; }, templateUrl: './modules/forms/views/directiveViews/form/configure-form.html', restrict: 'E', scope: { myform:'=', user:'=', pdfFields:'@', formFields:'@' } }; } ]); 'use strict'; angular.module('forms').directive('editFormDirective', ['$rootScope', '$q', '$http', '$timeout', 'timeCounter', 'Auth', 'FormFields', function ($rootScope, $q, $http, $timeout, timeCounter, Auth, FormFields) { return { templateUrl: './modules/forms/views/directiveViews/form/edit-form.html', restrict: 'E', scope: { myform:'=', user:'=' }, controller: function($scope){ //Populate local scope with rootScope methods/variables $scope.update = $rootScope.update; //Populate AddField with all available form field types $scope.addField = {}; $scope.addField.types = FormFields.fields; $scope.addField.types.forEach(function(type){ type.lastAddedID = 1; return type; }); // accordion settings $scope.accordion = {}; $scope.accordion.oneAtATime = true; // Add a new field $scope.addNewField = function(fieldType){ // incr field_id counter $scope.addField.lastAddedID++; var fieldTitle; for(var i = 0; i < $scope.addField.types.length; i++){ // console.log($scope.addField.types[i].name === fieldType); if($scope.addField.types[i].name === fieldType){ $scope.addField.types[i].lastAddedID++; // console.log($scope.addField.types[i].lastAddedID); fieldTitle = $scope.addField.types[i].value+$scope.addField.types[i].lastAddedID; break; } } var newField = { 'title' : fieldTitle, 'fieldType' : fieldType, 'fieldValue' : '', 'required' : true, 'disabled' : false }; // put newField into fields array $scope.myform.form_fields.unshift(newField); console.log('\n\n---------\nAdded field CLIENT'); console.log(Date.now()); // console.log($scope.myform.form_fields.length); }; // deletes particular field on button click $scope.deleteField = function (hashKey){ // console.log($scope.myform.form_fields); for(var i = 0; i < $scope.myform.form_fields.length; i++){ // console.log($scope.myform.form_fields[i].$$hashKey === hashKey); if($scope.myform.form_fields[i].$$hashKey === hashKey){ $scope.myform.form_fields.splice(i, 1); break; } } }; $scope.duplicateField = function (field, field_index){ for(var i = 0; i < $scope.myform.form_fields.length; i++){ if($scope.myform.form_fields[i].field_id === field.field_id){ $scope.addNewField($scope.myform.form_fields[i].fieldType); break; } } }; // add new option to the field $scope.addOption = function (field){ if(!field.field_options) field.field_options = []; var lastOptionID = 0; if(field.field_options[field.field_options.length-1]) lastOptionID = field.field_options[field.field_options.length-1].option_id; // new option's id var option_id = lastOptionID + 1; var newOption = { 'option_id' : option_id, 'option_title' : 'Option ' + option_id, 'option_value' : option_id }; // put new option into field_options array field.field_options.push(newOption); }; // delete particular option $scope.deleteOption = function (field, option){ for(var i = 0; i < field.field_options.length; i++){ if(field.field_options[i].option_id === option.option_id){ field.field_options.splice(i, 1); break; } } }; // decides whether field options block will be shown (true for dropdown and radio fields) $scope.showAddOptions = function (field){ if(field.fieldType == 'dropdown' || field.fieldType == 'checkbox' || field.fieldType == 'scale' || field.fieldType == 'rating' || field.fieldType == 'radio') return true; else return false; }; }, }; } ]); 'use strict'; angular.module('forms').directive('fieldIconDirective', function($http, $compile) { return { templateUrl: './modules/forms/views/directiveViews/form/fieldIcon.html', restrict: 'E', scope: { typeName: '@' }, controller: function($scope){ var iconTypeMap = { "textfield": "fa fa-pencil-square-o", "dropdown": "fa fa-th-list", "date": "fa fa-calendar", "checkbox": "fa fa-check-square-o", "radio": "fa fa-dot-circle-o", "email": "fa fa-envelope-o", "textarea": "fa fa-pencil-square", "legal": "fa fa-legal", "file": "fa fa-cloud-upload", "rating": "fa fa-star-half-o", "link": "fa fa-link", "scale": "fa fa-sliders", "stripe": "fa fa-credit-card", "statement": "fa fa-quote-left", } $scope.typeIcon = iconTypeMap[$scope.typeName]; }, }; }); 'use strict'; // coffeescript's for in loop var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; angular.module('forms').directive('fieldDirective', function($http, $compile) { var getTemplateUrl = function(field) { var type = field.fieldType; var templateUrl = './modules/forms/views/directiveViews/field/'; var supported_fields = [ 'textfield', 'email', 'textarea', 'checkbox', 'date', 'dropdown', 'hidden', 'password', 'radio' ]; if (__indexOf.call(supported_fields, type) >= 0) { return templateUrl += type + '.html'; } }; var linker = function(scope, element) { scope.field.required = scope.required; //Set format only if field is a date if(scope.field.fieldType === 'date'){ $scope.dateOptions = { changeYear: true, changeMonth: true, altFormat: "mm/dd/yyyy", yearRange: '1900:-0', defaultDate: 0, }; } // GET template content from path var templateUrl = getTemplateUrl(scope.field); $http.get(templateUrl).success(function(data) { element.html(data); $compile(element.contents())(scope); }); }; return { template: '