forked from commadelimited/autoComplete.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqm.autoComplete-1.3.js
executable file
·131 lines (125 loc) · 3.62 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
/*
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
*/
(function($) {
"use strict";
var defaults = {
target: $(),
source: null,
callback: null,
link: null,
minLength: 0,
transition: 'fade'
},
buildItems = function($this, data, settings) {
var str = [];
$.each(data, function(index, value) {
var urlParam, text;
// are we working with objects or strings?
if ($.isPlainObject(value)) {
urlParam = encodeURIComponent(value.value);
text = value.label;
} else {
urlParam = encodeURIComponent(value);
text = value;
}
urlParam = urlParam.replace('\'', '%27').replace('(', '%28').replace(')', '%29');
str.push('<li><a href="' + settings.link + urlParam + '" data-transition="' + settings.transition + '">' + text + '</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);
});
},
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();
// 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)) {
data = settings.source.sort().filter(function(element) {
var element_text, re = new RegExp('^' + text, 'i');
if ($.isPlainObject(element)) {
element_text = element.label;
} 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);