-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectbox.js
301 lines (248 loc) · 12.6 KB
/
selectbox.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
define(["app"], function(app) {
app.filter('myFilter', [function() {
return function(input, q) {
var finals = [];
input.map(function(elem, index) {
if (elem.cat.search(new RegExp(q, "gi")) > -1) {
finals.push(elem);
}
});
return finals;
};
}]);
app.directive('selectbox', ['$sce', '$http', '$compile', '$filter', '$document', function($sce, $http, $compile, $filter, $document) {
var quieryApi = function(q, $scope, isHighlight) {
$scope.showloading = true;
$http({
url: $scope.apiAddress,
method: "GET",
params: {
q: q
}
}).success(function(data) {
if (isHighlight) {
angular.forEach(data.extra, function(value, key) {
var index = value.cat.indexOf(q);
var result = value.cat.insert(index, "<strong>");
index = result.indexOf(q);
var result = result.insert(index + q.length, "</strong>");
data.extra[key].cat = result;
});
}
$scope.showloading = false;
$scope.cats = data.extra;
})
};
return {
transclude: true,
scope: {
options: "=",
initialOpen: "@",
apiAddress: "@",
searchPlaceholder: "@",
selectedObjectArray: "=selectedObjectArray",
initialSearchApi: "@"
},
controller: [
'$scope', '$element', '$attrs', '$transclude',
function($scope, $element, $attrs, $transclude) {
var hastTranscludedItems = false;
function addToCats(key, value) {
$scope.cats.push({
_id: "id_" + key,
cat: value
});
cats.push({
_id: "id_" + key,
cat: value
});
}
$scope.title = $attrs.title;
if (!$scope.searchPlaceholder) {
$scope.searchPlaceholder = "Start typing ...";
}
$scope.status = {
isopen: false
};
$scope.dropdown = function() {
$scope.status.isopen = !$scope.status.isopen;
}
var docClickHandler = function(event) {
$scope.status.isopen = false;
event.preventDefault();
$scope.$apply();
};
//$document.on("click",docClickHandler);
/*$scope.$on('$destroy', function(){
$document.off('click', docClickHandler);
});*/
var myFilter = $filter("myFilter");
var preFilterItems = [];
var multiple = false;
$scope.cats = [];
var cats = [];
$scope.q = "";
$scope.showloading = false;
var addedTags = 0;
var notAddedTags = 0;
var notAddedTagsItems = [];
var toggleDiv, tagsWrapper, toggleDivWidth, tagsWrapperWidth;
// First get all attrs and initialize
if ($scope.initialOpen == "true") {
$scope.status.isopen = true;
}
if ($attrs.multiple != undefined) {
multiple = true;
}
if (!$scope.apiAddress) {
if ($scope.options) {
if (typeof($scope.options[0]) === 'object') {
var k;
for (var index in $scope.options[0]) { // First we must know what is the key in each item object
k = index;
break;
};
// Why we do this ? because in our ng-options we've specifed a cat.cat , which we're not sure if the array that comes from the api
// is like that . so we need to simply transorm the object into our desired objects
for (var i = 0; i < $scope.options.length; i++) { // Then we can loop and get the object that what we actually need
addToCats(k, $scope.options[i][k]);
}
} else {
for (var i = 0; i < $scope.options.length; i++) {
addToCats(i, $scope.options[i]);
}
}
}
$transclude(function(clone, scope) {
angular.forEach(clone, function(elem, index) {
if (elem != "" && elem.nodeName.toLowerCase() === "option") {
hastTranscludedItems = true;
addToCats(elem.value, elem.innerHTML);
}
});
});
}
$scope.remove = function(id, $event) {
$event.stopPropagation()
$scope.selectedObjectArray.forEach(function(element, index) {
if (id === element._id) {
$scope.cats.push(element);
$scope.selectedObjectArray.splice(index, 1);
}
});
angular.element(document.querySelector('span#item_' + id)).remove();
tagsWrapperWidth = tagsWrapper[0].offsetWidth;
if (notAddedTagsItems.length > 0 && tagsWrapperWidth < (toggleDivWidth - 300)) {
$el = '<span id="item_' + notAddedTagsItems[0]._id + '" class="tag">' + notAddedTagsItems[0].cat + '<span ng-click=remove("' + notAddedTagsItems[0]._id + '",$event) class="remove"> x </span></span>';
tagsWrapper.prepend($compile($el)($scope));
notAddedTagsItems.splice(0, 1);
notAddedTags--;
addedTags++;
if (notAddedTags > 0) {
angular.element(document.querySelector('span#notAddedTags')).html(" " + notAddedTags + " more selected");
} else {
angular.element(document.querySelector('span#notAddedTags')).remove();
}
}
if ($scope.selectedObjectArray.length === 0) {
$scope.title = $attrs.title;
}
return false;
}
$scope.addItem = function(item) {
if (toggleDiv === undefined) {
toggleDiv = angular.element(document.querySelector('div.togglerdiv'));
toggleDivWidth = toggleDiv[0].offsetWidth;
}
if (tagsWrapper === undefined) {
tagsWrapper = angular.element(document.querySelector('div.tagsWrapper'));
}
tagsWrapperWidth = tagsWrapper[0].offsetWidth;
if (multiple) {
// First , add this new Item to selected Items
$scope.selectedObjectArray.push(item);
// Then remove this item from all Items ==> means from $scope.cats
if (tagsWrapperWidth < (toggleDivWidth - 300)) {
addedTags++;
$el = $compile('<span id="item_' + item._id + '" class="tag">' + item.cat + '<span ng-click=remove("' + item._id + '",$event) class="remove"> x </span></span>')($scope);
tagsWrapper.append($el);
} else {
notAddedTagsItems.push(item);
if (notAddedTags > 0) {
notAddedTags++;
angular.element(document.querySelector('span#notAddedTags')).html(" " + notAddedTags + " more selected");
} else {
notAddedTags++;
tagsWrapper.append('<span id="notAddedTags"> ' + notAddedTags + ' more selected</span>');
}
}
$scope.title = "";
} else {
// First , add this new Item to selected Items
if ($scope.selectedObjectArray[0]) {
$scope.cats.push($scope.selectedObjectArray[0]);
}
$scope.selectedObjectArray[0] = item;
//$scope.title = $sce.trustAsHtml(item.cat);
$scope.title = item.cat;
}
$scope.cats.forEach(function(element, index) {
if (element._id === item._id && element.cat === item.cat) {
$scope.cats.splice(index, 1);
return;
}
});
}
if ($attrs.preselected) {
if (isNaN($attrs.preselected)) {
$scope.cats.forEach(function(element, index) {
if (element._id === $attrs.preselected || element.cat === $attrs.preselected) {
$scope.addItem(element);
return;
}
});
} else {
$scope.addItem($scope.cats[$attrs.preselected]);
}
}
$scope.search = function(keyEvent) {
if (keyEvent.keyCode == 32) { // if the key is backspace , do nothing
return;
}
var q = $scope.q.trim();
if (q == "") {
if ($scope.options) {
$scope.cats = myFilter(cats, q, preFilterItems);
}
return;
}
if (keyEvent.keyCode == 40) { // if user has pressed down arrow key
var ul = angular.element(document.querySelector('ul.resultlist'));
//$log.info(ul.children()[0].triggerHandler('click'));
} else {
if ($scope.options || hastTranscludedItems) {
$scope.cats = myFilter(cats, q, preFilterItems);
} else {
quieryApi(q, $scope, true);
}
}
}
if ($scope.apiAddress) { // This means this selext box wants to connect to API for it's options
// if user wants to get some options when this selexbox is created , from Api !
if ($scope.initialSearchApi) {
quieryApi($scope.initialSearchApi, $scope, false);
}
}
}
],
restrict: 'E',
templateUrl: 'app/views/components/selectbox.html',
replace: true,
link: function($scope, elem, attrs) {
elem.bind("click", function() {
elem[0].querySelector('input#selectboxinput').focus();
});
}
};
}]);
});