bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
ngKeypad.js
(5554B)
1 /**
2 * Keypad directive, display a keypad on the screen. Template for it
3 * can be include inside the node containing the keypad attribute.
4 *
5 * Use of Namespace.js
6 *
7 * @author tommy.rochette[followed by the usual sign]universalmind.com
8 */
9
10 (function () {
11 "use strict";
12
13
14 /**
15 * @Class
16 */
17 window.Keypad = function ($scope, $element, $attrs, $rootScope) {
18
19 var locked = false,
20 opened = false,
21 padId = $attrs.ngKeypad,
22 body = $('body');
23
24 /**
25 * Initialize Keypad to default settings
26 * based on attributes.
27 */
28 function init() {
29 $scope.$on(Key.PRESSED, handleKeyPressed);
30 $rootScope.$on(Keypad.TOGGLE_LOCKING, handleLockingToggle);
31 $rootScope.$on(Keypad.TOGGLE_OPENING, handleOpeningToggle);
32 $rootScope.$on(Keypad.OPEN, handleOpeningToggle);
33 $rootScope.$on(Keypad.CLOSE, handleOpeningToggle);
34 $element.addClass("closed");
35
36 initScope();
37 };
38
39 /**
40 * Initialize scope variables
41 */
42 function initScope() {
43 $scope.close = function () {
44 close();
45 }
46 }
47
48
49 /**
50 * Triggered when a user press any key on the pad
51 * check current status then dispatch KEY_PRESSED event.
52 *
53 * @param event
54 * @param key
55 */
56 function handleKeyPressed(event, key) {
57 if (!locked) {
58 if (key.indexOf('[') === -1 && key.indexOf(']') === -1) {
59 $scope.$emit(Keypad.KEY_PRESSED, key, padId);
60 } else {
61 $scope.$emit(Keypad.MODIFIER_KEY_PRESSED, key.substring(1, key.length - 1), padId);
62 }
63 }
64 }
65
66
67 /**
68 * Triggered when a user press any key on the pad
69 * check current status then dispatch KEY_PRESSED event.
70 *
71 * @param event
72 * @param key
73 */
74 function handleLockingToggle(event, id) {
75 if (padId === id || !id) {
76 locked = !locked;
77
78 if (locked) {
79 $element.attr("disabled", "disabled");
80 } else {
81 $element.removeAttr("disabled");
82 }
83 }
84 }
85
86
87 /**
88 * Triggered when a user toggle keypad opening.
89 *
90 * @param event
91 * @param key
92 */
93 function handleOpeningToggle(event, id, params) {
94 if (padId === id || !id) {
95 switch (event.name) {
96 case Keypad.CLOSE:
97 opened = false;
98 break;
99 case Keypad.OPEN:
100 opened = true;
101 break;
102 default:
103 opened = !opened;
104 break;
105 }
106
107 if (!opened) {
108 close();
109 } else {
110 open();
111 applyOptions(params);
112 }
113 }
114 }
115
116 function applyOptions(params) {
117 if (params && params.position) {
118 $element.css("top", params.position.y);
119 $element.css("left", params.position.x);
120 }
121 }
122
123
124 /**
125 * Open current pad
126 */
127 function open() {
128 $element.removeClass("closed");
129 $scope.$emit(Keypad.OPENED, padId);
130 autoClose();
131 applyOptions();
132 }
133
134
135 /**
136 * Close current pad
137 */
138 function close() {
139 opened = false;
140 $scope.$emit(Keypad.CLOSED, padId);
141 body.off("click.keypad");
142 $element.addClass("closed");
143 }
144
145
146 /**
147 * Check if the attribute auto-close is set then
148 * add event for automatic closing.
149 *
150 * @param event
151 * @param key
152 */
153 function autoClose() {
154 if ($attrs.autoClose) {
155 //Timeout use to break the event flow.
156 setTimeout(function () {
157 body.on("click.keypad", function () {
158 opened = !opened;
159 close();
160 if (!$scope.$$phase) {
161 $scope.$apply();
162 }
163 });
164 $element.on("click.keypad", cancelEvent);
165 }, 1);
166 }
167 }
168
169
170 /**
171 * Dummy function to cancel event propagation.
172 *
173 * @param event
174 */
175 function cancelEvent(event) {
176 event.stopPropagation();
177 }
178
179
180 init();
181 };
182
183
184 /**
185 * @Events
186 */
187 Keypad.KEY_PRESSED = "Keypad.KEY_PRESSED";
188 Keypad.MODIFIER_KEY_PRESSED = "Keypad.MODIFIER_KEY_PRESSED";
189 Keypad.TOGGLE_LOCKING = "Keypad.TOGGLE_LOCKING";
190 Keypad.TOGGLE_OPENING = "Keypad.TOGGLE_OPENING";
191 Keypad.OPENED = "Keypad.OPENED";
192 Keypad.CLOSED = "Keypad.CLOSED";
193 Keypad.OPEN = "Keypad.OPEN";
194 Keypad.CLOSE = "Keypad.CLOSE";
195
196
197 angular.module('ngKeypad')
198 .directive('ngKeypad', ['$rootScope', function ($rootScope) {
199
200 return {
201 restrict: 'A',
202 link: function ($scope, $element, $attrs) {
203 new Keypad($scope, $element, $attrs, $rootScope);
204 }
205 }
206 }]);
207 })();