recordselect 4.0.15 → 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 +83 -64
- 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,61 +112,29 @@
|
|
|
112
112
|
|
|
113
113
|
window.RecordSelect = new Object();
|
|
114
114
|
RecordSelect.document_loaded = false;
|
|
115
|
+
RecordSelect.callbacks = {};
|
|
115
116
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
rs.current_xhr = xhr;
|
|
138
|
-
console.log(rs.current_xhr);
|
|
139
|
-
if (cur) cur.abort();
|
|
140
|
-
});
|
|
141
|
-
jQuery(document).on('ajax:complete', '.record-select-container', function(event, xhr, status) {
|
|
142
|
-
var rs = jQuery(this).data('recordselect');
|
|
143
|
-
if (status != 'abort' || rs.current_xhr == xhr) {
|
|
144
|
-
if (rs.is_open()) rs.show();
|
|
145
|
-
rs.current_xhr = null;
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
jQuery(document).on('click', 'input.recordselect ~ .clear-input-button', function(event) {
|
|
149
|
-
var $clear_button = jQuery(event.target), $input = $clear_button.prevAll('input');
|
|
150
|
-
if (!$input.length) return;
|
|
151
|
-
$input.val('').removeClass('selected');
|
|
152
|
-
$clear_button.removeClass('enabled');
|
|
153
|
-
});
|
|
154
|
-
jQuery(document).on('input recordselect:change', 'input.recordselect', function(event) {
|
|
155
|
-
var $clear_button = jQuery(event.target).nextAll('.clear-input-button').first();
|
|
156
|
-
if (!$clear_button.length) return;
|
|
157
|
-
if (jQuery(event.target).val()) $clear_button.addClass('enabled');
|
|
158
|
-
else $clear_button.removeClass('enabled');
|
|
159
|
-
});
|
|
160
|
-
jQuery(document).on('ajax:beforeSend', 'a.rs-mode', function() {
|
|
161
|
-
jQuery(this).closest('.record-select').find('form input[name="rs_mode"]').val(jQuery(this).data('value'));
|
|
162
|
-
});
|
|
163
|
-
jQuery(document).on('focus', 'input.recordselect', function() {
|
|
164
|
-
jQuery(this).prop('focused', true);
|
|
165
|
-
});
|
|
166
|
-
jQuery(document).on('blur', 'input.recordselect', function() {
|
|
167
|
-
jQuery(this).prop('focused', false);
|
|
168
|
-
});
|
|
169
|
-
});
|
|
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
|
+
};
|
|
170
138
|
|
|
171
139
|
RecordSelect.from_attributes = function(item) {
|
|
172
140
|
if (item.prop('disabled')) return;
|
|
@@ -225,12 +193,8 @@
|
|
|
225
193
|
this.options = options;
|
|
226
194
|
this.container;
|
|
227
195
|
this.min_length = options.min_length || 0;
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
if (this.options.onselect && typeof this.options.onselect != 'function') {
|
|
232
|
-
this.options.onselect = eval(this.options.onselect);
|
|
233
|
-
}
|
|
196
|
+
this.options.onchange = RecordSelect.resolveCallback(this.options.onchange, 'onchange');
|
|
197
|
+
this.options.onselect = RecordSelect.resolveCallback(this.options.onselect, 'onselect');
|
|
234
198
|
|
|
235
199
|
if (RecordSelect.document_loaded) {
|
|
236
200
|
this.onload();
|
|
@@ -473,7 +437,7 @@
|
|
|
473
437
|
|
|
474
438
|
/**
|
|
475
439
|
* Used by link_to_record_select
|
|
476
|
-
* The options hash
|
|
440
|
+
* The options hash may contain an onselect key with a function or registered callback name.
|
|
477
441
|
*/
|
|
478
442
|
RecordSelect.Dialog = RecordSelect.Abstract.extend({
|
|
479
443
|
onload: function() {
|
|
@@ -483,7 +447,7 @@
|
|
|
483
447
|
},
|
|
484
448
|
|
|
485
449
|
onselect: function(id, value, text, item) {
|
|
486
|
-
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();
|
|
487
451
|
},
|
|
488
452
|
|
|
489
453
|
toggle: function(e) {
|
|
@@ -496,7 +460,7 @@
|
|
|
496
460
|
/**
|
|
497
461
|
* Used by record_select_field helper
|
|
498
462
|
* The options hash may contain id: and label: keys, designating the current value
|
|
499
|
-
* 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.
|
|
500
464
|
* and field_name: key, where value will be set as name of the input field.
|
|
501
465
|
*/
|
|
502
466
|
RecordSelect.Single = RecordSelect.Abstract.extend({
|
|
@@ -547,7 +511,7 @@
|
|
|
547
511
|
/**
|
|
548
512
|
* Used by record_select_autocomplete helper
|
|
549
513
|
* The options hash may contain label: key, designating the current value
|
|
550
|
-
* 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.
|
|
551
515
|
*/
|
|
552
516
|
RecordSelect.Autocomplete = RecordSelect.Abstract.extend({
|
|
553
517
|
onload: function() {
|
|
@@ -635,6 +599,61 @@
|
|
|
635
599
|
this.list_container.prepend(entry)
|
|
636
600
|
}
|
|
637
601
|
});
|
|
602
|
+
|
|
603
|
+
jQuery(document).ready(function() {
|
|
604
|
+
RecordSelect.document_loaded = true;
|
|
605
|
+
jQuery('[data-rs-type]').each(function() { RecordSelect.from_attributes(jQuery(this)); });
|
|
606
|
+
jQuery(document).on('click', 'div.record-select li.record', function(event) {
|
|
607
|
+
var link = jQuery(this);
|
|
608
|
+
if (link.length) {
|
|
609
|
+
RecordSelect.select_item(link);
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
return true;
|
|
613
|
+
});
|
|
614
|
+
jQuery(document).on('click', '.record-select-option .remove', function(event) {
|
|
615
|
+
var line = jQuery(this).parent(), value = line.find(':input:hidden').val();
|
|
616
|
+
line.parent().data('recordselect').obj.trigger('recordselect:remove', [value]);
|
|
617
|
+
line.remove();
|
|
618
|
+
return false;
|
|
619
|
+
});
|
|
620
|
+
jQuery(document).on('ajax:beforeSend', '.record-select-container', function(event, xhr) {
|
|
621
|
+
var rs = jQuery(this).data('recordselect'), cur = rs.current_xhr, found = jQuery(this).find('.found');
|
|
622
|
+
jQuery(this).find('.record, .pagination').remove();
|
|
623
|
+
found.html(found.data('searching'));
|
|
624
|
+
rs.current_xhr = xhr;
|
|
625
|
+
console.log(rs.current_xhr);
|
|
626
|
+
if (cur) cur.abort();
|
|
627
|
+
});
|
|
628
|
+
jQuery(document).on('ajax:complete', '.record-select-container', function(event, xhr, status) {
|
|
629
|
+
var rs = jQuery(this).data('recordselect');
|
|
630
|
+
if (status != 'abort' || rs.current_xhr == xhr) {
|
|
631
|
+
if (rs.is_open()) rs.show();
|
|
632
|
+
rs.current_xhr = null;
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
jQuery(document).on('click', 'input.recordselect ~ .clear-input-button', function(event) {
|
|
636
|
+
var $clear_button = jQuery(event.target), $input = $clear_button.prevAll('input');
|
|
637
|
+
if (!$input.length) return;
|
|
638
|
+
$input.val('').removeClass('selected');
|
|
639
|
+
$clear_button.removeClass('enabled');
|
|
640
|
+
});
|
|
641
|
+
jQuery(document).on('input recordselect:change', 'input.recordselect', function(event) {
|
|
642
|
+
var $clear_button = jQuery(event.target).nextAll('.clear-input-button').first();
|
|
643
|
+
if (!$clear_button.length) return;
|
|
644
|
+
if (jQuery(event.target).val()) $clear_button.addClass('enabled');
|
|
645
|
+
else $clear_button.removeClass('enabled');
|
|
646
|
+
});
|
|
647
|
+
jQuery(document).on('ajax:beforeSend', 'a.rs-mode', function() {
|
|
648
|
+
jQuery(this).closest('.record-select').find('form input[name="rs_mode"]').val(jQuery(this).data('value'));
|
|
649
|
+
});
|
|
650
|
+
jQuery(document).on('focus', 'input.recordselect', function() {
|
|
651
|
+
jQuery(this).prop('focused', true);
|
|
652
|
+
});
|
|
653
|
+
jQuery(document).on('blur', 'input.recordselect', function() {
|
|
654
|
+
jQuery(this).prop('focused', false);
|
|
655
|
+
});
|
|
656
|
+
});
|
|
638
657
|
};
|
|
639
658
|
|
|
640
659
|
if (window.jQuery) {
|
|
@@ -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
|