-
Notifications
You must be signed in to change notification settings - Fork 176
/
provider.js
318 lines (285 loc) · 8.11 KB
/
provider.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(function() {
angular
.module('validation.provider')
.provider('$validation', Provider);
function Provider() {
var $injector;
var $scope;
var $http;
var $q;
var $timeout;
var _this = this;
/**
* Setup the provider
* @param injector
*/
var setup = function(injector) {
$injector = injector;
$scope = $injector.get('$rootScope');
$http = $injector.get('$http');
$q = $injector.get('$q');
$timeout = $injector.get('$timeout');
};
/**
* Define validation type RegExp
* @type {{}}
*/
var expression = {};
/**
* default valid method
* @type {{}}
*/
var validMethod = null;
/**
* default error, success message
* @type {{}}
*/
var defaultMsg = {};
/**
* Allow user to set a custom Expression, do remember set the default message using setDefaultMsg
* @param obj
* @returns {*}
*/
this.setExpression = function(obj) {
angular.extend(expression, obj);
return _this;
};
/**
* Get the Expression
* @param exprs
* @returns {*}
*/
this.getExpression = function(exprs) {
return expression[exprs];
};
/**
* Allow user to set default message
* @param obj
* @returns {*}
*/
this.setDefaultMsg = function(obj) {
angular.extend(defaultMsg, obj);
return _this;
};
/**
* Get the Default Message
* @param msg
* @returns {*}
*/
this.getDefaultMsg = function(msg) {
return defaultMsg[msg];
};
/**
* allow user to set the global valid method
* @param v
* @returns {*}
*/
this.setValidMethod = function(v) {
validMethod = v;
};
/**
* Get the valid method
* @returns {*}
*/
this.getValidMethod = function() {
return validMethod;
};
/**
* Override the errorHTML function
* @param func
* @returns {*}
*/
this.setErrorHTML = function(func) {
if (func.constructor !== Function) {
return;
}
_this.getErrorHTML = func;
return _this;
};
/**
* Invalid message HTML, here's the default
* @param message
* @returns {string}
*/
this.getErrorHTML = function(message) {
return '<p class="validation-invalid">' + message + '</p>';
};
/**
* Override the successHTML function
* @param func
* @returns {*}
*/
this.setSuccessHTML = function(func) {
if (func.constructor !== Function) {
return;
}
_this.getSuccessHTML = func;
return _this;
};
/**
* Valid message HTML, here's the default
* @param message
* @returns {string}
*/
this.getSuccessHTML = function(message) {
return '<p class="validation-valid">' + message + '</p>';
};
/**
* Whether show the validation success message
* You can easily change this to false in your config
* example: $validationProvider.showSuccessMessage = false;
* @type {boolean}
*/
this.showSuccessMessage = true;
/**
* Whether show the validation error message
* You can easily change this to false in your config
* example: $validationProvider.showErrorMessage = false;
* @type {boolean}
*/
this.showErrorMessage = true;
/**
* Check form valid, return true
* checkValid(Form): Check the specific form(Form) valid from angular `$valid`
* @param form
* @returns {boolean}
*/
this.checkValid = function(form) {
return !!(form && form.$valid);
};
/**
* Validate the form when click submit, when `validMethod = submit`
* @param form
* @returns {promise|*}
*/
this.validate = function(form) {
var deferred = $q.defer();
var idx = 0;
if (form === undefined) {
console.error('This is not a regular Form name scope');
deferred.reject('This is not a regular Form name scope');
return deferred.promise;
}
if (form.validationId) { // single
$scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++);
} else if (form.constructor === Array) { // multiple
for (var k in form) {
$scope.$broadcast(form[k].$name + 'submit-' + form[k].validationId, idx++);
}
} else {
for (var i in form) { // whole scope
if (i[0] !== '$' && form[i].hasOwnProperty('$dirty')) {
$scope.$broadcast(i + 'submit-' + form[i].validationId, idx++);
}
}
}
deferred.promise.success = function(fn) {
deferred.promise.then(function(value) {
fn(value);
});
return deferred.promise;
};
deferred.promise.error = function(fn) {
deferred.promise.then(null, function(value) {
fn(value);
});
return deferred.promise;
};
$timeout(function() {
if (_this.checkValid(form)) {
deferred.resolve('success');
} else {
deferred.reject('error');
}
});
return deferred.promise;
};
/**
* Do this function if validation valid
* @param element
*/
this.validCallback = null;
/**
* Do this function if validation invalid
* @param element
*/
this.invalidCallback = null;
/**
* Do this function when reset is performed
* @param element
*/
this.resetCallback = null;
/**
* reset the specific form
* @param form
*/
this.reset = function(form) {
if (form === undefined) {
console.error('This is not a regular Form name scope');
return;
}
if (form.validationId) {
$scope.$broadcast(form.$name + 'reset-' + form.validationId);
} else if (form.constructor === Array) {
for (var k in form) {
$scope.$broadcast(form[k].$name + 'reset-' + form[k].validationId);
}
} else {
for (var i in form) {
if (i[0] !== '$' && form[i].hasOwnProperty('$dirty')) {
$scope.$broadcast(i + 'reset-' + form[i].validationId);
}
}
}
};
/**
* Add Message Element in config phase
* When you need custom your messageElement
* NODE: this funtion & and `message-id` attribute, have similar purpose.
* This function will help you add your `messageElement` automatically instead of pre-defined.
* @param element
*/
this.addMsgElement = function(element) {
return element.after('<span></span>');
};
/**
* Add Message Element in config phase
* When you need custom your messageElement
* NODE: this funtion & and `message-id` attribute, have similar purpose.
* This function will help you add your `messageElement` automatically instead of pre-defined.
* @param element
*/
this.getMsgElement = function(element) {
return element.next();
};
/**
* $get
* @returns {{setErrorHTML: *, getErrorHTML: Function, setSuccessHTML: *, getSuccessHTML: Function, setExpression: *, getExpression: Function, setDefaultMsg: *, getDefaultMsg: Function, checkValid: Function, validate: Function, reset: Function}}
*/
this.$get = ['$injector', function($injector) {
setup($injector);
return {
setValidMethod: this.setValidMethod,
getValidMethod: this.getValidMethod,
setErrorHTML: this.setErrorHTML,
getErrorHTML: this.getErrorHTML,
setSuccessHTML: this.setSuccessHTML,
getSuccessHTML: this.getSuccessHTML,
setExpression: this.setExpression,
getExpression: this.getExpression,
setDefaultMsg: this.setDefaultMsg,
getDefaultMsg: this.getDefaultMsg,
showSuccessMessage: this.showSuccessMessage,
showErrorMessage: this.showErrorMessage,
checkValid: this.checkValid,
validate: this.validate,
validCallback: this.validCallback,
invalidCallback: this.invalidCallback,
resetCallback: this.resetCallback,
reset: this.reset,
addMsgElement: this.addMsgElement,
getMsgElement: this.getMsgElement
};
}];
}
}).call(this);