Created
November 27, 2012 04:32
-
-
Save jaseflow/4152391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*globals jQuery, $, wotif, alert */ | |
// Home search | |
wotif.homeSearch = (function (utils) { | |
var selectors = { | |
searchForm: "#search-form", | |
searchButton: "#search-btn", | |
freetextField: "#freetext", | |
freetextButton: "#freetext-btn", | |
toggleAdvancedOptionsButton: "#toggle-advanced-options", | |
country: "#country", | |
region: "#region", | |
countryFields: "input[name=country]", | |
regionFields: "input[name=region]", | |
selectedCountry: "input[name=country]:checked", | |
selectedRegion: "input[name=region]:checked" | |
}, | |
classes = { | |
selected: "location-checked" | |
}, | |
dom = {}, | |
keyboardInput = '', | |
freetextFormAction = "search/TextSearch"; | |
function searchAction (event) { | |
if ($(selectors.selectedRegion).length < 1) { | |
event.preventDefault(); | |
alert("Please select a destination."); | |
dom.selectedRegion.focus(); | |
} | |
} | |
function toggleAdvancedSearch (event) { | |
if (dom.searchForm.hasClass("show-advanced-options")) { | |
dom.searchForm.removeClass("show-advanced-options"); | |
dom.toggleAdvancedOptionsButton.text("Show advanced options"); | |
} else { | |
dom.searchForm.addClass("show-advanced-options"); | |
dom.toggleAdvancedOptionsButton.text("Hide advanced options"); | |
} | |
} | |
function freetextAction (event) { | |
event.preventDefault(); | |
if ($.trim(dom.freetextField.val()).length < 2) { | |
alert("Please specify at least two characters"); | |
dom.freetextField.focus(); | |
} else { | |
dom.searchForm.attr("action", freetextFormAction).submit(); | |
} | |
} | |
function freetextEnter (event) { | |
if (event.which === $.ui.keyCode.ENTER) { | |
freetextAction(event); | |
} | |
} | |
function initKeyboardSearch () { | |
var regions = []; | |
dom.region.find("li").map(function () { | |
regions.push([$(this).data("region"), $(this).find("input")]); | |
}); | |
dom.region.on("keyup", keyboardDebounce(function() { | |
var regionIndex = keyboardInput !== "" ? keyboardSearch(keyboardInput, regions) : -1; | |
if (regionIndex !== -1) { | |
match = regions[regionIndex][1]; | |
if (match.length > 0) { | |
match.attr("checked", "checked").change(); | |
dom.selectedRegion = match; | |
wotif.homeSearch.scrollToSelected.call(dom.selectedRegion, dom.region); | |
} | |
} | |
})); | |
} | |
function keyboardSearch (search, regions) { | |
var allKeys = new RegExp("^" + search), | |
firstKey = search.charAt(0), | |
firstKeyIndex = -1, | |
i; | |
for (i = 0; i < regions.length; i++) { | |
if (regions[i][0].match(allKeys)) { | |
return i; | |
} else if (firstKey === regions[i][0].charAt(0) && firstKeyIndex === -1) { | |
firstKeyIndex = i; | |
} | |
} | |
return firstKeyIndex; | |
} | |
function keyboardDebounce (handler) { | |
var delay = 200, | |
timer = null; | |
return function(e) { | |
keyboardInput += String.fromCharCode(e.which).replace(/\W|\d/,"").toLowerCase(); | |
if (timer !== null) { | |
clearTimeout(timer); | |
timer = null; | |
} | |
var args = arguments; | |
timer = setTimeout(function() { | |
handler.apply(null, args); | |
keyboardInput = ''; | |
}, delay); | |
}; | |
} | |
return { | |
highlightSelected: function (args) { | |
args.data.parent.find("li").removeClass(classes.selected); | |
$(this).parents("li").addClass(classes.selected); | |
$(this).focus(); | |
}, | |
scrollToSelected: function (args) { | |
var top = $(this).position().top, | |
container = args; | |
if (args.data.parent !== undefined) { | |
if (args.keyCode === $.ui.keyCode.UP || args.keyCode === $.ui.keyCode.LEFT) { | |
top -= 15; | |
} else if (args.keyCode === $.ui.keyCode.DOWN || args.keyCode === $.ui.keyCode.RIGHT) { | |
top += 15; | |
} | |
container = args.data.parent; | |
} | |
container.scrollTop((top + container.scrollTop()) - (container.height() / 2)); | |
}, | |
getCountryRegions: function () { | |
var country = $(this), | |
newCountryId = country.val(), | |
countryGroup = country.attr("class"), | |
allCountriesFlag = ''; | |
if (countryGroup !== "popular-country") { | |
allCountriesFlag = '&all'; | |
} | |
if (newCountryId !== dom.selectedCountry.val() && newCountryId !== null) { | |
window.location = ($('base').attr('href') || '') + "/?country=" + newCountryId + allCountriesFlag; | |
} | |
}, | |
init: function () { | |
var selector; | |
for (selector in selectors) { | |
dom[selector] = $(selectors[selector]); | |
} | |
dom.searchButton.click(searchAction); | |
dom.freetextButton.click(freetextAction); | |
dom.freetextField.keydown(freetextEnter); | |
dom.toggleAdvancedOptionsButton.click(toggleAdvancedSearch); | |
if (window.location.search.indexOf('country=') < 0 && window.location.search.indexOf('regionId=') < 0) { | |
dom.selectedCountry.removeAttr("checked"); | |
} | |
dom.countryFields.on("change", this.getCountryRegions); | |
if (dom.selectedCountry.length > 0) { | |
this.scrollToSelected.call(dom.selectedCountry, dom.country); | |
} | |
dom.regionFields.on("keydown", { parent: dom.region }, this.scrollToSelected); | |
dom.regionFields.on("change", { parent: dom.region }, this.highlightSelected); | |
if (dom.selectedRegion.length > 0) { | |
dom.selectedRegion.change(); | |
this.scrollToSelected.call(dom.selectedRegion, dom.region); | |
} | |
initKeyboardSearch(); | |
// Google Analytics events | |
wotif.analyticEvents.add([ | |
{ | |
delegateSelector: ".advanced", | |
selector: "input, select", | |
event: "change", | |
category: "Homepage advanced search", | |
action: "Change advanced search field", | |
page: "home" | |
} | |
]); | |
} | |
}; | |
}(wotif.utils)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment