-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpicklist.js
359 lines (317 loc) · 10.7 KB
/
picklist.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
function fxPickList($compile, $templateCache){
/*
* what do we need
* 1. source options: the ngoptiond
* 2. dest inst to put the select values: ng-model
*/
return {
replace:true,
priority:99,
templateUrl:"picklist.html",
restrict: 'E',
terminal:false,
scope:true, //do not pollute parent scope
controller: function($scope, $element, $attrs, $parse){
//size
$scope.size=$attrs.size;
if (!angular.isDefined($scope.size)){
$scope.size=10;
}
//name
$scope.formname=$attrs.name;
$scope.ngOptions=$attrs.pickOptions;
$scope.ngModel=$attrs.ngModel;
if (!angular.isDefined($attrs.ngModel)){
$scope.ngModel=$attrs.pickModel;
}
/*
* model directive is never processed.
* The new ctrl is the subform.
*/
delete($attrs['ngModel']);
$element.removeAttr('data-ng-model');
$element.removeAttr('ng-model');
//hold the selection.
$scope.picklist_src=[];
$scope.picklist_dest=[];
// match parsing is from angular js
var NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/;
var match= $scope.ngOptions.match(NG_OPTIONS_REGEXP);
$scope.srcoptionsExp = match[7];
var displayFn = $parse(match[2] || match[1]),
valueName = match[4] || match[6],
keyName = match[5],
groupByFn = $parse(match[3] || ''),
valueFn = $parse(match[2] ? match[1] : valueName),
valuesFn = $parse(match[7]),
track = match[8],
trackFn = track ? $parse(match[8]) : null;
var hasValueFn=match[2]?true:false;
var modelFn=$parse($scope.ngModel);
var models=modelFn($scope);
//give models an empty array if needed.
if (!angular.isDefined(models) || models==null){
modelFn.assign($scope, []);
}
$scope.srcoptions=[];
$scope.destoptions=[];
$scope.valueToItem=function(value){
if (!hasValueFn){
return value;
}
return $scope.valueMap[value];
};
$scope.itemToValue=function(item){
if (!hasValueFn){
return item;
}
var local={};
local[valueName]=item;
return valueFn($scope, local);
};
//set it to $scope.srcoptions
$scope.init=function(){
$scope.srcoptions.length=0;
$scope.destoptions.length=0;
$scope.valueMap={};
var getter = $parse($scope.srcoptionsExp);
var options=getter($scope);
if (!options){
return;
}
for(var i=0; i<options.length; i++){
$scope.srcoptions.push(options[i]);
//map option value to object
if (hasValueFn){
var value=$scope.itemToValue(options[i]);
$scope.valueMap[value]=options[i];
}
}
var models=$parse($scope.ngModel)($scope);
angular.forEach(models, function(v){
$scope.destoptions.push($scope.valueToItem(v));
});
$scope.removeLeftFromRight($scope.destoptions, $scope.srcoptions);
};
$scope.$watchCollection($scope.srcoptionsExp, $scope.init);
$scope.$watchCollection($scope.ngModel, $scope.init);
//always assign to model and expect destoptions is updated from model.
//do not update desttoptions to model.
$scope.rightShift=function(){
var models=modelFn($scope);
for (var i=0; i<$scope.picklist_src.length; i++){
models.push($scope.itemToValue($scope.picklist_src[i]));
}
$scope.removeLeftFromRight($scope.picklist_src, $scope.srcoptions);
$scope.picklist_src.length=0;
};
$scope.rightShiftAll=function(){
var models=modelFn($scope);
for (var i=0; i<$scope.srcoptions.length; i++){
models.push($scope.itemToValue($scope.srcoptions[i]));
}
$scope.picklist_src.length=0;
$scope.srcoptions.length=0;
};
$scope.leftShift=function(){
var models=modelFn($scope);
for (var i=0; i<$scope.picklist_dest.length; i++){
$scope.srcoptions.push($scope.picklist_dest[i]);
}
$scope.removeLeftFromRight($scope.picklist_dest, $scope.destoptions);
while(models.length>0){
models.pop();
}
angular.forEach($scope.destoptions, function(item){
models.push($scope.itemToValue(item));
});
$scope.picklist_dest.length=0;
};
$scope.leftShiftAll=function(){
for (var i=0; i<$scope.destoptions.length; i++){
$scope.srcoptions.push($scope.destoptions[i]);
}
$scope.picklist_dest.length=0;
$scope.destoptions.length=0;
while(models.length>0){
models.pop();
}
};
$scope.arrowUp=function(){
var idxs=new Array();
for (var i=0; i<$scope.picklist_dest.length; i++){
for (var j=0; j<$scope.destoptions.length; j++){
if($scope.picklist_dest[i]==$scope.destoptions[j]){
idxs.push(j);
}
}
}
idxs.sort();
for (var i=0; i<idxs.length; i++){
var idx=idxs[i];
if (idx>0){
var temp=$scope.destoptions[idx-1];
$scope.destoptions[idx-1]=$scope.destoptions[idx];
$scope.destoptions[idx]=temp;
}
}
while(models.length>0){
models.pop();
}
angular.forEach($scope.destoptions, function(item){
models.push($scope.itemToValue(item));
});
};
$scope.arrowDown=function(){
var idxs=new Array();
for (var i=0; i<$scope.picklist_dest.length; i++){
for (var j=0; j<$scope.destoptions.length; j++){
if($scope.picklist_dest[i]==$scope.destoptions[j]){
idxs.push(j);
}
}
}
idxs.sort();
for (var i=idxs.length-1; i>=0; i--){
var idx=idxs[i];
if (idx<$scope.destoptions.length-1){
var temp=$scope.destoptions[idx+1];
$scope.destoptions[idx+1]=$scope.destoptions[idx];
$scope.destoptions[idx]=temp;
}
}
while(models.length>0){
models.pop();
}
angular.forEach($scope.destoptions, function(item){
models.push($scope.itemToValue(item));
});
};
/**
* Remove from right all elements in left;
*/
$scope.removeLeftFromRight=function(left, right){
for(var i=0; i<left.length; i++){
for (var j=0; j<right.length; j++){
if (left[i]===right[j]){
right.splice(j, 1);
break;
}
}
}
};
},
};
}
function fxPickListForm($compile, $templateCache){
/*
* what do we need
* 1. source options: the ngoptiond
* 2. dest inst to put the select values: ng-model
*/
return {
require: "^picklist",
priority:99,
restrict: 'A',
controller: function($scope, $element, $attrs){
if (angular.isDefined($attrs.ngForm) && angular.isDefined($scope.formname)){
$attrs.ngForm=$scope.formname;
}
}
};
}
function fxPickListSrc($compile, $templateCache){
/*
* what do we need
* 1. source options: the ngoptiond
* 2. dest inst to put the select values: ng-model
*/
return {
require: "^picklist",
priority:99,
restrict: 'A',
controller: function($scope, $element, $attrs){
$attrs.ngOptions=$scope.ngOptions;
$attrs.size=$scope.size;
$element.attr("size", $scope.size);
$attrs.name=$scope.formname+"_src";
//always use item as value.
$attrs.ngOptions=$scope.ngOptions.replace($scope.srcoptionsExp, "srcoptions").replace(/^.+\s+as\s+/, "");
}
};
}
function fxPickListDest($compile, $templateCache){
/*
* what do we need
* 1. source options: the ngoptiond
* 2. dest inst to put the select values: ng-model
*/
return {
require: "^picklist",
priority:99,
restrict: 'A',
controller: function($scope, $element, $attrs){
$attrs.size=$scope.size;
$element.attr("size", $scope.size);
//always use item as value. real valye is synchoinized to model by watcher function
$attrs.ngOptions=$scope.ngOptions.replace($scope.srcoptionsExp, "destoptions").replace(/^.+\s+as\s+/, "");
$attrs.name=$scope.formname+"_dest";
}
};
}
var fxPickListTpl=
" <div style=\"display: table;width:100%\" data-ng-form=\"fake\" data-picklist-form>"+
" <div style=\"display: table-row;\">"+
" <div style=\"display: table-cell; width: 40%;vertical-align: middle;\">"+
" <select multiple size=\"5\" class=\"form-control\" data-ng-options=\"fake\" name=\"fake\" data-ng-model=\"picklist_src\" data-picklist-src>"+
" </select>"+
" </div>"+
" <div style=\"display: table-cell; width: 10%; vertical-align: middle;\" class=\"btn-group-vertical\">"+
" <div>"+
" <button type=\"button\" class=\"btn btn-info\" data-ng-click=\"rightShift();\">"+
" <span class=\"glyphicon glyphicon-step-forward\"></span>"+
" </button>"+
" </div>"+
" <div>"+
" <button type=\"button\" class=\"btn btn-default\" data-ng-click=\"rightShiftAll();\">"+
" <span class=\"glyphicon glyphicon-fast-forward\"></span>"+
" </button>"+
" </div>"+
" <div>"+
" <button type=\"button\" class=\"btn btn-info\" data-ng-click=\"leftShift();\">"+
" <span class=\"glyphicon glyphicon-step-backward\"></span>"+
" </button>"+
" </div>"+
" <div>"+
" <button type=\"button\" class=\"btn btn-default\" data-ng-click=\"leftShiftAll();\">"+
" <span class=\"glyphicon glyphicon-fast-backward\"></span>"+
" </button>"+
" </div>"+
" </div>"+
" <div style=\"display: table-cell; width: 40%;vertical-align: middle;\">"+
" <select multiple size=\"5\" class=\"form-control\" data-ng-options=\"fake\" name=\"fake\" data-ng-model=\"picklist_dest\" data-picklist-dest>"+
" </select>"+
" </div>"+
" <div style=\"display: table-cell; width: 10%; vertical-align: middle;\" class=\"btn-group-vertical\">"+
" <div>"+
" <button type=\"button\" class=\"btn btn-default\" data-ng-click=\"arrowUp();\">"+
" <span class=\"glyphicon glyphicon-arrow-up\"></span>"+
" </button>"+
" </div>"+
" <div>"+
" <button type=\"button\" class=\"btn btn-default\" data-ng-click=\"arrowDown();\">"+
" <span class=\"glyphicon glyphicon-arrow-down\"></span>"+
" </button>"+
" </div>"+
" </div>"+
" </div>"+
" </div> "
;
angular.module("fxpicklist", [])
.directive("picklist", fxPickList)
.directive("picklistForm", fxPickListForm)
.directive("picklistSrc", fxPickListSrc)
.directive("picklistDest", fxPickListDest)
.run(["$templateCache", function($templateCache) {
$templateCache.put("picklist.html", fxPickListTpl);
}]);