slight refactor
This commit is contained in:
+9
-11
@@ -222,32 +222,31 @@
|
|||||||
})(config);
|
})(config);
|
||||||
|
|
||||||
var Form = (function(config) {
|
var Form = (function(config) {
|
||||||
|
var urlRegex = /(\b(https?|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
|
||||||
var searchForm = $('#js-search-form');
|
var searchForm = $('#js-search-form');
|
||||||
var searchInput = $('#js-search-input');
|
var searchInput = $('#js-search-input');
|
||||||
var urlRegex = /(\b(https?|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
|
|
||||||
|
|
||||||
searchForm.addEventListener('submit', function(event) {
|
searchForm.addEventListener('submit', function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
var q = searchInput.value.trim();
|
var q = searchInput.value.trim();
|
||||||
var qSplit = q.split(config.searchDelimiter);
|
|
||||||
var validCommand = false;
|
|
||||||
var redirect = '';
|
|
||||||
|
|
||||||
if (q === '' || q === '?') {
|
if (q === '' || q === '?') {
|
||||||
Help.toggle();
|
Help.toggle();
|
||||||
searchInput.value = '';
|
searchInput.value = '';
|
||||||
return false;
|
} else {
|
||||||
}
|
var qSplit = q.split(config.searchDelimiter);
|
||||||
|
var qIsUrl = q.match(new RegExp(urlRegex));
|
||||||
|
var redirect = '';
|
||||||
|
|
||||||
redirect = q.match(new RegExp(urlRegex)) ? q
|
if (qIsUrl) redirect = q;
|
||||||
: config.defaultCommand + encodeURIComponent(q);
|
else redirect = config.defaultCommand + encodeURIComponent(q);
|
||||||
|
|
||||||
config.commands.forEach(function(command) {
|
config.commands.forEach(function(command) {
|
||||||
if (qSplit[0] === command.key) {
|
if (qSplit[0] === command.key) {
|
||||||
if (qSplit[1] && command.search) {
|
if (qSplit[1] && command.search) {
|
||||||
qSplit.shift();
|
qSplit.shift();
|
||||||
var search = encodeURIComponent(qSplit.join(config.searchDelimiter).trim());
|
var search = encodeURIComponent(qSplit[0].trim());
|
||||||
redirect = command.url + command.search + search;
|
redirect = command.url + command.search + search;
|
||||||
} else {
|
} else {
|
||||||
redirect = command.url;
|
redirect = command.url;
|
||||||
@@ -256,8 +255,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
window.location.href = redirect;
|
window.location.href = redirect;
|
||||||
|
}
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
return { searchInput: searchInput };
|
|
||||||
})(config);
|
})(config);
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user