vanilla_nested 1.2.5 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b6d8ed9c309bf72014920bec0254c922804f9077967492047139cb3aabc54f3
4
- data.tar.gz: 890d67164554668f8054724329cb9bfe56884e73ab44a864ef3b8283c011cae9
3
+ metadata.gz: ca0695302433ce2ff7b48e7673894ca698034f0cfffe9eeb822eb1e47b78943b
4
+ data.tar.gz: 9db10e3cba8445ab747f8c360403518e8bcfb4c296b1dc7aaab0bdbc629ed980
5
5
  SHA512:
6
- metadata.gz: c6df5288710235fce651f21a7d03aff41c9d7d529bbb1f0626677bdfe3bf8f670b1277fde0b942c8978a7240de1ecd0586f96d218e6c63f4a27eb63325539964
7
- data.tar.gz: f2b7ec1060c226133354d30cb0643620bf12251657771357429f21a3069c76390085715eeb7fcc381ae4ca016ec0d6a632f0573eb1ec5f7df7bac6f1fa835296
6
+ metadata.gz: 55c3480096e5d17f8a2a0c14ae3f1c976e3b081177a6ff51b3d8c4da3dbfd1d5af2caa720ff6c6c546c51a379a0adbc385d0f16d798532e0377bd26d3a1486f2
7
+ data.tar.gz: 4f64f3dcad06add84d899ac23415ca18489b1f2fe22c8ea57862aae7278c24eb82e056327cf7877fb7c5406ec42e57f2b83c9b49408200a626eef7c41b33a56e
@@ -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.children.length;
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
  }
@@ -65,7 +69,13 @@
65
69
  // Hides an element, mainly the wrapper of a group of fields
66
70
  // "wrapper" is the wrapper of the link to remove fields
67
71
  function hideWrapper(wrapper) {
68
- 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
+ }
69
79
  }
70
80
 
71
81
  // Unhides the children given a fields wrapper
@@ -4,14 +4,15 @@ 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
13
14
  # @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)
15
+ 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', &link_content)
15
16
  association_class = form.object.class.reflections[association.to_s].klass
16
17
  object = association_class.new
17
18
 
@@ -35,11 +36,12 @@ module VanillaNested
35
36
  nested_options = form.object.class.nested_attributes_options[association.to_sym]
36
37
  data['limit'] = nested_options[:limit] if nested_options[:limit]
37
38
 
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
39
+ content_tag(tag, class: classes, data: data) do
40
+ if block_given?
41
+ yield link_content
42
+ else
43
+ link_text || "Add #{association_class.model_name}"
44
+ end
43
45
  end
44
46
  end
45
47
 
@@ -50,9 +52,10 @@ module VanillaNested
50
52
  # @param undo_link_text [String] text to show as "undo"
51
53
  # @param undo_link_classes [String] space separated list of classes for the "undo" link
52
54
  # @param ulink_classes [String] space separated list of classes for the "x" link
55
+ # @param tag [String] HTML tag to use for the html generated, defaults to and `a` tag
53
56
  # @param link_content [Block] block of code for the link content
54
57
  # @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)
58
+ 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', &link_content)
56
59
  data = {
57
60
  'fields-wrapper-selector': fields_wrapper_selector,
58
61
  'undo-timeout': undo_link_timeout,
@@ -65,10 +68,12 @@ module VanillaNested
65
68
  capture do
66
69
  concat form.hidden_field(:_destroy, value: 0)
67
70
  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)
71
+ content_tag(tag, class: classes, data: data) do
72
+ if block_given?
73
+ yield link_content
74
+ else
75
+ link_text.html_safe
76
+ end
72
77
  end
73
78
  )
74
79
  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.5
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Juodziukynas <arieljuod@gmail.com>