vanilla_nested 1.6.0 → 1.7.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: 7164d9ce14876a0445fc8d3b30d14c18e5caca9771313e0f53034266d99363af
4
- data.tar.gz: f49863e8b1d520de26f2ea5088753f89d228ce63a90f46fa0410655b5fe6c1f7
3
+ metadata.gz: 689fd14c5a2ab6ceafb0b30c3e4b38df746f9a6a2dae2d4b01b01bdbb5c70a9d
4
+ data.tar.gz: 45d3c2365ee778b388aea76105c9d1ae55eb12695f3858caa1b6af1db8dcdfea
5
5
  SHA512:
6
- metadata.gz: 1ccd4bdf78c71155a3fde6bb26c250244b382868c3a74fb395463c76d55f8b7c7f318ef1b586d93ce191c8fc86fea0239ef8e301c257fe5753003cd3de04d85f
7
- data.tar.gz: 9d3d0c45b8d9e7bcd8e456b8d4a7beacc05f97a46b677079add599b2741d03f48e25e3d826be600499ede979a6af414e9ed42af474e6b023c1d772579c20ba5e
6
+ metadata.gz: aa4051da66403dd8c93e21d907d4c8e97031dd2d9a9f3ddfd0782b748241f6e2249ffae5dd23f86dd9a99ae5063cf7941d55862e18c6e9157465b1f3dc822a32
7
+ data.tar.gz: 19862c814bc73fef0b19c8cb1da65761f0d64f164ed23475752ce49ba0c2ecf9014724dd6a7dec5764129226dc9e129d6d7a34e7bd60fd350f639ed104e71621
@@ -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,10 +37,7 @@
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
 
@@ -82,14 +72,28 @@
82
72
  // Unhides the children given a fields wrapper
83
73
  // "wrapper" is the wrapper of the link to remove fields
84
74
  function unhideFields(wrapper) {
85
- [...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
+ });
86
82
  }
87
83
 
88
84
  // Hides an element and adds an "undo" link to unhide it
89
85
  // "wrapper" is the wrapper to hide
90
86
  // "element" is the link to remove the wrapper
91
87
  function hideFieldsWithUndo(wrapper, element) {
92
- [...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
+ });
93
97
 
94
98
  // add the 'undo' link with it's callback
95
99
  const undoLink = _createUndoWithElementsData(element.dataset);
@@ -140,27 +144,42 @@
140
144
  }
141
145
 
142
146
  function initVanillaNested() {
143
- document.querySelectorAll('.vanilla-nested-add').forEach(el => {
144
- 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
+ }
145
156
  })
146
157
 
147
- document.querySelectorAll('.vanilla-nested-remove').forEach(el => {
148
- 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
+ }
149
167
  })
150
168
  }
151
169
 
152
- document.addEventListener('DOMContentLoaded', function(){
153
- initVanillaNested();
154
- })
155
-
156
- // Don't run turbolinks/turbo event callback for first load, we already do it with DOMContentLoaded
157
- const notEmpty = (obj) => Object.keys(obj).length;
158
-
159
- document.addEventListener('turbolinks:load', function(e){
160
- if (notEmpty(e.data.timing)) initVanillaNested();
161
- })
170
+ let vanillaNestedInitialized = false
171
+ const initOnce = () => {
172
+ if (!vanillaNestedInitialized) {
173
+ vanillaNestedInitialized = true
174
+ initVanillaNested()
175
+ }
176
+ }
162
177
 
163
- document.addEventListener('turbo:load', function(e){
164
- if (notEmpty(e.detail.timing)) initVanillaNested();
165
- })
178
+ if (["complete", "interactive"].includes(document.readyState)) {
179
+ // if DOMContentLoaded was already fired
180
+ initOnce()
181
+ } else {
182
+ // else wait for it
183
+ document.addEventListener("DOMContentLoaded", () => initOnce())
184
+ }
166
185
  })()
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.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Juodziukynas <arieljuod@gmail.com>
@@ -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.2.22
41
+ rubygems_version: 3.0.9
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Dynamic nested forms using vanilla javascript