rallly/public/js/directives/form.directive.js
Luke Vella 1b20a11bba New Features
- added notifications
- added toggle switches
- added delete feature
- added email verification
2015-01-26 13:11:48 +01:00

114 lines
3.4 KiB
JavaScript

angular.module('rallly')
.service('FormHelper', function(){
this.emailRegexString = '^([\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4})?$';
this.emailRegex = new RegExp(this.emailRegexString);
this.prettyError = function(errors, field){
if (errors.required) return field + " is required";
if (errors.pattern) return field + " is invalid" ;
if (errors.maxlength) return field + " is too long";
return false;
}
})
.directive('userForm', function(FormHelper){
return {
scope : {
event : '=',
form : '='
},
templateUrl : 'templates/directives/eventForm/userForm.html',
link : function(scope, el, attrs) {
scope.errors = {};
scope.emailRegex = FormHelper.emailRegex;
scope.$watchCollection('form.name.$error',function(errors){
scope.errors.name = FormHelper.prettyError(errors, "Name");
});
scope.$watchCollection('form.email.$error',function(errors){
scope.errors.email = FormHelper.prettyError(errors, "Email");
});
}
}
})
.directive('eventForm', function(FormHelper){
return {
scope : {
event : '=',
form : '='
},
templateUrl : 'templates/directives/eventForm/eventForm.html',
link : function(scope, el, attrs) {
scope.errors = {};
scope.$watchCollection('form.title.$error',function(errors){
scope.errors.title = FormHelper.prettyError(errors, "Title");
});
scope.$watchCollection('form.location.$error',function(errors){
scope.errors.location = FormHelper.prettyError(errors, "Location");
});
}
}
})
.directive('dateForm', function(){
return {
scope : {
event : '=',
form : '='
},
templateUrl : 'templates/directives/eventForm/dateForm.html'
}
})
.directive('participantsForm', function(FormHelper){
return {
scope : {
event : '=',
form : '='
},
templateUrl : 'templates/directives/eventForm/participantsForm.html',
link : function(scope, el, attrs){
scope.emailRegex = FormHelper.emailRegexString;
}
}
})
.directive('settingsForm', function(Event){
return {
scope : {
event : '=',
form : '='
},
templateUrl : 'templates/directives/eventForm/settingsForm.html',
link : function(scope, el, attrs){
scope.deleteEvent = function(){
Event.delete({'id' : scope.event._id}, function(){
console.log('delete');
});
}
}
}
})
.directive('switchToggle', function(){
return {
scope : {
model : '=ngModel'
},
require : 'ngModel',
link : function(scope, el, attrs, ngModel) {
el.addClass('switch-toggle');
var setClass = function(){
if (scope.model ^ typeof(attrs.invert) !== 'undefined'){
el.addClass('active');
} else {
el.removeClass('active');
}
}
scope.$watch('model', setClass);
el.bind('click', function(e){
scope.model = !scope.model;
ngModel.$setViewValue(scope.model, e);
});
}
}
});