vanilla_nested 1.2.2 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a6640e42d6a7195a2266b57ce2c77d3edffb1a1de0662ede0b6d19aa4b126bf
4
- data.tar.gz: 1b74a76aed703a1a2019f2b98d24ba6595f7faf72ac52acc22f8470b43204fc3
3
+ metadata.gz: 62b2d8f1c6e1c74ee3ade42bc1a96b311b008773cf2817742d3d859d255e256a
4
+ data.tar.gz: 52d1585917b4858208409fdf4c2c65d591ee8da58c823d56b57b83eb87d42f5e
5
5
  SHA512:
6
- metadata.gz: 536f8c0238182518afda5f82662a90a8695d0324f526d2666b69235c5e482275f3d95b4a77e8e519d523c208a81beb4fcfa776dceded510187052ce97f384b5d
7
- data.tar.gz: 50775a3000f06c76bc2e99c53669ded2a26c7c92775438b4ef4400f905835c60028a947f47c74c7b6853474926efdf669e2ff724e08ac5e7049b203e427759de
6
+ metadata.gz: d33496672d2818c8d9ae85503dfa136289ef1e709f3403e005313628cdd774d0af52182c02a5f22f6893e0e96de3846e8e9d3b928955ddbb5ee532c474bce1be
7
+ data.tar.gz: 1766822ad50b0046cc023d999d6f441f0cea3b15c3ecebcd0ffff5b9387c6d8aef2b959a5cbe84c1a674c4de88afff71d5dfb1ee2578b6bca1b72a1215c48a99
@@ -4,11 +4,15 @@
4
4
  window.addVanillaNestedFields = function(event) {
5
5
  event.preventDefault();
6
6
 
7
- const element = event.target;
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.children.length;
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
- const element = event.target;
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.style.display = 'none';
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,28 @@
121
137
  return undo;
122
138
  }
123
139
 
124
- document.addEventListener('DOMContentLoaded', function(){
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/turbo 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();
159
+ })
160
+
161
+ document.addEventListener('turbo:load', function(e){
162
+ if (notEmpty(e.detail.timing)) initVanillaNested();
132
163
  })
133
164
  })()
@@ -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 inser the fields
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
- if block_given?
39
- link_to '#', class: classes, data: data, &link_content
40
- else
41
- text = link_text || "Add #{association_class.model_name}"
42
- link_to text, '#', class: classes, data: data
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
- if block_given?
69
- link_to('#', class: classes, data: data, &link_content)
70
- else
71
- link_to(link_text.html_safe, '#', class: classes, data: data)
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
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.2.2
4
+ version: 1.5.1
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
- - GPL-3.0
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.6
41
+ rubygems_version: 3.0.9
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Dynamic nested forms using vanilla javascript