-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
477 lines (405 loc) · 14.8 KB
/
Copy pathindex.html
File metadata and controls
477 lines (405 loc) · 14.8 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Kamil | Simple and highly customisable autocomplete</title>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/prism.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="kamil.css">
<style>
.kamil-autocomplete li.kamil-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
background-color: #dadada;
cursor: default;
}
</style>
</head>
<body class="language-markup">
<header>
<h1>Kamil autocomplete</h1>
<h2>
Dependency-free and highly customisable autocomplete widget
</h2>
<nav>
<div class="nav-wrapper">
<ul class="left">
<li><a href="#basic-usage">Basic usage</a></li>
<li><a href="#customisation">Customisation</a></li>
<li><a href="#extension-points">Extension points</a></li>
<li><a href="#methods">Methods</a></li>
<li><a href="#events">Events</a></li>
<li><a href="#download-contribute">Download and contribute</a></li>
<li><a href="tests/index.html">Tests</a></li>
</ul>
</div>
</nav>
</header>
<div class="content">
<div class="demo">
<h1 id="basic-usage">Basic usage</h1>
<p>Install the library through NPM:</p>
<pre class="language-shell">
<code>
$ npm install kamil --save
</code>
</pre>
<p>or download the <a href="https://github.com/oss6/kamil/archive/v0.2.1.zip">zip</a> file.</p>
<p>In the following examples, assume that the source list is:</p>
<pre class="language-javascript">
<code>
var tags = ['ActionScript', 'AppleScript', 'Asp', 'BASIC', 'C', 'C++', 'Clojure', 'COBOL', ... ]
</code>
</pre>
<p>The basic usage of Kamil autocomplete is:</p>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
var autoComplete = new Kamil('#tags', {
source: tags
});
</code>
</pre>
<div class="example">
<input id="tags1" placeholder="Search">
</div>
<p>The same result can be achieved by passing a selector instead of an array:</p>
<pre class="language-markup"><code>
<input id="tags" placeholder="search">
<ul id="list">
<li>ActionScript</li>
<li>AppleScript</li>
<li>Asp</li>
<li>BASIC</li>
<li>C</li>
<li>C++</li>
...
</ul>
</code></pre>
<pre class="language-javascript">
<code>
var autoComplete = new Kamil('#tags', { source: '#list' });
</code>
</pre>
</div>
<div class="demo">
<h1 id="customisation">Customisation</h1>
<p>
Kamil is highly customisable; you can pass and change various options through the Kamil object.
The table below lists all the available options:
</p>
<table>
<thead>
<tr>
<th>Option</th>
<th>Description</th>
<th>Type</th>
<th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td>source</td>
<td>The list of suggestions given as an array or a selector</td>
<td>Array | String</td>
<td>null</td>
</tr>
<tr>
<td>appendTo</td>
<td>Where to append the menu</td>
<td>String - selector</td>
<td>Directly after the input element</td>
</tr>
<tr>
<td>disabled</td>
<td>Whether the autocomplete functionality is disabled</td>
<td>Boolean</td>
<td>false</td>
</tr>
<tr>
<td>autoFocus</td>
<td>Should the first element be selected once the menu is open?</td>
<td>Boolean</td>
<td>false</td>
</tr>
<tr>
<td>minChars</td>
<td>Minimum number of character to type before a search starts</td>
<td>Number</td>
<td>1</td>
</tr>
<tr>
<td>noResultsMessage</td>
<td>Message to display when there are no matches</td>
<td>String</td>
<td>null</td>
</tr>
<tr>
<td>property</td>
<td>The property to choose for the suggestion</td>
<td>String</td>
<td>null - no nested structure</td>
</tr>
<tr>
<td>exclude</td>
<td>Class name given to the list items to exclude from being part of the data</td>
<td>String</td>
<td>'kamil-autocomplete-category'</td>
</tr>
<tr>
<td>filter</td>
<td>
The filter function to apply to the source list:
<pre class="language-javascript">
<code>
filter: function (listItem, input) { ... }
</code>
</pre>
</td>
<td>Function</td>
<td>Match anywhere, case insensitive</td>
</tr>
<tr>
<td>sort</td>
<td>
Sort function to sort the items after they have been filtered
</td>
<td>Function</td>
<td>Sort by ascending length</td>
</tr>
</tbody>
</table>
<p>Below you can find some examples of customisation.</p>
<h2>Auto focus</h2>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
var autoComplete = new Kamil('#tags', {
source: tags,
autoFocus: true
});
</code>
</pre>
<div class="example">
<input id="tags2" placeholder="Search">
</div>
<h2>Minimum characters</h2>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
var autoComplete = new Kamil('#tags', {
source: tags,
minChars: 2
});
</code>
</pre>
<p>Try typing 'co' or 'ja'</p>
<div class="example">
<input id="tags3" placeholder="Search">
</div>
<h2>Sort</h2>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
var autoComplete = new Kamil('#tags', {
source: tags,
sort: function (a, b) {
return a.localeCompare(b);
}
});
</code>
</pre>
<div class="example">
<input id="tags4" placeholder="Search">
</div>
</div>
<div class="demo">
<h1 id="extension-points">Extension points</h1>
<p>
When extending the widget, you have the ability to override or add to the behavior of existing methods.
Extension points gives you more freedom than options.
</p>
<h2><code>renderItem(ul, item)</code></h2>
<p>
The renderItem extension point allows you to change the rendering of each list item.
The method must create an item, append it to the ul parameter and return it (the item):
</p>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
// Set up an array of objects
var tags = [
{
label: "jQuery",
desc: "the write less, do more, JavaScript library"
},
{
label: "jQuery UI",
desc: "the official user interface library for jQuery"
},
{
label: "Sizzle JS",
desc: "a pure-JavaScript CSS selector engine"
}
];
var ac = new Kamil('#tags', {
source: tags
});
ac.renderItem = function (ul, item) {
// Render item and append to 'ul'
var li = document.createElement('li');
li.innerHTML = "<a>" + item.label + "<br><small>" + item.desc + "</small></a>";
ul.appendChild(li);
return li; // Return the list item!
};
// More on events later
ac.on('kamilfocus', function (e) {
e.inputElement.value = e.item.label;
});
ac.on('kamilselect', function (e) {
e.inputElement.value = e.item.label;
});
</code>
</pre>
<div class="example">
<input id="tags5" placeholder="Search">
</div>
<h2><code>renderMenu(ul, items)</code></h2>
<p>
This method controls the rendering of the menu. It is passed an empty ul element and a list of
items. Creation of the individual <li> elements should be delegated to _renderItemData() like
in the following example:
</p>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
var ac = new Kamil('#tags6', {
source: tags
});
ac.renderMenu = function (ul, items) {
var i, l;
// Render items
for (i = 0, l = items.length; i < l; i++) {
this._renderItemData(ul, items[i], i);
}
// Striped menu
var listItems = ul.getElementsByTagName('li');
for (i = 0, l = listItems.length; i < l; i++) {
if (i % 2 === 0) {
listItems[i].classList.add('striped-even');
}
}
}
</code>
</pre>
<div class="example">
<input id="tags6" placeholder="Search">
</div>
<p>Categories (example from <a href="https://jqueryui.com/autocomplete/#categories">jQuery UI Autocomplete</a>):</p>
<pre class="language-markup"><code><input id="tags" placeholder="search"></code></pre>
<pre class="language-javascript">
<code>
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ label: "annhhx10", category: "Products" },
{ label: "annk K12", category: "Products" },
{ label: "annttop C13", category: "Products" },
{ label: "anders andersson", category: "People" },
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
],
ac = new Kamil('#tags7', { source: data });
// Customise menu rendering
ac.renderMenu = function (ul, items) {
var self = this,
currentCategory = "";
// Loop through items
items.forEach(function (item, index) {
var li;
// Create category
if (item.category !== currentCategory) {
var catLi = document.createElement('li');
catLi.className = 'kamil-autocomplete-category';
catLi.innerHTML = item.category;
ul.appendChild(catLi);
currentCategory = item.category;
}
// Create item
li = self._renderItemData(ul, item, index);
if (item.category) {
li.setAttribute('aria-label', item.category + ' : ' + item.label);
}
});
};
</code>
</pre>
<div class="example">
<input id="tags7" placeholder="Search">
</div>
</div>
<div class="demo">
<h1 id="methods">Methods</h1>
<p>
The following methods allow you to programmatically control the widget.
</p>
<h2><code>close()</code></h2>
<p>Closes the autocomplete menu.</p>
<h2><code>destroy()</code></h2>
<p>Removes the menu and the events attached to the input element.</p>
<h2><code>disable()</code></h2>
<p>Disables the autocomplete.</p>
<h2><code>enable()</code></h2>
<p>Enabls the autocomplete.</p>
<h2><code>option()</code></h2>
<p>Returns the options object.</p>
<h2><code>option(name)</code></h2>
<p>Return the value of the given option name.</p>
<h2><code>option(name, value)</code></h2>
<p>Sets the option to the given value.</p>
<h2><code>start([value])</code></h2>
<p>Starts a suggestions' search. If value is not specified then it takes the input's text.</p>
</div>
<div class="demo">
<h1 id="events">Events</h1>
You can add an event listener using the <code>on</code> method:
<pre class="language-javascript">
<code>
autoComplete.on('kamilopen', function (e) {
...
});
</code>
</pre>
<h2><code>kamilclose</code></h2>
<p>Triggered when the menu is closed (hidden).</p>
<h2><code>kamilfocus</code></h2>
<p>
Triggered when focus is moved to an item. The default behaviour is to
replace the input's field with the 'property' option (if it's not
specified then it will fallback on 'label' then 'value' then the
item's text itself).
</p>
<h2><code>kamilopen</code></h2>
<p>
Fired when the menu is opened or updated.
</p>
<h2><code>kamilselect</code></h2>
<p>
Triggered when an item is selected from the menu.
</p>
</div>
<div class="demo">
<h1 id="download-contribute">Download and contribute</h1>
<p>Contributions are very welcome! <a href="https://github.com/oss6/kamil/issues/new">Suggest a feature? Spot a bug?</a></p>
<a href="https://github.com/oss6/kamil/archive/v0.2.1.zip" class="btn-large" download>Download</a>
</div>
</div>
<script src="js/prism.js"></script>
<script src="kamil.js"></script>
<script src="js/examples.js"></script>
</body>
</html>