forked from adityasharat/angular-chosen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-chosen.js
117 lines (101 loc) · 3.13 KB
/
angular-chosen.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
/*
* Use this directive to convert drop downs into chosen drop downs.
* http://harvesthq.github.io/chosen/
* http://adityasharat.github.io/angular-chosen/
*/
(function (angular) {
var AngularChosen = angular.module('angular.chosen', []);
function chosen($timeout) {
var EVENTS, scope, linker, watchCollection;
/*
* List of events and the alias used for binding with angularJS
*/
EVENTS = [{
onChange: 'change'
}, {
onReady: 'chosen:ready'
}, {
onMaxSelected: 'chosen:maxselected'
}, {
onShowDropdown: 'chosen:showing_dropdown'
}, {
onHideDropdown: 'chosen:hiding_dropdown'
}, {
onNoResult: 'chosen:no_results'
}];
/*
* Items to be added in the scope of the directive
*/
scope = {
options: '=', // the options array
ngModel: '=', // the model to bind to,,
ngDisabled: '='
};
/*
* initialize the list of items
* to watch to trigger the chosen:updated event
*/
watchCollection = [];
Object.keys(scope).forEach(function (scopeName) {
watchCollection.push(scopeName);
});
/*
* Add the list of event handler of the chosen
* in the scope.
*/
EVENTS.forEach(function (event) {
var eventNameAlias = Object.keys(event)[0];
scope[eventNameAlias] = '=';
});
/* Linker for the directive */
linker = function ($scope, iElm, iAttr) {
var maxSelection = parseInt(iAttr.maxSelection, 10),
searchThreshold = parseInt(iAttr.searchThreshold, 10);
if (isNaN(maxSelection) || maxSelection === Infinity) {
maxSelection = undefined;
}
if (isNaN(searchThreshold) || searchThreshold === Infinity) {
searchThreshold = undefined;
}
var allowSingleDeselect = iElm.attr('allow-single-deselect') !== undefined ? true : false;
var includeGroupLabel = iElm.attr('include-group-label') !== undefined ? true : false;
iElm.chosen({
width: '100%',
max_selected_options: maxSelection,
disable_search_threshold: searchThreshold,
search_contains: true,
allow_single_deselect: allowSingleDeselect,
include_group_label_in_selected: includeGroupLabel
});
iElm.on('change', function () {
iElm.trigger('chosen:updated');
});
$scope.$watchGroup(watchCollection, function () {
$timeout(function () {
iElm.trigger('chosen:updated');
}, 100);
});
// assign event handlers
EVENTS.forEach(function (event) {
var eventNameAlias = Object.keys(event)[0];
if (typeof $scope[eventNameAlias] === 'function') { // check if the handler is a function
iElm.on(event[eventNameAlias], function (event) {
$scope.$apply(function () {
$scope[eventNameAlias](event);
});
}); // listen to the event triggered by chosen
}
});
};
// return the directive
return {
name: 'chosen',
scope: scope,
restrict: 'A',
link: {
post: linker
}
};
}
AngularChosen.directive('chosen', ['$timeout', chosen]);
}(angular));