This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-toolqit.js
153 lines (125 loc) · 3.62 KB
/
ng-toolqit.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
/*
* ng-toolqit
* https://github.com/matyax/ng-toolqit
* Version: 1.1.2
* License: MIT
*/
(function(window, angular, undefined) {'use strict';
angular.module('ngToolqit', ['ng']).
/**
* @ngdoc filter
* @name range
*
* @description
* Creates a range of `quantity´ elements for an ng-repeat.
*/
filter('range', function () {
return function (range, quantity) {
if (typeof range !== 'object') {
throw new Error('Filter input must be an empty array.');
}
quantity = parseInt(quantity);
for (var i = 0; i < quantity; i++) {
range.push(i);
}
return range;
};
});
angular.module('ngToolqit').
/**
* @ngdoc directive
* @name format
*
* @description
* Formats the input of an ngModel according to the given format rules.
*/
directive('format', ['$browser', function ($browser) {
var lastInput = null;
return {
require: 'ngModel',
link: link
};
function link(scope, element, attr, ngModel) {
if (! attr.formatRules) {
throw new Error('Missing format rules.');
}
ngModel.$formatters.push(function (value) {
return applyFormatRules(value, attr.formatRules);
});
ngModel.$parsers.push(function (value) {
value = applyFormatRules(value, attr.formatRules);
element.val(value);
return value;
});
var listener = function () {
element.val(
applyFormatRules(element.val(), attr.formatRules)
);
};
element.bind('change', listener);
element.bind('keydown', function() {
$browser.defer(listener);
});
element.bind('cut paste', function() {
$browser.defer(listener);
});
}
/**
* Apply a set of rules to a given string.
*
* @param {string} input Input string
* @param {string} rules Rules
* @return {string} Transformed string
*
* @example
* Rules:
* 9: [0-9]
* a: [a-zA-Z]
* *: [a-zA-Z0-9]
*
* E.g. ssn: 999-99-9999
*/
function applyFormatRules(input, rules) {
if ((! input) || (input === lastInput)) {
return input;
}
if (typeof input !== 'string') {
input = input.toString();
}
var rulesHash = {
'9': '[0-9]',
'a': '[a-zA-Z]',
'*': '[a-zA-Z0-9]'
},
fullRegExp = rules, //Full pattern regular expression to test the entire input.
output = '';
for (var rule in rulesHash) {
if (rule === '*') {
rule = '\\*';
}
fullRegExp = fullRegExp.replace(new RegExp(rule, 'g'), rulesHash[rule]);
}
input = input.substring(0, rules.length); //Cut the string to 'rule' size
if (input.match(fullRegExp)) {
return input; //No need to keep testing the input
}
var inputChars = input.split(''),
ruleChars = rules.split('');
inputChars.filter(function (value, i) {
if (! rulesHash[ruleChars[i]]) { //Literal character, insert in output
output += ruleChars[i];
if (value === ruleChars[i]) {
return true;
}
}
if (value.match(rulesHash[ruleChars[i]])) {
output += value;
return true;
}
return false;
});
lastInput = output;
return output;
}
}]);
})(window, window.angular);