ajax-chosen-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chosen-rails.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Tse-Ching Ho
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Ajax-Chosen for rails asset pipeline
2
+
3
+ [Chosen](https://github.com/harvesthq/chosen) is a library for making long, unwieldy select boxes more user friendly.
4
+
5
+ The `ajax-chosen-rails` gem integrates `Chosen` and `Ajax-Chosen` with the Rails asset pipeline.
6
+
7
+ ## Usage
8
+
9
+ ### Install ajax-chosen-rails gem
10
+
11
+ Include `ajax-chosen-rails` in Gemfile
12
+
13
+ gem 'ajax-chosen-rails'
14
+
15
+ Then run `bundle install`
16
+
17
+ ### Include chosen and ajax-chosen javascript assets
18
+
19
+ Add to your `app/assets/stylesheets/application.js` if use with jQuery
20
+
21
+ //= require chosen-jquery
22
+ //= require ajax-chosen-jquery
23
+
24
+ Or with Prototype
25
+
26
+ //= require chosen-prototype
27
+ //= require ajax-chosen-prototype
28
+
29
+
30
+ ### Include chosen stylesheet assets
31
+
32
+ Add to your `app/assets/stylesheets/application.css`
33
+
34
+ *= require chosen
35
+
36
+ ## Gem maintainance
37
+
38
+ Maintain `ajax-chosen-rails` gem with `Rake` commands.
39
+
40
+ Update origin chosen source files.
41
+
42
+ rake update
43
+
44
+ Publish gem.
45
+
46
+ rake release
47
+
48
+ ## License
49
+
50
+ use MIT license.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require File.expand_path('../lib/ajax-chosen-rails/source_file', __FILE__)
4
+
5
+ desc "Update with FFCRM's Ajax-Chosen Library"
6
+ task "update" do
7
+ files = SourceFile.new
8
+ files.fetch
9
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ajax-chosen-rails/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nathan Broadbent"]
6
+ gem.email = ["nathan.f77@gmail.com"]
7
+ gem.description = %q{Chosen is a javascript library of select box enhancer for jQuery and Protoype. This gem integrates Ajax-Chosen with Rails asset pipeline for ease of use.}
8
+ gem.summary = %q{Integrate Ajax-Chosen javascript library with Rails asset pipeline}
9
+ gem.homepage = "https://github.com/fatfreecrm/ajax-chosen-rails"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "ajax-chosen-rails"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AjaxChosen::Rails::VERSION
17
+
18
+ gem.add_dependency "railties", "~> 3.0"
19
+ gem.add_dependency "thor", "~> 0.14"
20
+ gem.add_dependency "chosen-rails"
21
+ gem.add_development_dependency "bundler", "~> 1.0"
22
+ gem.add_development_dependency "rails", "~> 3.0"
23
+ gem.add_development_dependency "sass", "~> 3.1"
24
+ end
@@ -0,0 +1,11 @@
1
+ require "ajax-chosen-rails/version"
2
+
3
+ module AjaxChosen
4
+ module Rails
5
+ if ::Rails.version < "3.1"
6
+ require "ajax-chosen-rails/railtie"
7
+ else
8
+ require "ajax-chosen-rails/engine"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ module AjaxChosen
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module AjaxChosen
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,23 @@
1
+ require "thor"
2
+
3
+ class SourceFile < Thor
4
+ include Thor::Actions
5
+
6
+ desc "fetch source files", "fetch source files from GitHub"
7
+ def fetch
8
+ self.destination_root = "vendor/assets"
9
+ remote = "https://github.com/fatfreecrm/ajax-chosen"
10
+ branch = "master"
11
+ get "#{remote}/raw/#{branch}/src/ajax-chosen.jquery.coffee", "javascripts/ajax-chosen.jquery.coffee"
12
+ get "#{remote}/raw/#{branch}/src/ajax-chosen.proto.coffee", "javascripts/ajax-chosen.proto.coffee"
13
+ get "#{remote}/raw/#{branch}/lib/ajax-chosen.jquery.js", "javascripts/ajax-chosen.jquery.js"
14
+ get "#{remote}/raw/#{branch}/lib/ajax-chosen.proto.js", "javascripts/ajax-chosen.proto.js"
15
+ get "#{remote}/raw/#{branch}/VERSION", "VERSION"
16
+ inside destination_root do
17
+ version = File.read("VERSION").sub("\n", "")
18
+ gsub_file "../../lib/ajax-chosen-rails/version.rb", /VERSION\s=\s"(\d|\.)+"$/ do |match|
19
+ %Q{VERSION = "#{version}"}
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ module AjaxChosen
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ EDITOR_VERSION = "0.1.0"
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,80 @@
1
+ (($) ->
2
+
3
+ $.fn.ajaxChosen = (options, callback) ->
4
+ # This will come in handy later.
5
+ select = this
6
+
7
+ # Load chosen. To make things clear, I have taken the liberty
8
+ # of using the .chzn-autoselect class to specify input elements
9
+ # we want to use with ajax autocomplete.
10
+ this.chosen()
11
+
12
+ # Now that chosen is loaded normally, we can bootstrap it with
13
+ # our ajax autocomplete code.
14
+ this.next('.chzn-container')
15
+ .find(".search-field > input")
16
+ .bind 'keyup', ->
17
+ # This code will be executed every time the user types a letter
18
+ # into the input form that chosen has created
19
+
20
+ # Retrieve the current value of the input form
21
+ val = $.trim $(this).attr('value')
22
+
23
+ # Some simple validation so we don't make excess ajax calls. I am
24
+ # assuming you don't want to perform a search with less than 3
25
+ # characters.
26
+ return false if val.length < 3 or val is $(this).data('prevVal')
27
+
28
+ # Set the current search term so we don't execute the ajax call if
29
+ # the user hits a key that isn't an input letter/number/symbol
30
+ $(this).data('prevVal', val)
31
+
32
+ # This is a useful reference for later
33
+ field = $(this)
34
+
35
+ # I'm assuming that it's ok to use the parameter name `term` to send
36
+ # the form value during the ajax call. Change if absolutely needed.
37
+ options.data = term: val
38
+
39
+ # If the user provided an ajax success callback, store it so we can
40
+ # call it after our bootstrapping is finished.
41
+ success ?= options.success
42
+
43
+ # Create our own callback that will be executed when the ajax call is
44
+ # finished.
45
+ options.success = (data) ->
46
+ # Exit if the data we're given is invalid
47
+ return if not data?
48
+
49
+ # Go through all of the <option> elements in the <select> and remove
50
+ # ones that have not been selected by the user.
51
+ select.find('option').each -> $(this).remove() if not $(this).is(":selected")
52
+
53
+ # Send the ajax results to the user callback so we can get an object of
54
+ # value => text pairs to inject as <option> elements.
55
+ items = callback data
56
+
57
+ # Iterate through the given data and inject the <option> elements into
58
+ # the DOM
59
+ $.each items, (value, text) ->
60
+ $("<option />")
61
+ .attr('value', value)
62
+ .html(text)
63
+ .appendTo(select)
64
+
65
+ # Tell chosen that the contents of the <select> input have been updated
66
+ # This makes chosen update its internal list of the input data.
67
+ select.trigger("liszt:updated")
68
+
69
+ # For some reason, the contents of the input field get removed once you
70
+ # call trigger above. Often, this can be very annoying (and can make some
71
+ # searches impossible), so we add the value the user was typing back into
72
+ # the input field.
73
+ field.attr('value', val)
74
+
75
+ # Finally, call the user supplied callback (if it exists)
76
+ success() if success?
77
+
78
+ # Execute the ajax call to search for autocomplete data
79
+ $.ajax(options)
80
+ )(jQuery)
@@ -0,0 +1,47 @@
1
+ (function() {
2
+ (function($) {
3
+ return $.fn.ajaxChosen = function(options, callback) {
4
+ var select;
5
+ select = this;
6
+ this.chosen();
7
+ return this.next('.chzn-container').find(".search-field > input").bind('keyup', function() {
8
+ var field, val;
9
+ val = $.trim($(this).attr('value'));
10
+ if (val.length < 3 || val === $(this).data('prevVal')) {
11
+ return false;
12
+ }
13
+ $(this).data('prevVal', val);
14
+ field = $(this);
15
+ options.data = {
16
+ term: val
17
+ };
18
+ if (typeof success !== "undefined" && success !== null) {
19
+ success;
20
+ } else {
21
+ success = options.success;
22
+ };
23
+ options.success = function(data) {
24
+ var items;
25
+ if (!(data != null)) {
26
+ return;
27
+ }
28
+ select.find('option').each(function() {
29
+ if (!$(this).is(":selected")) {
30
+ return $(this).remove();
31
+ }
32
+ });
33
+ items = callback(data);
34
+ $.each(items, function(value, text) {
35
+ return $("<option />").attr('value', value).html(text).appendTo(select);
36
+ });
37
+ select.trigger("liszt:updated");
38
+ field.attr('value', val);
39
+ if (typeof success !== "undefined" && success !== null) {
40
+ return success();
41
+ }
42
+ };
43
+ return $.ajax(options);
44
+ });
45
+ };
46
+ })(jQuery);
47
+ }).call(this);
@@ -0,0 +1,88 @@
1
+ root = this
2
+
3
+ class ajaxChosen extends Chosen
4
+ activate_field: ->
5
+ if @options.show_on_activate and not @active_field
6
+ this.results_show()
7
+ super
8
+
9
+ constructor: (select, @options, callback) ->
10
+ # Load chosen. To make things clear, I have taken the liberty
11
+ # of using the .chzn-autoselect class to specify input elements
12
+ # we want to use with ajax autocomplete.
13
+ super select, options
14
+
15
+ # Now that chosen is loaded normally, we can bootstrap it with
16
+ # our ajax autocomplete code.
17
+ select.next('.chzn-container')
18
+ .down('.chzn-search > input')
19
+ .observe 'keyup', ->
20
+ # This code will be executed every time the user types a letter
21
+ # into the input form that chosen has created
22
+
23
+ # Retrieve the current value of the input form
24
+ val = $(this).value.strip()
25
+ # Some simple validation so we don't make excess ajax calls. I am
26
+ # assuming you don't want to perform a search with less than 3
27
+ # characters.
28
+
29
+ return false if val.length < 3 or val is $(this).readAttribute('data-prevVal')
30
+
31
+ # Set the current search term so we don't execute the ajax call if
32
+ # the user hits a key that isn't an input letter/number/symbol
33
+ $(this).writeAttribute('data-prevVal', val)
34
+
35
+ # This is a useful reference for later
36
+ field = $(this)
37
+
38
+ # I'm assuming that it's ok to use the parameter name `term` to send
39
+ # the form value during the ajax call. Change if absolutely needed.
40
+ query_key = options.query_key || "term"
41
+ (options.parameters ||= {})[query_key] = val
42
+
43
+ # If the user provided an ajax success callback, store it so we can
44
+ # call it after our bootstrapping is finished.
45
+ success = options.success
46
+
47
+ # Create our own callback that will be executed when the ajax call is
48
+ # finished.
49
+ options.onSuccess = (data) ->
50
+ # Exit if the data we're given is invalid
51
+ return if not data?
52
+
53
+ # Go through all of the <option> elements in the <select> and remove
54
+ # ones that have not been selected by the user.
55
+ select.childElements().each (el) -> el.remove() if not el.selected
56
+
57
+ # Send the ajax results to the user callback so we can get an object of
58
+ # value => text pairs to inject as <option> elements.
59
+ items = if callback then callback(data.responseJSON) else data.responseJSON
60
+
61
+ # Iterate through the given data and inject the <option> elements into
62
+ # the DOM
63
+ $H(items).each (pair) ->
64
+ if select.value != pair.key
65
+ select.insert
66
+ bottom:
67
+ new Element("option", {value: pair.key})
68
+ .update(pair.value)
69
+
70
+ val = field.value
71
+
72
+ # Tell chosen that the contents of the <select> input have been updated
73
+ # This makes chosen update its internal list of the input data.
74
+ select.fire("liszt:updated")
75
+
76
+ # For some reason, the contents of the input field get removed once you
77
+ # call trigger above. Often, this can be very annoying (and can make some
78
+ # searches impossible), so we add the value the user was typing back into
79
+ # the input field.
80
+ field.value = val
81
+
82
+ # Finally, call the user supplied callback (if it exists)
83
+ success() if success?
84
+
85
+ # Execute the ajax call to search for autocomplete data
86
+ new Ajax.Request options.url, options
87
+
88
+ root.ajaxChosen = ajaxChosen
@@ -0,0 +1,67 @@
1
+ (function() {
2
+ var ajaxChosen, root;
3
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
4
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
5
+ function ctor() { this.constructor = child; }
6
+ ctor.prototype = parent.prototype;
7
+ child.prototype = new ctor;
8
+ child.__super__ = parent.prototype;
9
+ return child;
10
+ };
11
+ root = this;
12
+ ajaxChosen = (function() {
13
+ __extends(ajaxChosen, Chosen);
14
+ ajaxChosen.prototype.activate_field = function() {
15
+ if (this.options.show_on_activate && !this.active_field) {
16
+ this.results_show();
17
+ }
18
+ return ajaxChosen.__super__.activate_field.apply(this, arguments);
19
+ };
20
+ function ajaxChosen(select, options, callback) {
21
+ this.options = options;
22
+ ajaxChosen.__super__.constructor.call(this, select, options);
23
+ select.next('.chzn-container').down('.chzn-search > input').observe('keyup', function() {
24
+ var field, query_key, success, val;
25
+ val = $(this).value.strip();
26
+ if (val.length < 3 || val === $(this).readAttribute('data-prevVal')) {
27
+ return false;
28
+ }
29
+ $(this).writeAttribute('data-prevVal', val);
30
+ field = $(this);
31
+ query_key = options.query_key || "term";
32
+ (options.parameters || (options.parameters = {}))[query_key] = val;
33
+ success = options.success;
34
+ options.onSuccess = function(data) {
35
+ var items;
36
+ if (!(data != null)) {
37
+ return;
38
+ }
39
+ select.childElements().each(function(el) {
40
+ if (!el.selected) {
41
+ return el.remove();
42
+ }
43
+ });
44
+ items = callback ? callback(data.responseJSON) : data.responseJSON;
45
+ $H(items).each(function(pair) {
46
+ if (select.value !== pair.key) {
47
+ return select.insert({
48
+ bottom: new Element("option", {
49
+ value: pair.key
50
+ }).update(pair.value)
51
+ });
52
+ }
53
+ });
54
+ val = field.value;
55
+ select.fire("liszt:updated");
56
+ field.value = val;
57
+ if (success != null) {
58
+ return success();
59
+ }
60
+ };
61
+ return new Ajax.Request(options.url, options);
62
+ });
63
+ }
64
+ return ajaxChosen;
65
+ })();
66
+ root.ajaxChosen = ajaxChosen;
67
+ }).call(this);
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ajax-chosen-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathan Broadbent
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: &74066110 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *74066110
25
+ - !ruby/object:Gem::Dependency
26
+ name: thor
27
+ requirement: &74065860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.14'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *74065860
36
+ - !ruby/object:Gem::Dependency
37
+ name: chosen-rails
38
+ requirement: &74065670 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *74065670
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &74065400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *74065400
58
+ - !ruby/object:Gem::Dependency
59
+ name: rails
60
+ requirement: &74065150 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *74065150
69
+ - !ruby/object:Gem::Dependency
70
+ name: sass
71
+ requirement: &74064920 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '3.1'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *74064920
80
+ description: Chosen is a javascript library of select box enhancer for jQuery and
81
+ Protoype. This gem integrates Ajax-Chosen with Rails asset pipeline for ease of
82
+ use.
83
+ email:
84
+ - nathan.f77@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - .gitignore
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - ajax-chosen-rails.gemspec
95
+ - lib/ajax-chosen-rails.rb
96
+ - lib/ajax-chosen-rails/engine.rb
97
+ - lib/ajax-chosen-rails/railtie.rb
98
+ - lib/ajax-chosen-rails/source_file.rb
99
+ - lib/ajax-chosen-rails/version.rb
100
+ - vendor/assets/VERSION
101
+ - vendor/assets/javascripts/ajax-chosen.jquery.coffee
102
+ - vendor/assets/javascripts/ajax-chosen.jquery.js
103
+ - vendor/assets/javascripts/ajax-chosen.proto.coffee
104
+ - vendor/assets/javascripts/ajax-chosen.proto.js
105
+ homepage: https://github.com/fatfreecrm/ajax-chosen-rails
106
+ licenses: []
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.10
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Integrate Ajax-Chosen javascript library with Rails asset pipeline
129
+ test_files: []