katalyst-tables 2.2.0 → 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: fc97f1a686a28e4542c65f076afde15d61523726efb4dcaa3586e6c945121c13
4
- data.tar.gz: 10eee025b9e0daee5d3aeda7a78625e6491d91d02f99bf831092072845276185
3
+ metadata.gz: 6b8413e971a3164d1ab1fa688a20ce34e1f25768da7650565d9f600e1612a80e
4
+ data.tar.gz: d14455c86030073e293aafbaaf86a4da483be27912cca5a8d46cc43cc3a532b8
5
5
  SHA512:
6
- metadata.gz: 2a7e06d525551918f60e7b9a225f4ddb34bb3eeaeaecde37e817d0c61ade348f52e290235b21e84bedc72c92615f81e1226a10305242ff3e95e33fbb47a21878
7
- data.tar.gz: 2d23b372c056506b054c02165e7191eb73d00f76df054e0d70eafbd88d6979126a3803c11bef29889f9095f644c7e40b30396da8d48580239978a7c9ee795019
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,28 +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)
19
- # Add `orderable` columns to row components
20
- table_class.header_row_component.include(HeaderRow)
21
- table_class.body_row_component.include(BodyRow)
22
-
16
+ # Support for inclusion in a table component class
17
+ # Adds an `orderable` slot and component configuration
18
+ included do
23
19
  # Add `orderable` slot to table component
24
- table_class.config_component :orderable, default: "FormComponent"
25
- table_class.renders_one(:orderable, lambda do |**attrs|
20
+ config_component :orderable, default: "Katalyst::Tables::Orderable::FormComponent"
21
+ renders_one(:orderable, lambda do |**attrs|
26
22
  orderable_component.new(table: self, **attrs)
27
23
  end)
28
24
  end
29
25
 
30
- # Support for inclusion in a table component class
31
- included do
32
- Orderable.make_orderable(self)
33
- end
34
-
35
26
  # Support for extending a table component instance
27
+ # Adds methods to the table component instance
36
28
  def self.extended(table)
37
- Orderable.make_orderable(table.class)
29
+ table.extend(TableMethods)
30
+
31
+ # ensure row components support orderable column calls
32
+ table.send(:add_orderable_columns)
33
+ end
34
+
35
+ def initialize(**attributes)
36
+ super
37
+
38
+ # ensure row components support orderable column calls
39
+ add_orderable_columns
38
40
  end
39
41
 
40
42
  def tbody_attributes
@@ -53,6 +55,31 @@ module Katalyst
53
55
  )
54
56
  end
55
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
+
56
83
  module HeaderRow # :nodoc:
57
84
  def ordinal(attribute = :ordinal, **)
58
85
  cell(attribute, class: "ordinal", label: "")
@@ -60,13 +87,17 @@ module Katalyst
60
87
  end
61
88
 
62
89
  module BodyRow # :nodoc:
63
- def ordinal(attribute = :ordinal, id: :id)
64
- name = @table.orderable.record_scope(@record, id, attribute)
65
- 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
+ }
66
98
  cell(attribute, class: "ordinal", data: {
67
- controller: ITEM_CONTROLLER,
68
- "#{ITEM_CONTROLLER}-name-value" => name,
69
- "#{ITEM_CONTROLLER}-value-value" => value,
99
+ controller: ITEM_CONTROLLER,
100
+ "#{ITEM_CONTROLLER}-params-value": params.to_json,
70
101
  }) { t("katalyst.tables.orderable.value") }
71
102
  end
72
103
 
@@ -94,8 +125,8 @@ module Katalyst
94
125
  @scope = scope
95
126
  end
96
127
 
97
- def record_scope(record, id, attribute)
98
- "#{scope}[#{record.public_send(id)}][#{attribute}]"
128
+ def record_scope(id, attribute)
129
+ "#{scope}[#{id}][#{attribute}]"
99
130
  end
100
131
 
101
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
@@ -16,7 +16,7 @@ module Katalyst
16
16
  included do
17
17
  attr_accessor :items
18
18
 
19
- delegate :model, :to_model, :each, :count, :empty?, to: :items, allow_nil: true
19
+ delegate :model, :each, :count, :empty?, to: :items, allow_nil: true
20
20
  delegate :model_name, to: :model, allow_nil: true
21
21
  end
22
22
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Tables
5
- VERSION = "2.2.0"
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.0
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive