Skip listing all examples if query param is set.
Also use the `input` event instead of `keyup` and addEventlistener instead of setting the onload / onchange properties. And handle space character encoded as `+` in query string correctly.
This commit is contained in:
+15
-13
@@ -102,30 +102,32 @@
|
|||||||
listExamples(examples);
|
listExamples(examples);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseQuery() {
|
function parseParams() {
|
||||||
const params = {};
|
const params = {};
|
||||||
const list = window.location.search.substring(1).split('&');
|
const list = window.location.search
|
||||||
|
.substring(1)
|
||||||
|
.replace(/\+/g, '%20')
|
||||||
|
.split('&');
|
||||||
for (let i = 0; i < list.length; ++i) {
|
for (let i = 0; i < list.length; ++i) {
|
||||||
const pair = list[i].split('=');
|
const pair = list[i].split('=');
|
||||||
if (pair.length == 2) {
|
if (pair.length === 2) {
|
||||||
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
|
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (params['q']) {
|
return params;
|
||||||
const input = document.getElementById('keywords');
|
|
||||||
input.value = params['q'];
|
|
||||||
inputChange.call(input);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onload = function () {
|
window.addEventListener('load', function () {
|
||||||
for (let i = 0; i < info.examples.length; ++i) {
|
for (let i = 0; i < info.examples.length; ++i) {
|
||||||
info.examples[i].link += window.location.search;
|
info.examples[i].link += window.location.search;
|
||||||
}
|
}
|
||||||
template = new jugl.Template('template');
|
template = new jugl.Template('template');
|
||||||
target = document.getElementById('examples');
|
target = document.getElementById('examples');
|
||||||
listExamples(info.examples);
|
const params = parseParams();
|
||||||
document.getElementById('keywords').onkeyup = inputChange;
|
const text = params['q'] || '';
|
||||||
parseQuery();
|
const input = document.getElementById('keywords');
|
||||||
};
|
input.addEventListener('input', inputChange);
|
||||||
|
input.value = text;
|
||||||
|
filterList(text);
|
||||||
|
});
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user