forked from alalonde/angular-multi-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-multi-select.js
228 lines (210 loc) · 7.87 KB
/
angular-multi-select.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Multiple-select directive for AngularJS
(c) 2013 Alec LaLonde (https://github.com/alalonde/angular-multi-select)
License: MIT
*/
(function(angular) {
'use strict';
angular.module('multi-select', [])
.directive('multiSelect', ['$q', '$parse',
function($q, $parse) {
function appendSelected(entities) {
var newEntities = [];
angular.forEach(entities, function(entity) {
var appended = entity;
appended.selected = false;
newEntities.push(appended);
});
return newEntities;
}
return {
restrict: 'E',
require: 'ngModel',
scope: {
selectedLabel: "@",
availableLabel: "@",
available: "=",
model: "=ngModel"
},
template:
'<div class="multiSelect">' +
'<table>' +
'<thead>' +
'<tr>' +
'<th>' +
'<label class="control-label" for="multiSelectSelected">{{ selectedLabel }} ' +
'({{ model.length }})</label>' +
'</th>' +
'<th>' +
' ' +
'</th>' +
'<th>' +
'<label class="control-label" for="multiSelectAvailable">{{ availableLabel }} ' +
'({{ available.length }})</label>' +
'</th>' +
'</tr>' +
'</thead>' +
'<tr>' +
'<td>' +
'<ul>' +
'<li>' +
'<input type="checkbox" ng-model="leftallchkd" ng-change="leftallnone(model)" title="Select None"> ' +
'</li>' +
'</ul>' +
'</td>' +
'<td>' +
' ' +
'</td>' +
'<td>' +
'<ul>' +
'<li>' +
'<input type="checkbox" ng-model="rightallchkd" ng-change="rightallnone(available)" title="Check/Uncheck all"> ' +
'</li>' +
'</ul>' +
'</td>' +
'</tr>' +
'<tr>' +
'<td class="selectcell">' +
'<div class="select">' + // select list on the left
'<ul>' +
'<li ng-repeat="entity in model">' +
'<label class="checkbox" title="{{ renderTitle(entity) }}">' +
'<input type="checkbox" ng-model="selected.current[$index].selected"> ' +
'{{ renderItem(entity) }}' +
'</label>' +
'</li>' +
'</ul>' +
'</div>' +
'</td>' +
'<td>' +
'<div class="select buttons">' + // buttons to transfer between lists
'<button class="btn mover left" ng-click="add()" title="Add selected" ' +
'ng-disabled="!selected(selected.available).length">' +
'<i class="icon-arrow-left"></i>' +
'</button>' +
'<button class="btn mover right" ng-click="remove()" title="Remove selected" ' +
'ng-disabled="!selected(selected.current).length">' +
'<i class="icon-arrow-right"></i>' +
'</button>' +
'</div>' +
'</td>' +
'<td class="selectcell">' +
'<div class="select">' + // select list on the right
'<ul>' +
'<li ng-repeat="entity in available">' +
'<label class="checkbox" title="{{ renderTitle(entity) }}">' +
'<input type="checkbox" ng-model="selected.available[$index].selected"> ' +
'{{ renderItem(entity) }}' +
'</label>' +
'</li>' +
'</ul>' +
'</div>' +
'</td>' +
'</tr>' +
'</table>' +
'</div>',
link: function(scope, elm, attrs) {
scope.leftallchkd=false;
scope.rightallchkd=false;
scope.selected = {
available: [],
current: []
};
/* Handles cases where scope data hasn't been initialized yet */
var dataLoading = function(scopeAttr) {
var loading = $q.defer();
if (scope[scopeAttr] && !scope[scopeAttr].hasOwnProperty('$promise')) {
loading.resolve(scope[scopeAttr]);
} else {
scope.$watch(scopeAttr, function(newValue, oldValue) {
if (newValue !== undefined && oldValue === undefined)
loading.resolve(newValue);
});
}
return loading.promise;
};
/* Filters out items in original that are also in toFilter. Compares by reference. */
function filterOut(original, toFilter) {
var filtered = [];
angular.forEach(original, function(entity) {
var match = false;
for (var i = 0; i < toFilter.length; i++) {
if (scope.renderItem(toFilter[i]) == scope.renderItem(entity)) {
match = true;
break;
}
}
if (!match) {
filtered.push(entity);
}
});
return filtered;
}
scope.refreshAvailable = function() {
scope.available = filterOut(scope.available, scope.model);
scope.selected.available = appendSelected(scope.available);
scope.selected.current = appendSelected(scope.model);
};
scope.add = function() {
scope.model = scope.model.concat(scope.selected(scope.selected.available));
scope.leftallchkd = false;
scope.rightallchkd = false;
};
scope.remove = function() {
var selected = scope.selected(scope.selected.current);
scope.available = scope.available.concat(selected);
scope.model = filterOut(scope.model, selected);
scope.leftallchkd = false;
scope.rightallchkd = false;
};
scope.selected = function(list) {
var found = [];
angular.forEach(list, function(item) {
if (item.selected === true) found.push(item);
});
return found;
};
scope.leftallnone = function(list) {
angular.forEach(list, function(item) {
item.selected = scope.leftallchkd;
});
};
scope.rightallnone = function(list) {
angular.forEach(list, function(item) {
item.selected = scope.rightallchkd;
});
};
scope.selnone = function(list) {
angular.forEach(list, function(item) {
item.selected = false
});
};
scope.selall = function(list) {
angular.forEach(list, function(item) {
item.selected = true
});
};
$q.all([dataLoading("model"), dataLoading("available")]).then(function(results) {
scope.refreshAvailable();
scope.$watch('model', scope.refreshAvailable);
});
function parseExpression(item, expr) {
var displayComponents = expr.match(/(.+)\s+as\s+(.+)/);
var ctx = {};
ctx[displayComponents[1]] = item;
return $parse(displayComponents[2])(ctx);
}
scope.renderItem = function(item) {
return parseExpression(item, attrs.display);
};
scope.renderTitle = function(item) {
if (attrs.title) {
return parseExpression(item, attrs.title);
}
return "";
};
}
};
}
]);
})(angular);