Skip to content

Commit

Permalink
Task to create a uglified version of the JS file
Browse files Browse the repository at this point in the history
  • Loading branch information
David Padilla committed Jun 18, 2011
1 parent c14f524 commit 30c7334
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 99 deletions.
10 changes: 9 additions & 1 deletion Rakefile
Expand Up @@ -3,10 +3,18 @@ Bundler::GemHelper.install_tasks

require 'rake/testtask'

task :default => :test
task :default => [:uglify, :test]

Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end

task :uglify do
require 'uglifier'
file_folder = "lib/generators/templates"
File.open("#{file_folder}/autocomplete-rails.js", "w") do |f|
f << Uglifier.compile(File.read("#{file_folder}/autocomplete-rails-uncompressed.js"))
end
end
113 changes: 113 additions & 0 deletions lib/generators/templates/autocomplete-rails-uncompressed.js
@@ -0,0 +1,113 @@
/*
* Unobtrusive autocomplete
*
* To use it, you just have to include the HTML attribute autocomplete
* with the autocomplete URL as the value
*
* Example:
* <input type="text" data-autocomplete="/url/to/autocomplete">
*
* Optionally, you can use a jQuery selector to specify a field that can
* be updated with the element id whenever you find a matching value
*
* Example:
* <input type="text" data-autocomplete="/url/to/autocomplete" id_element="#id_field">
*/

$(document).ready(function(){
$('input[data-autocomplete]').railsAutocomplete();
});

(function(jQuery)
{
var self = null;
jQuery.fn.railsAutocomplete = function() {
return this.live('focus',function() {
if (!this.railsAutoCompleter) {
this.railsAutoCompleter = new jQuery.railsAutocomplete(this);
}
});
};

jQuery.railsAutocomplete = function (e) {
_e = e;
this.init(_e);
};

jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = {
railsAutocomplete: '0.0.1'
};

jQuery.railsAutocomplete.fn.extend = jQuery.railsAutocomplete.extend = jQuery.extend;
jQuery.railsAutocomplete.fn.extend({
init: function(e) {
e.delimiter = $(e).attr('data-delimiter') || null;
function split( val ) {
return val.split( e.delimiter );
}
function extractLast( term ) {
return split( term ).pop().replace(/^\s+/,"");
}

$(e).autocomplete({
source: function( request, response ) {
$.getJSON( $(e).attr('data-autocomplete'), {
term: extractLast( request.term )
}, function() {
$(arguments[0]).each(function(i, el) {
var obj = {};
obj[el.id] = el;
$(e).data(obj);
});
response.apply(null, arguments);
});
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
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
if (e.delimiter != null) {
terms.push( "" );
this.value = terms.join( e.delimiter );
} else {
this.value = terms.join("");
if ($(this).attr('id_element')) {
$($(this).attr('id_element')).val(ui.item.id);
}
if ($(this).attr('data-update-elements')) {
var data = $(this).data(ui.item.id.toString());
var update_elements = $.parseJSON($(this).attr("data-update-elements"));
for (var key in update_elements) {
$(update_elements[key]).val(data[key]);
}
}
}
var remember_string = this.value;
$(this).bind('keyup.clearId', function(){
if($(this).val().trim() != remember_string.trim()){
$($(this).attr('id_element')).val("");
$(this).unbind('keyup.clearId');
}
});
$(this).trigger('railsAutocomplete.select');

return false;
}
});
}
});
})(jQuery);
99 changes: 1 addition & 98 deletions lib/generators/templates/autocomplete-rails.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rails3-jquery-autocomplete.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('mongo_mapper', '>= 0.9')
s.add_development_dependency('bson_ext', '~>1.3.0')
s.add_development_dependency('shoulda', '~>2.11.1')
s.add_development_dependency('uglifier')

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down

0 comments on commit 30c7334

Please sign in to comment.