vanilla_nested 1.2.5 → 1.6.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/app/assets/javascripts/vanilla_nested.js +21 -5
- data/lib/vanilla_nested/view_helpers.rb +27 -12
- data/lib/vanilla_nested.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7164d9ce14876a0445fc8d3b30d14c18e5caca9771313e0f53034266d99363af
|
4
|
+
data.tar.gz: f49863e8b1d520de26f2ea5088753f89d228ce63a90f46fa0410655b5fe6c1f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ccd4bdf78c71155a3fde6bb26c250244b382868c3a74fb395463c76d55f8b7c7f318ef1b586d93ce191c8fc86fea0239ef8e301c257fe5753003cd3de04d85f
|
7
|
+
data.tar.gz: 9d3d0c45b8d9e7bcd8e456b8d4a7beacc05f97a46b677079add599b2741d03f48e25e3d826be600499ede979a6af414e9ed42af474e6b023c1d772579c20ba5e
|
@@ -12,6 +12,7 @@
|
|
12
12
|
const container = document.querySelector(data.containerSelector);
|
13
13
|
const newHtml = data.html.replace(/_idx_placeholder_/g, Date.now());
|
14
14
|
|
15
|
+
// insert and store reference
|
15
16
|
let inserted;
|
16
17
|
switch (data.methodForInsert) {
|
17
18
|
case ('append'):
|
@@ -24,6 +25,9 @@
|
|
24
25
|
break;
|
25
26
|
}
|
26
27
|
|
28
|
+
// add a class to show it was added dynamically
|
29
|
+
inserted.classList.add('added-by-vanilla-nested');
|
30
|
+
|
27
31
|
_dispatchEvent(container, 'vanilla-nested:fields-added', element, {added: inserted})
|
28
32
|
|
29
33
|
let removeLink = inserted.querySelector('.vanilla-nested-remove');
|
@@ -32,7 +36,7 @@
|
|
32
36
|
|
33
37
|
// dispatch an event if we reached the limit configured on the model
|
34
38
|
if (data.limit) {
|
35
|
-
let nestedElements = container.
|
39
|
+
let nestedElements = container.querySelectorAll('[name$="[_destroy]"][value="0"]').length;
|
36
40
|
if (nestedElements >= data.limit)
|
37
41
|
_dispatchEvent(container, 'vanilla-nested:fields-limit-reached', element)
|
38
42
|
}
|
@@ -49,7 +53,8 @@
|
|
49
53
|
|
50
54
|
const data = element.dataset;
|
51
55
|
let wrapper = element.parentElement;
|
52
|
-
|
56
|
+
const sel = data.fieldsWrapperSelector;
|
57
|
+
if (sel) wrapper = element.closest(sel);
|
53
58
|
|
54
59
|
if (data.undoTimeout) {
|
55
60
|
hideFieldsWithUndo(wrapper, element);
|
@@ -65,7 +70,13 @@
|
|
65
70
|
// Hides an element, mainly the wrapper of a group of fields
|
66
71
|
// "wrapper" is the wrapper of the link to remove fields
|
67
72
|
function hideWrapper(wrapper) {
|
68
|
-
wrapper.
|
73
|
+
if (wrapper.classList.contains('added-by-vanilla-nested')) {
|
74
|
+
wrapper.remove();
|
75
|
+
} else {
|
76
|
+
const destroyInput = wrapper.querySelector('[name$="[_destroy]"');
|
77
|
+
wrapper.innerHTML = '';
|
78
|
+
wrapper.insertAdjacentElement('afterbegin', destroyInput);
|
79
|
+
}
|
69
80
|
}
|
70
81
|
|
71
82
|
// Unhides the children given a fields wrapper
|
@@ -119,7 +130,8 @@
|
|
119
130
|
const undo = document.createElement('A');
|
120
131
|
|
121
132
|
undo.classList.add('vanilla-nested-undo');
|
122
|
-
|
133
|
+
const classes = data.undoLinkClasses;
|
134
|
+
if (classes)
|
123
135
|
undo.classList.add(...classes.split(' '));
|
124
136
|
|
125
137
|
undo.innerText = data.undoText;
|
@@ -141,10 +153,14 @@
|
|
141
153
|
initVanillaNested();
|
142
154
|
})
|
143
155
|
|
144
|
-
// Don't run turbolinks event callback for first load, we already do it with DOMContentLoaded
|
156
|
+
// Don't run turbolinks/turbo event callback for first load, we already do it with DOMContentLoaded
|
145
157
|
const notEmpty = (obj) => Object.keys(obj).length;
|
146
158
|
|
147
159
|
document.addEventListener('turbolinks:load', function(e){
|
148
160
|
if (notEmpty(e.data.timing)) initVanillaNested();
|
149
161
|
})
|
162
|
+
|
163
|
+
document.addEventListener('turbo:load', function(e){
|
164
|
+
if (notEmpty(e.detail.timing)) initVanillaNested();
|
165
|
+
})
|
150
166
|
})()
|
@@ -4,14 +4,16 @@ module VanillaNested
|
|
4
4
|
module ViewHelpers
|
5
5
|
# @param form [FormBuild] builder on a "form_for" block
|
6
6
|
# @param association [Symbol] name of the association
|
7
|
-
# @param container_selector [String] selector of the element to
|
7
|
+
# @param container_selector [String] selector of the element to insert the fields
|
8
8
|
# @param link_text [String, nil] text to use for the link tag
|
9
9
|
# @param link_classes [String] space separated classes for the link tag
|
10
10
|
# @param insert_method [:append, :prepend] tells javascript if the new fields should be appended or prepended to the container
|
11
11
|
# @param partial_form_variable [String, Symbol] name of the variable that represents the form builder inside the fields partial
|
12
|
+
# @param tag [String] HTML tag to use for the html generated, defaults to and `a` tag
|
12
13
|
# @param link_content [Block] block of code for the link content
|
14
|
+
# @param tag_attributes [Hash<attribute, value>] hash with attribute,value pairs for the html tag
|
13
15
|
# @return [String] link tag
|
14
|
-
def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form, &link_content)
|
16
|
+
def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form, tag: 'a', tag_attributes: {}, &link_content)
|
15
17
|
association_class = form.object.class.reflections[association.to_s].klass
|
16
18
|
object = association_class.new
|
17
19
|
|
@@ -35,11 +37,16 @@ module VanillaNested
|
|
35
37
|
nested_options = form.object.class.nested_attributes_options[association.to_sym]
|
36
38
|
data['limit'] = nested_options[:limit] if nested_options[:limit]
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
attributes = tag_attributes
|
41
|
+
attributes[:class] = "#{attributes.fetch(:class, '')} #{classes}"
|
42
|
+
attributes[:data] = attributes.fetch(:data, {}).merge(data)
|
43
|
+
|
44
|
+
content_tag(tag, attributes) do
|
45
|
+
if block_given?
|
46
|
+
yield link_content
|
47
|
+
else
|
48
|
+
link_text || "Add #{association_class.model_name}"
|
49
|
+
end
|
43
50
|
end
|
44
51
|
end
|
45
52
|
|
@@ -50,9 +57,11 @@ module VanillaNested
|
|
50
57
|
# @param undo_link_text [String] text to show as "undo"
|
51
58
|
# @param undo_link_classes [String] space separated list of classes for the "undo" link
|
52
59
|
# @param ulink_classes [String] space separated list of classes for the "x" link
|
60
|
+
# @param tag [String] HTML tag to use for the html generated, defaults to and `a` tag
|
53
61
|
# @param link_content [Block] block of code for the link content
|
62
|
+
# @param tag_attributes [Hash<attribute, value>] hash with attribute,value pairs for the html tag
|
54
63
|
# @return [String] hidden field and link tag
|
55
|
-
def link_to_remove_nested(form, link_text: 'X', fields_wrapper_selector: nil, undo_link_timeout: nil, undo_link_text: 'Undo', undo_link_classes: '', link_classes: '', &link_content)
|
64
|
+
def link_to_remove_nested(form, link_text: 'X', fields_wrapper_selector: nil, undo_link_timeout: nil, undo_link_text: 'Undo', undo_link_classes: '', link_classes: '', tag: 'a', tag_attributes: {}, &link_content)
|
56
65
|
data = {
|
57
66
|
'fields-wrapper-selector': fields_wrapper_selector,
|
58
67
|
'undo-timeout': undo_link_timeout,
|
@@ -62,13 +71,19 @@ module VanillaNested
|
|
62
71
|
|
63
72
|
classes = "vanilla-nested-remove #{link_classes}"
|
64
73
|
|
74
|
+
attributes = tag_attributes
|
75
|
+
attributes[:class] = "#{attributes.fetch(:class, '')} #{classes}"
|
76
|
+
attributes[:data] = attributes.fetch(:data, {}).merge(data)
|
77
|
+
|
65
78
|
capture do
|
66
79
|
concat form.hidden_field(:_destroy, value: 0)
|
67
80
|
concat(
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
81
|
+
content_tag(tag, attributes) do
|
82
|
+
if block_given?
|
83
|
+
yield link_content
|
84
|
+
else
|
85
|
+
link_text.html_safe
|
86
|
+
end
|
72
87
|
end
|
73
88
|
)
|
74
89
|
end
|
data/lib/vanilla_nested.rb
CHANGED
@@ -9,5 +9,11 @@ module VanillaNested
|
|
9
9
|
ActionView::Base.send :include, VanillaNested::ViewHelpers
|
10
10
|
end
|
11
11
|
end
|
12
|
+
|
13
|
+
initializer "vanilla_nested.assets" do
|
14
|
+
if Rails.application.config.respond_to?(:assets)
|
15
|
+
Rails.application.config.assets.precompile += %w( vanilla_nested.js )
|
16
|
+
end
|
17
|
+
end
|
12
18
|
end
|
13
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vanilla_nested
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ariel Juodziukynas <arieljuod@gmail.com>
|
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
|
-
rubygems_version: 3.
|
41
|
+
rubygems_version: 3.2.22
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: Dynamic nested forms using vanilla javascript
|