From 150f676332c29190d734da28b40a94fdff2d69e8 Mon Sep 17 00:00:00 2001 From: Raphael Jedidiah Sofaer Date: Thu, 3 Feb 2011 19:14:46 -0800 Subject: [PATCH] Do a bunch of work on publisher autocompletion JS --- app/views/aspects/index.html.haml | 4 +- app/views/layouts/_header.html.haml | 1 - .../javascripts/jquery.autocomplete-custom.js | 11 +-- public/javascripts/publisher.js | 68 +++++++++++++++++-- spec/javascripts/publisher-spec.js | 21 ++++++ 5 files changed, 89 insertions(+), 16 deletions(-) diff --git a/app/views/aspects/index.html.haml b/app/views/aspects/index.html.haml index 723d4f4f7..c9b358534 100644 --- a/app/views/aspects/index.html.haml +++ b/app/views/aspects/index.html.haml @@ -5,7 +5,9 @@ - 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 diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index f1b6d2c3f..fd8dabc99 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -54,4 +54,3 @@ .facebox_content #add_aspect_pane = render "aspects/new_aspect" - diff --git a/public/javascripts/jquery.autocomplete-custom.js b/public/javascripts/jquery.autocomplete-custom.js index 80d7779d5..e210e8cef 100644 --- a/public/javascripts/jquery.autocomplete-custom.js +++ b/public/javascripts/jquery.autocomplete-custom.js @@ -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", diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js index 1750b506f..ceb114df2 100644 --- a/public/javascripts/publisher.js +++ b/public/javascripts/publisher.js @@ -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.form().find('#status_message_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(){ + Publisher.input().autocomplete(Publisher.autocompletion.contactsJSON(), + Publisher.autocompletion.options()); + Publisher.input().result(Publisher.autocompletion.selectItemCallback); + } }, initialize: function() { - var $publisher = Publisher.form(); + 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'); }); diff --git a/spec/javascripts/publisher-spec.js b/spec/javascripts/publisher-spec.js index 203390b9c..d4e82fd41 100644 --- a/spec/javascripts/publisher-spec.js +++ b/spec/javascripts/publisher-spec.js @@ -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(''); + }); + }); + }); });