Do a bunch of work on publisher autocompletion JS
This commit is contained in:
parent
216a2c3cdb
commit
150f676332
5 changed files with 89 additions and 16 deletions
|
|
@ -6,6 +6,8 @@
|
||||||
- content_for :head do
|
- content_for :head do
|
||||||
= include_javascripts :home
|
= include_javascripts :home
|
||||||
|
|
||||||
|
= hidden_field_tag :contact_json, @contacts.map{|contact| contact.person}.to_json
|
||||||
|
|
||||||
.span-15.append-2
|
.span-15.append-2
|
||||||
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count
|
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,4 +54,3 @@
|
||||||
.facebox_content
|
.facebox_content
|
||||||
#add_aspect_pane
|
#add_aspect_pane
|
||||||
= render "aspects/new_aspect"
|
= render "aspects/new_aspect"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
;(function($) {
|
;(function($) {
|
||||||
|
|
||||||
|
function lastWord(s){return s;} //Fuck you
|
||||||
$.fn.extend({
|
$.fn.extend({
|
||||||
autocomplete: function(urlOrData, options) {
|
autocomplete: function(urlOrData, options) {
|
||||||
var isUrl = typeof urlOrData == "string";
|
var isUrl = typeof urlOrData == "string";
|
||||||
|
|
@ -233,7 +234,7 @@ $.Autocompleter = function(input, options) {
|
||||||
|
|
||||||
previousValue = currentValue;
|
previousValue = currentValue;
|
||||||
|
|
||||||
currentValue = lastWord(currentValue);
|
currentValue = options.searchTermFromValue(currentValue);
|
||||||
if ( currentValue.length >= options.minChars) {
|
if ( currentValue.length >= options.minChars) {
|
||||||
$input.addClass(options.loadingClass);
|
$input.addClass(options.loadingClass);
|
||||||
if (!options.matchCase)
|
if (!options.matchCase)
|
||||||
|
|
@ -258,13 +259,6 @@ $.Autocompleter = function(input, options) {
|
||||||
return result;
|
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)
|
// fills in the input box w/the first match (assumed to be the best match)
|
||||||
// q: the term entered
|
// q: the term entered
|
||||||
// sValue: the first matching result
|
// sValue: the first matching result
|
||||||
|
|
@ -386,6 +380,7 @@ $.Autocompleter = function(input, options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
$.Autocompleter.defaults = {
|
$.Autocompleter.defaults = {
|
||||||
|
searchTermFromValue: lastWord,
|
||||||
inputClass: "ac_input",
|
inputClass: "ac_input",
|
||||||
resultsClass: "ac_results",
|
resultsClass: "ac_results",
|
||||||
loadingClass: "ac_loading",
|
loadingClass: "ac_loading",
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,68 @@ var Publisher = {
|
||||||
Publisher.form().removeClass('closed');
|
Publisher.form().removeClass('closed');
|
||||||
Publisher.form().find(".options_and_submit").show();
|
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){
|
updateHiddenField: function(evt){
|
||||||
Publisher.form().find('#status_message_message').val(
|
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(){
|
||||||
|
Publisher.input().autocomplete(Publisher.autocompletion.contactsJSON(),
|
||||||
|
Publisher.autocompletion.options());
|
||||||
|
Publisher.input().result(Publisher.autocompletion.selectItemCallback);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
var $publisher = Publisher.form();
|
Publisher.cachedForm = false;
|
||||||
|
Publisher.cachedInput = false;
|
||||||
$("div.public_toggle input").live("click", function(evt) {
|
$("div.public_toggle input").live("click", function(evt) {
|
||||||
$("#publisher_service_icons").toggleClass("dim");
|
$("#publisher_service_icons").toggleClass("dim");
|
||||||
if ($(this).attr('checked') == true) {
|
if ($(this).attr('checked') == true) {
|
||||||
|
|
@ -31,10 +86,11 @@ var Publisher = {
|
||||||
Publisher.close();
|
Publisher.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Publisher.autocompletion.initialize();
|
||||||
Publisher.updateHiddenField();
|
Publisher.updateHiddenField();
|
||||||
$publisher.find('#status_message_fake_message').change(
|
Publisher.form().find('#status_message_fake_message').change(
|
||||||
Publisher.updateHiddenField);
|
Publisher.updateHiddenField);
|
||||||
$publisher.find("textarea").bind("focus", function(evt) {
|
Publisher.form().find("textarea").bind("focus", function(evt) {
|
||||||
Publisher.open();
|
Publisher.open();
|
||||||
$(this).css('min-height', '42px');
|
$(this).css('min-height', '42px');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -78,4 +78,25 @@ describe("Publisher", function() {
|
||||||
Publisher.form().find('#status_message_fake_message').val());
|
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('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue