ajax-chosen-rails 0.1.1 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,8 +10,6 @@ class SourceFile < Thor
10
10
  branch = "master"
11
11
  get "#{remote}/raw/#{branch}/src/ajax-chosen.jquery.coffee", "javascripts/ajax-chosen.jquery.coffee"
12
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
13
  get "#{remote}/raw/#{branch}/VERSION", "VERSION"
16
14
  inside destination_root do
17
15
  version = File.read("VERSION").sub("\n", "")
@@ -1,6 +1,6 @@
1
1
  module AjaxChosen
2
2
  module Rails
3
- VERSION = "0.1.1"
4
- EDITOR_VERSION = "0.1.1"
3
+ VERSION = "0.1.5"
4
+ EDITOR_VERSION = "0.1.5"
5
5
  end
6
6
  end
@@ -3,7 +3,7 @@ root = this
3
3
  class ajaxChosen extends Chosen
4
4
  activate_field: ->
5
5
  if @options.show_on_activate and not @active_field
6
- this.results_show()
6
+ this.results_show()
7
7
  super
8
8
 
9
9
  constructor: (select, @options, callback) ->
@@ -12,6 +12,9 @@ class ajaxChosen extends Chosen
12
12
  # we want to use with ajax autocomplete.
13
13
  super select, options
14
14
 
15
+ # Save a reference to the chosen object
16
+ chosen = $(this)
17
+
15
18
  # Now that chosen is loaded normally, we can bootstrap it with
16
19
  # our ajax autocomplete code.
17
20
  select.next('.chzn-container')
@@ -22,67 +25,74 @@ class ajaxChosen extends Chosen
22
25
 
23
26
  # Retrieve the current value of the input form
24
27
  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?
28
+ # Don't perform the AJAX search until user stops typing for a
29
+ # minimum delay
52
30
 
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
31
+ if window.ajaxChosenDelayTimer
32
+ clearTimeout window.ajaxChosenDelayTimer
33
+ window.ajaxChosenDelayTimer = nil
34
+
35
+ # This reference is frozen in the following closures
36
+ search_field = $(this)
60
37
 
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")
38
+ window.ajaxChosenDelayTimer = setTimeout ->
39
+ return false if val is search_field.readAttribute('data-prevVal')
40
+
41
+ # Set the current search term so we don't execute the ajax call if
42
+ # the user hits a key that isn't an input letter/number/symbol
43
+ search_field.writeAttribute('data-prevVal', val)
75
44
 
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
45
+ # I'm assuming that it's ok to use the parameter name `term` to send
46
+ # the form value during the ajax call. Change if absolutely needed.
47
+ query_key = options.query_key || "term"
48
+ (options.parameters ||= {})[query_key] = val
81
49
 
82
- # Finally, call the user supplied callback (if it exists)
83
- success() if success?
50
+ # If the user provided an ajax success callback, store it so we can
51
+ # call it after our bootstrapping is finished.
52
+ success = options.success
84
53
 
85
- # Execute the ajax call to search for autocomplete data
86
- new Ajax.Request options.url, options
54
+ # Create our own callback that will be executed when the ajax call is
55
+ # finished.
56
+ options.onSuccess = (data) ->
57
+ # Exit if the data we're given is invalid
58
+ return if not data?
59
+
60
+ # Go through all of the <option> elements in the <select> and remove
61
+ # ones that have not been selected by the user.
62
+ select.childElements().each (el) -> el.remove() if not el.selected
63
+
64
+ # Send the ajax results to the user callback so we can get an object of
65
+ # value => text pairs to inject as <option> elements.
66
+ items = if callback then callback(data.responseJSON) else data.responseJSON
67
+
68
+ # Iterate through the given data and inject the <option> elements into
69
+ # the DOM
70
+ $H(items).each (pair) ->
71
+ if select.value != pair.key
72
+ select.insert
73
+ bottom:
74
+ new Element("option", {value: pair.key})
75
+ .update(pair.value)
76
+
77
+ val = search_field.value
78
+
79
+ # Tell chosen that the contents of the <select> input have been updated
80
+ # This makes chosen update its internal list of the input data.
81
+ select.fire("liszt:updated")
82
+
83
+ # For some reason, the contents of the input field get removed once you
84
+ # call trigger above. Often, this can be very annoying (and can make some
85
+ # searches impossible), so we add the value the user was typing back into
86
+ # the input field.
87
+ search_field.value = val
88
+
89
+ chosen.winnow_results()
90
+
91
+ # Finally, call the user supplied callback (if it exists)
92
+ success() if success?
93
+
94
+ # Execute the ajax call to search for autocomplete data
95
+ new Ajax.Request options.url, options
96
+ , 300
87
97
 
88
98
  root.ajaxChosen = ajaxChosen
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajax-chosen-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-03 00:00:00.000000000Z
12
+ date: 2012-02-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &84780440 !ruby/object:Gem::Requirement
16
+ requirement: &81776980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *84780440
24
+ version_requirements: *81776980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: thor
27
- requirement: &84779900 !ruby/object:Gem::Requirement
27
+ requirement: &81776730 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.14'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *84779900
35
+ version_requirements: *81776730
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: chosen-rails
38
- requirement: &84779240 !ruby/object:Gem::Requirement
38
+ requirement: &81776540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *84779240
46
+ version_requirements: *81776540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &84776960 !ruby/object:Gem::Requirement
49
+ requirement: &81776270 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '1.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *84776960
57
+ version_requirements: *81776270
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rails
60
- requirement: &84776670 !ruby/object:Gem::Requirement
60
+ requirement: &81776020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *84776670
68
+ version_requirements: *81776020
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sass
71
- requirement: &84776090 !ruby/object:Gem::Requirement
71
+ requirement: &81775790 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '3.1'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *84776090
79
+ version_requirements: *81775790
80
80
  description: Chosen is a javascript library of select box enhancer for jQuery and
81
81
  Protoype. This gem integrates Ajax-Chosen with Rails asset pipeline for ease of
82
82
  use.