Do a bunch of work on publisher autocompletion JS

This commit is contained in:
Raphael Jedidiah Sofaer 2011-02-03 19:14:46 -08:00 committed by Raphael Sofaer
parent 216a2c3cdb
commit 150f676332
5 changed files with 89 additions and 16 deletions

View file

@ -6,6 +6,8 @@
- content_for :head do
= include_javascripts :home
= hidden_field_tag :contact_json, @contacts.map{|contact| contact.person}.to_json
.span-15.append-2
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count

View file

@ -54,4 +54,3 @@
.facebox_content
#add_aspect_pane
= render "aspects/new_aspect"

View file

@ -13,6 +13,7 @@
;(function($) {
function lastWord(s){return s;} //Fuck you
$.fn.extend({
autocomplete: function(urlOrData, options) {
var isUrl = typeof urlOrData == "string";
@ -233,7 +234,7 @@ $.Autocompleter = function(input, options) {
previousValue = currentValue;
currentValue = lastWord(currentValue);
currentValue = options.searchTermFromValue(currentValue);
if ( currentValue.length >= options.minChars) {
$input.addClass(options.loadingClass);
if (!options.matchCase)
@ -258,13 +259,6 @@ $.Autocompleter = function(input, options) {
return result;
}
function lastWord(value) {
if ( !options.multiple )
return value;
var words = trimWords(value);
return words[words.length - 1];
}
// fills in the input box w/the first match (assumed to be the best match)
// q: the term entered
// sValue: the first matching result
@ -386,6 +380,7 @@ $.Autocompleter = function(input, options) {
};
$.Autocompleter.defaults = {
searchTermFromValue: lastWord,
inputClass: "ac_input",
resultsClass: "ac_results",
loadingClass: "ac_loading",

View file

@ -13,13 +13,68 @@ var Publisher = {
Publisher.form().removeClass('closed');
Publisher.form().find(".options_and_submit").show();
},
form: function(){return $('#publisher');},
cachedForm : false,
form: function(){
if(!Publisher.cachedForm){
Publisher.cachedForm = $('#publisher');
}
return Publisher.cachedForm;
},
cachedInput : false,
input: function(){
if(!Publisher.cachedInput){
Publisher.cachedInput = Publisher.form().find('#status_message_fake_message');
}
return Publisher.cachedInput;
},
updateHiddenField: function(evt){
Publisher.form().find('#status_message_message').val(
Publisher.form().find('#status_message_fake_message').val());
Publisher.input().val());
},
autocompletion: {
options : function(){return {
minChars : 1,
max : 5,
searchTermFromValue: Publisher.autocompletion.searchTermFromValue,
scroll : false,
formatItem: function(row, i, max) {
return row.name;
},
formatMatch: function(row, i, max) {
return row.name;
},
formatResult: function(row) {
return row.name;
}
};},
selectItemCallback : function(event, data, formatted) {
var textarea = Publisher.input();
textarea.val(formatted);
},
searchTermFromValue: function(value)
{
matches = value.match(/@(.+)/);
if(matches){
return matches[1];
}else{
return '';
}
},
contactsJSON: function(){
return $.parseJSON($('#contact_json').val());
},
initialize: function(){
var $publisher = Publisher.form();
Publisher.input().autocomplete(Publisher.autocompletion.contactsJSON(),
Publisher.autocompletion.options());
Publisher.input().result(Publisher.autocompletion.selectItemCallback);
}
},
initialize: function() {
Publisher.cachedForm = false;
Publisher.cachedInput = false;
$("div.public_toggle input").live("click", function(evt) {
$("#publisher_service_icons").toggleClass("dim");
if ($(this).attr('checked') == true) {
@ -31,10 +86,11 @@ var Publisher = {
Publisher.close();
};
Publisher.autocompletion.initialize();
Publisher.updateHiddenField();
$publisher.find('#status_message_fake_message').change(
Publisher.form().find('#status_message_fake_message').change(
Publisher.updateHiddenField);
$publisher.find("textarea").bind("focus", function(evt) {
Publisher.form().find("textarea").bind("focus", function(evt) {
Publisher.open();
$(this).css('min-height', '42px');
});

View file

@ -78,4 +78,25 @@ describe("Publisher", function() {
Publisher.form().find('#status_message_fake_message').val());
});
});
describe("autocompletion", function(){
describe("searchTermFromValue", function(){
var func;
beforeEach(function(){func = Publisher.autocompletion.searchTermFromValue;});
it("returns everything after an @", function(){
expect(func('not @dan grip')).toBe('dan grip');
});
it("returns nothing if there is no @", function(){
expect(func('dan')).toBe('');
});
it("returns nothing for just an @", function(){
expect(func('@')).toBe('');
});
it("returns everything after the last @", function(){
expect(func('@siojfoi @dan"')).toBe('dan"');
});
it("returns nothing if there are letters preceding the @", function(){
expect(func('ioj@asdo')).toBe('');
});
});
});
});