-
Notifications
You must be signed in to change notification settings - Fork 10
/
keypad.controller.js
146 lines (111 loc) · 3.82 KB
/
keypad.controller.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
import backspaceLeftTemplate from './templates/backspace-left.html';
import backspaceRightTemplate from './templates/backspace-right.html';
import submitLeftTemplate from './templates/submit-left.html';
import submitRightTemplate from './templates/submit-right.html';
export class KeypadController {
constructor(
$rootScope, $templateCache,
bcKeypadConfig
) {
'ngInject';
this.$rootScope = $rootScope;
this.$templateCache = $templateCache;
this.bcKeypadConfig = bcKeypadConfig;
this._activate();
}
_activate() {
// If anything other than a string was bound, overwrite with an empty string
if (!angular.isString(this.bcNumberModel)) {
this.bcNumberModel = '';
}
this.templates = {
backspaceRight: backspaceRightTemplate,
backspaceLeft: backspaceLeftTemplate,
submitRight: submitRightTemplate,
submitLeft: submitLeftTemplate,
};
// The numbers that make up the keypad
this.numbers = this.bcKeypadConfig.numbers.slice();
// Pull the last number off of the array so that we can inject it outside of the ng-repeat
this.lastNumber = this.numbers.splice(this.numbers.length - 1, 1)[0];
// Set the max length
this.bcMaxLength = this.bcMaxLength || this.bcKeypadConfig.maxLength;
this._setCustomTemplates();
}
/**
* Add the selected number to the number string
*
* @param {String} number
*/
setNumber(number) {
// If a max length is defined, verify we have not yet reached it
if (!this.bcMaxLength || this.bcNumberModel.length < this.bcMaxLength) {
this.bcNumberModel += number;
}
}
/**
* Delete the last number from the number model
*/
backspace() {
// If at least one number exists
if (this.bcNumberModel.length > 0) {
this.bcNumberModel = this.bcNumberModel.substring(0, this.bcNumberModel.length - 1);
} else {
// If backspace was hit when the model is already empty
this.bcEmptyBackspaceMethod();
}
}
/**
* Actions for the LEFT button
*
* @param {Object} $event
* @param {String} type
*/
leftButtonTrigger($event, type) {
if (type && type === 'backspace') {
this.backspace();
}
// Call the bound method
this.bcLeftButtonMethod({ '$event': $event, 'numbers': this.bcNumberModel });
}
/**
* Actions for the RIGHT button
*
* @param {Object} $event
* @param {String} type
*/
rightButtonTrigger($event, type) {
if (type && type === 'backspace') {
this.backspace();
}
// Call the bound method
this.bcRightButtonMethod({ '$event': $event, 'numbers': this.bcNumberModel });
}
/**
* Determine the correct template for the left button
*
* @param {String} side
* @return {String} template
*/
keyTemplate(type, side) {
// If the button type matches one of the plug'n'play types
if (this.bcKeypadConfig.types.indexOf(type) >= 0) {
return this.templates[type + side];
} else {
return;
}
}
/**
* Overwrite templates if any custom templates were set in the provider
*/
_setCustomTemplates() {
if (this.bcKeypadConfig.customSubmitTemplate) {
const path = this.bcKeypadConfig.submitTemplate;
this.$templateCache.put(path, this.bcKeypadConfig.customSubmitTemplate);
}
if (this.bcKeypadConfig.customBackspaceTemplate) {
const path = this.bcKeypadConfig.backspaceTemplate;
this.$templateCache.put(path, this.bcKeypadConfig.customBackspaceTemplate);
}
}
}