vanilla_nested 1.5.1 → 1.6.2

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: 62b2d8f1c6e1c74ee3ade42bc1a96b311b008773cf2817742d3d859d255e256a
4
- data.tar.gz: 52d1585917b4858208409fdf4c2c65d591ee8da58c823d56b57b83eb87d42f5e
3
+ metadata.gz: d7d2002ba8ba1ccf4f6e749969bff008be4a64f2a30d2e3ec011947d1cf0956e
4
+ data.tar.gz: e0a2d6e189be30823c4a68a95fbc48fb07c83fb1b02e1e46b4cfa1f8cf2c2323
5
5
  SHA512:
6
- metadata.gz: d33496672d2818c8d9ae85503dfa136289ef1e709f3403e005313628cdd774d0af52182c02a5f22f6893e0e96de3846e8e9d3b928955ddbb5ee532c474bce1be
7
- data.tar.gz: 1766822ad50b0046cc023d999d6f441f0cea3b15c3ecebcd0ffff5b9387c6d8aef2b959a5cbe84c1a674c4de88afff71d5dfb1ee2578b6bca1b72a1215c48a99
6
+ metadata.gz: a35ecfef2084cc706603b16a2ec45088d1d061a5007ea98ba2643769e21e6c4ea24b7f80b2057bbf80e33c5c01b0cfc52c84312b3bc16a248f65329a8a0bcd18
7
+ data.tar.gz: 372d0a1a456a17cd339fa95dcb7b65a8d16c9940acee89aeb5a803f15af0e73ff757f93b85a756dbbe9a64cc25dc8142d203b2331c8dc6f7fe171679e4622ee1
@@ -1,10 +1,7 @@
1
1
  (function(){
2
2
  // Get the html from the data attribute and insert the new fields on the container
3
3
  // "event" is the click event of the link created by the rails helper
4
- window.addVanillaNestedFields = function(event) {
5
- event.preventDefault();
6
-
7
- let element = event.target;
4
+ window.addVanillaNestedFields = function(element) {
8
5
  if (!element.classList.contains('vanilla-nested-add'))
9
6
  element = element.closest('.vanilla-nested-add')
10
7
 
@@ -30,10 +27,6 @@
30
27
 
31
28
  _dispatchEvent(container, 'vanilla-nested:fields-added', element, {added: inserted})
32
29
 
33
- let removeLink = inserted.querySelector('.vanilla-nested-remove');
34
- if (removeLink)
35
- removeLink.addEventListener('click', removeVanillaNestedFields, true);
36
-
37
30
  // dispatch an event if we reached the limit configured on the model
38
31
  if (data.limit) {
39
32
  let nestedElements = container.querySelectorAll('[name$="[_destroy]"][value="0"]').length;
@@ -44,16 +37,14 @@
44
37
 
45
38
  // Removes the fields or hides them until the undo timer times out
46
39
  // "event" is the click event of the link created by the rails helper
47
- window.removeVanillaNestedFields = function(event) {
48
- event.preventDefault();
49
-
50
- let element = event.target;
40
+ window.removeVanillaNestedFields = function(element) {
51
41
  if (!element.classList.contains('vanilla-nested-remove'))
52
42
  element = element.closest('.vanilla-nested-remove')
53
43
 
54
44
  const data = element.dataset;
55
45
  let wrapper = element.parentElement;
56
- if (sel = data.fieldsWrapperSelector) wrapper = element.closest(sel);
46
+ const sel = data.fieldsWrapperSelector;
47
+ if (sel) wrapper = element.closest(sel);
57
48
 
58
49
  if (data.undoTimeout) {
59
50
  hideFieldsWithUndo(wrapper, element);
@@ -81,14 +72,28 @@
81
72
  // Unhides the children given a fields wrapper
82
73
  // "wrapper" is the wrapper of the link to remove fields
83
74
  function unhideFields(wrapper) {
84
- [...wrapper.children].forEach(child => child.style.display = 'initial');
75
+ [...wrapper.children].forEach(child => {
76
+ if (child.dataset.hasAttributeStyle) {
77
+ child.style.display = child.dataset.originalDisplay;
78
+ } else {
79
+ child.removeAttribute("style");
80
+ }
81
+ });
85
82
  }
86
83
 
87
84
  // Hides an element and adds an "undo" link to unhide it
88
85
  // "wrapper" is the wrapper to hide
89
86
  // "element" is the link to remove the wrapper
90
87
  function hideFieldsWithUndo(wrapper, element) {
91
- [...wrapper.children].forEach(child => child.style.display = 'none');
88
+ [...wrapper.children].forEach(child => {
89
+ // store original style for after undo
90
+ if (child.getAttribute("style")) {
91
+ child.dataset.hasAttributeStyle = true;
92
+ child.dataset.originalDisplay = child.style.display;
93
+ }
94
+
95
+ child.style.display = 'none';
96
+ });
92
97
 
93
98
  // add the 'undo' link with it's callback
94
99
  const undoLink = _createUndoWithElementsData(element.dataset);
@@ -129,7 +134,8 @@
129
134
  const undo = document.createElement('A');
130
135
 
131
136
  undo.classList.add('vanilla-nested-undo');
132
- if (classes = data.undoLinkClasses)
137
+ const classes = data.undoLinkClasses;
138
+ if (classes)
133
139
  undo.classList.add(...classes.split(' '));
134
140
 
135
141
  undo.innerText = data.undoText;
@@ -138,27 +144,34 @@
138
144
  }
139
145
 
140
146
  function initVanillaNested() {
141
- document.querySelectorAll('.vanilla-nested-add').forEach(el => {
142
- el.addEventListener('click', addVanillaNestedFields, true);
147
+ document.addEventListener('click', ev => {
148
+ const addVanillaNested =
149
+ ev.target.classList.contains('vanilla-nested-add') ||
150
+ ev.target.closest('.vanilla-nested-add');
151
+
152
+ if (addVanillaNested) {
153
+ ev.preventDefault();
154
+ addVanillaNestedFields(ev.target);
155
+ }
143
156
  })
144
157
 
145
- document.querySelectorAll('.vanilla-nested-remove').forEach(el => {
146
- el.addEventListener('click', removeVanillaNestedFields, true);
158
+ document.addEventListener('click', ev => {
159
+ const removeVanillaNested =
160
+ ev.target.classList.contains('vanilla-nested-remove') ||
161
+ ev.target.closest('.vanilla-nested-remove');
162
+
163
+ if (removeVanillaNested) {
164
+ ev.preventDefault();
165
+ removeVanillaNestedFields(ev.target);
166
+ }
147
167
  })
148
168
  }
149
169
 
170
+ let vanillaNestedInitialized = false;
150
171
  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();
172
+ if (!vanillaNestedInitialized) {
173
+ vanillaNestedInitialized = true;
174
+ initVanillaNested();
175
+ }
163
176
  })
164
177
  })()
@@ -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.5.1
4
+ version: 1.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Juodziukynas <arieljuod@gmail.com>