katalyst-tables 2.2.1 → 2.2.2

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
  SHA256:
3
- metadata.gz: 187059d630f338d5bcdd0ecc98eb3850fa7d4d01e6d37f0918143873c7a50033
4
- data.tar.gz: ac469214c3cdcf72b97ec5e5ab973356307d4d218d13face3ed48a82f9c09035
3
+ metadata.gz: 6b8413e971a3164d1ab1fa688a20ce34e1f25768da7650565d9f600e1612a80e
4
+ data.tar.gz: d14455c86030073e293aafbaaf86a4da483be27912cca5a8d46cc43cc3a532b8
5
5
  SHA512:
6
- metadata.gz: 0a134c643f8780ff95287de876492b356adaa0e822b4fd72afe68980135859813cc5e9a1ac0c391bb227b27bc597a32c79266c1c88afcd005cc3186c3c30e27c
7
- data.tar.gz: 5eb871b2122d8edd2e95724f52d8a073ff4109e8281aadb1b7e8df231dd1f00d1096de80af370f8a3a6b42bfa8b9025254e29e2e16fb18ccc488c56222bee61e
6
+ metadata.gz: 5825f4058bc40aa823dbdd0719d8738a5fa4e4738442438c7e0c86ae9942cb5fc163d19b7754eb27b1abd061699dccfd79b249c93ceef8ce3605c101a4262522
7
+ data.tar.gz: ed4ccfa22d6342eff7c6f0a84ac6c28472263c249846594d794492c40c2b2a8bd9265f26fe897cb5a331c489b2b2d5caf59da98619ad16763a8a4dd4e8124f05
@@ -1,20 +1,26 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class OrderableFormController extends Controller {
4
- add(name, value) {
4
+ add(item, index) {
5
+ const { id_name, id_value, index_name } = item.paramsValue;
5
6
  this.element.insertAdjacentHTML(
6
7
  "beforeend",
7
- `<input type="hidden" name="${name}" value="${value}" data-generated>`,
8
+ `<input type="hidden" name="${id_name}" value="${id_value}" data-generated>
9
+ <input type="hidden" name="${index_name}" value="${index}" data-generated>`,
8
10
  );
9
11
  }
10
12
 
11
13
  submit() {
14
+ if (this.inputs.length === 0) return;
15
+
12
16
  this.element.requestSubmit();
13
17
  }
14
18
 
15
19
  clear() {
16
- this.element
17
- .querySelectorAll("input[data-generated]")
18
- .forEach((input) => input.remove());
20
+ this.inputs.forEach((input) => input.remove());
21
+ }
22
+
23
+ get inputs() {
24
+ return this.element.querySelectorAll("input[data-generated]");
19
25
  }
20
26
  }
@@ -2,7 +2,10 @@ import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class OrderableRowController extends Controller {
4
4
  static values = {
5
- name: String,
6
- value: Number,
5
+ params: Object,
7
6
  };
7
+
8
+ get index() {
9
+ return this.paramsValue.index_value;
10
+ }
8
11
  }
@@ -41,8 +41,8 @@ export default class OrderableListController extends Controller {
41
41
 
42
42
  // insert any items that have changed position
43
43
  this.tablesOrderableItemOutlets.forEach((item, index) => {
44
- if (item.valueValue !== index) {
45
- this.tablesOrderableFormOutlet.add(item.nameValue, index);
44
+ if (item.index !== index) {
45
+ this.tablesOrderableFormOutlet.add(item, index);
46
46
  }
47
47
  });
48
48
 
@@ -26,7 +26,7 @@ module Katalyst
26
26
  def row_partial(row, record = nil)
27
27
  partial = @partial || model_name&.param_key&.to_s
28
28
  as = @as || model_name&.param_key&.to_sym
29
- render(partial: partial, variants: [:row], locals: { as => record, row: row })
29
+ render(partial: partial, variants: [:row], formats: [:html], locals: { as => record, row: row })
30
30
  end
31
31
  end
32
32
  end
@@ -13,32 +13,30 @@ module Katalyst
13
13
 
14
14
  using HasHtmlAttributes
15
15
 
16
- # Enhance a given table component class with orderable support.
17
- # Supports extension via `included` and `extended` hooks.
18
- def self.make_orderable(table_class)
16
+ # Support for inclusion in a table component class
17
+ # Adds an `orderable` slot and component configuration
18
+ included do
19
19
  # Add `orderable` slot to table component
20
- table_class.config_component :orderable, default: "FormComponent"
21
- table_class.renders_one(:orderable, lambda do |**attrs|
20
+ config_component :orderable, default: "Katalyst::Tables::Orderable::FormComponent"
21
+ renders_one(:orderable, lambda do |**attrs|
22
22
  orderable_component.new(table: self, **attrs)
23
23
  end)
24
24
  end
25
25
 
26
- # Support for inclusion in a table component class
27
- included do
28
- Orderable.make_orderable(self)
29
- end
30
-
31
26
  # Support for extending a table component instance
27
+ # Adds methods to the table component instance
32
28
  def self.extended(table)
33
- Orderable.make_orderable(table.class)
29
+ table.extend(TableMethods)
30
+
31
+ # ensure row components support orderable column calls
32
+ table.send(:add_orderable_columns)
34
33
  end
35
34
 
36
35
  def initialize(**attributes)
37
36
  super
38
37
 
39
- # Add `orderable` columns to row components
40
- header_row_component.include(HeaderRow)
41
- body_row_component.include(BodyRow)
38
+ # ensure row components support orderable column calls
39
+ add_orderable_columns
42
40
  end
43
41
 
44
42
  def tbody_attributes
@@ -57,6 +55,31 @@ module Katalyst
57
55
  )
58
56
  end
59
57
 
58
+ private
59
+
60
+ # Add `orderable` columns to row components
61
+ def add_orderable_columns
62
+ header_row_component.include(HeaderRow)
63
+ body_row_component.include(BodyRow)
64
+ end
65
+
66
+ # Methods required to emulate a slot when extending an existing table.
67
+ module TableMethods
68
+ def with_orderable(**attrs)
69
+ @orderable = FormComponent.new(table: self, **attrs)
70
+
71
+ self
72
+ end
73
+
74
+ def orderable?
75
+ @orderable.present?
76
+ end
77
+
78
+ def orderable
79
+ @orderable
80
+ end
81
+ end
82
+
60
83
  module HeaderRow # :nodoc:
61
84
  def ordinal(attribute = :ordinal, **)
62
85
  cell(attribute, class: "ordinal", label: "")
@@ -64,13 +87,17 @@ module Katalyst
64
87
  end
65
88
 
66
89
  module BodyRow # :nodoc:
67
- def ordinal(attribute = :ordinal, id: :id)
68
- name = @table.orderable.record_scope(@record, id, attribute)
69
- value = @record.public_send(attribute)
90
+ def ordinal(attribute = :ordinal, primary_key: :id)
91
+ id = @record.public_send(primary_key)
92
+ params = {
93
+ id_name: @table.orderable.record_scope(id, primary_key),
94
+ id_value: id,
95
+ index_name: @table.orderable.record_scope(id, attribute),
96
+ index_value: @record.public_send(attribute),
97
+ }
70
98
  cell(attribute, class: "ordinal", data: {
71
- controller: ITEM_CONTROLLER,
72
- "#{ITEM_CONTROLLER}-name-value" => name,
73
- "#{ITEM_CONTROLLER}-value-value" => value,
99
+ controller: ITEM_CONTROLLER,
100
+ "#{ITEM_CONTROLLER}-params-value": params.to_json,
74
101
  }) { t("katalyst.tables.orderable.value") }
75
102
  end
76
103
 
@@ -98,8 +125,8 @@ module Katalyst
98
125
  @scope = scope
99
126
  end
100
127
 
101
- def record_scope(record, id, attribute)
102
- "#{scope}[#{record.public_send(id)}][#{attribute}]"
128
+ def record_scope(id, attribute)
129
+ "#{scope}[#{id}][#{attribute}]"
103
130
  end
104
131
 
105
132
  def call
@@ -13,10 +13,16 @@ module Katalyst
13
13
 
14
14
  include ::Turbo::StreamsHelper
15
15
 
16
+ # Is turbo rendering enabled for this component?
16
17
  def turbo?
17
18
  @turbo
18
19
  end
19
20
 
21
+ # Are we rendering a turbo stream response?
22
+ def turbo_stream_response?
23
+ response.media_type.eql?("text/vnd.turbo-stream.html")
24
+ end
25
+
20
26
  def initialize(turbo: true, **options)
21
27
  super(**options)
22
28
 
@@ -44,16 +50,27 @@ module Katalyst
44
50
  super
45
51
 
46
52
  redefinition_lock.synchronize do
47
- component_class.alias_method(:vc_render_template_for, :render_template_for)
48
- component_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
49
- def render_template_for(variant = nil)
50
- if turbo? && response.media_type.eql?("text/vnd.turbo-stream.html")
51
- turbo_stream.replace(id, vc_render_template_for(variant))
53
+ # Capture the instance method added by the default compiler and
54
+ # wrap it in a turbo stream replacement. Take care to ensure that
55
+ # subclasses of this component don't break delegation, as each
56
+ # subclass of ViewComponent::Base defines its own version of this
57
+ # method.
58
+ vc_render_template = component_class.instance_method(:render_template_for)
59
+ component_class.define_method(:render_template_for) do |variant = nil|
60
+ # VC discards the output from this method and uses the buffer
61
+ # if both are set. Capture and wrap the output.
62
+ content = capture { vc_render_template.bind_call(self, variant) }
63
+ # In turbo mode, replace the inner-most element using a turbo
64
+ # stream. Note that we only want one turbo stream per component
65
+ # from this mechanism, as subclasses may want to concat their
66
+ # own additional streams.
67
+ if turbo? && turbo_stream_response? && !@streamed
68
+ @streamed = true
69
+ concat(turbo_stream.replace(id, content))
52
70
  else
53
- vc_render_template_for(variant)
71
+ concat(content)
54
72
  end
55
73
  end
56
- RUBY
57
74
  end
58
75
  end
59
76
  end
@@ -7,6 +7,8 @@ module Katalyst
7
7
  class TableComponent < ::Katalyst::TableComponent
8
8
  include Tables::TurboReplaceable
9
9
 
10
+ attr_reader :id
11
+
10
12
  def initialize(collection:, id:, header: true, **options)
11
13
  header = if header.is_a?(Hash)
12
14
  default_header_options.merge(header)
@@ -14,11 +16,9 @@ module Katalyst
14
16
  default_header_options
15
17
  end
16
18
 
17
- super(collection: collection, header: header, id: id, **options)
18
- end
19
+ @id = id
19
20
 
20
- def id
21
- html_attributes[:id]
21
+ super(collection: collection, header: header, id: id, **options)
22
22
  end
23
23
 
24
24
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Tables
5
- VERSION = "2.2.1"
5
+ VERSION = "2.2.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive