Skip to content

Commit 9b86312

Browse files
author
Aditya Sharat
committed
fixes #7
1 parent b8a6225 commit 9b86312

1 file changed

Lines changed: 102 additions & 97 deletions

File tree

angular-chosen.js

Lines changed: 102 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,105 +4,110 @@
44
* http://adityasharat.github.io/angular-chosen/
55
*/
66
(function (angular) {
7-
var AngularChosen = angular.module('angular.chosen', []);
8-
9-
AngularChosen.directive('chosen', ['$timeout', function ($timeout) {
10-
var EVENTS, scope, linker, watchCollection;
11-
12-
/*
13-
* List of events and the alias used for binding with angularJS
14-
*/
15-
EVENTS = [{
16-
onChange: 'change'
17-
}, {
18-
onReady: 'chosen:ready'
19-
}, {
20-
onMaxSelected: 'chosen:maxselected'
21-
}, {
22-
onShowDropdown: 'chosen:showing_dropdown'
23-
}, {
24-
onHideDropdown: 'chosen:hiding_dropdown'
25-
}, {
26-
onNoResult: 'chosen:no_results'
27-
}];
28-
29-
/*
30-
* Items to be added in the scope of the directive
31-
*/
32-
scope = {
33-
options: '=', // the options array
34-
ngModel: '=', // the model to bind to,,
35-
ngDisabled: '='
36-
};
37-
38-
/*
39-
* initialize the list of items
40-
* to watch to trigger the chosen:updated event
41-
*/
42-
watchCollection = [];
43-
Object.keys(scope).forEach(function (scopeName) {
44-
watchCollection.push(scopeName);
45-
});
46-
47-
/*
48-
* Add the list of event handler of the chosen
49-
* in the scope.
50-
*/
51-
EVENTS.forEach(function (event) {
52-
var eventNameAlias = Object.keys(event)[0];
53-
scope[eventNameAlias] = '=';
54-
});
55-
56-
/* Linker for the directive */
57-
linker = function ($scope, iElm, iAttr) {
58-
var maxSelection = parseInt(iAttr.maxSelection, 10),
59-
searchThreshold = parseInt(iAttr.searchThreshold, 10);
60-
61-
if (isNaN(maxSelection) || maxSelection === Infinity) {
62-
maxSelection = undefined;
63-
}
64-
65-
if (isNaN(searchThreshold) || searchThreshold === Infinity) {
66-
searchThreshold = undefined;
67-
}
68-
69-
iElm.chosen({
70-
width: '100%',
71-
max_selected_options: maxSelection,
72-
disable_search_threshold: searchThreshold,
73-
search_contains: true
74-
});
7+
var AngularChosen = angular.module('angular.chosen', []);
758

76-
iElm.on('change', function () {
77-
iElm.trigger('chosen:updated');
78-
});
799

80-
$scope.$watchGroup(watchCollection, function () {
81-
$timeout(function () {
82-
iElm.trigger('chosen:updated');
83-
}, 100);
84-
});
10+
function chosen($timeout) {
11+
var EVENTS, scope, linker, watchCollection;
12+
13+
/*
14+
* List of events and the alias used for binding with angularJS
15+
*/
16+
EVENTS = [{
17+
onChange: 'change'
18+
}, {
19+
onReady: 'chosen:ready'
20+
}, {
21+
onMaxSelected: 'chosen:maxselected'
22+
}, {
23+
onShowDropdown: 'chosen:showing_dropdown'
24+
}, {
25+
onHideDropdown: 'chosen:hiding_dropdown'
26+
}, {
27+
onNoResult: 'chosen:no_results'
28+
}];
29+
30+
/*
31+
* Items to be added in the scope of the directive
32+
*/
33+
scope = {
34+
options: '=', // the options array
35+
ngModel: '=', // the model to bind to,,
36+
ngDisabled: '='
37+
};
38+
39+
/*
40+
* initialize the list of items
41+
* to watch to trigger the chosen:updated event
42+
*/
43+
watchCollection = [];
44+
Object.keys(scope).forEach(function (scopeName) {
45+
watchCollection.push(scopeName);
46+
});
47+
48+
/*
49+
* Add the list of event handler of the chosen
50+
* in the scope.
51+
*/
52+
EVENTS.forEach(function (event) {
53+
var eventNameAlias = Object.keys(event)[0];
54+
scope[eventNameAlias] = '=';
55+
});
56+
57+
/* Linker for the directive */
58+
linker = function ($scope, iElm, iAttr) {
59+
var maxSelection = parseInt(iAttr.maxSelection, 10),
60+
searchThreshold = parseInt(iAttr.searchThreshold, 10);
8561

86-
// assign event handlers
87-
EVENTS.forEach(function (event) {
88-
var eventNameAlias = Object.keys(event)[0];
89-
90-
if (typeof $scope[eventNameAlias] === 'function') { // check if the handler is a function
91-
iElm.on(event[eventNameAlias], function (event) {
92-
$scope.$apply(function () {
93-
$scope[eventNameAlias](event);
94-
});
95-
}); // listen to the event triggered by chosen
96-
}
62+
if (isNaN(maxSelection) || maxSelection === Infinity) {
63+
maxSelection = undefined;
64+
}
65+
66+
if (isNaN(searchThreshold) || searchThreshold === Infinity) {
67+
searchThreshold = undefined;
68+
}
69+
70+
var allowSingleDeselect = iElm.attr('allow-single-deselect') !== undefined ? true : false;
71+
72+
iElm.chosen({
73+
width: '100%',
74+
max_selected_options: maxSelection,
75+
disable_search_threshold: searchThreshold,
76+
search_contains: true,
77+
allow_single_deselect: allowSingleDeselect
78+
});
79+
80+
iElm.on('change', function () {
81+
iElm.trigger('chosen:updated');
82+
});
83+
84+
$scope.$watchGroup(watchCollection, function () {
85+
$timeout(function () {
86+
iElm.trigger('chosen:updated');
87+
}, 100);
88+
});
89+
90+
// assign event handlers
91+
EVENTS.forEach(function (event) {
92+
var eventNameAlias = Object.keys(event)[0];
93+
94+
if (typeof $scope[eventNameAlias] === 'function') { // check if the handler is a function
95+
iElm.on(event[eventNameAlias], function (event) {
96+
$scope.$apply(function () {
97+
$scope[eventNameAlias](event);
9798
});
98-
};
99-
100-
// return the directive
101-
return {
102-
name: 'chosen',
103-
scope: scope,
104-
restrict: 'A',
105-
link: linker
106-
};
107-
}]);
99+
}); // listen to the event triggered by chosen
100+
}
101+
});
102+
};
103+
104+
// return the directive
105+
return {
106+
name: 'chosen',
107+
scope: scope,
108+
restrict: 'A',
109+
link: linker
110+
};
111+
}
112+
AngularChosen.directive('chosen', ['$timeout', chosen]);
108113
}(angular));

0 commit comments

Comments
 (0)