bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
ngKeypadInput.js
(5510B)
1 /**
2 * @overview
3 * @copyright 2013 Tommy Rochette http://trochette.github.com/
4 * @author Tommy Rochette
5 * @version 0.0.1
6 *
7 * @license MIT
8 */
9 (function () {
10 "use strict";
11
12 /**
13 * @Class
14 */
15 window.KeypadInput = function ($scope, $element, $attrs, controller) {
16
17 var self = this;
18
19 this.modifierKeyListener = null;
20 this.keyListener = null;
21 this.element = $element;
22 this.active = false;
23 this.model = null;
24 this.padId = $attrs.ngKeypadInput;
25
26
27 /**
28 * Initialize Key to default settings
29 */
30 function init() {
31 $element.on('click.ngKeypadInput', handleElementSelected);
32 $element.on('focus.ngKeypadInput', handleElementSelected);
33 $element.on('blur.ngKeypadInput', handleElementBlur);
34
35 if (!$attrs.ngModel) {
36 throw new Error("KeypadInput requires the use of ng-model on this element", $element);
37 }
38
39 $scope.$on('destroy', destroy);
40 };
41
42
43 /**
44 * Triggered when a user select a keypad input field.
45 * Set listeners and add focused class.
46 *
47 * @param event
48 */
49 function handleElementSelected(event) {
50 event.stopPropagation();
51
52 if (!self.active) {
53 clearSelectedElement();
54
55 self.active = true;
56 self.keyListener = $scope.$on(Keypad.KEY_PRESSED, handleKeyPressed);
57 self.closeListener = $scope.$on(Keypad.CLOSED, handleKeypadClosed);
58 self.modifierKeyListener = $scope.$on(Keypad.MODIFIER_KEY_PRESSED, handleModifierKeyPressed);
59
60 $element.addClass("focus");
61 $element.focus();
62
63 $scope.$emit(Keypad.OPEN, $attrs.ngKeypadInput);
64
65 KeypadInput.selectedInput = self;
66 }
67 };
68
69
70 /**
71 * Clear old reference of the selected element
72 */
73 function clearSelectedElement() {
74 if (KeypadInput.selectedInput) {
75 KeypadInput.selectedInput.active = false;
76 KeypadInput.selectedInput.keyListener();
77 KeypadInput.selectedInput.element.removeClass("focus");
78 KeypadInput.selectedInput = null;
79 }
80 }
81
82
83 /**
84 * Triggered when keypad is closing to clear
85 * the current input being edited.
86 *
87 * @param event
88 * @param padId
89 */
90
91 function handleKeypadClosed(event, padId) {
92 if (self.padId === padId && self.active) {
93 clearSelectedElement();
94 }
95 }
96
97
98 /**
99 * Trigerred when the input loose focus to
100 * clear selected element.
101 *
102 * @param event
103 */
104 function handleElementBlur(event) {
105 clearSelectedElement();
106 }
107
108
109 /**
110 * Event triggered from ngKeyPad called only if the current
111 * input is selected. Works with ngModel.
112 *
113 * @param event
114 * @param key
115 * @param padId
116 */
117 function handleKeyPressed(event, key, padId) {
118 if (self.padId === padId && self.active) {
119 var value = controller.$viewValue;
120
121
122 if (!value) {
123 value = "";
124 }
125
126 if (!isRestricted(value, key)) {
127 value += key;
128 }
129
130 controller.$setViewValue(value);
131 controller.$render();
132 $scope.$apply();
133 }
134 };
135
136
137 /**
138 * Triggered when a modifier key is pressed
139 *
140 * @param event
141 * @param key
142 * @param padId
143 */
144 function handleModifierKeyPressed(event, key, padId) {
145 if (self.padId === padId && self.active) {
146 if (key === "CLEAR") {
147 controller.$setViewValue("");
148 controller.$render();
149 $scope.$apply();
150 }
151 }
152 };
153
154
155 /**
156 * Check if the input is retricted by a RegExp.
157 * data-ng-keypad-restric is used to save the string
158 * corresponding to the RegExp
159 *
160 * @param value
161 * @param key
162 */
163 function isRestricted(value, key) {
164 var restricted = false,
165 regExp = new RegExp($attrs.ngKeypadRestrict, 'gi');
166
167 if ($attrs.ngKeypadRestrict) {
168 if (!key.match(regExp)) {
169 restricted = true;
170 } else if (!String(value + key).match(regExp, 'gi')) {
171 restricted = true;
172 }
173 }
174
175 return restricted;
176 }
177
178
179 /**
180 * Cleanup all listeners before destroying this directive.
181 */
182 function destroy() {
183 $element.off('click.ngKeypadInput');
184 $element.on('focus.ngKeypadInput', this.handleElementSelected);
185 };
186
187
188 init();
189 };
190
191
192 KeypadInput.selectedInput = null;
193
194
195 angular.module('ngKeypad')
196 .directive('ngKeypadInput', function () {
197 return {
198 restrict: 'A',
199 require: '^ngModel',
200 link: function ($scope, $element, $attrs, controller) {
201 new KeypadInput($scope, $element, $attrs, controller);
202 }
203 }
204 });
205 })();