forked from vufind-org/autocomplete.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
199 lines (193 loc) · 8.16 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<link rel="stylesheet" href="autocomplete.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/themes/prism.min.css"/>
<script src="autocomplete.js"></script>
<script src="autocomplete-static.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.js"></script>
<title>autocomplete.js</title>
<style>
html {
font-size: 16px;
background-color: #f7fafc;
}
body {
max-width: 64rem;
margin: auto;
padding: 1rem;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial,
sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
input {
box-sizing: border-box;
width: 100%;
padding: 0.25rem 0.5rem;
border: 1px solid gray;
font-size: 1rem;
line-height: inherit;
}
input:focus {
border-color: orange;
outline: 2px solid orange;
}
h2 {
margin: 0;
margin-top: 2rem;
}
blockquote {
margin-left: 0;
margin-bottom: 0;
font-style: italic;
color: #555;
}
footer {
padding: 1rem;
font-size: small;
color: #555;
}
footer a {
color: #333;
}
@media (min-width: 768px) {
h1,
blockquote {
margin-left: 1rem;
}
.row {
display: flex;
width: 100%;
}
.col {
flex: 0 1 100%;
margin-right: 1rem;
padding-left: 1rem;
padding-right: 1rem;
}
}
.code-btn {
display: block;
width: 100%;
margin-top: 2rem;
margin-bottom: -2px;
padding: 0.7rem;
border: 1px solid gray;
border-bottom: 0;
background-color: #f5f2f0;
border-radius: .25rem .25rem 0 0;
cursor: pointer;
}
#code-examples {
min-height: 2rem;
border: 1px solid gray;
background-color: #f5f2f0;
}
pre.language-javascript {
margin: 0;
padding: 0 2rem;
}
pre.language-javascript:not(.is-open) {
display: none;
}
</style>
</head>
<body>
<h1><a href="https://github.com/vufind-org/autocomplete.js">autocomplete.js</a></h1>
<blockquote>Here to provide the interface so you can control the results.</blockquote>
<div class="row">
<div class="col">
<h2>Handler w/ AJAX</h2>
<p>Basic example</p>
<input id="ajax" type="text" placeholder="Search MealDB"/>
<script id="example-1">
const AC = new Autocomplete({ limit: 10, minInputLength: 1 });
AC(document.getElementById("ajax"), function achandler(query, callback) {
const url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`;
fetch(url).then(function(response) {
return response.json();
}).then(function(json) {
if (!json.meals) {
callback([{ _disabled: "no results" }]);
return;
}
callback([{ _header: "meals" }, ...json.meals.map( meal => meal.strMeal ) ]);
});
});
</script>
</div>
<div class="col">
<h2>Autocomplete.static</h2>
<p>Using optional Autocomplete.static</p>
<input id="static" type="text" placeholder="List of Animals"/>
<script id="example-2">
const animals = ['aardvark','albatross','alligator','alpaca','ant','anteater','antelope','ape','armadillo','baboon','badger','barracuda','bat','bear','bee','beetle','bison','boar','buffalo','bushbaby','bustard','butterfly','camel','capuchin','caribou','cat','caterpillar','chameleon','chamois','cheetah','chicken','chimpanzee','chinchilla','chipmunk','chough','clam','cobra','cockroach','cod','cormorant','coyote','crab','crane','crocodile','crow','curlew','deer','dinosaur','dog','dogfish','dolphin','donkey','dotterel','dove','dragon','dragonfly','duck','dugong','dunlin','eagle','echidna','eel','eland','elephant','elk','emu','falcon','ferret','finch','fish','flamingo','fly','fox','frog','gaur','gazelle','gecko','gerbil','giraffe','gnu','goat','goldfish','goose','gorilla','goshawk','grasshopper','grouse','guanaco','gull','hamster','hare','hawk','hedgehog','heron','herring','hippopotamus','hornet','horse','horsecrab','hummingbird','hyena','hyrax','ibex','ibis','iguana','impala','jackal','jaguar','jay','jellyfish','kangaroo','koala','kouprey','kudu','lapwing','lark','lemming','lemur','leopard','lion','llama','lobster','locust','loris','louse','lynx','lyrebird','magpie','mallard','manatee','marten','meerkat','mink','mole','mongoose','monkey','moose','mosquito','mouse','mule','narwhal','newt','nightingale','numbat','octopus','okapi','opossum','orangutan','oryx','ostrich','otter','owl','ox','oyster','panther','parrot','partridge','peacock','peafowl','pelican','penguin','pheasant','pig','pigeon','platypus','pony','porcupine','porpoise','puffin','quagga','quail','quelea','rabbit','raccoon','ram','rat','raven','reindeer','rhea','rhinoceros','rook','ruff','salamander','salmon','sambar','sandpiper','sardine','scorpion','seahorse','seal','shark','sheep','shrew','shrimp','skink','skunk','snail','snake','spider','squid','squirrel','starling','stinkbug','stork','swan','tapir','tarsier','termite','tern','tiger','toad','trout','turkey','turtle','viper','vulture','wallaby','walrus','wasp','weasel','whale','wolf','wolverine','wombat','woodcock','woodpecker','worm','wren','yak','zebra','zorilla'];
AC(document.getElementById("static"), Autocomplete.static(animals));
</script>
</div>
<div class="col">
<h2>Items with HREFs</h2>
<p>and subtitles (<code>{text: , sub: }</code>)</p>
<input id="href" type="text" placeholder="Search MealDB"/>
<script id="example-3">
AC(document.getElementById("href"), function achandler(query, callback) {
const url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`;
let x = fetch(url).then(function(response) {
return response.json();
}).then(function(json) {
if (!json.meals) {
callback([{ _disabled: "no results" }]);
return;
}
callback( json.meals.map( meal => ({
text: meal.strMeal,
sub: meal.strCategory,
href: meal.strMealThumb
}) ) );
});
});
</script>
</div>
</div>
<div id="code-examples">
</div>
<footer>
<p>Made with <3 by <a href="https://crhallberg.com">Chris Hallberg</a> for <a href="http://vufind.org">VuFind</a>.</p>
<p>AJAX powered by the <a href="https://www.themealdb.com/api.php">TheMealDB API</a>.</p>
</footer>
<script>
const examplesEl = document.getElementById("code-examples");
function showCode(srcId) {
var pre = document.createElement('pre');
const srcEl = document.getElementById(srcId);
pre.innerHTML = '<code class="language-javascript" id="code-' + srcId + '">' + srcEl.innerHTML + '</code>';
examplesEl.appendChild(pre);
const btn = document.createElement("button");
btn.className = "code-btn";
btn.textContent = "Show Code";
btn.dataset.target = "code-" + srcId;
srcEl.parentNode.appendChild(btn);
}
showCode("example-1");
showCode("example-2");
showCode("example-3");
const codeTabs = document.querySelectorAll("code.language-javascript");
document.querySelectorAll(".code-btn").forEach(btn => {
btn.addEventListener("click", function() {
codeTabs.forEach(tab => {
if (tab.id === btn.dataset.target) {
tab.parentNode.classList.add("is-open");
} else {
tab.parentNode.classList.remove("is-open");
}
});
}, false);
});
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('[data-target="code-example-1"]').click();
});
</script>
</body>
</html>