Created
May 17, 2011 22:30
-
-
Save diegoeche/977571 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
// This is a bit non-DRY | |
// XXX Check how to properly refactor. | |
var AxAutocompletes = function () { | |
// Javascript permite nesting. Es mejor que nada quede definido en la parte externa. | |
var split = function (val) { return val.split( /,\s*/ ); } | |
var extractLast = function (term) { return split(term).pop(); } | |
// lift del $.bind | |
var bindTabEvent = function (element) { | |
return element.bind("keydown", function(event) { | |
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) { | |
event.preventDefault(); | |
} | |
}); | |
} | |
var commonSearch = { | |
search: function() { | |
var term = extractLast(this.value); | |
if ( term.length < 2 ) { return false; } | |
} | |
} | |
var commonFocus = { | |
focus: function() { return false; } | |
} | |
return { | |
blogPost : function () { | |
var autocompleteOpts = { | |
source: function(request, response) { | |
$.getJSON("/admin/blog_tags", { term: extractLast(request.term) }, response); | |
}, | |
select: function(event, ui) { | |
var terms = split( this.value ); | |
// remove the current input | |
terms.pop(); | |
// add the selected item | |
terms.push(ui.item.value); | |
// add placeholder to get the comma-and-space at the end | |
terms.push(""); | |
this.value = terms.join( ", " ); | |
return false; | |
} | |
}; | |
$.extend(autocompleteOpts, commonSearch, commonFocus); | |
// don't navigate away from the field on tab when selecting an item | |
bindTabEvent($("#blog_post_tag_list")).autocomplete($); | |
}, | |
companyName: function() { | |
bindTabEvent($("#user_company_name")).autocomplete({ | |
source: function(request, response) { | |
$.getJSON("/company_names", { term: request.term }, response); | |
}, | |
search: function() { | |
if ( this.value.length < 2 ) { return false; } | |
}, | |
focus: function() { return false; }, | |
}); | |
}, | |
specialtyAreas: function() { | |
bindTabEvent($("#partner_page_areas_of_specialty_list")).autocomplete({ | |
source: function(request, response) { | |
$.getJSON("/specialty_areas", { term: extractLast(request.term) }, response); | |
}, | |
search: function() { | |
var term = extractLast(this.value); | |
if ( term.length < 2 ) { return false; } | |
}, | |
select: function(event, ui) { | |
var terms = split(this.value); | |
terms.pop(); | |
terms.push(ui.item.value); | |
terms.push(""); | |
this.value = terms.join(", "); | |
return false; | |
}, | |
focus: function() { return false; }, | |
}); | |
}, | |
partnerPageNames: function() { | |
bindTabEvent($("#partner_query")).autocomplete({ | |
source: function(request, response) { | |
json_url ="/autocomplete/partner_page_names" | |
$.getJSON(json_url, { term: request.term }, response); | |
}, | |
search: function() { | |
var term = extractLast(this.value); | |
if ( term.length < 2 ) { return false; } | |
}, | |
select: function(event, ui) { | |
this.value = ui.item.value; | |
window.location = ui.item.id; | |
return false; | |
}, | |
focus: function() { return false; }, | |
}); | |
}, | |
init: function() { | |
this.companyName(); | |
this.blogPost(); | |
this.specialtyAreas(); | |
this.partnerPageNames(); | |
} | |
} | |
}(); | |
$(document).ready(function() { | |
AxAutocompletes.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment