-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.qs.js
143 lines (116 loc) · 3.04 KB
/
jquery.qs.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
jQuery(function ($) {
$.fn.qs = function (target, opt) {
var timeout, cache, rowcache, jq_results, e = this, options = $.extend({
delay: 200,
selector: null,
stripeRows: null,
loader: null,
noResults: '',
onBefore: function () {
return;
},
onAfter: function () {
return;
},
filter: function (i) {
return i;
}
}, opt);
this.go = function (val) {
var i = 0, noresults = true, vals = val.split(' ');
var rowcache_length = rowcache.length;
for (var i = 0; i < rowcache_length; i++)
{
if (this.test(vals, cache[i]) || val == "") {
rowcache[i].style.display = "";
noresults = false;
} else {
rowcache[i].style.display = "none";
}
}
if (noresults) {
this.results(false);
} else {
this.results(true);
this.stripe();
}
this.loader(false);
options.onAfter();
return this;
};
this.stripe = function () {
if (typeof options.stripeRows === "object" && options.stripeRows !== null)
{
var joined = options.stripeRows.join(' ');
var stripeRows_length = options.stripeRows.length;
jq_results.not(':hidden').each(function (i) {
$(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]);
});
}
return this;
};
this.strip_html = function (input) {
var output = input.replace(new RegExp('/<[^<]+\>/g'), "");
output = $.trim(output.toLowerCase());
return output;
};
this.results = function (bool) {
if (typeof options.noResults === "string" && options.noResults !== "") {
if (bool) {
$(options.noResults).hide();
} else {
$(options.noResults).show();
}
}
return this;
};
this.loader = function (bool) {
if (typeof options.loader === "string" && options.loader !== "") {
(bool) ? $(options.loader).show() : $(options.loader).hide();
}
return this;
};
this.test = function (vals, t) {
for (var i = 0; i < vals.length; i += 1) {
if (t.indexOf(vals[i]) === -1) {
return false;
}
}
return true;
};
this.cache = function () {
jq_results = $(target);
if (typeof options.noResults === "string" && options.noResults !== "") {
jq_results = jq_results.not(options.noResults);
}
var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults);
cache = t.map(function () {
return e.strip_html(this.innerHTML);
});
rowcache = jq_results.map(function () {
return this;
});
return this;
};
this.trigger = function (val) {
this.loader(true);
options.onBefore();
window.clearTimeout(timeout);
timeout = window.setTimeout(function () {
console.time('go_' + val);
e.go(val);
console.timeEnd('go_' + val);
}, options.delay);
};
this.cache();
this.results(true);
this.stripe();
this.loader(false);
return this.each(function () {
$(this).bind('keyup', function () {
var val = $(this).val();
e.trigger(val);
});
});
};
});