dependent_select 0.6.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -59,8 +59,7 @@ Finally, the controller might look like this:
59
59
  This will generate a regular +collection_select+ for the country and a +dependent_collection_select+
60
60
  for province. The later will be a regular +collection_select+ followed by a js +<script>+ tag that:
61
61
  * Will create an array with all the provinces. (+var array=[province1, province2...];+)
62
- * Will place a listeners on the +country_id+ +select+ in order to update the provinces select if
63
- the countries select is modified
62
+ * Will place a listeners on the +country_id+ +select+ in order to update the provinces select if the countries select is modified
64
63
  * Fill up the provinces select with appropiate values.
65
64
 
66
65
  *Note that this will not work if you haven't followed the installation procedure - see below*
@@ -71,7 +70,7 @@ There's a more complex example at the end of this document.
71
70
 
72
71
  ===As a gem
73
72
  Copy this on config/environment.erb, inside the gems section
74
- config.gem "splendeo-dependent_select", :lib => 'dependent_select', :source => "http://gems.github.com"
73
+ config.gem "dependent_select"
75
74
  Then execute
76
75
  rake gems:install
77
76
 
@@ -88,7 +87,7 @@ Several steps are needed:
88
87
  * And then install the new version
89
88
 
90
89
  In other words:
91
- sudo gem uninstall splendeo-dependent_select
90
+ sudo gem uninstall dependent_select
92
91
  rm public/javascripts/dependent_select/dependent_select.js
93
92
  rake gems:install
94
93
 
@@ -180,4 +179,4 @@ On your controller:
180
179
  @provinces = Province.find(:all, :order => 'name ASC') # all provinces for all countries
181
180
  @cities = City.find(:all, :order => 'name ASC') # all cities for all provinces
182
181
  end
183
- end
182
+ end
@@ -8,7 +8,7 @@ module DependentSelect
8
8
  # :collapse_spaces => false
9
9
  # )
10
10
  def self.default_options
11
- @default_options ||= { :collapse_spaces => true }
11
+ @default_options ||= { :collapse_spaces => true, :use_jquery => false }
12
12
  end
13
13
 
14
14
  # By default, spaces are collapsed on browsers when printing the select option texts
@@ -24,5 +24,18 @@ module DependentSelect
24
24
  def self.collapse_spaces=(value)
25
25
  default_options[:collapse_spaces] = value
26
26
  end
27
-
27
+
28
+ def self.collapse_spaces?
29
+ default_options[:collapse_spaces]
30
+ end
31
+
32
+ # By default, dependent_select will use Prototype. If you need to use jquery, you can do so by doing
33
+ # DependentSelect.use_jquery = true
34
+ def self.use_jquery=(value)
35
+ default_options[:use_jquery] = value
36
+ end
37
+
38
+ def self.use_jquery?
39
+ default_options[:use_jquery]
40
+ end
28
41
  end
@@ -390,18 +390,31 @@ module DependentSelect::FormHelpers
390
390
  filter_method, html_options, extra_options)
391
391
  initial_value = dependent_select_initial_value(object, method)
392
392
  include_blank = options[:include_blank] || false
393
- collapse_spaces = extra_options[:collapse_spaces] || false
394
-
393
+ collapse_spaces = extra_options[:collapse_spaces] || DependentSelect.collapse_spaces?
394
+
395
+
396
+ if(DependentSelect.use_jquery?)
397
+ js_function = 'update_depentent_select_jquery'
398
+ else
399
+ js_function = 'update_dependent_select'
400
+ end
401
+
395
402
  js_callback =
396
- "function(e) { update_dependent_select( '#{dependent_field_id}', '#{observed_field_id}', #{js_array_name}, " +
403
+ "function(e) { #{js_function}( '#{dependent_field_id}', '#{observed_field_id}', #{js_array_name}, " +
397
404
  "'#{initial_value}', #{include_blank}, #{collapse_spaces}, false); }"
405
+
406
+ if(DependentSelect.use_jquery?)
407
+ observers = "$('##{observed_field_id}').bind('change', #{js_callback});\n"
408
+ else
409
+ observers = "$('#{observed_field_id}').observe('change', #{js_callback});\n" +
410
+ "$('#{observed_field_id}').observe('DependentSelectFormBuilder:change', #{js_callback}); \n"
411
+ end
398
412
 
399
- javascript_tag(js_array_code +
400
- "$('#{observed_field_id}').observe ('change', #{js_callback});\n" +
401
- "$('#{observed_field_id}').observe ('DependentSelectFormBuilder:change', #{js_callback}); \n" +
402
- "update_dependent_select( '#{dependent_field_id}', '#{observed_field_id}', #{js_array_name}, " +
403
- "'#{initial_value}', #{include_blank}, #{collapse_spaces}, true);"
404
- )
413
+ initial_call =
414
+ "#{js_function}( '#{dependent_field_id}', '#{observed_field_id}', #{js_array_name}, " +
415
+ "'#{initial_value}', #{include_blank}, #{collapse_spaces}, true);\n"
416
+
417
+ javascript_tag(js_array_code + observers + initial_call)
405
418
  end
406
419
 
407
420
  # generates the js script for a dependent_collection_select. See +dependent_select_js+
@@ -42,4 +42,61 @@ function update_dependent_select( dependent_id, observed_id, values_array, // ma
42
42
 
43
43
  // launch a custom event (Prototype doesn't allow launcthing "change") to support dependencies of dependencies
44
44
  dependent_field.fire('DependentSelectFormBuilder:change');
45
- }
45
+ }
46
+
47
+ function update_dependent_select_jquery( dependent_id, observed_id, values_array, // mandatory
48
+ initial_value, include_blank, collapse_spaces, first_run ) { // optional
49
+
50
+ // parse the optional parameters ....
51
+ initial_value = initial_value || '';
52
+ include_blank = include_blank || false;
53
+ collapse_spaces = collapse_spaces || false;
54
+ first_run = first_run || false;
55
+
56
+ // select DOM node whose options are modified
57
+ var dependent_field = $('#'+dependent_id);
58
+
59
+ // select DOM node whose changes trigger this
60
+ var observed_field = $('#'+observed_id);
61
+
62
+ // value chosen on observed_select, used for filtering dependent_select
63
+ var filter = observed_field.value;
64
+
65
+ // the first time the update_func is executed (on edit) the value the select has
66
+ // comes directly from the model. From them on, it will only use the client's input
67
+ var previous_value = first_run ? initial_value : dependent_field.value;
68
+
69
+ // removes all options from dependent_field
70
+ dependent_field[0].options.length = 0; // empty() gives some trouble in IE7, it seems
71
+
72
+ // adds a blank option, only if specified on options
73
+ if(include_blank) {
74
+ if(!previous_value) { // it will be selected if previous_value is nil
75
+ dependent_field.append('<option selected="selected" value=""></option>');
76
+ } else {
77
+ dependent_field.append('<option value=""></option>');
78
+ }
79
+ }
80
+
81
+ // this fills the dependent select
82
+ values_array.each (function (index) {
83
+ var arrFilter = this[2];
84
+ var arrValue = this[1];
85
+ var arrText = this[0];
86
+ var opt;
87
+ if (arrFilter==filter) { // only include options with the right filter field
88
+ if(collapse_spaces == false) { // assign the text (spaces are automatically collapsed)
89
+ arrText = arrText.replace(/ /g, '\240'); // replacing spaces with &nbsp; if requested
90
+ }
91
+ if(arrValue == previous_value) { // mark it as selected if appropiate
92
+ opt = '<option selected="selected" value="'+arrValue+'">'+arrText+'</option>'
93
+ } else {
94
+ opt = '<option value="'+arrValue+'">'+arrText+'</option>';
95
+ }
96
+ dependent_field.append(opt);
97
+ }
98
+ });
99
+
100
+ // trigger change event so the dependent field is populated
101
+ dependent_field.trigger('change');
102
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependent_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Enrique Garcia Cota (egarcia)