-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathngListSelect.js
269 lines (255 loc) · 15.4 KB
/
ngListSelect.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
(function(window, angular, undefined) {
'use strict';
angular.module('ngListSelect', [])
.run(['$templateCache',
function($templateCache) {
var addRemoveHtml =
"<div class='ngListSelect container' style='margin-left: 0;' ng-style='containerStyle'>" +
"<div style='float:left; width:40%'>" +
"<div ng-style='panelCssStyle' ng-class='panelClass' class='panel'>" +
"<div class='panel-heading' ng-style='panelCssStyle' ng-class='panelClass'><b><span ng-bind='availableText'></span></b></div>" +
"<select size='9' ng-style='dropdownStyle' style='width: 100%; vertical-align: top; color: black;' ng-model='leftMouseSelectedItems' ng-options='{{leftSelectNgOption}}' ng-dblclick='addItemsToRight()' multiple></select>" +
"</div>" +
"</div>" +
"<div style='display: inline-block; width: 20%; text-align: center'>" +
"<br/><br/>" +
"<div class='btn-group-vertical'>" +
"<button ng-click='addItemsToRight()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; vertical-align:middle;outline:none !important;margin-bottom:0px'><i class='glyphicon glyphicon-triangle-right'></i></button>" +
"<button ng-click='addItemsToLeft()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; vertical-align:middle;outline:none !important;margin-bottom:0px'><i class='glyphicon glyphicon-triangle-left'></i></button>" +
"<button ng-click='addAllItemsToRight()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; vertical-align:middle;outline:none !important;margin-bottom:0px'><i class='glyphicon glyphicon-forward'></i></button>" +
"<button ng-click='addAllItemsToLeft()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; vertical-align:middle;outline:none !important;margin-bottom:0px'><i class='glyphicon glyphicon-backward'></i></button>" +
"</div>" +
"</div>" +
"<div style='float: right; width: 40%'>" +
"<div ng-style='panelCssStyle' ng-class='panelClass' class='panel'>" +
"<div class='panel-heading' ng-style='panelCssStyle' ng-class='panelClass'><b><span ng-bind='selectedText'></span></b></div>" +
"<select size='9' ng-style='dropdownStyle' style='width: 100%; vertical-align: top; color: black;' id='selectedlist' ng-model='rightMouseSelectedItems' ng-options='{{rightSelectNgOption}}' ng-dblclick='addItemsToLeft()' multiple></select>" +
"</div>" +
"<div class='btn-group' style='float: left; margin-left: 13%; display: inline-flex;' ng-hide='noSortButtons'>" +
"<button ng-disabled='isSelectedOptionDisabled' ng-click='addItemsToTop()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; outline:none !important;margin-bottom:7px;'><i class='glyphicon glyphicon-triangle-top'></i></button>" +
"<button ng-disabled='isSelectedOptionDisabled' ng-click='addItemsToDown()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; outline:none !important;margin-bottom:7px;'><i class='glyphicon glyphicon-triangle-bottom'></i></button>" +
"<button ng-disabled='isSelectedOptionDisabled' title='Ascending/ Descending' ng-click='setAscendingDescendingOrder()' ng-class='buttonClass' ng-style='buttonCssStyle' class='btn' style='width:60px; height: 34px; line-height: 17px; outline:none !important;margin-bottom:7px;'><i ng-class='orderButtonIcon'></i></button>" +
"</div>" +
"</div>" +
"</div>";
$templateCache.put("ngListSelect.html", addRemoveHtml);
}
])
.directive('ngListSelect', ['$filter',
function($filter) {
return {
restrict: 'E',
replace: true,
scope: {
selectedListItems: "=selectedList",
availableListItems: "=availableList",
key: "@key",
buttonStyle: "@buttonStyle",
panelStyle: "@panelStyle",
height: "@height",
width: "@width",
availableLabel: "@availableLabel",
selectedLabel: "@selectedLabel",
noSortButtons: "@noSortButtons"
},
templateUrl: 'ngListSelect.html',
compile: function(tElem, tAttrs) {
return {
pre: function(scope, iElem, iAttrs) {
scope.noSortButtons = angular.isUndefined(scope.noSortButtons) ? false : true;
scope.availableText = angular.isUndefined(scope.availableLabel) ? 'Available' : scope.availableLabel;
scope.selectedText = angular.isUndefined(scope.selectedLabel) ? 'Selected' : scope.selectedLabel;
scope.height = angular.isUndefined(scope.height) ? '144px' : scope.height;
scope.width = angular.isUndefined(scope.width) ? '640px' : scope.width;
scope.buttonStyle = angular.isUndefined(scope.buttonStyle) ? 'alpha' : scope.buttonStyle;
scope.buttonClass = getColor(scope.buttonStyle, 'button');
scope.panelStyle = angular.isUndefined(scope.panelStyle) ? 'alpha' : scope.panelStyle;
scope.panelClass = getColor(scope.panelStyle, 'panel');
scope.leftMouseSelectedItems = [];
scope.rightMouseSelectedItems = [];
scope.dropdownStyle = {
height: scope.height
};
scope.containerStyle = {
width: scope.width
};
scope.ascendingOrderFlag = true;
scope.orderButtonIcon = 'glyphicon glyphicon-sort-by-alphabet';
if (scope.availableListItems[0] instanceof Object) {
scope.leftSelectNgOption = "item as item." + scope.key + " for item in availableListItems | orderBy:'" + scope.key + "'";
scope.rightSelectNgOption = "item as item." + scope.key + " for item in selectedListItems";
scope.availableListItems = getUnique(scope.availableListItems, scope.key);
}
else {
scope.leftSelectNgOption = "item as item for item in availableListItems | orderBy:'toString()'";
scope.rightSelectNgOption = "item as item for item in selectedListItems";
scope.availableListItems = getUnique(scope.availableListItems);
}
function getColor(colorClass, type) {
var data = {}, color = {};
switch (colorClass) {
case 'pearl':
type === 'button' ? (data['btn-default'] = true) : (data['panel-default'] = true);
break;
case 'blue':
type === 'button' ? (data['btn-primary'] = true) : (data['panel-primary'] = true);
break;
case 'alpha':
color = {};
color['background-color'] = 'hsl(193, 32%, 49%) !important';color['background-repeat'] = 'repeat-x';color['filter'] = 'progid:DXImageTransform.Microsoft.gradient(startColorstr="#b8d3da", endColorstr="#5493a4")';color['background-image'] = '-khtml-gradient(linear, left top, left bottom, from(#b8d3da), to(#5493a4))';color['background-image'] = '-moz-linear-gradient(top, #b8d3da, #5493a4)';color['background-image'] = '-ms-linear-gradient(top, #b8d3da, #5493a4)';color['background-image'] = '-webkit-gradient(linear, left top, left bottom, color-stop(0%, #b8d3da), color-stop(100%, #5493a4))';color['background-image'] = '-webkit-linear-gradient(top, #b8d3da, #5493a4)';color['background-image'] = '-o-linear-gradient(top, #b8d3da, #5493a4)';color['background-image'] = 'linear-gradient(#b8d3da, #5493a4)';color['border-color'] = '#5493a4 #5493a4 hsl(193, 32%, 41.5%)';color['color'] = '#333 !important';color['text-shadow'] = '0 1px 1px rgba(255, 255, 255, 0.49)';color['-webkit-font-smoothing'] = 'antialiased';
type === 'button' ? (scope.buttonCssStyle = color) : (scope.panelCssStyle = color);
break;
case 'sand':
color = {};
color['background-color'] = 'hsl(33, 32%, 49%) !important';color['background-repeat'] = 'repeat-x';color['filter'] = 'progid:DXImageTransform.Microsoft.gradient(startColorstr="#dacbb8", endColorstr="#a48054")';color['background-image'] = '-khtml-gradient(linear, left top, left bottom, from(#dacbb8), to(#a48054))';color['background-image'] = '-moz-linear-gradient(top, #dacbb8, #a48054)';color['background-image'] = '-ms-linear-gradient(top, #dacbb8, #a48054)';color['background-image'] = '-webkit-gradient(linear, left top, left bottom, color-stop(0%, #dacbb8), color-stop(100%, #a48054))';color['background-image'] = '-webkit-linear-gradient(top, #dacbb8, #a48054)';color['background-image'] = '-o-linear-gradient(top, #dacbb8, #a48054)';color['background-image'] = 'linear-gradient(#dacbb8, #a48054)';color['border-color'] = '#a48054 #a48054 hsl(33, 32%, 41.5%)';color['color'] = '#333 !important';color['text-shadow'] = '0 1px 1px rgba(255, 255, 255, 0.49)';color['-webkit-font-smoothing'] = 'antialiased';
type === 'button' ? (scope.buttonCssStyle = color) : (scope.panelCssStyle = color);
break;
case 'olive':
color = {};
color['background-color'] = 'hsl(89, 32%, 49%) !important';color['background-repeat'] = 'repeat-x';color['filter'] = 'progid:DXImageTransform.Microsoft.gradient(startColorstr="#cadab8", endColorstr="#7ea454")';color['background-image'] = '-khtml-gradient(linear, left top, left bottom, from(#cadab8), to(#7ea454))';color['background-image'] = '-moz-linear-gradient(top, #cadab8, #7ea454)';color['background-image'] = '-ms-linear-gradient(top, #cadab8, #7ea454)';color['background-image'] = '-webkit-gradient(linear, left top, left bottom, color-stop(0%, #cadab8), color-stop(100%, #7ea454))';color['background-image'] = '-webkit-linear-gradient(top, #cadab8, #7ea454)';color['background-image'] = '-o-linear-gradient(top, #cadab8, #7ea454)';color['background-image'] = 'linear-gradient(#cadab8, #7ea454)';color['border-color'] = '#7ea454 #7ea454 hsl(89, 32%, 41.5%)';color['color'] = '#333 !important';color['text-shadow'] = '0 1px 1px rgba(255, 255, 255, 0.49)';color['-webkit-font-smoothing'] = 'antialiased';
type === 'button' ? (scope.buttonCssStyle = color) : (scope.panelCssStyle = color);
break;
default:
data[colorClass] = true;
}
return data;
}
function getUnique(array, key) {
if (array[0] instanceof Object) {
var object = {};
for (var i = 0; i < array.length; i++){
object[array[i][key]] = array[i];
}
array = [];
for (var objKey in object) {
array.push(object[objKey]);
}
return array;
} else {
return array.sort().filter(function(item, pos, ary) {
return !pos || item != ary[pos - 1];
});
}
}
function orderSelections() {
if (scope.selectedListItems[0] instanceof Object) {
scope.selectedListItems = $filter('orderBy')(scope.selectedListItems, scope.key);
}
else {
scope.selectedListItems = $filter('orderBy')(scope.selectedListItems, 'toString()');
}
}
scope.setAscendingDescendingOrder = function() {
if(scope.ascendingOrderFlag) {
scope.orderButtonIcon = 'glyphicon glyphicon-sort-by-alphabet-alt';
scope.ascendingOrderFlag = false;
if (scope.selectedListItems[0] instanceof Object) {
scope.selectedListItems = setOrdering(scope.key, true);
}
else {
scope.selectedListItems = setOrdering('toString()', true);
}
}
else {
scope.orderButtonIcon = 'glyphicon glyphicon-sort-by-alphabet';
scope.ascendingOrderFlag = true;
if (scope.selectedListItems[0] instanceof Object) {
scope.selectedListItems = setOrdering(scope.key, false);
}
else {
scope.selectedListItems = setOrdering('toString()', false);
}
}
};
function setOrdering(orderVariable, reverse) {
return $filter('orderBy')(scope.selectedListItems, orderVariable, reverse);
}
scope.addItemsToRight = function() {
angular.forEach(scope.leftMouseSelectedItems, function(leftMouseSelectedItem, key) {
scope.selectedListItems.push(leftMouseSelectedItem);
angular.forEach(scope.availableListItems, function(availableListItem, index) {
if (scope.availableListItems[0] instanceof Object) {
if (availableListItem[scope.key] === leftMouseSelectedItem[scope.key]) {
scope.availableListItems.splice(index, 1);
}
} else {
if (availableListItem === leftMouseSelectedItem) {
scope.availableListItems.splice(index, 1);
}
}
});
});
scope.leftMouseSelectedItems = [];
orderSelections();
setSelectedOptionDisabledEnabled();
};
scope.addAllItemsToRight = function() {
angular.forEach(scope.availableListItems, function(availableListItem, key) {
scope.selectedListItems.push(availableListItem);
});
scope.availableListItems = [];
scope.leftMouseSelectedItems = [];
orderSelections();
setSelectedOptionDisabledEnabled();
};
scope.addItemsToLeft = function() {
angular.forEach(scope.rightMouseSelectedItems, function(rightMouseSelectedItem, key) {
scope.availableListItems.push(rightMouseSelectedItem);
angular.forEach(scope.selectedListItems, function(selectedListItem, index) {
if (scope.availableListItems[0] instanceof Object) {
if (selectedListItem[scope.key] === rightMouseSelectedItem[scope.key]) {
scope.selectedListItems.splice(index, 1);
}
} else {
if (selectedListItem === rightMouseSelectedItem) {
scope.selectedListItems.splice(index, 1);
}
}
});
});
scope.rightMouseSelectedItems = [];
setSelectedOptionDisabledEnabled();
};
scope.addAllItemsToLeft = function() {
angular.forEach(scope.selectedListItems, function(selectedListItem, key) {
scope.availableListItems.push(selectedListItem);
});
scope.selectedListItems = [];
scope.rightMouseSelectedItems = [];
setSelectedOptionDisabledEnabled();
};
function setSelectedOptionDisabledEnabled() {
scope.isSelectedOptionDisabled = scope.selectedListItems.length<=0;
}
scope.addItemsToTop = function() {
var prevIndex = -1;
angular.forEach(scope.rightMouseSelectedItems, function(rightMouseSelectedItem, key) {
var itemIndex = scope.selectedListItems.indexOf(rightMouseSelectedItem);
if (itemIndex - 1 === prevIndex) {
prevIndex = itemIndex;
} else if (itemIndex > 0) {
var itemToMove = scope.selectedListItems.splice(itemIndex, 1);
scope.selectedListItems.splice(itemIndex - 1, 0, itemToMove[0]);
}
});
};
scope.addItemsToDown = function() {
var prevIndex = scope.selectedListItems.length;
angular.forEach(scope.rightMouseSelectedItems.concat().reverse(), function(rightMouseSelectedItem, key) {
var itemIndex = scope.selectedListItems.indexOf(rightMouseSelectedItem);
if (itemIndex + 1 === prevIndex) {
prevIndex = itemIndex;
} else if (itemIndex < scope.selectedListItems.length - 1) {
var itemToMove = scope.selectedListItems.splice(itemIndex, 1);
scope.selectedListItems.splice(itemIndex + 1, 0, itemToMove[0]);
}
});
};
setSelectedOptionDisabledEnabled();
}
};
}
};
}
]);
})(window, window.angular);