forked from commadelimited/autoComplete.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqm.autoComplete-1.3.js~
executable file
·152 lines (141 loc) · 4.42 KB
/
jqm.autoComplete-1.3.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
/*
Name: autoComplete
Author: Raymond Camden & Andy Matthews
Contributors: Jim Pease (@jmpease)
Website: http://raymondcamden.com/
http://andyMatthews.net
Packed With: http://jsutility.pjoneil.net/
Version: 1.3
Modified for wildcard support and caseSensitive searching
inspired by Tom Coote jsonSuggest jQuery plugin
http://tomcoote.co.uk/javascript/jquery-json-suggestsearch-box-v2/
*/
(function($) {
"use strict";
var defaults = {
target: $(),
source: null,
callback: null,
link: null,
wildcard: '',
caseSensitive: false,
minLength: 0,
transition: 'fade'
},
buildItems = function($this, data, settings) {
var str = [];
$.each(data, function(index, value) {
// are we working with objects or strings?
if ($.isPlainObject(value)) {
str.push("<li><a href='javascript:selectItem(" + value.areaid + ',"' + value.name + "\")'>" + value.name + "</a></li>");
} else {
str.push("<li><a href='javascript:selectItem(" + areaid + ',"' + areaid + "\")'>" + areaid + "</a></li>");
}
});
$(settings.target).html(str.join('')).listview("refresh");
// is there a callback?
if (settings.callback !== null && $.isFunction(settings.callback)) {
attachCallback(settings);
}
if (str.length > 0) {
$this.trigger("targetUpdated.autocomplete");
} else {
$this.trigger("targetCleared.autocomplete");
}
},
attachCallback = function(settings) {
$('li a', $(settings.target)).bind('click.autocomplete',function(e){
e.stopPropagation();
e.preventDefault();
settings.callback(e);
});
},
regexEscape = function(txt, omit) {
var specials = ['/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\'];
if (omit) {
for (var i = 0; i < specials.length; i++) {
if (specials[i] === omit) { specials.splice(i,1); }
}
}
var escapePatt = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
return txt.replace(escapePatt, '\\$1');
},
clearTarget = function($this, $target) {
$target.html('').listview('refresh');
$this.trigger("targetCleared.autocomplete");
},
handleInput = function(e) {
var $this = $(this), text, data, settings = $this.jqmData("autocomplete");
if (settings) {
// get the current text of the input field
text = $this.val();
var filterTxt = (!settings.wildcard) ? regexEscape(text) : regexEscape(text, settings.wildcard).replace(wildCardPatt, '.*');
filterTxt = filterTxt || '.*';
filterTxt = settings.wildcard ? '^' + filterTxt : filterTxt;
// if we don't have enough text zero out the target
if (text.length < settings.minLength) {
clearTarget($this, $(settings.target));
} else {
// are we looking at a source array or remote data?
if ($.isArray(settings.source)) {
var wildCardPatt = new RegExp(regexEscape(settings.wildcard || ''),'g');
data = settings.source.sort().filter(function(element) {
var element_text,
re = settings.caseSensitive ? new RegExp(filterTxt) : new RegExp(filterTxt, 'i');
if ($.isPlainObject(element)) {
element_text = element.name;
} else {
element_text = element;
}
return re.test(element_text);
});
buildItems($this, data, settings);
} else {
$.get(settings.source, { term: text }, function(data) {
buildItems($this, data, settings);
},"json");
}
}
}
},
methods = {
init: function(options) {
this.jqmData("autocomplete", $.extend({}, defaults, options));
return this.unbind("input.autocomplete").bind("input.autocomplete", handleInput);
},
// Allow dynamic update of source and link
update: function(options) {
var settings = this.jqmData("autocomplete");
if (settings) {
this.jqmData("autocomplete", $.extend(settings, options));
}
return this;
},
// Method to forcibly clear our target
clear: function() {
var settings = this.jqmData("autocomplete");
if (settings) {
clearTarget(this, $(settings.target));
}
return this;
},
// Method to destroy (cleanup) plugin
destroy: function() {
var settings = this.jqmData("autocomplete");
if (settings) {
clearTarget(this, $(settings.target));
this.jqmRemoveData("autocomplete");
this.unbind(".autocomplete");
}
return this;
}
};
$.fn.autocomplete = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
};
})(jQuery);