2015-07-14 20:50:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Init the application configuration module for AngularJS application
|
|
|
|
var ApplicationConfiguration = (function() {
|
|
|
|
// Init module configuration options
|
|
|
|
var applicationModuleName = 'medform';
|
2015-07-28 22:29:07 +00:00
|
|
|
var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.utils', 'ngRaven', 'cgBusy'];
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
// 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('!');
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
//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',
|
|
|
|
});
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
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) {
|
|
|
|
$state.previous = fromState;
|
|
|
|
|
2015-07-28 22:44:06 +00:00
|
|
|
//Redirect to listForms if user is authenticated
|
|
|
|
if(toState.name === 'home' || toState.name === 'signin' || toState.name === 'resendVerifyEmail' || toState.name === 'verify' || toState.name === 'signup' || toState.name === 'signup-success'){
|
|
|
|
if(Auth.isAuthenticated()){
|
|
|
|
event.preventDefault(); // stop current execution
|
|
|
|
$state.go('listForms'); // go to listForms page
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Redirect to 'home' route if user is not authenticated
|
|
|
|
else if(toState.name !== 'access_denied' && !Auth.isAuthenticated() ){
|
|
|
|
event.preventDefault(); // stop current execution
|
|
|
|
$state.go('home'); // go to listForms page
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
});
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
|
|
|
]);
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
//Page access/authorization logic
|
2015-07-21 23:25:45 +00:00
|
|
|
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;
|
2015-07-27 21:40:00 +00:00
|
|
|
permissions = next && next.data && next.data.permissions ? next.data.permissions : null;
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
Auth.ensureHasCurrentUser(User);
|
2015-07-21 23:25:45 +00:00
|
|
|
user = Auth.currentUser;
|
|
|
|
|
2015-07-28 22:44:06 +00:00
|
|
|
if(user){
|
|
|
|
authenticator = new Authorizer(user);
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-28 22:44:06 +00:00
|
|
|
if( (permissions !== null) && !authenticator.canAccess(permissions) ){
|
|
|
|
event.preventDefault();
|
|
|
|
console.log('access denied')
|
|
|
|
$state.go('access_denied');
|
|
|
|
}
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
});
|
|
|
|
}]);
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
//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
|
2015-07-29 17:53:22 +00:00
|
|
|
ApplicationConfiguration.registerModule('forms', ['ngFileUpload', 'ui.date', 'ui.sortable', 'angular-input-stars', 'users']);
|
2015-07-14 20:50:29 +00:00
|
|
|
'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;
|
2015-07-28 22:44:06 +00:00
|
|
|
// if(!$scope.user || !$scope.user.username){
|
|
|
|
// $scope.user = $rootScope.user = User.getCurrent();
|
|
|
|
// $scope.authentication.currentUser = $rootScope.authentication.currentUser = $scope.user;
|
|
|
|
// }
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
$rootScope.languages = $scope.languages = ['english', 'french', 'spanish'];
|
|
|
|
|
|
|
|
$scope.isCollapsed = false;
|
2015-07-28 22:29:07 +00:00
|
|
|
$rootScope.hideNav = false;
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.menu = Menus.getMenu('topbar');
|
|
|
|
|
|
|
|
$scope.signout = function() {
|
|
|
|
var promise = User.logout();
|
|
|
|
promise.then(function() {
|
|
|
|
Auth.logout();
|
2015-07-28 22:44:06 +00:00
|
|
|
Auth.ensureHasCurrentUser(User);
|
|
|
|
$scope.user = $rootScope.user = null;
|
2015-07-14 20:50:29 +00:00
|
|
|
$state.go('home');
|
2015-07-28 22:44:06 +00:00
|
|
|
},
|
2015-07-14 20:50:29 +00:00
|
|
|
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;
|
2015-07-28 22:29:07 +00:00
|
|
|
$rootScope.hideNav = false;
|
2015-07-14 20:50:29 +00:00
|
|
|
if ( angular.isDefined( toState.data ) ) {
|
|
|
|
|
|
|
|
if ( angular.isDefined( toState.data.hideNav ) ) {
|
2015-07-28 22:29:07 +00:00
|
|
|
$rootScope.hideNav = toState.data.hideNav;
|
2015-07-14 20:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
angular.module('core').controller('HomeController', ['$rootScope', '$scope', 'User', 'Auth', '$state',
|
|
|
|
function($rootScope, $scope, User, Auth, $state) {
|
|
|
|
$scope = $rootScope;
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'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';
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
// Configuring the Forms drop-down menus
|
2015-07-14 20:50:29 +00:00
|
|
|
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){
|
2015-07-27 21:40:00 +00:00
|
|
|
if(formObj && formObj.form_fields && formObj.visible_form_fields){
|
|
|
|
|
|
|
|
//get keys
|
|
|
|
var formKeys = Object.keys(formObj);
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
//we only care about things that don't start with $
|
|
|
|
var fieldKeys = formKeys.filter(function(key){
|
|
|
|
return key[0] !== '$';
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
var fields = formObj.form_fields;
|
|
|
|
// fieldKeys.map(function(key){
|
|
|
|
// return formObj[key];
|
|
|
|
// });
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
var valid_count = fields.filter(function(field){
|
|
|
|
if(typeof field === 'object'){
|
|
|
|
return !!(field.fieldValue);
|
|
|
|
}
|
|
|
|
}).length;
|
|
|
|
return valid_count - (formObj.form_fields.length - formObj.visible_form_fields.length);
|
|
|
|
}
|
|
|
|
return 0;
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
2015-07-27 21:40:00 +00:00
|
|
|
}).config(['$provide', function ($provide){
|
|
|
|
$provide.decorator('accordionDirective', function($delegate) {
|
|
|
|
var directive = $delegate[0];
|
|
|
|
directive.replace = true;
|
|
|
|
return $delegate;
|
|
|
|
});
|
|
|
|
}]);
|
2015-07-14 20:50:29 +00:00
|
|
|
'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',
|
2015-07-28 22:44:06 +00:00
|
|
|
data: {
|
|
|
|
permissions: [ 'editForm' ]
|
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
}).
|
|
|
|
state('viewForm', {
|
|
|
|
url: '/forms/:formId/admin',
|
|
|
|
templateUrl: 'modules/forms/views/view-form.client.view.html',
|
2015-07-27 21:40:00 +00:00
|
|
|
data: {
|
|
|
|
permissions: [ 'editForm' ]
|
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
}).
|
|
|
|
state('viewPublicForm', {
|
|
|
|
url: '/forms/:formId',
|
|
|
|
templateUrl: 'modules/forms/views/view-public-form.client.view.html',
|
|
|
|
data: {
|
|
|
|
hideNav: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
2015-07-30 00:22:53 +00:00
|
|
|
// Forms controller
|
|
|
|
angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http',
|
|
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) {
|
|
|
|
|
|
|
|
$scope = $rootScope;
|
|
|
|
$rootScope.showCreateModal = false;
|
|
|
|
|
|
|
|
// Return all user's Forms
|
|
|
|
$scope.findAll = function() {
|
2015-08-05 22:24:24 +00:00
|
|
|
Forms.query(function(_forms){
|
|
|
|
$scope.myforms = _forms;
|
|
|
|
});
|
2015-07-30 00:22:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//Modal functions
|
|
|
|
$scope.openCreateModal = function(){
|
|
|
|
if(!$rootScope.showCreateModal){
|
|
|
|
$rootScope.showCreateModal = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.closeCreateModal = function(){
|
|
|
|
if($rootScope.showCreateModal){
|
|
|
|
$rootScope.showCreateModal = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.setForm = function (form) {
|
|
|
|
$scope.myform = form;
|
|
|
|
};
|
2015-08-05 22:24:24 +00:00
|
|
|
$scope.goToWithId = function(route, id) {
|
|
|
|
$state.go(route, {'formId': id}, {reload: true});
|
|
|
|
};
|
2015-07-30 00:22:53 +00:00
|
|
|
|
|
|
|
// Create new Form
|
|
|
|
$scope.createNew = function(){
|
|
|
|
var form = {};
|
|
|
|
form.title = $scope.myform.name.$modelValue;
|
|
|
|
form.language = $scope.myform.language.$modelValue;
|
|
|
|
console.log(form);
|
|
|
|
$rootScope.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', data._id+'');
|
|
|
|
}).error(function(errorResponse){
|
|
|
|
console.log(errorResponse);
|
|
|
|
$scope.error = errorResponse.data.message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-08-05 22:24:24 +00:00
|
|
|
|
|
|
|
$scope.remove = function(form_id) {
|
|
|
|
|
|
|
|
console.log('Remove existing form');
|
|
|
|
|
|
|
|
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', {}, {reload: true});
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
2015-07-30 00:22:53 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
// Forms controller
|
2015-07-28 22:29:07 +00:00
|
|
|
angular.module('forms').controller('SubmitFormController', ['$scope', '$rootScope', '$stateParams', '$state', 'Forms', 'CurrentForm',
|
|
|
|
function($scope, $rootScope, $stateParams, $state, Forms, CurrentForm) {
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
Forms.get({
|
|
|
|
formId: $stateParams.formId
|
|
|
|
}).$promise.then(
|
|
|
|
//success
|
|
|
|
function(form){
|
|
|
|
$scope.form = form;
|
|
|
|
|
|
|
|
//Show navbar if form is not public AND user is loggedin
|
|
|
|
if(!$scope.form.isLive && $rootScope.authentication.isAuthenticated()){
|
|
|
|
$rootScope.hideNav = false;
|
|
|
|
}else if(!$scope.form.isLive){
|
|
|
|
$state.go('access_denied');
|
|
|
|
}else {
|
|
|
|
CurrentForm.setForm($scope.form);
|
|
|
|
}
|
|
|
|
console.log('$rootScope.hideNav: '+$rootScope.hideNav);
|
|
|
|
console.log('$scope.form.isLive: '+$scope.form.isLive);
|
|
|
|
},
|
|
|
|
//error
|
|
|
|
function( error ){
|
|
|
|
$scope.error = error.message;
|
|
|
|
console.log('ERROR: '+error.message);
|
|
|
|
$state.go('access_denied');
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Forms controller
|
2015-08-05 22:24:24 +00:00
|
|
|
angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$modal',
|
|
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $modal) {
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
var deleteModal;
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope = $rootScope;
|
2015-08-05 22:24:24 +00:00
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.myform = CurrentForm.getForm();
|
2015-07-28 22:44:06 +00:00
|
|
|
$rootScope.saveInProgress = false;
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.viewSubmissions = false;
|
|
|
|
$scope.table = {
|
2015-07-28 22:44:06 +00:00
|
|
|
masterChecker: false,
|
2015-07-14 20:50:29 +00:00
|
|
|
rows: []
|
|
|
|
};
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// Find a specific Form
|
|
|
|
$scope.findOne = function() {
|
|
|
|
$scope.myform = Forms.get({
|
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
|
|
|
CurrentForm.setForm($scope.myform);
|
|
|
|
};
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.setForm = function (form) {
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.myform = form;
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
2015-08-05 22:24:24 +00:00
|
|
|
$rootScope.resetForm = function(){
|
|
|
|
$scope.myform = Forms.get({
|
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
/*
|
|
|
|
* Table Functions
|
|
|
|
*/
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.isAtLeastOneChecked = function(){
|
2015-07-28 22:29:07 +00:00
|
|
|
// console.log('isAtLeastOneChecked');
|
2015-07-27 21:40:00 +00:00
|
|
|
for(var i=0; i<$scope.table.rows.length; i++){
|
|
|
|
if($scope.table.rows[i].selected) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.toggleAllCheckers = function(){
|
2015-07-28 22:29:07 +00:00
|
|
|
// console.log('toggleAllCheckers');
|
2015-07-14 20:50:29 +00:00
|
|
|
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();
|
2015-07-21 23:25:45 +00:00
|
|
|
};
|
|
|
|
$scope.rowClicked = function(obj) {
|
2015-07-14 20:50:29 +00:00
|
|
|
obj.selected = !obj.selected;
|
2015-07-21 23:25:45 +00:00
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
/*
|
|
|
|
* Form Submission Methods
|
|
|
|
*/
|
|
|
|
//Delete selected submissions of Form
|
|
|
|
$scope.deleteSelectedSubmissions = function(){
|
|
|
|
// console.log('deleteSelectedSubmissions');
|
|
|
|
var delete_ids = _.chain($scope.table.rows).filter(function(row){
|
|
|
|
return !!row.selected;
|
|
|
|
}).pluck('_id').value();
|
|
|
|
console.log(delete_ids);
|
|
|
|
|
|
|
|
$http({ url: '/forms/'+$scope.myform._id+'/submissions',
|
|
|
|
method: 'DELETE',
|
|
|
|
data: {deleted_submissions: delete_ids},
|
|
|
|
headers: {"Content-Type": "application/json;charset=utf-8"}
|
|
|
|
}).success(function(data, status, headers){
|
|
|
|
//Remove deleted ids from table
|
|
|
|
for(var i=0; i<$scope.table.rows.length; i++){
|
|
|
|
if($scope.table.rows[i].selected){
|
|
|
|
$scope.table.rows.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function(err){
|
|
|
|
console.log('Could not delete form submissions.\nError: ');
|
|
|
|
console.log(err);
|
|
|
|
console.error = err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
//Fetch and display submissions of Form
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.showSubmissions = function(){
|
|
|
|
$scope.viewSubmissions = true;
|
2015-07-27 21:40:00 +00:00
|
|
|
|
|
|
|
$http.get('/forms/'+$scope.myform._id+'/submissions')
|
|
|
|
.success(function(data, status, headers){
|
|
|
|
// console.log(data[0].form_fields);
|
|
|
|
|
|
|
|
var _data = [];
|
|
|
|
for(var i=0; i<data.length; i++){
|
|
|
|
|
|
|
|
var _tmpSubFormFields = JSON.parse(JSON.stringify($scope.myform.form_fields));
|
|
|
|
|
|
|
|
for(var x=0; x<_tmpSubFormFields.length; x++){
|
|
|
|
|
|
|
|
var currField__id = _tmpSubFormFields[x]._id,
|
|
|
|
currField;
|
|
|
|
|
|
|
|
_.find(data[i].form_fields, function(fieldItem, fieldIdx){
|
|
|
|
if(fieldItem._id === currField__id){
|
|
|
|
currField = fieldItem;
|
|
|
|
// console.log(fieldItem.fieldValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(currField !== undefined){
|
|
|
|
_tmpSubFormFields[x].fieldValue = currField.fieldValue;
|
|
|
|
_tmpSubFormFields[x].$$hashKey = currField.$$hashKey;
|
|
|
|
}else {
|
|
|
|
_tmpSubFormFields[x].fieldValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_data[i] = data[i];
|
|
|
|
_data[i].form_fields = _tmpSubFormFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log(JSON.stringify(_data));
|
|
|
|
$scope.submissions = _data;
|
|
|
|
$scope.table.rows = _data;
|
|
|
|
if(!$scope.$$phase && !$scope.$digest){
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
// console.log('form submissions successfully fetched');
|
|
|
|
// console.log( JSON.parse(JSON.stringify($scope.submissions)) ) ;
|
|
|
|
// console.log( JSON.parse(JSON.stringify($scope.myform.form_fields)) );
|
|
|
|
})
|
|
|
|
.error(function(err){
|
|
|
|
console.log('Could not fetch form submissions.\nError: '+err);
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
//hide submissions of Form
|
|
|
|
$scope.hideSubmissions = function(){
|
|
|
|
$scope.viewSubmissions = false;
|
|
|
|
};
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
/*
|
|
|
|
** DeleteModal Functions
|
|
|
|
*/
|
|
|
|
$scope.openDeleteModal = function() {
|
|
|
|
|
|
|
|
deleteModal = $modal.open({
|
|
|
|
animation: $scope.animationsEnabled,
|
|
|
|
templateUrl: 'myModalContent.html',
|
|
|
|
controller: 'ViewFormController',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
$scope.cancelDeleteModal = function(){
|
|
|
|
if(deleteModal){
|
|
|
|
deleteModal.dismiss('cancel');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
// Remove existing Form
|
|
|
|
$scope.remove = function(form_id) {
|
2015-08-05 22:24:24 +00:00
|
|
|
if(deleteModal && deleteModal.opened){
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
deleteModal.close();
|
|
|
|
|
|
|
|
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');
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
if(!form_id){
|
|
|
|
$state.go('listForms', {}, {reload: true});
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}).finally(function(){
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// Update existing Form
|
|
|
|
$scope.update = $rootScope.update = function(cb) {
|
2015-07-28 22:44:06 +00:00
|
|
|
if(!$rootScope.saveInProgress && $rootScope.finishedRender){
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
$rootScope.saveInProgress = true;
|
2015-07-28 22:29:07 +00:00
|
|
|
console.log('begin updating form');
|
2015-07-21 23:25:45 +00:00
|
|
|
var err = null;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
$scope.updatePromise = $http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
2015-07-21 23:25:45 +00:00
|
|
|
.then(function(response){
|
2015-07-27 21:40:00 +00:00
|
|
|
$rootScope.myform = $scope.myform = response.data;
|
|
|
|
console.log(response.data);
|
|
|
|
if(!$scope.$digest){
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
}).catch(function(response){
|
|
|
|
console.log('Error occured during form UPDATE.\n');
|
|
|
|
console.log(response.data);
|
|
|
|
err = response.data;
|
|
|
|
}).finally(function() {
|
2015-07-28 22:29:07 +00:00
|
|
|
console.log('finished updating');
|
2015-07-27 21:40:00 +00:00
|
|
|
$rootScope.saveInProgress = false;
|
2015-07-21 23:25:45 +00:00
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', function($rootScope, $timeout) {
|
|
|
|
|
|
|
|
return {
|
2015-08-05 22:24:24 +00:00
|
|
|
// require: ['^form'],
|
|
|
|
restrict: 'AE',
|
2015-07-14 20:50:29 +00:00
|
|
|
link: function($scope, $element, $attrs, $ctrls) {
|
2015-08-05 22:24:24 +00:00
|
|
|
angular.element(document).ready(function() {
|
|
|
|
$rootScope.finishedRender = false;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:44:06 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
// Log that the directive has been linked.
|
|
|
|
// console.log( "Linked: autoSaveForm");
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
var $formCtrl = $scope.editForm,
|
|
|
|
savePromise = null;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
$scope.$on('editFormFieldsStarted', function(ngRepeatFinishedEvent) {
|
|
|
|
$rootScope.finishedRender = false;
|
|
|
|
});
|
|
|
|
$scope.$on('editFormFieldsFinished', function(ngRepeatFinishedEvent) {
|
|
|
|
$rootScope.finishedRender = true;
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
$scope.anyDirtyAndTouched = function (form){
|
|
|
|
for(var prop in form) {
|
|
|
|
if(form.hasOwnProperty(prop) && prop[0] !== '$') {
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
// console.log(form[prop]);
|
|
|
|
if(form[prop].$dirty && form[prop].$untouched) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
// console.log('properties of $scope.editForm');
|
|
|
|
// for(var item in $scope.editForm){
|
|
|
|
// console.log(item);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// console.log($scope.editForm);
|
|
|
|
// console.log($scope.watchModel);
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
//Autosave Form when model (specificed in $attrs.autoSaveWatch) changes
|
|
|
|
$scope.$watch($attrs.autoSaveWatch, function(newValue, oldValue) {
|
|
|
|
if( !newValue && !oldValue ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var changedFields = !_.isEqual(oldValue,newValue);
|
|
|
|
|
|
|
|
// console.log('\n\n----------');
|
|
|
|
// console.log('$dirty: '+ $formCtrl.$dirty );
|
|
|
|
// console.log('oldValue: '+oldValue);
|
|
|
|
// console.log('newValue: '+newValue);
|
|
|
|
// console.log('form_fields changed: '+changedFields);
|
|
|
|
// console.log('finishedRender: '+$rootScope.finishedRender);
|
|
|
|
// console.log('saveInProgress: '+$rootScope.saveInProgress);
|
|
|
|
// console.log($scope.editForm);
|
|
|
|
var inputDirtyUntouched = $scope.anyDirtyAndTouched($scope.editForm);
|
|
|
|
|
|
|
|
// console.log('properties of $scope.editForm');
|
|
|
|
// for(var item in $scope.editForm){
|
|
|
|
// console.log(item);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// console.log('inputDirtyUntouched: '+inputDirtyUntouched);
|
|
|
|
// console.log($scope.editForm);
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
//Save form ONLY IF rendering is finished, form_fields have been change AND currently not save in progress
|
|
|
|
if($rootScope.finishedRender && ($formCtrl.$dirty || changedFields) && !$rootScope.saveInProgress) {
|
|
|
|
|
|
|
|
console.log('Saving Form');
|
|
|
|
if(savePromise) {
|
|
|
|
$timeout.cancel(savePromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
savePromise = $timeout(function() {
|
|
|
|
savePromise = null;
|
|
|
|
|
|
|
|
$rootScope[$attrs.autoSaveCallback](
|
|
|
|
function(err){
|
|
|
|
if(!err){
|
|
|
|
console.log('\n\nForm data persisted -- setting pristine flag');
|
|
|
|
// console.log('\n\n---------\nUpdate form CLIENT');
|
|
|
|
// console.log(Date.now());
|
|
|
|
$formCtrl.$setPristine();
|
|
|
|
// $formCtrl.$setUntouched();
|
|
|
|
}else{
|
|
|
|
console.error('Error form data NOT persisted');
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
// }
|
|
|
|
}else{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, true);
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
});
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}]);
|
2015-08-05 22:24:24 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').directive('bnLifecycle',['$rootScope', function($rootScope) {
|
|
|
|
return {
|
|
|
|
// I link the DOM element to the view model.
|
|
|
|
restrict: 'A',
|
|
|
|
link: function( scope, element, attributes ) {
|
|
|
|
// Log that the directive has been linked.
|
|
|
|
console.log('aoeuaoeu');
|
|
|
|
// console.log( 'Linked:', attributes.id );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
'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) {
|
2015-07-27 21:40:00 +00:00
|
|
|
console.log('focusUp');
|
|
|
|
elem[0].previousElementSibling.find('input').focus();
|
|
|
|
}
|
|
|
|
scope.apply();
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
scope.focusDown = function(){
|
|
|
|
if(!scope.$last) {
|
|
|
|
elem[0].nextElementSibling.focus();
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
|
|
|
scope.apply();
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//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';
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
angular.module('forms').directive('configureFormDirective', ['$rootScope', '$http', 'Upload', '$timeout', 'timeCounter', 'Auth', 'FormFields',
|
|
|
|
function ($rootScope, $http, Upload, $timeout, timeCounter, Auth, FormFields) {
|
2015-07-14 20:50:29 +00:00
|
|
|
return {
|
2015-08-05 22:24:24 +00:00
|
|
|
templateUrl: './modules/forms/views/directiveViews/form/configure-form.html',
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
myform:'=',
|
|
|
|
user:'=',
|
|
|
|
pdfFields:'@',
|
|
|
|
formFields:'@'
|
|
|
|
},
|
2015-07-14 20:50:29 +00:00
|
|
|
controller: function($scope){
|
|
|
|
$scope.log = '';
|
|
|
|
$scope.pdfLoading = false;
|
|
|
|
$scope.languages = $rootScope.languages;
|
2015-07-27 21:40:00 +00:00
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
var _current_upload = null;
|
|
|
|
$scope.resetForm = $rootScope.resetForm;
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.update = $rootScope.update;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
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];
|
2015-07-27 21:40:00 +00:00
|
|
|
console.log(file);
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
_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;
|
|
|
|
$scope.myform.pdf = angular.fromJson(angular.toJson(data));
|
2015-07-27 21:40:00 +00:00
|
|
|
|
|
|
|
console.log($scope.myform.pdf);
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.pdfLoading = false;
|
|
|
|
|
|
|
|
console.log($scope.log);
|
2015-07-27 21:40:00 +00:00
|
|
|
if(!$scope.$$phase && !$scope.$digest){
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
}).error(function(err){
|
|
|
|
$scope.pdfLoading = false;
|
|
|
|
console.log('Error occured during upload.\n');
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
angular.module('forms')
|
|
|
|
.directive('editFormDirective', ['$rootScope', '$q', '$http', '$timeout', 'timeCounter', 'Auth', 'FormFields',
|
2015-07-14 20:50:29 +00:00
|
|
|
function ($rootScope, $q, $http, $timeout, timeCounter, Auth, FormFields) {
|
|
|
|
return {
|
|
|
|
templateUrl: './modules/forms/views/directiveViews/form/edit-form.html',
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
myform:'=',
|
|
|
|
},
|
2015-08-05 22:24:24 +00:00
|
|
|
// transclude: true,
|
2015-07-14 20:50:29 +00:00
|
|
|
controller: function($scope){
|
2015-08-05 22:24:24 +00:00
|
|
|
|
|
|
|
// Log that the directive has been linked.
|
|
|
|
// console.log( "Linked: editForm Controller");
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Initialize scope with variables
|
|
|
|
*/
|
|
|
|
//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;
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
//Populate local scope with rootScope methods/variables
|
|
|
|
$scope.update = $rootScope.update;
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
/*
|
|
|
|
** FormFields (ui-sortable) drag-and-drop configuration
|
|
|
|
*/
|
|
|
|
$scope.dropzone = {
|
|
|
|
handle: ' .handle'
|
|
|
|
}
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
// console.log($scope.myform);
|
2015-07-27 21:40:00 +00:00
|
|
|
|
|
|
|
// $scope.draggable = {
|
|
|
|
// connectWith: ".dropzone",
|
|
|
|
// start: function (e, ui) {
|
|
|
|
// // $scope.$apply(function() {
|
|
|
|
// // $scope.dragging = true
|
|
|
|
// // });
|
|
|
|
// $('.dropzone').sortable('refresh');
|
|
|
|
// },
|
|
|
|
// update: function (e, ui) {
|
|
|
|
// var isInDropzone = $(e.target).parentsUntil('.panel-group').hasClass('dropzone');
|
|
|
|
|
|
|
|
// console.log('isInDropzone: '+isInDropzone);
|
|
|
|
// //Disable drag and drop if we aren't in dropzone
|
|
|
|
// if(!isInDropzone){
|
|
|
|
// ui.item.sortable.cancel();
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// stop: function (e, ui) {
|
|
|
|
// var isInDropzone = $(e.target).parentsUntil('.panel-group').hasClass('dropzone');
|
|
|
|
|
|
|
|
// //Disable drag and drop if we aren't in dropzone
|
|
|
|
// if(isInDropzone){
|
|
|
|
// console.log($(e.target));
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // if (ui.item.sortable.droptarget === undefined) {
|
|
|
|
// // $scope.$apply($scope.dragging = false);
|
|
|
|
// // return;
|
|
|
|
// // }else if (ui.item.sortable.droptarget[0].classList[0] === "dropzone") {
|
|
|
|
// // // run code when item is dropped in the dropzone
|
|
|
|
// // $scope.$apply($scope.dragging = false);
|
|
|
|
// // }else{
|
|
|
|
// // // $scope.$apply($scope.dragging = false);
|
|
|
|
// // }
|
|
|
|
// // console.log('has class .dropzone :'+);
|
|
|
|
// // if ($(e.target).hasClass('dropzone') && ui.item.sortable.droptarget && e.target != ui.item.sortable.droptarget[0] ) {
|
|
|
|
// // // restore original types
|
|
|
|
// // $scope.addField.types = FormFields.fields;
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
/*
|
|
|
|
** Field CRUD Methods
|
|
|
|
*/
|
2015-07-14 20:50:29 +00:00
|
|
|
// 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);
|
2015-07-27 21:40:00 +00:00
|
|
|
if($scope.addField.types[i].name === fieldType){
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.addField.types[i].lastAddedID++;
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.addField.types[i].lastAddedID);
|
2015-07-14 20:50:29 +00:00
|
|
|
fieldTitle = $scope.addField.types[i].value+$scope.addField.types[i].lastAddedID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var newField = {
|
|
|
|
'title' : fieldTitle,
|
|
|
|
'fieldType' : fieldType,
|
|
|
|
'fieldValue' : '',
|
|
|
|
'required' : true,
|
2015-07-27 21:40:00 +00:00
|
|
|
'disabled' : false,
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// put newField into fields array
|
|
|
|
$scope.myform.form_fields.unshift(newField);
|
2015-07-27 21:40:00 +00:00
|
|
|
// console.log('\n\n---------\nAdded field CLIENT');
|
|
|
|
// console.log(Date.now());
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.myform.form_fields.length);
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// deletes particular field on button click
|
|
|
|
$scope.deleteField = function (hashKey){
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.myform.form_fields);
|
2015-07-14 20:50:29 +00:00
|
|
|
for(var i = 0; i < $scope.myform.form_fields.length; i++){
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.myform.form_fields[i].$$hashKey === hashKey);
|
2015-07-14 20:50:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
/*
|
|
|
|
** Field Option Methods
|
|
|
|
*/
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
// add new option to the field
|
|
|
|
$scope.addOption = function (field){
|
2015-07-27 21:40:00 +00:00
|
|
|
if(!field.fieldOptions) field.fieldOptions = [];
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
var lastOptionID = 0;
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
if(field.fieldOptions[field.fieldOptions.length-1])
|
|
|
|
lastOptionID = field.fieldOptions[field.fieldOptions.length-1].option_id;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
// new option's id
|
|
|
|
var option_id = lastOptionID + 1;
|
|
|
|
|
|
|
|
var newOption = {
|
|
|
|
'option_id' : option_id,
|
|
|
|
'option_title' : 'Option ' + option_id,
|
|
|
|
'option_value' : option_id
|
|
|
|
};
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
// put new option into fieldOptions array
|
|
|
|
field.fieldOptions.push(newOption);
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// delete particular option
|
|
|
|
$scope.deleteOption = function (field, option){
|
2015-07-27 21:40:00 +00:00
|
|
|
for(var i = 0; i < field.fieldOptions.length; i++){
|
|
|
|
if(field.fieldOptions[i].option_id === option.option_id){
|
|
|
|
field.fieldOptions.splice(i, 1);
|
2015-07-14 20:50:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// decides whether field options block will be shown (true for dropdown and radio fields)
|
|
|
|
$scope.showAddOptions = function (field){
|
2015-07-27 21:40:00 +00:00
|
|
|
if(field.fieldType === 'dropdown' || field.fieldType === 'checkbox' || field.fieldType === 'scale' || field.fieldType === 'rating' || field.fieldType === 'radio'){
|
2015-07-14 20:50:29 +00:00
|
|
|
return true;
|
2015-07-27 21:40:00 +00:00
|
|
|
} else {
|
2015-07-14 20:50:29 +00:00
|
|
|
return false;
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').directive('fieldIconDirective', function($http, $compile) {
|
|
|
|
|
|
|
|
return {
|
2015-08-05 22:24:24 +00:00
|
|
|
template: '<i class="{{typeIcon}}"></i>',
|
2015-07-14 20:50:29 +00:00
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
typeName: '@'
|
|
|
|
},
|
|
|
|
controller: function($scope){
|
|
|
|
var iconTypeMap = {
|
2015-07-27 21:40:00 +00:00
|
|
|
'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',
|
2015-08-05 22:24:24 +00:00
|
|
|
'yes_no': 'fa fa-toggle-on'
|
2015-07-14 20:50:29 +00:00
|
|
|
}
|
|
|
|
$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) {
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
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',
|
2015-07-27 21:40:00 +00:00
|
|
|
'radio',
|
2015-07-29 17:53:22 +00:00
|
|
|
'legal',
|
2015-07-30 00:22:53 +00:00
|
|
|
'statement',
|
|
|
|
'rating',
|
2015-08-05 22:24:24 +00:00
|
|
|
'yes_no',
|
|
|
|
'natural'
|
2015-07-14 20:50:29 +00:00
|
|
|
];
|
|
|
|
if (__indexOf.call(supported_fields, type) >= 0) {
|
|
|
|
return templateUrl += type + '.html';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var linker = function(scope, element) {
|
2015-07-21 23:25:45 +00:00
|
|
|
|
|
|
|
//Set format only if field is a date
|
|
|
|
if(scope.field.fieldType === 'date'){
|
2015-07-27 21:40:00 +00:00
|
|
|
scope.dateOptions = {
|
2015-07-21 23:25:45 +00:00
|
|
|
changeYear: true,
|
|
|
|
changeMonth: true,
|
|
|
|
altFormat: "mm/dd/yyyy",
|
|
|
|
yearRange: '1900:-0',
|
|
|
|
defaultDate: 0,
|
|
|
|
};
|
|
|
|
}
|
2015-08-05 22:24:24 +00:00
|
|
|
//Set only if we have a natural lang processing field
|
|
|
|
else if(scope.field.fieldType === 'natural'){
|
|
|
|
scope.field.fieldMatchValue = '';
|
|
|
|
|
|
|
|
//Fires when field is changed
|
|
|
|
scope.$watch('scope.field', function(newField, oldField) {
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
// 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: '<div>{{field.title}}</div>',
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
field: '=',
|
|
|
|
required: '&'
|
|
|
|
},
|
|
|
|
link: linker
|
|
|
|
};
|
|
|
|
});
|
|
|
|
'use strict';
|
|
|
|
angular.module('forms').directive('formLocator', function() {
|
|
|
|
return {
|
|
|
|
link: function(scope) {
|
|
|
|
scope.$emit('formLocator');
|
|
|
|
}
|
2015-07-27 21:40:00 +00:00
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
});
|
|
|
|
'use strict';
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
angular.module('forms').directive('onFinishRender', function ($rootScope, $timeout) {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2015-08-05 22:24:24 +00:00
|
|
|
link: function (scope, element, attrs) {
|
|
|
|
|
|
|
|
//Don't do anything if we don't have a ng-repeat on the current element
|
|
|
|
if(!element.attr('ng-repeat')){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var broadcastMessage = attrs.onFinishRender || 'ngRepeat';
|
|
|
|
|
|
|
|
|
|
|
|
if(scope.$first && !scope.$last) {
|
2015-07-27 21:40:00 +00:00
|
|
|
$timeout(function () {
|
2015-08-05 22:24:24 +00:00
|
|
|
// console.log(broadcastMessage+'Started');
|
|
|
|
$rootScope.$broadcast(broadcastMessage+'Started');
|
2015-07-28 22:44:06 +00:00
|
|
|
});
|
2015-08-05 22:24:24 +00:00
|
|
|
}else if(scope.$last) {
|
2015-07-27 21:40:00 +00:00
|
|
|
$timeout(function () {
|
2015-08-05 22:24:24 +00:00
|
|
|
element.ready(function () {
|
|
|
|
// console.log(broadcastMessage+'Finished');
|
|
|
|
$rootScope.$broadcast(broadcastMessage+'Finished');
|
|
|
|
});
|
2015-07-28 22:44:06 +00:00
|
|
|
});
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
angular.module('forms').directive('formDirective', ['$http', '$timeout', 'timeCounter', 'Auth', '$filter',
|
|
|
|
function ($http, $timeout, timeCounter, Auth, $filter) {
|
2015-07-14 20:50:29 +00:00
|
|
|
return {
|
2015-08-05 22:24:24 +00:00
|
|
|
templateUrl: './modules/forms/views/directiveViews/form/submit-form.html',
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
form:'='
|
|
|
|
},
|
2015-07-14 20:50:29 +00:00
|
|
|
controller: function($scope){
|
2015-08-05 22:24:24 +00:00
|
|
|
console.log('rendering submitFormDirective');
|
2015-07-14 20:50:29 +00:00
|
|
|
timeCounter.startClock();
|
|
|
|
|
|
|
|
$scope.submit = function(){
|
|
|
|
var _timeElapsed = timeCounter.stopClock();
|
|
|
|
$scope.form.timeElapsed = _timeElapsed;
|
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
$scope.form.percentageComplete = $filter('formValidity')($scope.form.visible_form_fields)/$scope.visible_form_fields.length;
|
|
|
|
delete $scope.form.visible_form_fields;
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
$scope.authentication = Auth;
|
2015-08-05 22:24:24 +00:00
|
|
|
|
|
|
|
$scope.submitPromise = $http.post('/forms/'+$scope.form._id,$scope.form)
|
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form submitted successfully');
|
|
|
|
// alert('Form submitted..');
|
|
|
|
$scope.form.submitted = true;
|
|
|
|
})
|
|
|
|
.error(function(error){
|
|
|
|
console.log(error);
|
|
|
|
$scope.error = error.message;
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
};
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.reloadForm = function(){
|
2015-08-05 22:24:24 +00:00
|
|
|
timeCounter.stopClock();
|
|
|
|
timeCounter.startClock();
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.form.submitted = false;
|
|
|
|
$scope.form.form_fields = _.chain($scope.form.form_fields).map(function(field){
|
|
|
|
field.fieldValue = '';
|
|
|
|
return field;
|
|
|
|
}).value();
|
2015-08-05 22:24:24 +00:00
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').directive('tableDirective', ['$http', '$timeout', 'Auth',
|
|
|
|
function ($http, $timeout, Auth) {
|
|
|
|
return {
|
|
|
|
controller: function($scope){
|
|
|
|
|
|
|
|
$scope.toggleChecker = function(checked) {
|
|
|
|
var rows = $scope.gridOptions.$gridScope.renderedRows,
|
|
|
|
allChecked = true;
|
|
|
|
|
|
|
|
for (var r = 0; r < rows.length; r++) {
|
|
|
|
if (rows[r].entity.checker !== true) {
|
|
|
|
allChecked = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.gridOptions.$gridScope.checker = allChecked;
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
templateUrl: './modules/forms/views/directiveViews/table/table.html',
|
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
|
|
|
rows:'=',
|
|
|
|
extras:'=',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//Forms service used for communicating with the forms REST endpoints
|
|
|
|
angular.module('forms').service('CurrentForm', ['Forms',
|
|
|
|
function(Forms){
|
|
|
|
|
|
|
|
//Private variables
|
|
|
|
var _form = {};
|
|
|
|
|
|
|
|
//Public Methods
|
|
|
|
this.getForm = function() {
|
|
|
|
return _form;
|
|
|
|
};
|
|
|
|
this.setForm = function(form) {
|
|
|
|
_form = form;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').service('FormFields', [
|
|
|
|
function() {
|
|
|
|
this.fields = [
|
|
|
|
{
|
|
|
|
name : 'textfield',
|
|
|
|
value : 'Short Text'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'email',
|
|
|
|
value : 'Email'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'radio',
|
|
|
|
value : 'Multiple Choice'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'dropdown',
|
2015-08-05 22:24:24 +00:00
|
|
|
value : 'Dropdown'
|
2015-07-14 20:50:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'date',
|
|
|
|
value : 'Date'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'textarea',
|
2015-08-05 22:24:24 +00:00
|
|
|
value : 'Paragraph Text'
|
2015-07-14 20:50:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'checkbox',
|
|
|
|
value : 'Checkbox'
|
|
|
|
},
|
2015-07-29 17:53:22 +00:00
|
|
|
{
|
2015-07-30 00:22:53 +00:00
|
|
|
name : 'yes_no',
|
2015-08-05 22:24:24 +00:00
|
|
|
value : 'Yes/No'
|
2015-07-29 17:53:22 +00:00
|
|
|
},
|
2015-07-14 20:50:29 +00:00
|
|
|
{
|
|
|
|
name : 'legal',
|
|
|
|
value : 'Legal'
|
|
|
|
},
|
2015-07-28 22:44:06 +00:00
|
|
|
// {
|
2015-08-05 22:24:24 +00:00
|
|
|
// name : 'sig',
|
|
|
|
// value : 'Signature'
|
|
|
|
// },
|
|
|
|
// {
|
2015-07-28 22:44:06 +00:00
|
|
|
// name : 'file',
|
|
|
|
// value : 'File Upload'
|
|
|
|
// },
|
2015-07-29 17:53:22 +00:00
|
|
|
{
|
|
|
|
name : 'rating',
|
|
|
|
value : 'Rating'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'link',
|
|
|
|
value : 'Link'
|
|
|
|
},
|
2015-07-28 22:44:06 +00:00
|
|
|
// {
|
|
|
|
// name : 'scale',
|
|
|
|
// value : 'Opinion Scale'
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// name : 'stripe',
|
|
|
|
// value : 'Payment'
|
|
|
|
// },
|
2015-07-14 20:50:29 +00:00
|
|
|
{
|
|
|
|
name : 'statement',
|
|
|
|
value : 'Statement'
|
|
|
|
},
|
2015-08-05 22:24:24 +00:00
|
|
|
{
|
|
|
|
name : 'natural',
|
|
|
|
value : 'Natural Language Input'
|
|
|
|
},
|
2015-07-14 20:50:29 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//Forms service used for communicating with the forms REST endpoints
|
|
|
|
angular.module('forms').factory('Forms', ['$resource',
|
|
|
|
function($resource) {
|
|
|
|
return $resource('forms/:formId', {
|
|
|
|
formId: '@_id'
|
|
|
|
}, {
|
|
|
|
'query' : {
|
|
|
|
method: 'GET',
|
|
|
|
isArray: true,
|
2015-07-27 21:40:00 +00:00
|
|
|
transformResponse: function(data, header) {
|
|
|
|
var forms = angular.fromJson(data);
|
|
|
|
angular.forEach(forms, function(form, idx) {
|
|
|
|
form.visible_form_fields = _.filter(form.form_fields, function(field){
|
|
|
|
return field.deletePreserved === false;
|
|
|
|
}); //<-- replace each item with an instance of the resource object
|
|
|
|
});
|
|
|
|
return forms;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'get' : {
|
|
|
|
method: 'GET',
|
|
|
|
transformResponse: function(data, header) {
|
|
|
|
var form = angular.fromJson(data);
|
|
|
|
|
|
|
|
form.visible_form_fields = _.filter(form.form_fields, function(field){
|
|
|
|
return field.deletePreserved === false;
|
|
|
|
}); //<-- replace each item with an instance of the resource object
|
|
|
|
return form;
|
|
|
|
}
|
2015-07-14 20:50:29 +00:00
|
|
|
},
|
|
|
|
'update': {
|
|
|
|
method: 'PUT'
|
|
|
|
},
|
|
|
|
'save': {
|
|
|
|
method: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//Submissions service used for communicating with the forms REST endpoints
|
|
|
|
angular.module('forms').factory('Submissions', ['$resource',
|
|
|
|
function($resource) {
|
|
|
|
return $resource('forms/:formID/submissions/:submissionId', {
|
|
|
|
submissionId: '@_id',
|
|
|
|
formId: '@_id'
|
|
|
|
}, {
|
|
|
|
'query' : {
|
|
|
|
method: 'GET',
|
|
|
|
isArray: true,
|
|
|
|
},
|
|
|
|
'update': {
|
|
|
|
method: 'PUT'
|
|
|
|
},
|
|
|
|
'save': {
|
|
|
|
method: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').service('timeCounter', [
|
|
|
|
function(){
|
|
|
|
var _startTime, _endTime, that=this;
|
|
|
|
|
|
|
|
this.timeSpent = 0;
|
|
|
|
|
|
|
|
this.startClock = function(){
|
|
|
|
_startTime = Date.now();
|
|
|
|
// console.log('Clock Started');
|
|
|
|
};
|
|
|
|
|
|
|
|
this.stopClock = function(){
|
|
|
|
_endTime = Date.now();
|
|
|
|
that.timeSpent = Math.abs(_endTime.valueOf() - _startTime.valueOf())/1000;
|
|
|
|
// console.log('Clock Ended');
|
|
|
|
return that.timeSpent;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Config HTTP Error Handling
|
|
|
|
angular.module('users').config(['$httpProvider',
|
|
|
|
function($httpProvider) {
|
|
|
|
$httpProvider.interceptors.push(function($q, $location) {
|
|
|
|
return {
|
|
|
|
responseError: function(response) {
|
2015-07-28 22:44:06 +00:00
|
|
|
// console.log($location.path());
|
2015-07-28 22:29:07 +00:00
|
|
|
if( response.config.url !== '/users/me' && $location.path() !== '/users/me' && response.config){
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
console.log('intercepted rejection of ', response.config.url, response.status);
|
|
|
|
if (response.status === 401) {
|
|
|
|
// save the current location so that login can redirect back
|
|
|
|
$location.nextAfterLogin = $location.path();
|
|
|
|
$location.path('/signin');
|
|
|
|
}else if(response.status === 403){
|
|
|
|
$location.path('/access_denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return $q.reject(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}]);
|
2015-07-27 21:40:00 +00:00
|
|
|
'use strict';
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
// Setting up route
|
|
|
|
angular.module('users').config(['$stateProvider',
|
|
|
|
function($stateProvider) {
|
|
|
|
|
|
|
|
var checkLoggedin = function($q, $timeout, $state, User, Auth) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
// console.log(Auth.ensureHasCurrentUser(User));
|
|
|
|
|
|
|
|
if (Auth.currentUser && Auth.currentUser.email) {
|
|
|
|
$timeout(deferred.resolve);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Auth.currentUser = User.getCurrent(function() {
|
|
|
|
Auth.login();
|
|
|
|
$timeout(deferred.resolve());
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
Auth.logout();
|
|
|
|
$timeout(deferred.reject());
|
|
|
|
$state.go('sigin', {reload: true});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Users state routing
|
|
|
|
$stateProvider.
|
|
|
|
state('profile', {
|
|
|
|
resolve: {
|
|
|
|
loggedin: checkLoggedin
|
|
|
|
},
|
|
|
|
url: '/settings/profile',
|
|
|
|
templateUrl: 'modules/users/views/settings/edit-profile.client.view.html'
|
|
|
|
}).
|
|
|
|
state('password', {
|
|
|
|
resolve: {
|
|
|
|
loggedin: checkLoggedin
|
|
|
|
},
|
|
|
|
url: '/settings/password',
|
|
|
|
templateUrl: 'modules/users/views/settings/change-password.client.view.html'
|
|
|
|
}).
|
|
|
|
state('accounts', {
|
|
|
|
resolve: {
|
|
|
|
loggedin: checkLoggedin
|
|
|
|
},
|
|
|
|
url: '/settings/accounts',
|
|
|
|
templateUrl: 'modules/users/views/settings/social-accounts.client.view.html'
|
|
|
|
}).
|
|
|
|
|
|
|
|
state('signup', {
|
|
|
|
url: '/signup',
|
|
|
|
templateUrl: 'modules/users/views/authentication/signup.client.view.html'
|
|
|
|
}).
|
|
|
|
state('signup-success', {
|
|
|
|
url: '/signup-success',
|
2015-07-21 23:25:45 +00:00
|
|
|
templateUrl: 'modules/users/views/authentication/signup-success.client.view.html'
|
2015-07-14 20:50:29 +00:00
|
|
|
}).
|
|
|
|
state('signin', {
|
|
|
|
url: '/signin',
|
|
|
|
templateUrl: 'modules/users/views/authentication/signin.client.view.html'
|
|
|
|
}).
|
|
|
|
state('access_denied', {
|
|
|
|
url: '/access_denied',
|
|
|
|
templateUrl: 'modules/users/views/authentication/access-denied.client.view.html'
|
|
|
|
}).
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
state('resendVerifyEmail', {
|
|
|
|
url: '/verify',
|
|
|
|
templateUrl: 'modules/users/views/verify/resend-verify-email.client.view.html'
|
|
|
|
}).
|
|
|
|
state('verify', {
|
|
|
|
url: '/verify/:token',
|
|
|
|
templateUrl: 'modules/users/views/verify/verify-account.client.view.html'
|
|
|
|
}).
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
state('forgot', {
|
|
|
|
url: '/password/forgot',
|
|
|
|
templateUrl: 'modules/users/views/password/forgot-password.client.view.html'
|
|
|
|
}).
|
|
|
|
state('reset-invalid', {
|
|
|
|
url: '/password/reset/invalid',
|
|
|
|
templateUrl: 'modules/users/views/password/reset-password-invalid.client.view.html'
|
|
|
|
}).
|
|
|
|
state('reset-success', {
|
|
|
|
url: '/password/reset/success',
|
|
|
|
templateUrl: 'modules/users/views/password/reset-password-success.client.view.html'
|
|
|
|
}).
|
|
|
|
state('reset', {
|
|
|
|
url: '/password/reset/:token',
|
|
|
|
templateUrl: 'modules/users/views/password/reset-password.client.view.html'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('users').controller('AuthenticationController', ['$scope', '$location', '$state', '$rootScope', 'User', 'Auth',
|
|
|
|
function($scope, $location, $state, $rootScope, User, Auth) {
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope = $rootScope;
|
|
|
|
$scope.credentials = {};
|
|
|
|
$scope.error = null;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.signin = function() {
|
2015-07-28 22:44:06 +00:00
|
|
|
User.login($scope.credentials).then(
|
2015-07-27 21:40:00 +00:00
|
|
|
function(response) {
|
2015-07-28 22:44:06 +00:00
|
|
|
console.log(response)
|
2015-07-27 21:40:00 +00:00
|
|
|
Auth.login(response);
|
|
|
|
$scope.user = $rootScope.user = Auth.ensureHasCurrentUser(User);
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
if($state.previous.name !== 'home' && $state.previous.name !== 'verify' && $state.previous.name !== ''){
|
2015-07-27 21:40:00 +00:00
|
|
|
$state.go($state.previous.name);
|
|
|
|
}else{
|
2015-07-28 22:44:06 +00:00
|
|
|
$state.go('listForms');
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
$rootScope.user = Auth.ensureHasCurrentUser(User);
|
|
|
|
$scope.user = $rootScope.user;
|
|
|
|
|
|
|
|
$scope.error = error;
|
|
|
|
console.log('loginError: '+error);
|
2015-07-14 20:50:29 +00:00
|
|
|
}
|
2015-07-27 21:40:00 +00:00
|
|
|
);
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
$scope.signup = function() {
|
|
|
|
User.signup($scope.credentials).then(
|
|
|
|
function(response) {
|
|
|
|
console.log('signup-success');
|
|
|
|
$state.go('signup-success');
|
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
if(error) {
|
|
|
|
$scope.error = error;
|
|
|
|
}else {
|
|
|
|
console.log('No response received');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'User',
|
|
|
|
function($scope, $stateParams, $state, User) {
|
|
|
|
|
|
|
|
//If user is signed in then redirect back home
|
2015-07-28 22:29:07 +00:00
|
|
|
// if ($scope.authentication.isAuthenticated()) $state.go('home');
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
// Submit forgotten password account id
|
|
|
|
$scope.askForPasswordReset = function() {
|
|
|
|
User.askForPasswordReset($scope.credentials).then(
|
|
|
|
function(response){
|
|
|
|
$scope.success = response.message;
|
|
|
|
$scope.credentials = null;
|
|
|
|
},
|
|
|
|
function(error){
|
|
|
|
$scope.error = error;
|
|
|
|
$scope.credentials = null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
// Change user password
|
|
|
|
$scope.resetUserPassword = function() {
|
|
|
|
$scope.success = $scope.error = null;
|
|
|
|
User.resetPassword($scope.passwordDetails, $stateParams.token).then(
|
|
|
|
function(response){
|
|
|
|
// If successful show success message and clear form
|
|
|
|
$scope.success = response.message;
|
|
|
|
$scope.passwordDetails = null;
|
2015-07-14 20:50:29 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
// And redirect to the index page
|
|
|
|
$state.go('reset-success');
|
|
|
|
},
|
|
|
|
function(error){
|
|
|
|
$scope.error = error.message || error;
|
|
|
|
$scope.passwordDetails = null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2015-07-14 20:50:29 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('users').controller('SettingsController', ['$scope', '$rootScope', '$http', '$state', 'Users',
|
|
|
|
function($scope, $rootScope, $http, $state, Users) {
|
|
|
|
$scope.user = $rootScope.user;
|
|
|
|
console.log($scope.user);
|
|
|
|
// If user is not signed in then redirect back home
|
|
|
|
if (!$scope.user) $state.go('home');
|
|
|
|
|
|
|
|
// Check if there are additional accounts
|
|
|
|
$scope.hasConnectedAdditionalSocialAccounts = function(provider) {
|
|
|
|
for (var i in $scope.user.additionalProvidersData) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if provider is already in use with current user
|
|
|
|
$scope.isConnectedSocialAccount = function(provider) {
|
|
|
|
return $scope.user.provider === provider || ($scope.user.additionalProvidersData && $scope.user.additionalProvidersData[provider]);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove a user social account
|
|
|
|
$scope.removeUserSocialAccount = function(provider) {
|
|
|
|
$scope.success = $scope.error = null;
|
|
|
|
|
|
|
|
$http.delete('/users/accounts', {
|
|
|
|
params: {
|
|
|
|
provider: provider
|
|
|
|
}
|
|
|
|
}).success(function(response) {
|
|
|
|
// If successful show success message and clear form
|
|
|
|
$scope.success = true;
|
|
|
|
$scope.user = response;
|
|
|
|
}).error(function(response) {
|
|
|
|
$scope.error = response.message;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update a user profile
|
|
|
|
$scope.updateUserProfile = function(isValid) {
|
|
|
|
if (isValid) {
|
|
|
|
$scope.success = $scope.error = null;
|
|
|
|
var user = new Users($scope.user);
|
|
|
|
|
|
|
|
user.$update(function(response) {
|
|
|
|
$scope.success = true;
|
|
|
|
$scope.user = response;
|
|
|
|
}, function(response) {
|
|
|
|
$scope.error = response.data.message;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$scope.submitted = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Change user password
|
|
|
|
$scope.changeUserPassword = function() {
|
|
|
|
$scope.success = $scope.error = null;
|
|
|
|
|
|
|
|
$http.post('/users/password', $scope.passwordDetails).success(function(response) {
|
|
|
|
// If successful show success message and clear form
|
|
|
|
$scope.success = true;
|
|
|
|
$scope.passwordDetails = null;
|
|
|
|
}).error(function(response) {
|
|
|
|
$scope.error = response.message;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
angular.module('users').controller('VerifyController', ['$scope', '$state', '$rootScope', 'User', 'Auth', '$stateParams',
|
|
|
|
function($scope, $state, $rootScope, User, Auth, $stateParams) {
|
|
|
|
|
|
|
|
$scope.isReset = false;
|
2015-07-29 17:53:22 +00:00
|
|
|
$scope.credentials = {};
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
|
|
|
|
// Submit forgotten password account id
|
|
|
|
$scope.resendVerifyEmail = function() {
|
2015-07-29 17:53:22 +00:00
|
|
|
console.log($scope.credentials.email);
|
|
|
|
User.resendVerifyEmail($scope.credentials.email).then(
|
2015-07-27 21:40:00 +00:00
|
|
|
function(response){
|
|
|
|
$scope.success = response.message;
|
|
|
|
$scope.credentials = null;
|
2015-07-28 22:29:07 +00:00
|
|
|
$scope.isResetSent = true;
|
2015-07-27 21:40:00 +00:00
|
|
|
},
|
|
|
|
function(error){
|
|
|
|
$scope.error = error;
|
|
|
|
$scope.credentials = null;
|
2015-07-28 22:29:07 +00:00
|
|
|
$scope.isReset = false;
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Validate Verification Token
|
|
|
|
$scope.validateVerifyToken = function() {
|
|
|
|
if($stateParams.token){
|
|
|
|
console.log($stateParams.token);
|
|
|
|
User.validateVerifyToken($stateParams.token).then(
|
|
|
|
function(response){
|
|
|
|
console.log('Success: '+response.message);
|
|
|
|
$scope.success = response.message;
|
|
|
|
$scope.isReset = true;
|
|
|
|
$scope.credentials = null;
|
|
|
|
},
|
|
|
|
function(error){
|
|
|
|
console.log('Error: '+error.message);
|
|
|
|
$scope.isReset = false;
|
|
|
|
$scope.error = error;
|
|
|
|
$scope.credentials = null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-07-29 18:34:00 +00:00
|
|
|
};
|
2015-07-27 21:40:00 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('users').factory('Auth', function($window) {
|
2015-07-14 20:50:29 +00:00
|
|
|
var userState =
|
|
|
|
{
|
|
|
|
isLoggedIn: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var service = {
|
|
|
|
currentUser: null,
|
|
|
|
|
|
|
|
// Note: we can't make the User a dependency of Auth
|
|
|
|
// because that would create a circular dependency
|
|
|
|
// Auth <- $http <- $resource <- LoopBackResource <- User <- Auth
|
|
|
|
ensureHasCurrentUser: function(User) {
|
2015-08-05 22:24:24 +00:00
|
|
|
if (service.currentUser && service.currentUser.username) {
|
2015-07-28 22:29:07 +00:00
|
|
|
console.log('Using local current user.');
|
2015-08-05 22:24:24 +00:00
|
|
|
// console.log(service.currentUser);
|
2015-07-14 20:50:29 +00:00
|
|
|
return service.currentUser;
|
|
|
|
}
|
|
|
|
else if ($window.user){
|
2015-07-28 22:29:07 +00:00
|
|
|
console.log('Using cached current user.');
|
2015-07-14 20:50:29 +00:00
|
|
|
// console.log($window.user);
|
|
|
|
service.currentUser = $window.user;
|
|
|
|
return service.currentUser;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log('Fetching current user from the server.');
|
|
|
|
User.getCurrent().then(function(user) {
|
|
|
|
// success
|
|
|
|
service.currentUser = user;
|
|
|
|
userState.isLoggedIn = true;
|
|
|
|
$window.user = service.currentUser;
|
|
|
|
return service.currentUser;
|
|
|
|
},
|
|
|
|
function(response) {
|
|
|
|
userState.isLoggedIn = false;
|
|
|
|
service.currentUser = null;
|
|
|
|
$window.user = null;
|
|
|
|
console.log('User.getCurrent() err', response);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
isAuthenticated: function() {
|
|
|
|
return !!service.currentUser;
|
|
|
|
},
|
|
|
|
|
|
|
|
getUserState: function() {
|
|
|
|
return userState;
|
|
|
|
},
|
|
|
|
|
|
|
|
login: function(new_user) {
|
|
|
|
userState.isLoggedIn = true;
|
|
|
|
service.currentUser = new_user;
|
|
|
|
},
|
|
|
|
|
|
|
|
logout: function() {
|
|
|
|
$window.user = null;
|
|
|
|
userState.isLoggedIn = false;
|
|
|
|
service.currentUser = null;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return service;
|
|
|
|
});
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-27 21:40:00 +00:00
|
|
|
angular.module('users').service('Authorizer', function(APP_PERMISSIONS, USER_ROLES) {
|
2015-07-21 23:25:45 +00:00
|
|
|
return function(user) {
|
|
|
|
return {
|
|
|
|
canAccess: function(permissions) {
|
|
|
|
var i, len, permission;
|
|
|
|
if (!angular.isArray(permissions)) {
|
|
|
|
permissions = [permissions];
|
|
|
|
}
|
|
|
|
for (i = 0, len = permissions.length; i < len; i++) {
|
|
|
|
permission = permissions[i];
|
|
|
|
if (APP_PERMISSIONS[permission] === null) {
|
|
|
|
throw 'Bad permission value';
|
|
|
|
}
|
2015-07-27 21:40:00 +00:00
|
|
|
if (user && user.roles) {
|
2015-07-21 23:25:45 +00:00
|
|
|
switch (permission) {
|
|
|
|
case APP_PERMISSIONS.viewAdminSettings:
|
|
|
|
case APP_PERMISSIONS.editAdminSettings:
|
2015-07-27 21:40:00 +00:00
|
|
|
return user.roles.indexOf(USER_ROLES.admin) > -1;
|
2015-07-21 23:25:45 +00:00
|
|
|
case APP_PERMISSIONS.viewPrivateForm:
|
|
|
|
case APP_PERMISSIONS.editForm:
|
2015-07-27 21:40:00 +00:00
|
|
|
return user.roles.indexOf(USER_ROLES.admin) > -1 || user.roles.indexOf(USER_ROLES.normal) > -1;
|
2015-07-21 23:25:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 21:40:00 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2015-07-14 20:50:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '$state',
|
|
|
|
function($window, $q, $timeout, $http, $state) {
|
|
|
|
|
|
|
|
|
|
|
|
var userService = {
|
|
|
|
getCurrent: function() {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
$http.get('/users/me')
|
|
|
|
.success(function(response) {
|
|
|
|
deferred.resolve(response);
|
|
|
|
})
|
|
|
|
.error(function() {
|
|
|
|
deferred.reject('User\'s session has expired');
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
login: function(credentials) {
|
|
|
|
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.post('/auth/signin', credentials).success(function(response) {
|
|
|
|
deferred.resolve(response);
|
|
|
|
}).error(function(error) {
|
|
|
|
|
|
|
|
deferred.reject(error.message || error);
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
logout: function() {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.get('/auth/signout').success(function(response) {
|
|
|
|
deferred.resolve(null);
|
|
|
|
}).error(function(error) {
|
|
|
|
deferred.reject(error.message || error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
signup: function(credentials) {
|
|
|
|
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
$http.post('/auth/signup', credentials).success(function(response) {
|
|
|
|
// If successful we assign the response to the global user model
|
|
|
|
deferred.resolve(response);
|
|
|
|
}).error(function(error) {
|
|
|
|
|
|
|
|
deferred.reject(error.message || error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2015-07-29 17:53:22 +00:00
|
|
|
resendVerifyEmail: function(_email) {
|
2015-07-27 21:40:00 +00:00
|
|
|
var deferred = $q.defer();
|
2015-07-29 17:53:22 +00:00
|
|
|
$http.post('/auth/verify/', {email: _email}).success(function(response) {
|
2015-07-27 21:40:00 +00:00
|
|
|
deferred.resolve(response);
|
|
|
|
}).error(function(error) {
|
|
|
|
deferred.reject(error.message || error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
validateVerifyToken: function(token) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.get('/auth/verify/'+token).success(function(response) {
|
|
|
|
deferred.resolve(response);
|
|
|
|
}).error(function(error) {
|
|
|
|
deferred.reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2015-07-14 20:50:29 +00:00
|
|
|
resetPassword: function(passwordDetails, token) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.get('/auth/password/'+token, passwordDetails).success(function(response) {
|
|
|
|
|
|
|
|
deferred.resolve();
|
|
|
|
}).error(function(error) {
|
|
|
|
deferred.reject(error.message || error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Submit forgotten password account id
|
|
|
|
askForPasswordReset: function(credentials) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.post('/auth/forgot', credentials).success(function(response) {
|
|
|
|
// Show user success message and clear form
|
|
|
|
|
|
|
|
deferred.resolve(response);
|
|
|
|
|
|
|
|
}).error(function(error) {
|
|
|
|
// Show user error message
|
|
|
|
deferred.reject(error.message || error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return userService;
|
|
|
|
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Users service used for communicating with the users REST endpoint
|
|
|
|
angular.module('users').factory('Users', ['$resource',
|
|
|
|
function($resource) {
|
|
|
|
return $resource('users', {}, {
|
|
|
|
update: {
|
|
|
|
method: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]);
|