1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-22 19:23:52 +00:00

Form and field directives, form loading spinner

This commit is contained in:
Kyle Spearrin
2016-09-10 17:13:29 -04:00
parent 8716c50f81
commit d78dfac43c
11 changed files with 234 additions and 42 deletions

View File

@@ -0,0 +1,2 @@
angular
.module('bit.directives', []);

View File

@@ -0,0 +1,30 @@
angular
.module('bit.directives')
.directive('bitField', function () {
var linkFn = function (scope, element, attrs, ngModel) {
ngModel.$registerError = registerError;
ngModel.$validators.validate = validator;
function validator() {
ngModel.$setValidity('bit', true);
return true;
}
function registerError() {
ngModel.$setValidity('bit', false);
}
};
return {
require: 'ngModel',
restrict: 'A',
compile: function (elem, attrs) {
if (!attrs.name || attrs.name === '') {
throw 'bit-field element does not have a valid name attribute';
}
return linkFn;
}
};
});

View File

@@ -0,0 +1,35 @@
angular
.module('bit.directives')
.directive('bitForm', function ($rootScope, validationService) {
return {
require: 'form',
restrict: 'A',
link: function (scope, element, attrs, formCtrl) {
var watchPromise = attrs.bitForm || null;
if (watchPromise) {
scope.$watch(watchPromise, formSubmitted.bind(null, formCtrl, scope));
}
}
};
function formSubmitted(form, scope, promise) {
if (!promise || !promise.then) {
return;
}
// reset errors
form.$errors = null;
// start loading
form.$loading = true;
promise.then(function success(response) {
form.$loading = false;
}, function failure(reason) {
form.$loading = false;
validationService.addErrors(form, reason);
scope.$broadcast('show-errors-check-validity');
});
}
});