-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.mobile.instantsearch.js
90 lines (83 loc) · 2.74 KB
/
jquery.mobile.instantsearch.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
(function ($, window, undefined) {
$.widget("mobile.instantsearch", $.mobile.widget, {
inputField: undefined, //Search inupt
resultDisplay: undefined, //Result div
timeout: undefined,
options: {
delay: 500,
instantsearchResultboxid: null
},
_init: function () {
// Avoid multiple initialisation
if(typeof this.element.data('instantsearch_initialized') !== "undefined") {
return;
}
this.element.data('instantsearch_initialized', true);
if (this.options.instantsearchResultboxid !== null) {
this.resultDisplay = $('#' + this.options.instantsearchResultboxid);
} else {
this.resultDisplay = $('<div>');
this.element.after(this.resultDisplay);
}
this.element.submit(function () {
return false;
});
this.element.bind('search', $.proxy(this.delaysearch, this));
this.element.bind('clearResult', $.proxy(this.clearResult, this));
// Clear resultbox when the clear button of a search input is clicked
$('.ui-input-clear', this.element).bind('click', $.proxy(this.clearResult, this));
this.inputField = this.element.find(':jqmData(type="search")');
this.inputField.bind('keyup', $.proxy(this.delaysearch, this));
},
delaysearch: function () {
if (typeof this.timeout !== 'undefined') {
window.clearTimeout(this.timeout);
}
this.timeout = window.setTimeout($.proxy(this.submit, this), this.options.delay);
},
clearResult: function () {
this.resultDisplay.empty();
},
submit: function () {
$.ajax({
url: this.element.attr('action'),
context: this,
dataType: 'text',
type: this.element.attr('method'),
data: this.element.serialize(),
beforeSend: function () {
this.inputField.addClass('loading');
},
complete: function () {
this.inputField.removeClass('loading');
},
error: function (xhr) {
var message = "Sorry, your request could not be fullfilled.\nStatus Text:" + xhr.statusText + "\nStatus Code: " + xhr.status;
alert( message );
},
success: function (html) {
var content = $( html.trim() );
this.resultDisplay.html(content);
// Enhance the content if necessary
if ( typeof(content.attr('data-role')) !== 'undefined' ) {
var pluginName = content.attr('data-role');
content[pluginName]();
}
// Enhance the children of content if necessary
$.each(content.find('[data-role]'), function() {
var item = $(this)
, pluginName = item.attr('data-role');
if ( typeof item[pluginName] === 'function' ) {
item[pluginName]();
}
});
}
});
}
});
$.mobile.document.bind( "pagecreate", function( e ) {
$(":jqmData(role='instantsearch')", this).each(function () {
$(this).instantsearch();
});
});
})(jQuery, this);