vanilla_nested 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42bb398c9c058f38333cb4c3af17957fb5477da3
4
- data.tar.gz: bae3d4d29d855c18f9f676e7a3aeed0b58b67e5b
3
+ metadata.gz: d53bc9016018c8253312d3a74d267c8a3e2e953e
4
+ data.tar.gz: 2c96fb414de1b65ecf49ab9e7e546eda0878d623
5
5
  SHA512:
6
- metadata.gz: 8a45642b3b74560d5cbfa90178f37c9eac5daf890ed2e0f25915e57b2bccdc5f4006d3106d04dfac2370e95d3d5ca9c444b8c50266a105698aa4a8901484b535
7
- data.tar.gz: 5a476ebc770c15df32a6bd3579534acf7f3d7f4bed4f54ba9d278c63d2af6d679a373e0b0feccb33a8361428cec62cd36dbaeb81d4fa7a91204b08a375e50755
6
+ metadata.gz: 7af84e0fa64113f033fca6bed7e423d4f02f50e5e3e5e1f45fb207c25d2a6b38de425aa2721a34fc902376b0c342436fa6760ceddaf95c55af2313bf60f27643
7
+ data.tar.gz: b505d4899dc73b090bf8fb880caf7c43166f89a97367ec8bc49961529fc974e2d71251106b6309453f119252f5da421499c927de89deba38f45869e0a24fbf14
@@ -1,6 +1,9 @@
1
1
  (function(){
2
+ // Get the html from the data attribute and insert the new fields on the container
3
+ // "event" is the click event of the link created by the rails helper
2
4
  window.addVanillaNestedFields = function(event) {
3
5
  event.preventDefault();
6
+
4
7
  const element = event.target;
5
8
  const data = element.dataset;
6
9
  const newHtml = data.html.replace(/_idx_placeholder_/g, Date.now());
@@ -17,11 +20,15 @@
17
20
  inserted = container.firstElementChild;
18
21
  break;
19
22
  }
23
+
20
24
  _dispatchEvent(container, 'vanilla-nested:fields-added', element, {added: inserted})
21
25
  }
22
26
 
27
+ // Removes the fields or hides them until the undo timer times out
28
+ // "event" is the click event of the link created by the rails helper
23
29
  window.removeVanillaNestedFields = function(event) {
24
30
  event.preventDefault();
31
+
25
32
  const element = event.target;
26
33
  const data = element.dataset;
27
34
  let wrapper = element.parentElement;
@@ -38,14 +45,21 @@
38
45
  wrapper.querySelector('[name$="[_destroy]"]').value = '1';
39
46
  }
40
47
 
48
+ // Hides an element, mainly the wrapper of a group of fields
49
+ // "wrapper" is the wrapper of the link to remove fields
41
50
  function hideWrapper(wrapper) {
42
51
  wrapper.style.display = 'none';
43
52
  }
44
53
 
54
+ // Unhides the children given a fields wrapper
55
+ // "wrapper" is the wrapper of the link to remove fields
45
56
  function unhideFields(wrapper) {
46
57
  [...wrapper.children].forEach(child => child.style.display = 'initial');
47
58
  }
48
59
 
60
+ // Hides an element and adds an "undo" link to unhide it
61
+ // "wrapper" is the wrapper to hide
62
+ // "element" is the link to remove the wrapper
49
63
  function hideFieldsWithUndo(wrapper, element) {
50
64
  [...wrapper.children].forEach(child => child.style.display = 'none');
51
65
 
@@ -76,8 +90,6 @@
76
90
  let timer = setTimeout(_onTimerCompleted, ms);
77
91
  }
78
92
 
79
-
80
-
81
93
  function _dispatchEvent(element, eventName, triggeredBy, details) {
82
94
  if (!details) details = {};
83
95
  details.triggeredBy = triggeredBy;
@@ -1,29 +1,59 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module VanillaNested
2
4
  module ViewHelpers
5
+ # @param form [FormBuild] builder on a "form_for" block
6
+ # @param association [Symbol] name of the association
7
+ # @param container_selector [String] selector of the element to inser the fields
8
+ # @param link_text [String, nil] text to use for the link tag
9
+ # @param link_classes [String] space separated classes for the link tag
10
+ # @param insert_method [:append, :prepend] tells javascript if the new fields should be appended or prepended to the container
11
+ # @param partial_form_variable [String, Symbol] name of the variable that represents the form builder inside the fields partial
12
+ # @return [String] link tag
3
13
  def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form)
4
14
  association_class = form.object.class.reflections[association.to_s].klass
5
15
  object = association_class.new
6
16
 
7
- partial_name = partial ? partial : "#{association_class.name.downcase}_fields"
17
+ partial_name = partial || "#{association_class.name.underscore}_fields"
8
18
 
9
19
  html = capture do
10
- form.fields_for association, object, child_index: "_idx_placeholder_" do |ff|
11
- render partial: partial_name, locals: {partial_form_variable => ff}
20
+ form.fields_for association, object, child_index: '_idx_placeholder_' do |ff|
21
+ render partial: partial_name, locals: { partial_form_variable => ff }
12
22
  end
13
23
  end
14
24
 
15
- methodForInsert = [:append, :prepend].include?(insert_method.to_sym) ? insert_method : :append
25
+ method_for_insert = %i[append prepend].include?(insert_method.to_sym) ? insert_method : :append
16
26
 
17
27
  classes = "vanilla-nested-add #{link_classes}"
18
- link_to '#', class: classes, onclick: 'addVanillaNestedFields(event)', data: {'container-selector': container_selector, html: html, 'method-for-insert': methodForInsert} do
28
+ data = {
29
+ 'container-selector': container_selector,
30
+ 'html': html,
31
+ 'method-for-insert': method_for_insert
32
+ }
33
+
34
+ link_to '#', class: classes, onclick: 'addVanillaNestedFields(event)', data: data do
19
35
  link_text || "Add #{association_class.model_name}"
20
36
  end
21
37
  end
22
38
 
39
+ # @param form [FormBuilder] builder on a "form_for" block
40
+ # @param link_text [String, nil] text for the link, defaults to 'X'
41
+ # @param fields_wrapper_selector [String] selector for the wrapper of the fields, must be an ancestor
42
+ # @param undo_link_timeout [Integer] time until undo timeouts
43
+ # @param undo_link_text [String] text to show as "undo"
44
+ # @param undo_link_classes [String] space separated list of classes
45
+ # @return [String] hidden field and link tag
23
46
  def link_to_remove_nested(form, link_text: 'X', fields_wrapper_selector: nil, undo_link_timeout: nil, undo_link_text: 'Undo', undo_link_classes: '')
47
+ data = {
48
+ 'fields-wrapper-selector': fields_wrapper_selector,
49
+ 'undo-timeout': undo_link_timeout,
50
+ 'undo-text': undo_link_text,
51
+ 'undo-link-classes': undo_link_classes
52
+ }
53
+
24
54
  capture do
25
55
  concat form.hidden_field(:_destroy, value: 0)
26
- concat link_to(link_text, '#', class: 'vanilla-nested-remove', onclick: 'removeVanillaNestedFields(event)', data: {'fields-wrapper-selector': fields_wrapper_selector, 'undo-timeout': undo_link_timeout, 'undo-text': undo_link_text, 'undo-link-classes': undo_link_classes})
56
+ concat link_to(link_text, '#', class: 'vanilla-nested-remove', onclick: 'removeVanillaNestedFields(event)', data: data)
27
57
  end
28
58
  end
29
59
  end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'vanilla_nested/view_helpers'
2
4
 
3
5
  module VanillaNested
4
6
  class Engine < ::Rails::Engine
5
- initializer "vanilla_nested.initialize" do |app|
7
+ initializer 'vanilla_nested.initialize' do |_app|
6
8
  ActiveSupport.on_load :action_view do
7
9
  ActionView::Base.send :include, VanillaNested::ViewHelpers
8
10
  end
9
11
  end
10
12
  end
11
- end
13
+ 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.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Juodziukynas <arieljuod@gmail.com>