vanilla_nested 1.2.0 → 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: 4dffbc6a9079eef6fa23eb04580cd0744a9563bb48435d86e539c53500184fc6
4
- data.tar.gz: 922f56a43170f687d0aba466384841474fe0198d88f45e6a4a2ad203787ebbc9
3
+ metadata.gz: ca0695302433ce2ff7b48e7673894ca698034f0cfffe9eeb822eb1e47b78943b
4
+ data.tar.gz: 9db10e3cba8445ab747f8c360403518e8bcfb4c296b1dc7aaab0bdbc629ed980
5
5
  SHA512:
6
- metadata.gz: 7b5d7b4821aed2cfc669fdf75e512b7611324879824bd85a8a0b8e7c3589d5f917148d6f724c010c14fb4c5659630e510a4a61c4d08c55e25fd888c2aa7fc62d
7
- data.tar.gz: ff26be147b8e052a170389456c900da3a21a9369739c271bb7da9ebab4043b98edfae5807dcc0e6cdb5363f3567b5a93148f9ecdd91bc31636c857510a2e918d
6
+ metadata.gz: 55c3480096e5d17f8a2a0c14ae3f1c976e3b081177a6ff51b3d8c4da3dbfd1d5af2caa720ff6c6c546c51a379a0adbc385d0f16d798532e0377bd26d3a1486f2
7
+ data.tar.gz: 4f64f3dcad06add84d899ac23415ca18489b1f2fe22c8ea57862aae7278c24eb82e056327cf7877fb7c5406ec42e57f2b83c9b49408200a626eef7c41b33a56e
@@ -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,11 +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
 
33
+ let removeLink = inserted.querySelector('.vanilla-nested-remove');
34
+ if (removeLink)
35
+ removeLink.addEventListener('click', removeVanillaNestedFields, true);
36
+
26
37
  // dispatch an event if we reached the limit configured on the model
27
38
  if (data.limit) {
28
- let nestedElements = container.children.length;
39
+ let nestedElements = container.querySelectorAll('[name$="[_destroy]"][value="0"]').length;
29
40
  if (nestedElements >= data.limit)
30
41
  _dispatchEvent(container, 'vanilla-nested:fields-limit-reached', element)
31
42
  }
@@ -36,7 +47,10 @@
36
47
  window.removeVanillaNestedFields = function(event) {
37
48
  event.preventDefault();
38
49
 
39
- 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
+
40
54
  const data = element.dataset;
41
55
  let wrapper = element.parentElement;
42
56
  if (sel = data.fieldsWrapperSelector) wrapper = element.closest(sel);
@@ -55,7 +69,13 @@
55
69
  // Hides an element, mainly the wrapper of a group of fields
56
70
  // "wrapper" is the wrapper of the link to remove fields
57
71
  function hideWrapper(wrapper) {
58
- 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
+ }
59
79
  }
60
80
 
61
81
  // Unhides the children given a fields wrapper
@@ -116,4 +136,25 @@
116
136
 
117
137
  return undo;
118
138
  }
139
+
140
+ function initVanillaNested() {
141
+ document.querySelectorAll('.vanilla-nested-add').forEach(el => {
142
+ el.addEventListener('click', addVanillaNestedFields, true);
143
+ })
144
+
145
+ document.querySelectorAll('.vanilla-nested-remove').forEach(el => {
146
+ el.addEventListener('click', removeVanillaNestedFields, true);
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();
159
+ })
119
160
  })()
@@ -4,13 +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
13
+ # @param link_content [Block] block of code for the link content
12
14
  # @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)
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)
14
16
  association_class = form.object.class.reflections[association.to_s].klass
15
17
  object = association_class.new
16
18
 
@@ -34,8 +36,12 @@ module VanillaNested
34
36
  nested_options = form.object.class.nested_attributes_options[association.to_sym]
35
37
  data['limit'] = nested_options[:limit] if nested_options[:limit]
36
38
 
37
- link_to '#', class: classes, onclick: 'addVanillaNestedFields(event)', data: data do
38
- link_text || "Add #{association_class.model_name}"
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
39
45
  end
40
46
  end
41
47
 
@@ -44,9 +50,12 @@ module VanillaNested
44
50
  # @param fields_wrapper_selector [String] selector for the wrapper of the fields, must be an ancestor
45
51
  # @param undo_link_timeout [Integer] time until undo timeouts
46
52
  # @param undo_link_text [String] text to show as "undo"
47
- # @param undo_link_classes [String] space separated list of classes
53
+ # @param undo_link_classes [String] space separated list of classes for the "undo" link
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
56
+ # @param link_content [Block] block of code for the link content
48
57
  # @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: '')
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)
50
59
  data = {
51
60
  'fields-wrapper-selector': fields_wrapper_selector,
52
61
  'undo-timeout': undo_link_timeout,
@@ -54,9 +63,19 @@ module VanillaNested
54
63
  'undo-link-classes': undo_link_classes
55
64
  }
56
65
 
66
+ classes = "vanilla-nested-remove #{link_classes}"
67
+
57
68
  capture do
58
69
  concat form.hidden_field(:_destroy, value: 0)
59
- concat link_to(link_text, '#', class: 'vanilla-nested-remove', onclick: 'removeVanillaNestedFields(event)', data: data)
70
+ concat(
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
77
+ end
78
+ )
60
79
  end
61
80
  end
62
81
  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.0
4
+ version: 1.3.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
- - 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