forked from vdw/HideSeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.hideseek-mod.js
282 lines (201 loc) · 7.34 KB
/
jquery.hideseek-mod.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* HideSeek-Mod 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.5
*
* 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 ($, hideseek) {
"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,
throttle: 0,
wildcards: false
};
var options = $.extend(defaults, options);
return this.each(function () {
var $this = $(this);
$this.opts = [];
$this.state = {};
$.map(['list', 'nodata', 'attribute', 'matches', 'highlight', 'ignore', 'headers', 'navigation', 'ignore_accents', 'hidden_mode', 'min_chars', 'throttle', 'wildcards'], 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 getNormalizedText = function (text) {
text = text.toLowerCase();
return $this.opts.ignore_accents ? hideseek.removeAccents(text) : text;
};
var $list = $($this.opts.list);
if ($this.opts.navigation) $this.attr('autocomplete', 'off');
if ($this.opts.hidden_mode) $list.children().hide();
$this.keyup(throttle(function (e) {
var q = getNormalizedText($this.val());
if ($this.opts.min_chars && q.length < $this.opts.min_chars) {
// if query is too short show initial set of results
q = "";
}
if (shouldHandleQueryChange($this, q)) {
var isQueryFound = function (text) {
return text.indexOf(q) != -1;
}
if ($this.opts.wildcards && q.indexOf('*') !== -1 && q.length > 1) {
// convert query to regexp by escaping and replacing '*'
var regExpString = (q[0] == '*' ? '' : '\\b') + q.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '\\w*') + (q[q.length - 1] == '*' ? '' : '\\b');
var pattern = new RegExp(regExpString);
isQueryFound = function (text) {
return pattern.test(text);
}
}
$list.children($this.opts.ignore.trim() ? ':not(' + $this.opts.ignore + ')' : '').removeClass('selected').each(function () {
var data = getNormalizedText(
$this.opts.attribute != 'text'
? ($(this).attr($this.opts.attribute) || '')
: $(this).text()
);
var treaty = !isQueryFound(data) || q === ($this.opts.hidden_mode ? '' : false)
if (treaty) {
$(this).hide();
} else {
// TODO consider removing in the future
// matches is a map - when key matches query then the text is tested against value
// both keys and values from the map/matches object are treated as RegExp patterns
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();
}
}
else {
show_element($(this));
}
}
$this.trigger('_after_each');
});
// No results message
// When hidden_mode is on we show no results msg only when query is not empty
if ($this.opts.nodata && (!$this.opts.hidden_mode || q)) {
$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, $this.opts.ignore_accents).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());
};
};
};
}, $this.opts.throttle));
});
};
function throttle(func, time) {
if (!time || typeof time !== "number" || time <= 0) {
return func;
}
var throttleTimer = 0;
return function () {
var args = arguments;
clearTimeout(throttleTimer);
throttleTimer = setTimeout(function () {
func.apply(null, args);
}, time);
}
}
function shouldHandleQueryChange($this, q) {
if ($this.state.lastQuery == q) {
return false;
}
$this.state.lastQuery = q;
return true;
}
$(document).ready(function () { $('[data-toggle="hideseek"]').hideseek(); });
})(jQuery, window["hideseek"] = window["hideseek"] || {});