forked from mgcrea/angular-strap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonSelect.js
More file actions
42 lines (34 loc) · 1.07 KB
/
buttonSelect.js
File metadata and controls
42 lines (34 loc) · 1.07 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
'use strict';
angular.module('$strap.directives')
.directive('bsButtonSelect', ['$parse', '$timeout', function($parse, $timeout) {
return {
restrict: 'A',
require: '?ngModel',
link: function postLink(scope, element, attrs, ctrl) {
var getter = $parse(attrs.bsButtonSelect),
setter = getter.assign;
// Bind ngModelController
if(ctrl) {
element.text(scope.$eval(attrs.ngModel));
// Watch model for changes
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
element.text(newValue);
});
}
// Click handling
var values, value, index, newValue;
element.bind('click', function(ev) {
values = getter(scope);
value = ctrl ? scope.$eval(attrs.ngModel) : element.text();
index = values.indexOf(value);
newValue = index > values.length - 2 ? values[0] : values[index + 1];
scope.$apply(function() {
element.text(newValue);
if(ctrl) {
ctrl.$setViewValue(newValue);
}
});
});
}
};
}]);