recordselect 4.0.16 → 4.1.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.
- checksums.yaml +4 -4
- data/README +17 -0
- data/app/assets/javascripts/record_select.js +29 -10
- data/lib/record_select/helpers/record_select_helper.rb +2 -3
- data/lib/record_select/version.rb +2 -2
- data/lib/record_select.rb +4 -0
- data/test/recordselect_test.rb +23 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a243e59277ac5a6973dde6e3979ee5026aca33c723ff9d0adf5c7943dd21e3f4
|
|
4
|
+
data.tar.gz: 2c0f99881a36211228808d8aeddba434aba7425fd95c258b0108dbd6475b6a80
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a770452932e2c9dcf149f1b3c572b673c0506a530f04b9c9d19a5e6047fcfc4e996af97f797a098389d2658a4ea7c56e8daae1eeb3cbd472850ce587738a9684
|
|
7
|
+
data.tar.gz: 4f0ad104466ab6abfc12767b019d8b93e05206634292a596570ae3fa4c006b7bfeddd31ff21cc8c2012f6bdbf3257dc57075cd8a01ad34f7c334b3a131e5159b
|
data/README
CHANGED
|
@@ -43,5 +43,22 @@ import 'record_select'
|
|
|
43
43
|
|
|
44
44
|
Either using assets pipeline or importmap, it will be loaded when requiring or importing active_scaffold, so no need to add it to application.js, importmap.rb or manifest.js
|
|
45
45
|
|
|
46
|
+
= CALLBACKS
|
|
47
|
+
|
|
48
|
+
Register callbacks in a JavaScript file and pass their names to the helpers:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
RecordSelect.registerCallback('application.recordSelected', function(id, value, text, item) {
|
|
52
|
+
// `this` is the RecordSelect widget.
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```rb
|
|
57
|
+
link_to_record_select 'Select', 'users', onselect: 'application.recordSelected'
|
|
58
|
+
record_select_field 'user_id', user, rs: {onchange: 'application.recordSelected'}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
JavaScript source strings are no longer accepted for +onselect+ or +onchange+. JavaScript callers may still pass a function directly.
|
|
62
|
+
|
|
46
63
|
= DEPENDENCIES
|
|
47
64
|
This depends on the excellent Paginator gem by Bruce Williams. This simple gem is available at paginator.rubyforge.org.
|
|
@@ -112,6 +112,29 @@
|
|
|
112
112
|
|
|
113
113
|
window.RecordSelect = new Object();
|
|
114
114
|
RecordSelect.document_loaded = false;
|
|
115
|
+
RecordSelect.callbacks = {};
|
|
116
|
+
|
|
117
|
+
RecordSelect.registerCallback = function(name, callback) {
|
|
118
|
+
if (typeof callback != 'function') {
|
|
119
|
+
throw new TypeError('RecordSelect callback ' + name + ' must be a function');
|
|
120
|
+
}
|
|
121
|
+
RecordSelect.callbacks[name] = callback;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
RecordSelect.resolveCallback = function(value, option) {
|
|
125
|
+
if (!value || typeof value == 'function') return value;
|
|
126
|
+
if (typeof value != 'string') {
|
|
127
|
+
throw new TypeError('RecordSelect option ' + option + ' must be a function or registered callback name');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (Object.prototype.hasOwnProperty.call(RecordSelect.callbacks, value)) {
|
|
131
|
+
var callback = RecordSelect.callbacks[value];
|
|
132
|
+
if (typeof callback == 'function') return callback;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
throw new Error('RecordSelect option ' + option + ' references unknown callback ' + value +
|
|
136
|
+
'. Register it with RecordSelect.registerCallback.');
|
|
137
|
+
};
|
|
115
138
|
|
|
116
139
|
RecordSelect.from_attributes = function(item) {
|
|
117
140
|
if (item.prop('disabled')) return;
|
|
@@ -170,12 +193,8 @@
|
|
|
170
193
|
this.options = options;
|
|
171
194
|
this.container;
|
|
172
195
|
this.min_length = options.min_length || 0;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
if (this.options.onselect && typeof this.options.onselect != 'function') {
|
|
177
|
-
this.options.onselect = eval(this.options.onselect);
|
|
178
|
-
}
|
|
196
|
+
this.options.onchange = RecordSelect.resolveCallback(this.options.onchange, 'onchange');
|
|
197
|
+
this.options.onselect = RecordSelect.resolveCallback(this.options.onselect, 'onselect');
|
|
179
198
|
|
|
180
199
|
if (RecordSelect.document_loaded) {
|
|
181
200
|
this.onload();
|
|
@@ -418,7 +437,7 @@
|
|
|
418
437
|
|
|
419
438
|
/**
|
|
420
439
|
* Used by link_to_record_select
|
|
421
|
-
* The options hash
|
|
440
|
+
* The options hash may contain an onselect key with a function or registered callback name.
|
|
422
441
|
*/
|
|
423
442
|
RecordSelect.Dialog = RecordSelect.Abstract.extend({
|
|
424
443
|
onload: function() {
|
|
@@ -428,7 +447,7 @@
|
|
|
428
447
|
},
|
|
429
448
|
|
|
430
449
|
onselect: function(id, value, text, item) {
|
|
431
|
-
if (this.options.onselect(id, value, text, item) != false) this.close();
|
|
450
|
+
if (!this.options.onselect || this.options.onselect.call(this, id, value, text, item) != false) this.close();
|
|
432
451
|
},
|
|
433
452
|
|
|
434
453
|
toggle: function(e) {
|
|
@@ -441,7 +460,7 @@
|
|
|
441
460
|
/**
|
|
442
461
|
* Used by record_select_field helper
|
|
443
462
|
* The options hash may contain id: and label: keys, designating the current value
|
|
444
|
-
* The options hash may also include an onchange
|
|
463
|
+
* The options hash may also include an onchange key with a function or registered callback name.
|
|
445
464
|
* and field_name: key, where value will be set as name of the input field.
|
|
446
465
|
*/
|
|
447
466
|
RecordSelect.Single = RecordSelect.Abstract.extend({
|
|
@@ -492,7 +511,7 @@
|
|
|
492
511
|
/**
|
|
493
512
|
* Used by record_select_autocomplete helper
|
|
494
513
|
* The options hash may contain label: key, designating the current value
|
|
495
|
-
* The options hash may also include an onchange
|
|
514
|
+
* The options hash may also include an onchange key with a function or registered callback name.
|
|
496
515
|
*/
|
|
497
516
|
RecordSelect.Autocomplete = RecordSelect.Abstract.extend({
|
|
498
517
|
onload: function() {
|
|
@@ -13,14 +13,13 @@ module RecordSelectHelper
|
|
|
13
13
|
# Adds a link on the page that toggles a RecordSelect widget from the given controller.
|
|
14
14
|
#
|
|
15
15
|
# *Options*
|
|
16
|
-
# +onselect+::
|
|
16
|
+
# +onselect+:: Name of a callback registered with RecordSelect.registerCallback. If the callback returns false, the dialog will *not* close automatically.
|
|
17
17
|
# +params+:: Extra URL parameters. If any parameter is a column name, the parameter will be used as a search term to filter the result set.
|
|
18
18
|
# +html+:: Options for A tag
|
|
19
19
|
# +rs+:: Options for RecordSelect constructor
|
|
20
20
|
def link_to_record_select(name, controller, options = {})
|
|
21
21
|
options[:params] ||= {}
|
|
22
22
|
options[:params].merge!(:controller => controller, :action => :browse)
|
|
23
|
-
options[:onselect] = "(function(id, label) {#{options[:onselect]}})" if options[:onselect]
|
|
24
23
|
options[:html] ||= {}
|
|
25
24
|
options[:html][:id] ||= "rs_#{rand(9999)}"
|
|
26
25
|
js = options.include?(:js) ? options[:js] : request.xhr?
|
|
@@ -30,7 +29,7 @@ module RecordSelectHelper
|
|
|
30
29
|
record_select_options.merge! options[:rs] if options[:rs]
|
|
31
30
|
|
|
32
31
|
rs_data = {type: 'Dialog', id: options[:html][:id], url: url_for(options[:params]), options: record_select_options}
|
|
33
|
-
options[:html][:data] = rs_data.transform_keys { |k| "rs_#{k}" } unless js
|
|
32
|
+
options[:html][:data] = (options[:html][:data] || {}).merge(rs_data.transform_keys { |k| "rs_#{k}" }) unless js
|
|
34
33
|
html = link_to(name, '#', options[:html])
|
|
35
34
|
html << record_select_js(**rs_data) if js
|
|
36
35
|
|
data/lib/record_select.rb
CHANGED
data/test/recordselect_test.rb
CHANGED
|
@@ -5,8 +5,28 @@ require 'test/unit'
|
|
|
5
5
|
require 'active_support'
|
|
6
6
|
|
|
7
7
|
class RecordselectTest < ActiveSupport::TestCase
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
JAVASCRIPT_PATH = File.expand_path('../app/assets/javascripts/record_select.js', __dir__)
|
|
9
|
+
HELPERS_PATH = File.expand_path('../lib/record_select/helpers/record_select_helper.rb', __dir__)
|
|
10
|
+
|
|
11
|
+
def test_exposes_callback_registry_availability
|
|
12
|
+
require 'record_select'
|
|
13
|
+
|
|
14
|
+
assert RecordSelect.callback_registry?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_callbacks_are_resolved_without_eval
|
|
18
|
+
source = File.read(JAVASCRIPT_PATH)
|
|
19
|
+
|
|
20
|
+
assert_includes source, 'RecordSelect.registerCallback = function(name, callback)'
|
|
21
|
+
assert_includes source, "RecordSelect.resolveCallback(this.options.onselect, 'onselect')"
|
|
22
|
+
assert_includes source, 'this.options.onselect.call(this, id, value, text, item)'
|
|
23
|
+
assert_not_includes source, 'eval('
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_link_data_is_preserved_when_record_select_attributes_are_added
|
|
27
|
+
source = File.read(HELPERS_PATH)
|
|
28
|
+
|
|
29
|
+
assert_includes source, '(options[:html][:data] || {}).merge'
|
|
30
|
+
assert_not_includes source, '(function(id, label)'
|
|
11
31
|
end
|
|
12
32
|
end
|