vanilla_nested 1.2.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/vanilla_nested.js +35 -8
- data/lib/vanilla_nested/view_helpers.rb +36 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d47f68290b80623181ca454d3c30088e5c8178f6b7846b01affdbf07ec51e94
|
4
|
+
data.tar.gz: 20a430b78106da4df2220667b05b9ef0e14172768750b4e198b6642cf1689470
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94bc333d6a5ba8949068ff25a77f76eb4073bd0fe91bffea4cae5fa9865edbaaa274ed98618b5c532898073673e7709bbb6dc9abadfe84c29259eace7bf16f05
|
7
|
+
data.tar.gz: 979c5c4f836a8dc4685723a3226864f8d0b8a11c5df08ed4a78a228afa7f1199e907adac3f45c8c99d8671937c4bd5fe8052a10f2f717cf462c14fc5a2ce6ccb
|
@@ -4,11 +4,15 @@
|
|
4
4
|
window.addVanillaNestedFields = function(event) {
|
5
5
|
event.preventDefault();
|
6
6
|
|
7
|
-
|
7
|
+
let element = event.target;
|
8
|
+
if (!element.classList.contains('vanilla-nested-add'))
|
9
|
+
element = element.closest('.vanilla-nested-add')
|
10
|
+
|
8
11
|
const data = element.dataset;
|
9
12
|
const container = document.querySelector(data.containerSelector);
|
10
13
|
const newHtml = data.html.replace(/_idx_placeholder_/g, Date.now());
|
11
14
|
|
15
|
+
// insert and store reference
|
12
16
|
let inserted;
|
13
17
|
switch (data.methodForInsert) {
|
14
18
|
case ('append'):
|
@@ -21,15 +25,18 @@
|
|
21
25
|
break;
|
22
26
|
}
|
23
27
|
|
28
|
+
// add a class to show it was added dynamically
|
29
|
+
inserted.classList.add('added-by-vanilla-nested');
|
30
|
+
|
24
31
|
_dispatchEvent(container, 'vanilla-nested:fields-added', element, {added: inserted})
|
25
32
|
|
26
33
|
let removeLink = inserted.querySelector('.vanilla-nested-remove');
|
27
34
|
if (removeLink)
|
28
|
-
removeLink.addEventListener('click', removeVanillaNestedFields);
|
35
|
+
removeLink.addEventListener('click', removeVanillaNestedFields, true);
|
29
36
|
|
30
37
|
// dispatch an event if we reached the limit configured on the model
|
31
38
|
if (data.limit) {
|
32
|
-
let nestedElements = container.
|
39
|
+
let nestedElements = container.querySelectorAll('[name$="[_destroy]"][value="0"]').length;
|
33
40
|
if (nestedElements >= data.limit)
|
34
41
|
_dispatchEvent(container, 'vanilla-nested:fields-limit-reached', element)
|
35
42
|
}
|
@@ -40,7 +47,10 @@
|
|
40
47
|
window.removeVanillaNestedFields = function(event) {
|
41
48
|
event.preventDefault();
|
42
49
|
|
43
|
-
|
50
|
+
let element = event.target;
|
51
|
+
if (!element.classList.contains('vanilla-nested-remove'))
|
52
|
+
element = element.closest('.vanilla-nested-remove')
|
53
|
+
|
44
54
|
const data = element.dataset;
|
45
55
|
let wrapper = element.parentElement;
|
46
56
|
if (sel = data.fieldsWrapperSelector) wrapper = element.closest(sel);
|
@@ -59,7 +69,13 @@
|
|
59
69
|
// Hides an element, mainly the wrapper of a group of fields
|
60
70
|
// "wrapper" is the wrapper of the link to remove fields
|
61
71
|
function hideWrapper(wrapper) {
|
62
|
-
wrapper.
|
72
|
+
if (wrapper.classList.contains('added-by-vanilla-nested')) {
|
73
|
+
wrapper.remove();
|
74
|
+
} else {
|
75
|
+
const destroyInput = wrapper.querySelector('[name$="[_destroy]"');
|
76
|
+
wrapper.innerHTML = '';
|
77
|
+
wrapper.insertAdjacentElement('afterbegin', destroyInput);
|
78
|
+
}
|
63
79
|
}
|
64
80
|
|
65
81
|
// Unhides the children given a fields wrapper
|
@@ -121,13 +137,24 @@
|
|
121
137
|
return undo;
|
122
138
|
}
|
123
139
|
|
124
|
-
|
140
|
+
function initVanillaNested() {
|
125
141
|
document.querySelectorAll('.vanilla-nested-add').forEach(el => {
|
126
|
-
el.addEventListener('click', addVanillaNestedFields);
|
142
|
+
el.addEventListener('click', addVanillaNestedFields, true);
|
127
143
|
})
|
128
144
|
|
129
145
|
document.querySelectorAll('.vanilla-nested-remove').forEach(el => {
|
130
|
-
el.addEventListener('click', removeVanillaNestedFields);
|
146
|
+
el.addEventListener('click', removeVanillaNestedFields, true);
|
131
147
|
})
|
148
|
+
}
|
149
|
+
|
150
|
+
document.addEventListener('DOMContentLoaded', function(){
|
151
|
+
initVanillaNested();
|
152
|
+
})
|
153
|
+
|
154
|
+
// Don't run turbolinks event callback for first load, we already do it with DOMContentLoaded
|
155
|
+
const notEmpty = (obj) => Object.keys(obj).length;
|
156
|
+
|
157
|
+
document.addEventListener('turbolinks:load', function(e){
|
158
|
+
if (notEmpty(e.data.timing)) initVanillaNested();
|
132
159
|
})
|
133
160
|
})()
|
@@ -4,13 +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
|
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
|
12
15
|
# @return [String] link tag
|
13
|
-
def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form)
|
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)
|
14
17
|
association_class = form.object.class.reflections[association.to_s].klass
|
15
18
|
object = association_class.new
|
16
19
|
|
@@ -34,8 +37,16 @@ module VanillaNested
|
|
34
37
|
nested_options = form.object.class.nested_attributes_options[association.to_sym]
|
35
38
|
data['limit'] = nested_options[:limit] if nested_options[:limit]
|
36
39
|
|
37
|
-
|
38
|
-
|
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
|
39
50
|
end
|
40
51
|
end
|
41
52
|
|
@@ -44,9 +55,13 @@ module VanillaNested
|
|
44
55
|
# @param fields_wrapper_selector [String] selector for the wrapper of the fields, must be an ancestor
|
45
56
|
# @param undo_link_timeout [Integer] time until undo timeouts
|
46
57
|
# @param undo_link_text [String] text to show as "undo"
|
47
|
-
# @param undo_link_classes [String] space separated list of classes
|
58
|
+
# @param undo_link_classes [String] space separated list of classes for the "undo" link
|
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
|
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
|
48
63
|
# @return [String] hidden field and link tag
|
49
|
-
def link_to_remove_nested(form, link_text: 'X', fields_wrapper_selector: nil, undo_link_timeout: nil, undo_link_text: 'Undo', undo_link_classes: '')
|
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)
|
50
65
|
data = {
|
51
66
|
'fields-wrapper-selector': fields_wrapper_selector,
|
52
67
|
'undo-timeout': undo_link_timeout,
|
@@ -54,9 +69,23 @@ module VanillaNested
|
|
54
69
|
'undo-link-classes': undo_link_classes
|
55
70
|
}
|
56
71
|
|
72
|
+
classes = "vanilla-nested-remove #{link_classes}"
|
73
|
+
|
74
|
+
attributes = tag_attributes
|
75
|
+
attributes[:class] = "#{attributes.fetch(:class, '')} #{classes}"
|
76
|
+
attributes[:data] = attributes.fetch(:data, {}).merge(data)
|
77
|
+
|
57
78
|
capture do
|
58
79
|
concat form.hidden_field(:_destroy, value: 0)
|
59
|
-
concat
|
80
|
+
concat(
|
81
|
+
content_tag(tag, attributes) do
|
82
|
+
if block_given?
|
83
|
+
yield link_content
|
84
|
+
else
|
85
|
+
link_text.html_safe
|
86
|
+
end
|
87
|
+
end
|
88
|
+
)
|
60
89
|
end
|
61
90
|
end
|
62
91
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ariel Juodziukynas <arieljuod@gmail.com>
|
@@ -21,7 +21,7 @@ files:
|
|
21
21
|
- lib/vanilla_nested/view_helpers.rb
|
22
22
|
homepage: https://github.com/arielj/vanilla-nested
|
23
23
|
licenses:
|
24
|
-
-
|
24
|
+
- MIT
|
25
25
|
metadata: {}
|
26
26
|
post_install_message:
|
27
27
|
rdoc_options: []
|
@@ -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.0.
|
41
|
+
rubygems_version: 3.0.9
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: Dynamic nested forms using vanilla javascript
|