Last active
August 29, 2015 14:01
-
-
Save FotoVerite/a1d4cda7b2f4654c716d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
novafabricaGlobalSearch = { | |
searchField: null, | |
searchListContainer: null, | |
list: null, | |
searchUrl: null, | |
autoCompleteVars: null, | |
_createAutoList: function(json) { | |
var self = novafabricaGlobalSearch; | |
var searchField = self.searchField; | |
var list = self.list; | |
list.html(''); | |
if(json.length > 0 ) { | |
list.parent().find('.search-error').hide(); | |
for (i=0;i<json.length;i++){ | |
var li = $("<li></li>"); | |
li.append($("<a></a>").html(json[i])); | |
list.append(li); | |
//Mouseover allows for list to be inserted into search field | |
li.mouseover(function() { | |
searchField.addClass('inactive stop-prop'); | |
list.find('li').removeClass('active'); | |
$(this).addClass("active"); | |
searchField.data("selected", $(this)); | |
return false; | |
}); | |
li.click(function(e) { | |
searchField.val(self._sanitize($(this).find('a').html())); | |
$('.autocomplete-arrow').hide(); | |
list.hide(); | |
searchField.parent().submit(); | |
}); | |
li.mouseout(function() { | |
searchField.removeClass('stop-prop'); | |
}); | |
$('.autocomplete-arrow').show(); | |
list.show(); | |
} | |
} else { | |
$('.autocomplete-arrow').hide(); | |
list.hide(); | |
list.parent().find('.search-error').show(); | |
} | |
}, | |
_getContainer: function() { | |
var self = novafabricaGlobalSearch; | |
if(self.searchField.parent().find('.autocomplete-options-container').length > 0 ) { | |
return self.searchField.parent().find('.autocomplete-options-container'); | |
} | |
else { | |
return self.searchField.parent().find('.autocomplete-options'); | |
} | |
}, | |
init: function(options) { | |
var self = novafabricaGlobalSearch; | |
options = options ||{}; | |
self.searchField = options.searchField || $('.auto-search'); | |
self.searchListContainer = options.searchListContainer || self._getContainer(); | |
if( self.searchListContainer.hasClass('autocomplete-options')) { | |
self.list = self.searchListContainer; | |
} | |
else { | |
self.list = options.list || self.searchListContainer.find('.autocomplete-options'); | |
} | |
searchUrl = options.searchUrl || self.searchField.data('href'); | |
// Hide when focus out | |
self.searchField.focusout(function(event) { | |
if(!($(this).hasClass('stop-prop'))) { | |
self.searchListContainer.hide(); | |
} | |
}); | |
// Show when focus and list is greater than zero | |
self.searchField.focus(function(event) { | |
if($(this).parent().find('.autocomplete-options li').length > 0) { | |
self.searchListContainer.show(); | |
} | |
}); | |
self.searchField.keydown(function(event){ | |
self._resondToKeydown(event); | |
} | |
); | |
self._observeSearchField(); | |
}, | |
_moveArrowToDesignatedListItem: function(defaultNode, direction){ | |
var selectedComplete; | |
var self = novafabricaGlobalSearch; | |
var searchField = self.searchField; | |
var list = self.list; | |
if(searchField.data("selected") === null || searchField.data("selected") === undefined ) { | |
selectedComplete = list.find(defaultNode); | |
selectedComplete.addClass("active"); | |
searchField.val(self._sanitize(selectedComplete.find("a").html())); | |
searchField.data("selected", selectedComplete); | |
} | |
else { | |
if(direction == "prev" && searchField.data("selected").prev().length !== 0) { | |
selectedComplete = searchField.data("selected").prev(); | |
selectedComplete.addClass("active"); | |
searchField.val(self._sanitize(selectedComplete.find("a").html())); | |
searchField.data("selected", selectedComplete); | |
} | |
else if(direction == "next" && searchField.data("selected").next().length !== 0) { | |
selectedComplete = searchField.data("selected").next(); | |
selectedComplete.addClass("active"); | |
searchField.val(self._sanitize(selectedComplete.find("a").html())); | |
searchField.data("selected", selectedComplete); | |
} | |
else { | |
selectedComplete = null; | |
searchField.val(typedValue); | |
searchField.data("selected", selectedComplete); | |
} | |
} | |
}, | |
_resondToKeydown: function(event) { | |
var self = novafabricaGlobalSearch; | |
var searchField = self.searchField; | |
var list = self.list; | |
var typedValue = searchField.data("typedValue"); | |
searchField.removeClass("inactive"); | |
list.find("li").removeClass("active"); | |
// We deal with the up arrow event | |
// We keey track of where the arrow is and where it should go next | |
if (event.keyCode === 38) { | |
event.preventDefault(); | |
searchField.addClass("inactive"); | |
self._moveArrowToDesignatedListItem("li:last-child", "prev"); | |
// We deal with the down arrow event | |
// We keey track of where the arrow is and where it should go next | |
} else if (event.keyCode === 40) { | |
event.preventDefault(); | |
searchField.addClass("inactive"); | |
self._moveArrowToDesignatedListItem("li:first-child", "next"); | |
} else { | |
list.find("li").removeClass("active"); | |
} | |
}, | |
_observeSearchField: function() { | |
var self = novafabricaGlobalSearch; | |
$('.auto-search').observe_field(0.5, function() { | |
if(self.searchField.val() === "") { | |
self.searchListContainer.hide(); | |
} | |
else { | |
self.searchListContainer.show(); | |
} | |
self._updateSearch(); | |
}); | |
}, | |
_sanitize: function(input) { | |
return input.replace(/(<([^>]+)>)/gi, "").replace("&", "&"); | |
}, | |
_updateSearch: function() { | |
var self = novafabricaGlobalSearch; | |
var searchField = self.searchField; | |
// We save the typed value to compare when displaying list | |
searchField.data("typedValue", searchField.val()); | |
$.ajax({ | |
url: searchUrl, | |
type: "GET", | |
dataType: "Json", | |
data: { | |
doc: searchField.attr("data-doc"), | |
value: searchField.val(), | |
'klass': $("#class").val() | |
}, | |
success: function(response, textStatus) { | |
// response should either be the assignment_id or 'Failure' | |
if(response !== self.autoCompleteVars) { | |
self.autoCompleteVars = response; | |
selectedComplete = null; | |
} | |
self._createAutoList(self.autoCompleteVars); | |
} | |
}); | |
} | |
}; | |
$(document).ready(function() { | |
novafabricaGlobalSearch.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment