-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.js
More file actions
122 lines (107 loc) · 5.42 KB
/
counter.js
File metadata and controls
122 lines (107 loc) · 5.42 KB
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
/**
@toc
@param {Object} scope (attrs that must be defined on the scope (i.e. in the controller) - they can't just be defined in the partial html). REMEMBER: use snake-case when setting these on the partial!
@param {Object} attrs REMEMBER: use snake-case when setting these on the partial! i.e. my-attr='1' NOT myAttr='1'
@usage
| Attribute | Default | Description |
| ---------------------- | ------- | ----------------------------------------------------------------- |
| min/data-min | null | A minimum value, never to go below. |
| max/data-min | null | A maximum value, never to go above. |
| step/data-step | 1 | How much to increment/decrement by. |
| addclass/data-addclass | null | Add a class to the container. |
| width/data-width | null | Set the width of the input field. |
| editable/data-editable | false | Whether the field is readyonly or not. By default, it's readonly. |
partial / html:
<div fs-counter value="someValue"
data-min="0"
data-max="100"
data-step="1"
data-addclass="someClass"
data-width="130px"
data-editable
></div>
//end: usage
*/
'use strict';
angular.module('Firestitch.angular-counter', []).directive('fsCounter', ['$timeout', function ($timeout) {
return {
restrict: "A",
scope: {
value: "=value"
},
template: "<div class=\"fs-counter input-group\" ng-class=\"addclass\" ng-style=\"width\"><span class=\"input-group-btn\" ng-click=\"minus()\"><button class=\"btn btn-default\"><span class=\"glyphicon glyphicon-minus\"></span></button></span><input type=\"text\" class=\"form-control text-center\" ng-model=\"value\" ng-blur=\"blurred()\" ng-change=\"changed()\" ng-readonly=\"readonly\"><span class=\"input-group-btn\" ng-click=\"plus()\"><button class=\"btn btn-default\"><span class=\"glyphicon glyphicon-plus\"></span></button></span></div>",
replace: true,
link: function(scope, element, attrs) {
var min = (angular.isUndefined(attrs.min) ? void 0 : parseInt(attrs.min)),
max = (angular.isUndefined(attrs.max) ? void 0 : parseInt(attrs.max)),
step = (angular.isUndefined(attrs.step) || parseInt(attrs.step) === 0 ? 1 : parseInt(attrs.step)),
setValue,
changeDelay;
/**
* Sets the value as an integer. If the value cannot be parsed,
* i.e. returns NaN, then the min value or 0 will be used instead.
*/
setValue = function(val) {
var parsedVal = parseInt(val);
if (!isNaN(parsedVal)) {
if (min !== undefined && min > parsedVal) {
parsedVal = min;
return parsedVal;
}
if (max !== undefined && max < parsedVal) {
parsedVal = max;
return parsedVal;
}
return parsedVal;
} else {
console.log("parsedValue must parse to a number.");
parsedVal = min || 0;
return parsedVal;
}
};
/**
* Confirm the value attribute exists on the element
*/
if (angular.isUndefined(scope.value)) {
throw "Missing the value attribute on the counter directive.";
}
/**
* Set some scope wide properties
*/
scope.readonly = (angular.isUndefined(attrs.editable) ? true : false);
scope.addclass = (angular.isUndefined(attrs.addclass) ? null : attrs.addclass);
scope.width = (angular.isUndefined(attrs.width) ? {} : {width:attrs.width});
scope.value = setValue(scope.value);
/**
* Decrement the value and make sure we stay within the limits, if defined.
*/
scope.minus = function() {
scope.value = setValue(scope.value - step);
};
/**
* Increment the value and make sure we stay within the limits, if defined.
*/
scope.plus = function() {
scope.value = setValue(scope.value + step);
};
/**
* This is only triggered 1 second after a field is manually edited
* by the user. Where we can perform some validation and make sure
* that they enter the correct values from within the restrictions.
*/
scope.changed = function() {
changeDelay = $timeout(function (){
scope.value = setValue(scope.value);
}, 1000, true);
};
/**
* This is only triggered when user leaves a manually edited field.
* Where we can perform some validation and make sure that they
* enter the correct values from within the restrictions.
*/
scope.blurred = function() {
scope.value = setValue(scope.value);
};
}
};
}]);