recordselect 3.1.5 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -161,11 +161,8 @@ RecordSelect.notify = function(item) {
161
161
  if (onselect)
162
162
  {
163
163
  try {
164
- // .unescapeHTML() not implemented so far
165
- var label = $.trim(item.find('label').first().html());
166
- if (label.length == 0) {
167
- label = item.html();
168
- }
164
+ var label = $.trim(item.find('label').first().text());
165
+ if (!label) label = item.text();
169
166
  onselect(item.parent().attr('id').substr(2), label, e);
170
167
  } catch(e) {
171
168
  alert(e);
@@ -191,6 +188,9 @@ RecordSelect.Abstract = Class.extend({
191
188
  this.url = url;
192
189
  this.options = options;
193
190
  this.container;
191
+ if (this.options.onchange && typeof this.options.onchange != 'function') {
192
+ this.options.onchange = eval(this.options.onchange);
193
+ }
194
194
 
195
195
  if (RecordSelect.document_loaded) {
196
196
  this.onload();
@@ -438,6 +438,7 @@ RecordSelect.Single = RecordSelect.Abstract.extend({
438
438
  onselect: function(id, value) {
439
439
  this.set(id, value);
440
440
  if (this.options.onchange) this.options.onchange.call(this, id, value);
441
+ this.obj.trigger("recordselect:change", [id, value]);
441
442
  this.close();
442
443
  },
443
444
 
@@ -479,6 +480,7 @@ RecordSelect.Autocomplete = RecordSelect.Abstract.extend({
479
480
  onselect: function(id, value) {
480
481
  this.set(value);
481
482
  if (this.options.onchange) this.options.onchange.call(this, id, value);
483
+ this.obj.trigger("recordselect:change", [id, value]);
482
484
  this.close();
483
485
  },
484
486
 
@@ -61,6 +61,9 @@ Object.extend(RecordSelect.Abstract.prototype, {
61
61
  this.url = url;
62
62
  this.options = options;
63
63
  this.container;
64
+ if (this.options.onchange && typeof this.options.onchange != 'function') {
65
+ this.options.onchange = eval(this.options.onchange);
66
+ }
64
67
 
65
68
  if (RecordSelect.document_loaded) this.onload();
66
69
  else Event.observe(window, 'load', this.onload.bind(this));
@@ -304,6 +307,7 @@ RecordSelect.Single.prototype = Object.extend(new RecordSelect.Abstract(), {
304
307
  onselect: function(id, value) {
305
308
  this.set(id, value);
306
309
  if (this.options.onchange) this.options.onchange.call(this, id, value);
310
+ this.obj.fire('recordselect:change', {"id": id, "label": value});
307
311
  this.close();
308
312
  },
309
313
 
@@ -345,6 +349,7 @@ RecordSelect.Autocomplete.prototype = Object.extend(new RecordSelect.Abstract(),
345
349
  onselect: function(id, value) {
346
350
  this.set(value);
347
351
  if (this.options.onchange) this.options.onchange.call(this, id, value);
352
+ this.obj.fire('recordselect:change', {"id": id, "label": value});
348
353
  this.close();
349
354
  },
350
355
 
@@ -30,25 +30,27 @@ module RecordSelectHelper
30
30
  # +params+:: A hash of extra URL parameters
31
31
  # +id+:: The id to use for the input. Defaults based on the input's name.
32
32
  # +field_name+:: The name to use for the text input. Defaults to '', so field is not submitted.
33
- # +onchange+:: A JavaScript function that will be called whenever something new is selected. It should accept the new id as the first argument, and the new label as the second argument. For example, you could set onchange to be "function(id, label) {alert(id);}", or you could create a JavaScript function somewhere else and set onchange to be "my_function" (without the parantheses!).
34
33
  def record_select_field(name, current, options = {})
35
34
  options[:controller] ||= current.class.to_s.pluralize.underscore
36
35
  options[:params] ||= {}
37
36
  options[:id] ||= name.gsub(/[\[\]]/, '_')
37
+ options[:class] ||= ''
38
+ options[:class] << ' recordselect'
38
39
 
39
- id = options.delete(:id)
40
- html = text_field_tag(name, nil, :autocomplete => 'off', :id => id, :class => options.delete(:class), :onfocus => "this.focused=true", :onblur => "this.focused=false")
40
+ ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]
41
41
 
42
- controller = assert_controller_responds(options[:controller])
43
-
44
- options[:id] = options[:label] = ''
42
+ controller = assert_controller_responds(options.delete(:controller))
43
+ params = options.delete(:params)
44
+ record_select_options = {}
45
+ record_select_options[:field_name] = options.delete(:field_name) if options[:field_name]
45
46
  if current and not current.new_record?
46
- options[:id] = current.id
47
- options[:label] = label_for_field(current, controller)
47
+ record_select_options[:id] = current.id
48
+ record_select_options[:label] = label_for_field(current, controller)
48
49
  end
49
50
 
50
- url = url_for({:action => :browse, :controller => options.delete(:controller), :escape => false}.merge(options.delete(:params)))
51
- html << javascript_tag("new RecordSelect.Single(#{id.to_json}, #{url.to_json}, #{options.to_json});")
51
+ html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
52
+ url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
53
+ html << javascript_tag("new RecordSelect.Single(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")
52
54
 
53
55
  return html
54
56
  end
@@ -63,24 +65,25 @@ module RecordSelectHelper
63
65
  # +controller+:: The controller configured to provide the result set. Optional if you have standard resource controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the class of +current+ (the second argument)
64
66
  # +params+:: A hash of extra URL parameters
65
67
  # +id+:: The id to use for the input. Defaults based on the input's name.
66
- # +onchange+:: A JavaScript function that will be called whenever something new is selected. It should accept the new id as the first argument, and the new label as the second argument. For example, you could set onchange to be "function(id, label) {alert(id);}", or you could create a JavaScript function somewhere else and set onchange to be "my_function" (without the parantheses!).
67
68
  def record_select_autocomplete(name, current, options = {})
68
69
  options[:controller] ||= current.class.to_s.pluralize.underscore
69
70
  options[:params] ||= {}
70
71
  options[:id] ||= name.gsub(/[\[\]]/, '_')
72
+ options[:class] ||= ''
73
+ options[:class] << ' recordselect'
71
74
 
72
- id = options.delete(:id)
73
- html = text_field_tag(name, nil, :autocomplete => 'off', :id => id, :class => options.delete(:class), :onfocus => "this.focused=true", :onblur => "this.focused=false")
74
-
75
- controller = assert_controller_responds(options[:controller])
75
+ ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]
76
76
 
77
- options[:label] = ''
77
+ controller = assert_controller_responds(options.delete(:controller))
78
+ params = options.delete(:params)
79
+ record_select_options = {}
78
80
  if current and not current.new_record?
79
- options[:label] = label_for_field(current, controller)
81
+ record_select_options[:label] = label_for_field(current, controller)
80
82
  end
81
83
 
82
- url = url_for({:action => :browse, :controller => options.delete(:controller), :escape => false}.merge(options.delete(:params)))
83
- html << javascript_tag("new RecordSelect.Autocomplete(#{id.to_json}, #{url.to_json}, #{options.to_json});")
84
+ html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
85
+ url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
86
+ html << javascript_tag("new RecordSelect.Autocomplete(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")
84
87
 
85
88
  return html
86
89
  end
@@ -116,19 +119,22 @@ module RecordSelectHelper
116
119
  options[:controller] ||= current.first.class.to_s.pluralize.underscore
117
120
  options[:params] ||= {}
118
121
  options[:id] ||= name.gsub(/[\[\]]/, '_')
122
+ options[:class] ||= ''
123
+ options[:class] << ' recordselect'
124
+ options.delete(:name)
119
125
 
120
- controller = assert_controller_responds(options[:controller])
126
+ controller = assert_controller_responds(options.delete(:controller))
127
+ params = options.delete(:params)
128
+ record_select_options = {}
129
+ record_select_options[:current] = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
130
+
131
+ html = text_field_tag("#{name}[]", nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
132
+ html << content_tag('ul', '', :class => 'record-select-list');
121
133
 
122
134
  # js identifier so we can talk to it.
123
135
  widget = "rs_%s" % name.gsub(/[\[\]]/, '_').chomp('_')
124
-
125
- current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
126
-
127
- url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
128
-
129
- html = text_field_tag("#{name}[]", nil, :autocomplete => 'off', :id => options[:id], :class => options[:class], :onfocus => "this.focused=true", :onblur => "this.focused=false")
130
- html << content_tag('ul', '', :class => 'record-select-list');
131
- html << javascript_tag("#{widget} = new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, {current: #{current.to_json}});")
136
+ url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
137
+ html << javascript_tag("#{widget} = new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")
132
138
 
133
139
  return html
134
140
  end
@@ -2,7 +2,7 @@ module RecordSelect
2
2
  module Version
3
3
  MAJOR = 3
4
4
  MINOR = 1
5
- PATCH = 5
5
+ PATCH = 6
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recordselect
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 5
10
- version: 3.1.5
9
+ - 6
10
+ version: 3.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergio Cambra
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-11-11 00:00:00 Z
20
+ date: 2011-11-14 00:00:00 Z
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
23
  requirement: &id001 !ruby/object:Gem::Requirement