-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-awesome-error.js
86 lines (74 loc) · 2.79 KB
/
angular-awesome-error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* jshint -W069 */
/*
* Template for error/success message,
* Show/hide error message when required,
* Show/hide success message when required.
*/
(function(window, angular, undefined) {
'use strict';
angular.module('ngAngularError', ['ng'])
.factory('list', function($http) {
/* errors_list.json should contain a map of errors and user-feedback.
* we use, $http to retrieve values from file errors_list.json,
* errorsList factory returns a promise. */
return $http.get('errors_list.json');
})
.directive('awesomeError', function (list) {
return {
restrict: 'E',
replace: true,
scope: {
target: '='
},
template: '<div> \
<table ng-show="showErr" class="alert alert-error fade in" style="width:100%;"><tr><td> \
<i class="fa fa-warning"></i> {{showMessage}} \
</td></td></table> \
<table ng-show="showSucc" class="alert fade in alert-success" style="width:100%;"><tr><td> \
<i class="fa fa-check-circle"></i> {{showMessage}} \
</td></tr></table> \
</div>',
link: function(scope) {
scope.messageMap = {};
var _showMsg = function(val, args, target, str) {
if (scope.target === target) {
if (!str) {
str = 'Undefined';
}
scope.showMessage = scope.messageMap[str];
/* Handle case where we are missing an error in errors-list.json. */
if (scope.showMessage === undefined) {
console.log('User-feedback message not defined: ' + str);
scope.showMessage = scope.messageMap['Undefined'];
}
if (args.name.indexOf('error') !== -1) {
scope.showErr = val;
} else {
scope.showSucc = val;
}
}
};
/* fetching data present in 'errors-list.json' file */
list.success(function(data) {
scope.messageMap = data;
});
scope.$on('show error', function (args, target, err) {
_showMsg(true, args, target, err);
});
scope.$on('hide all', function(args, target) {
scope.showErr = false;
scope.showSucc = false;
});
scope.$on('hide error', function (args, target) {
_showMsg(false, args, target);
});
scope.$on('show success', function (args, target, succ) {
_showMsg(true, args, target, succ);
});
scope.$on('hide success', function (args, target) {
_showMsg(false, args, target);
});
}
};
});
})(window, window.angular);