-
Notifications
You must be signed in to change notification settings - Fork 74
/
jquery.hideseek.js
252 lines (171 loc) · 6.95 KB
/
jquery.hideseek.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
/**
* HideSeek jQuery plugin
*
* @copyright Copyright 2015, Dimitris Krestos
* @license Apache License, Version 2.0 (http://www.opensource.org/licenses/apache2.0.php)
* @link http://vdw.staytuned.gr
* @version v0.8.0
*
* Dependencies are include in minified versions at the bottom:
* 1. Highlight v4 by Johann Burkard
*
*/
/* Sample html structure
<input name="search" placeholder="Start typing here" type="text" data-list=".list">
<ul class="list">
<li>item 1</li>
<li>...</li>
<li><a href="#">item 2</a></li>
</ul>
or
<input name="search" placeholder="Start typing here" type="text" data-list=".list">
<div class="list">
<span>item 1</span>
<span>...</span>
<span>item 2</span>
</div>
or any similar structure...
*/
;(function($, window, undefined) {
"use strict";
$.fn.hideseek = function(options) {
var defaults = {
list: '.hideseek-data',
nodata: '',
attribute: 'text',
matches: false,
highlight: false,
ignore: '',
headers: '',
navigation: false,
ignore_accents: false,
hidden_mode: false,
min_chars: 1
};
var options = $.extend(defaults, options);
return this.each(function() {
var $this = $(this);
$this.opts = [];
$.map( ['list', 'nodata', 'attribute', 'matches', 'highlight', 'ignore', 'headers', 'navigation', 'ignore_accents', 'hidden_mode', 'min_chars'], function( val, i ) {
$this.opts[val] = $this.data(val) || options[val];
} );
if ($this.opts.headers)
$this.opts.ignore += $this.opts.ignore ? ', ' + $this.opts.headers : $this.opts.headers;
var $list = $($this.opts.list);
if ($this.opts.navigation) $this.attr('autocomplete', 'off');
if ($this.opts.hidden_mode) $list.children().hide();
$this.keyup(function(e) {
if ( [38, 40, 13].indexOf(e.keyCode) == -1 && ( e.keyCode != 8 ? $this.val().length >= $this.opts.min_chars : true ) ) {
var q = $this.val().toLowerCase();
$list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() {
var data = (
$this.opts.attribute != 'text'
? ($(this).attr($this.opts.attribute) || '')
: $(this).text()
).toLowerCase();
var treaty = data.removeAccents($this.opts.ignore_accents).indexOf(q) == -1 || q === ($this.opts.hidden_mode ? '' : false)
if (treaty) {
$(this).hide();
} else {
show_element($(this));
if ($this.opts.matches && q.match(new RegExp(Object.keys($this.opts.matches)[0])) !== null) {
if (data.match(new RegExp(Object.values($this.opts.matches)[0])) !== null) {
show_element($(this));
} else {
$(this).hide();
}
}
}
$this.trigger('_after_each');
});
// No results message
if ($this.opts.nodata) {
$list.find('.no-results').remove();
if (!$list.children(':not([style*="display: none"])').length) {
$list
.children()
.first()
.clone()
.removeHighlight()
.addClass('no-results')
.show()
.prependTo($this.opts.list)
.text($this.opts.nodata);
$this.trigger('_after_nodata');
}
}
// hide headers with no results
if ($this.opts.headers) {
$($this.opts.headers, $list).each(function() {
if (!$(this).nextUntil($this.opts.headers).not('[style*="display: none"],' + $this.opts.ignore).length) {
$(this).hide();
} else {
$(this).show();
}
});
}
$this.trigger('_after');
};
function show_element(element) {
$this.opts.highlight ? element.removeHighlight().highlight(q).show() : element.show();
}
// Navigation
function current(element) {
return element.children('.selected:visible');
};
function prev(element) {
return current(element).prevAll(":visible:first");
};
function next(element) {
return current(element).nextAll(":visible:first");
};
if ($this.opts.navigation) {
if (e.keyCode == 38) {
if (current($list).length) {
prev($list).addClass('selected');
current($list).last().removeClass('selected');
} else {
$list.children(':visible').last().addClass('selected');
};
} else if (e.keyCode == 40) {
if (current($list).length) {
next($list).addClass('selected');
current($list).first().removeClass('selected');
} else {
$list.children(':visible').first().addClass('selected');
};
} else if (e.keyCode == 13) {
if (current($list).find('a').length) {
document.location = current($list).find('a').attr('href');
} else {
$this.val(current($list).text());
};
};
};
});
});
};
$(document).ready(function () { $('[data-toggle="hideseek"]').hideseek(); });
})(jQuery);
/*
highlight v4
Highlights arbitrary terms.
<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
MIT license.
Johann Burkard
<http://johannburkard.de>
<mailto:[email protected]>
*/
jQuery.fn.highlight=function(t){function e(t,i){var n=0;if(3==t.nodeType){var a=t.data.removeAccents(true).toUpperCase().indexOf(i);if(a>=0){var s=document.createElement("mark");s.className="highlight";var r=t.splitText(a);r.splitText(i.length);var o=r.cloneNode(!0);s.appendChild(o),r.parentNode.replaceChild(s,r),n=1}}else if(1==t.nodeType&&t.childNodes&&!/(script|style)/i.test(t.tagName))for(var h=0;h<t.childNodes.length;++h)h+=e(t.childNodes[h],i);return n}return this.length&&t&&t.length?this.each(function(){e(this,t.toUpperCase())}):this},jQuery.fn.removeHighlight=function(){return this.find("mark.highlight").each(function(){with(this.parentNode.firstChild.nodeName,this.parentNode)replaceChild(this.firstChild,this),normalize()}).end()};
// Ignore accents
String.prototype.removeAccents = function(enabled) {
if (enabled) return this
.replace(/[áàãâä]/gi,"a")
.replace(/[éè¨ê]/gi,"e")
.replace(/[íìïî]/gi,"i")
.replace(/[óòöôõ]/gi,"o")
.replace(/[úùüû]/gi, "u")
.replace(/[ç]/gi, "c")
.replace(/[ñ]/gi, "n");
return this;
};