From 171c787fade4d9d5a1920c0a8b60b6ca24b640b2 Mon Sep 17 00:00:00 2001 From: Cade Scroggins Date: Sun, 25 Sep 2016 12:17:46 -0700 Subject: [PATCH] slight refactor --- index.html | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/index.html b/index.html index c748b0d..ba088d9 100644 --- a/index.html +++ b/index.html @@ -222,42 +222,40 @@ })(config); var Form = (function(config) { + var urlRegex = /(\b(https?|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i; var searchForm = $('#js-search-form'); var searchInput = $('#js-search-input'); - var urlRegex = /(\b(https?|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i; searchForm.addEventListener('submit', function(event) { event.preventDefault(); var q = searchInput.value.trim(); - var qSplit = q.split(config.searchDelimiter); - var validCommand = false; - var redirect = ''; if (q === '' || q === '?') { Help.toggle(); 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 - : config.defaultCommand + encodeURIComponent(q); + if (qIsUrl) redirect = q; + else redirect = config.defaultCommand + encodeURIComponent(q); - config.commands.forEach(function(command) { - if (qSplit[0] === command.key) { - if (qSplit[1] && command.search) { - qSplit.shift(); - var search = encodeURIComponent(qSplit.join(config.searchDelimiter).trim()); - redirect = command.url + command.search + search; - } else { - redirect = command.url; + config.commands.forEach(function(command) { + if (qSplit[0] === command.key) { + if (qSplit[1] && command.search) { + qSplit.shift(); + var search = encodeURIComponent(qSplit[0].trim()); + redirect = command.url + command.search + search; + } else { + redirect = command.url; + } } - } - }); + }); - window.location.href = redirect; + window.location.href = redirect; + } }, false); - - return { searchInput: searchInput }; })(config);