vanilla_nested 1.5.1 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/vanilla_nested.js +45 -32
- data/lib/vanilla_nested.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7d2002ba8ba1ccf4f6e749969bff008be4a64f2a30d2e3ec011947d1cf0956e
|
4
|
+
data.tar.gz: e0a2d6e189be30823c4a68a95fbc48fb07c83fb1b02e1e46b4cfa1f8cf2c2323
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
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(
|
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
|
-
|
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 =>
|
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 =>
|
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
|
-
|
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.
|
142
|
-
|
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.
|
146
|
-
|
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
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
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
|
})()
|
data/lib/vanilla_nested.rb
CHANGED
@@ -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
|