This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathautocomplete.js
executable file
·303 lines (280 loc) · 11.5 KB
/
autocomplete.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
'use strict';
/*global angular, $, setTimeout*/
/*
* AngularJS Autocomplete, version 0.6.0
* Wrapper for the jQuery UI Autocomplete Widget - v1.11.2
* API @ http://api.jqueryui.com/autocomplete/
*
* <input type="text" ng-model="modelObj" ui-autocomplete="myOptions">
* $scope.myOptions = {
* options: {
* html: false, // boolean, uiAutocomplete extend, if true, you can use html string or DOM object in data.label for source
* onlySelectValid: false, // boolean, uiAutocomplete extend, if true, element value must be selected from suggestion menu, otherwise set validity of 'onlyselect' to false.
* focusOpen: false, // boolean, uiAutocomplete extend, if true, the suggestion menu auto open with all source data when focus
* groupLabel: null, // html string or DOM object, uiAutocomplete extend, it is used to group suggestion result, it can't be seleted.
* outHeight: 0, // number, uiAutocomplete extend, it is used to adjust suggestion menu' css style "max-height".
* appendTo: null, // jQuery UI Autocomplete Widget Options, the same below. http://api.jqueryui.com/autocomplete/#option
* autoFocus: false,
* delay: 300,
* disabled: false,
* minLength: 1,
* position: { my: "left top", at: "left bottom", collision: "none" },
* source: undefined // must be specified
* },
* events: // jQuery UI Autocomplete Widget Events, http://api.jqueryui.com/autocomplete/#event
* methods: // extend jQuery UI Autocomplete Widget Methods to AngularJS, http://api.jqueryui.com/autocomplete/#methods
* // then you can invoke methods like this: $scope.myOptions.methods.search('term');
* // add a new method 'filter' for filtering source data in AngularJS controller
* };
*/
angular.module('ui.autocomplete', [])
.directive('uiAutocomplete', ['$timeout', '$exceptionHandler',
function ($timeout, $exceptionHandler) {
var proto = $.ui.autocomplete.prototype;
var initSource = proto._initSource;
var slice = Array.prototype.slice;
function filter(array, term) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(term), 'i');
return $.grep(array, function (value) {
return matcher.test($('<div>').html(value.label || value.value || value).text());
});
}
$.extend(proto, {
_initSource: function () {
if (this.options.html && $.isArray(this.options.source)) {
this.source = function (request, response) {
response(filter(this.options.source, request.term));
};
} else {
initSource.call(this);
}
},
_normalize: function (items) {
// assume all items have the right format
return $.map(items, function (item) {
if (item && typeof item === "object") {
return $.extend({
label: item.label || item.value,
value: item.value || item.label
}, item);
} else {
return {
label: item + '',
value: item
};
}
});
},
_renderItemData: function (ul, item) {
var element = item.groupLabel || item.label;
if (item.groupLabel) {
element = $('<div>').append(element).addClass('ui-menu-group');
} else if (this.options.html) {
if (typeof element === 'object') {
element = $(element);
}
if (typeof element !== 'object' || element.length > 1 || !element.is('a')) {
element = $('<a>').append(element);
}
} else {
element = $('<a>').text(element);
}
return $('<li>').append(element).appendTo(ul).data('ui-autocomplete-item', item);
},
_resizeMenu: function () {
var that = this;
setTimeout(function () {
var ul = that.menu.element;
var maxHeight = ul.css('max-height') || 0,
width = Math.max(
ul.width('').outerWidth() + 1,
that.element.outerWidth()),
oHeight = that.element.height(),
height = $(window).height() - that.options.outHeight - ul.offset().top;
height = maxHeight && height > maxHeight ? maxHeight : height;
ul.css({
width: width,
maxHeight: height
});
}, 10);
}
});
return {
require: 'ngModel',
link: function (scope, element, attr, ctrl) {
var status = false,
selectItem = null,
events = {},
ngModel = null,
each = angular.forEach,
isObject = angular.isObject,
extend = angular.extend,
autocomplete = scope.$eval(attr.uiAutocomplete),
valueMethod = angular.bind(element, element.val),
methodsName = ['close', 'destroy', 'disable', 'enable', 'instance', 'option', 'search', 'widget'],
eventsName = ['change', 'close', 'create', 'focus', 'open', 'response', 'search', 'select'];
var unregisterWatchModel = scope.$watch(attr.ngModel, function (value) {
ngModel = value;
if (isObject(ngModel)) {
// not only primitive type ngModel, you can also use object type ngModel!
// there must have a property 'value' in ngModel if object type
ctrl.$formatters.push(function (obj) {
return obj.value;
});
ctrl.$parsers.push(function (value) {
ngModel.value = value;
return ngModel;
});
scope.$watch(attr.ngModel, function (model) {
if (valueMethod() !== model.value) {
ctrl.$viewValue = model.value;
ctrl.$render();
}
}, true);
ctrl.$pristine = false; // hack:prevent change to dirty
ctrl.$setViewValue(ngModel.value);
ctrl.$pristine = true;
}
if (value) {
// unregister the watch after get value
unregisterWatchModel();
}
});
var uiEvents = {
open: function (event, ui) {
status = true;
selectItem = null;
},
close: function (event, ui) {
status = false;
},
select: function (event, ui) {
selectItem = ui;
$timeout(function () {
element.blur();
}, 0);
},
change: function (event, ui) {
// update view value and Model value
var value = valueMethod(),
selected = false;
if (selectItem && selectItem.item && (value.indexOf(selectItem.item.value) !== -1)) {
value = selectItem.item.value;
selected = true;
}
scope.$apply(function () {
if (autocomplete.options.onlySelectValid) {
ctrl.$setValidity('onlyselect', selected);
}
if (value === null) {
ctrl.$render();
} else if (value === '') {
changeNgModel();
} else if (ctrl.$viewValue !== value) {
ctrl.$setViewValue(value);
ctrl.$render();
changeNgModel(selectItem);
}
});
}
};
function changeNgModel(data) {
if (isObject(ngModel)) {
if (!ctrl.$viewValue && ctrl.$viewValue !== 0) {
emptyObj(ngModel);
} else if (data && data.item) {
data.item.label = isObject(data.item.label) ? $('<div>').append(data.item.label).html() : data.item.label;
extend(ngModel, data.item);
}
each(ctrl.$viewChangeListeners, function (listener) {
try {
listener();
} catch (e) {
$exceptionHandler(e);
}
});
}
}
function cleanNgModel() {
ctrl.$setViewValue('');
ctrl.$render();
changeNgModel();
}
function autoFocusHandler() {
if (autocomplete.options.focusOpen && !status) {
element.autocomplete('search', '');
}
}
function checkOptions(options) {
options = isObject(options) ? options : {};
// if source not set, disabled autocomplete
options.disabled = options.source ? options.disabled : true;
// if focusOpen, minLength must be 0
options.appendTo = options.appendTo || element.parents('.ng-view')[0] || element.parents('[ng-view]')[0] || null;
options.minLength = options.focusOpen ? 0 : options.minLength;
options.outHeight = options.outHeight || 0;
options.position = options.position || {
my: 'left top',
at: 'left bottom',
collision: 'flipfit'
};
return options;
}
function emptyObj(a) {
if (isObject(a)) {
var reg = /^\$/;
each(a, function (value, key) {
var type = typeof value;
if (reg.test(key)) {
return; // don't clean private property of AngularJS
} else if (type === 'number') {
a[key] = 0;
} else if (type === 'string') {
a[key] = '';
} else if (type === 'boolean') {
a[key] = false;
} else if (isObject(value)) {
emptyObj(value);
}
});
}
}
if (!isObject(autocomplete)) {
return;
}
autocomplete.methods = {};
autocomplete.options = checkOptions(autocomplete.options);
// extend events to Autocomplete
each(eventsName, function (name) {
var _event = autocomplete.options[name];
_event = typeof _event === 'function' ? _event : angular.noop;
events[name] = function (event, ui) {
if (uiEvents[name]) {
uiEvents[name](event, ui);
}
_event(event, ui);
if (autocomplete.events && typeof autocomplete.events[name] === 'function') {
autocomplete.events[name](event, ui);
}
};
});
// extend Autocomplete methods to AngularJS
each(methodsName, function (name) {
autocomplete.methods[name] = function () {
var args = slice.call(arguments);
args.unshift(name);
return element.autocomplete.apply(element, args);
};
});
// add filter method to AngularJS
autocomplete.methods.filter = filter;
autocomplete.methods.clean = cleanNgModel;
element.on('focus', autoFocusHandler);
element.autocomplete(extend({}, autocomplete.options, events));
autocomplete.widget = element.autocomplete('widget');
// remove default class, use bootstrap style
// autocomplete.widget.removeClass('ui-menu ui-corner-all ui-widget-content').addClass('dropdown-menu');
}
};
}
]);