katalyst-tables 3.0.0.beta1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -2
  3. data/README.md +65 -187
  4. data/app/assets/builds/katalyst/tables.esm.js +17 -47
  5. data/app/assets/builds/katalyst/tables.js +17 -47
  6. data/app/assets/builds/katalyst/tables.min.js +1 -1
  7. data/app/assets/builds/katalyst/tables.min.js.map +1 -1
  8. data/app/assets/stylesheets/katalyst/tables/_index.scss +1 -0
  9. data/app/assets/stylesheets/katalyst/tables/_summary.scss +14 -0
  10. data/app/assets/stylesheets/katalyst/tables/typed-columns/_boolean.scss +1 -1
  11. data/app/assets/stylesheets/katalyst/tables/typed-columns/_currency.scss +3 -0
  12. data/app/assets/stylesheets/katalyst/tables/typed-columns/_date.scss +1 -1
  13. data/app/assets/stylesheets/katalyst/tables/typed-columns/_datetime.scss +1 -1
  14. data/app/assets/stylesheets/katalyst/tables/typed-columns/_number.scss +3 -0
  15. data/app/components/concerns/katalyst/tables/has_table_content.rb +19 -10
  16. data/app/components/concerns/katalyst/tables/identifiable.rb +51 -0
  17. data/app/components/concerns/katalyst/tables/orderable.rb +35 -105
  18. data/app/components/concerns/katalyst/tables/row_renderer.rb +1 -1
  19. data/app/components/concerns/katalyst/tables/selectable.rb +18 -75
  20. data/app/components/concerns/katalyst/tables/sortable.rb +51 -17
  21. data/app/components/katalyst/summary_table_component.html.erb +15 -0
  22. data/app/components/katalyst/summary_table_component.rb +44 -0
  23. data/app/components/katalyst/table_component.html.erb +4 -4
  24. data/app/components/katalyst/table_component.rb +271 -53
  25. data/app/components/katalyst/tables/body_row_component.html.erb +5 -0
  26. data/app/components/katalyst/tables/body_row_component.rb +4 -31
  27. data/app/components/katalyst/tables/cell_component.rb +85 -0
  28. data/app/components/katalyst/tables/{body → cells}/boolean_component.rb +8 -2
  29. data/app/components/katalyst/tables/{body → cells}/currency_component.rb +7 -7
  30. data/app/components/katalyst/tables/{body → cells}/date_component.rb +12 -9
  31. data/app/components/katalyst/tables/{body → cells}/date_time_component.rb +13 -10
  32. data/app/components/katalyst/tables/{body → cells}/number_component.rb +5 -5
  33. data/app/components/katalyst/tables/cells/ordinal_component.rb +44 -0
  34. data/app/components/katalyst/tables/{body → cells}/rich_text_component.rb +8 -5
  35. data/app/components/katalyst/tables/cells/select_component.rb +39 -0
  36. data/app/components/katalyst/tables/data.rb +30 -0
  37. data/app/components/katalyst/tables/header_row_component.html.erb +5 -0
  38. data/app/components/katalyst/tables/header_row_component.rb +4 -25
  39. data/app/components/katalyst/tables/label.rb +37 -0
  40. data/app/components/katalyst/tables/orderable/form_component.rb +38 -0
  41. data/app/components/katalyst/tables/selectable/form_component.html.erb +3 -3
  42. data/app/components/katalyst/tables/selectable/form_component.rb +8 -11
  43. data/app/components/katalyst/tables/summary/body_component.html.erb +3 -0
  44. data/app/components/katalyst/tables/summary/body_component.rb +10 -0
  45. data/app/components/katalyst/tables/summary/header_component.html.erb +3 -0
  46. data/app/components/katalyst/tables/summary/header_component.rb +10 -0
  47. data/app/components/katalyst/tables/summary/row_component.html.erb +4 -0
  48. data/app/components/katalyst/tables/summary/row_component.rb +12 -0
  49. data/app/controllers/concerns/katalyst/tables/backend.rb +17 -28
  50. data/app/helpers/katalyst/tables/frontend.rb +67 -2
  51. data/app/javascript/tables/application.js +0 -5
  52. data/app/javascript/tables/orderable/form_controller.js +8 -6
  53. data/app/javascript/tables/orderable/item_controller.js +9 -0
  54. data/app/models/concerns/katalyst/tables/collection/core.rb +6 -1
  55. data/app/models/concerns/katalyst/tables/collection/pagination.rb +2 -2
  56. data/app/models/concerns/katalyst/tables/collection/sorting.rb +86 -18
  57. data/app/models/katalyst/tables/collection/array.rb +38 -0
  58. data/app/models/katalyst/tables/collection/base.rb +4 -0
  59. data/app/models/katalyst/tables/collection/filter.rb +2 -2
  60. data/config/importmap.rb +1 -0
  61. data/config/locales/tables.en.yml +0 -6
  62. data/lib/katalyst/tables/config.rb +23 -0
  63. data/lib/katalyst/tables.rb +9 -0
  64. metadata +32 -30
  65. data/app/components/concerns/katalyst/tables/body/typed_columns.rb +0 -132
  66. data/app/components/concerns/katalyst/tables/configurable_component.rb +0 -52
  67. data/app/components/concerns/katalyst/tables/header/typed_columns.rb +0 -179
  68. data/app/components/katalyst/tables/body/attachment_component.rb +0 -58
  69. data/app/components/katalyst/tables/body/link_component.rb +0 -40
  70. data/app/components/katalyst/tables/body_cell_component.rb +0 -55
  71. data/app/components/katalyst/tables/header/attachment_component.rb +0 -15
  72. data/app/components/katalyst/tables/header/boolean_component.rb +0 -15
  73. data/app/components/katalyst/tables/header/currency_component.rb +0 -15
  74. data/app/components/katalyst/tables/header/date_component.rb +0 -15
  75. data/app/components/katalyst/tables/header/date_time_component.rb +0 -15
  76. data/app/components/katalyst/tables/header/link_component.rb +0 -15
  77. data/app/components/katalyst/tables/header/number_component.rb +0 -15
  78. data/app/components/katalyst/tables/header/rich_text_component.rb +0 -15
  79. data/app/components/katalyst/tables/header_cell_component.rb +0 -97
  80. data/app/helpers/katalyst/tables/frontend/helper.rb +0 -31
  81. data/app/javascript/tables/turbo/collection_controller.js +0 -38
  82. data/app/models/katalyst/tables/collection/sort_form.rb +0 -120
@@ -1,179 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Katalyst
4
- module Tables
5
- module Header
6
- module TypedColumns
7
- extend ActiveSupport::Concern
8
-
9
- # Renders a boolean column header
10
- # @param method [Symbol] the method to call on the record to get the value
11
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
12
- # @option attributes [String] :label (nil) The label options to display in the header
13
- # @option attributes [Hash] :link ({}) The link options for the sorting link
14
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
15
- #
16
- # @example Render a boolean column header
17
- # <% row.boolean :active %> # => <th>Active</th>
18
- #
19
- # @example Render a boolean column header with a custom label
20
- # <% row.boolean :active, label: "Published" %> # => <th>Published</th>
21
- #
22
- # @example Render a boolean column header with small width
23
- # <% row.boolean :active, width: :s %>
24
- # => <th class="width-s">Active</th>
25
- #
26
- def boolean(method, **attributes, &)
27
- with_column(Header::BooleanComponent.new(@table, method, link: @link_attributes, **attributes), &)
28
- end
29
-
30
- # Renders a date column header
31
- # @param method [Symbol] the method to call on the record to get the value
32
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
33
- # @option attributes [String] :label (nil) The label options to display in the header
34
- # @option attributes [Hash] :link ({}) The link options for the sorting link
35
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
36
- #
37
- # @example Render a date column header
38
- # <% row.date :published_on %> # => <th>Published on</th>
39
- #
40
- # @example Render a date column header with a custom label
41
- # <% row.date :published_on, label: "Date" %> # => <th>Date</th>
42
- #
43
- # @example Render a boolean column header with small width
44
- # <% row.date :published_on, width: :s %>
45
- # => <th class="width-s">Published on</th>
46
- #
47
- def date(method, **attributes, &)
48
- with_column(Header::DateComponent.new(@table, method, link: @link_attributes, **attributes), &)
49
- end
50
-
51
- # Renders a datetime column header
52
- # @param method [Symbol] the method to call on the record to get the value
53
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
54
- # @option attributes [String] :label (nil) The label options to display in the header
55
- # @option attributes [Hash] :link ({}) The link options for the sorting link
56
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
57
- #
58
- # @example Render a datetime column header
59
- # <% row.datetime :created_at %> # => <th>Created at</th>
60
- #
61
- # @example Render a datetime column header with a custom label
62
- # <% row.datetime :created_at, label: "Published at" %> # => <th>Published at</th>
63
- #
64
- # @example Render a boolean column header with small width
65
- # <% row.datetime :created_at, width: :s %>
66
- # => <th class="width-s">Created at</th>
67
- #
68
- def datetime(method, **attributes, &)
69
- with_column(Header::DateTimeComponent.new(@table, method, link: @link_attributes, **attributes), &)
70
- end
71
-
72
- # Renders a number column header
73
- # @param method [Symbol] the method to call on the record to get the value
74
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
75
- # @option attributes [String] :label (nil) The label options to display in the header
76
- # @option attributes [Hash] :link ({}) The link options for the sorting link
77
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
78
- #
79
- # @example Render a number column header
80
- # <% row.number :comment_count %> # => <th>Comments</th>
81
- #
82
- # @example Render a number column header with a custom label
83
- # <% row.number :comment_count, label: "Comments" %> # => <th>Comments</th>
84
- #
85
- # @example Render a boolean column header with small width
86
- # <% row.number :comment_count, width: :s %>
87
- # => <th class="width-s">Comments</th>
88
- #
89
- def number(method, **attributes, &)
90
- with_column(Header::NumberComponent.new(@table, method, link: @link_attributes, **attributes), &)
91
- end
92
-
93
- # Renders a currency column header
94
- # @param method [Symbol] the method to call on the record to get the value
95
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
96
- # @option attributes [String] :label (nil) The label options to display in the header
97
- # @option attributes [Hash] :link ({}) The link options for the sorting link
98
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
99
- #
100
- # @example Render a currency column header
101
- # <% row.currency :price %> # => <th>Price</th>
102
- #
103
- # @example Render a currency column header with a custom label
104
- # <% row.currency :price, label: "Amount($)" %> # => <th>Amount($)</th>
105
- #
106
- # @example Render a boolean column header with small width
107
- # <% row.currency :price, width: :s %>
108
- # => <th class="width-s">Price</th>
109
- #
110
- def currency(method, **attributes, &)
111
- with_column(Header::CurrencyComponent.new(@table, method, link: @link_attributes, **attributes), &)
112
- end
113
-
114
- # Renders a rich text column header
115
- # @param method [Symbol] the method to call on the record to get the value
116
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
117
- # @option attributes [String] :label (nil) The label options to display in the header
118
- # @option attributes [Hash] :link ({}) The link options for the sorting link
119
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
120
- #
121
- # @example Render a rich text header
122
- # <% row.rich_text :content %> # => <th>Content</th>
123
- #
124
- # @example Render a rich text column header with a custom label
125
- # <% row.currency :content, label: "Content!" %> # => <th>Content!</th>
126
- #
127
- # @example Render a boolean column header with small width
128
- # <% row.currency :content, width: :s %>
129
- # => <th class="width-s">Content</th>
130
- #
131
- def rich_text(method, **attributes, &)
132
- with_column(Header::RichTextComponent.new(@table, method, link: @link_attributes, **attributes), &)
133
- end
134
-
135
- # Renders a link column header
136
- # @param method [Symbol] the method to call on the record to get the value
137
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
138
- # @option attributes [String] :label (nil) The label options to display in the header
139
- # @option attributes [Hash] :link ({}) The link options for the sorting link
140
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
141
- #
142
- # @example Render a link column header
143
- # <% row.link :link %> # => <th>Link</th>
144
- #
145
- # @example Render a link column header with a custom label
146
- # <% row.link :link, label: "Post" %> # => <th>Post</th>
147
- #
148
- # @example Render a boolean column header with small width
149
- # <% row.link :link, width: :s %>
150
- # => <th class="width-s">Link</th>
151
- #
152
- def link(method, **attributes, &)
153
- with_column(Header::LinkComponent.new(@table, method, link: @link_attributes, **attributes), &)
154
- end
155
-
156
- # Renders a attachment column header
157
- # @param method [Symbol] the method to call on the record to get the value
158
- # @param attributes [Hash] additional arguments are applied as html attributes to the th element
159
- # @option attributes [String] :label (nil) The label options to display in the header
160
- # @option attributes [Hash] :link ({}) The link options for the sorting link
161
- # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil
162
- #
163
- # @example Render a attachment column header
164
- # <% row.attachment :attachment %> # => <th>Attachment</th>
165
- #
166
- # @example Render a attachment column header with a custom label
167
- # <% row.attachment :attachment, label: "Document" %> # => <th>Document</th>
168
- #
169
- # @example Render a boolean column header with small width
170
- # <% row.attachment :attachment, width: :s %>
171
- # => <th class="width-s">Attachment</th>
172
- #
173
- def attachment(method, **attributes, &)
174
- with_column(Header::AttachmentComponent.new(@table, method, link: @link_attributes, **attributes), &)
175
- end
176
- end
177
- end
178
- end
179
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Body
8
- # Shows an attachment
9
- #
10
- # The value is expected to be an ActiveStorage attachment
11
- #
12
- # If it is representable, shows as a image tag using a default variant named :thumb.
13
- #
14
- # Otherwise shows as a link to download.
15
- class AttachmentComponent < BodyCellComponent
16
- def initialize(table, record, attribute, variant: :thumb, **options)
17
- super(table, record, attribute, **options)
18
-
19
- @variant = variant
20
- end
21
-
22
- def rendered_value
23
- representation
24
- end
25
-
26
- def representation
27
- if value.try(:variable?) && named_variant.present?
28
- image_tag(value.variant(@variant))
29
- elsif value.try(:attached?)
30
- filename.to_s
31
- else
32
- ""
33
- end
34
- end
35
-
36
- def filename
37
- value.blob.filename
38
- end
39
-
40
- # Utility for accessing the path Rails provides for retrieving the
41
- # attachment for use in cells. Example:
42
- # <% row.attachment :file do |cell| %>
43
- # <%= link_to "Download", cell.internal_path %>
44
- # <% end %>
45
- def internal_path
46
- rails_blob_path(value, disposition: :attachment)
47
- end
48
-
49
- private
50
-
51
- # Find the reflective variant by name (i.e. :thumb by default)
52
- def named_variant
53
- object.attachment_reflections[@attribute.to_s].named_variants[@variant.to_sym]
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Body
8
- # Displays a link to the record
9
- # The link text is the value of the attribute
10
- # @see Koi::Tables::BodyRowComponent#link
11
- class LinkComponent < BodyCellComponent
12
- def initialize(table, record, attribute, url:, link: {}, **options)
13
- super(table, record, attribute, **options)
14
-
15
- @url = url
16
- @link_options = link
17
- end
18
-
19
- def call
20
- content # ensure content is set before rendering options
21
-
22
- link = content.present? && url.present? ? link_to(content, url, @link_options) : content.to_s
23
- content_tag(@type, link, **html_attributes)
24
- end
25
-
26
- def url
27
- case @url
28
- when Symbol
29
- # helpers are not available until the component is rendered
30
- @url = helpers.public_send(@url, record)
31
- when Proc
32
- @url = @url.call(record)
33
- else
34
- @url
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Katalyst
4
- module Tables
5
- class BodyCellComponent < ViewComponent::Base # :nodoc:
6
- include Katalyst::HtmlAttributes
7
-
8
- attr_reader :record
9
-
10
- def initialize(table, record, attribute, heading: false, **html_attributes)
11
- super(**html_attributes)
12
-
13
- @table = table
14
- @record = record
15
- @attribute = attribute
16
- @type = heading ? :th : :td
17
- end
18
-
19
- def before_render
20
- # fallback if no content block is given
21
- with_content(rendered_value) unless content?
22
- end
23
-
24
- def call
25
- content # ensure content is set before rendering options
26
-
27
- content_tag(@type, content, **html_attributes)
28
- end
29
-
30
- # @return the object for this row.
31
- def object
32
- @record
33
- end
34
-
35
- def value
36
- @record.public_send(@attribute)
37
- end
38
-
39
- def rendered_value
40
- value.to_s
41
- end
42
-
43
- def to_s
44
- value.to_s
45
- end
46
-
47
- def inspect
48
- "#<#{self.class.name} attribute: #{@attribute.inspect}, value: #{value.inspect}>"
49
- end
50
-
51
- # Backwards compatibility with tables 1.0
52
- alias_method :options, :html_attributes=
53
- end
54
- end
55
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class AttachmentComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-attachment")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class BooleanComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-boolean")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class CurrencyComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-currency")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class DateComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-date")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class DateTimeComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-datetime")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class LinkComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-link")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class NumberComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-number")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- module Header
8
- class RichTextComponent < HeaderCellComponent
9
- def default_html_attributes
10
- super.merge_html(class: "type-rich-text")
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- using Katalyst::HtmlAttributes::HasHtmlAttributes
4
-
5
- module Katalyst
6
- module Tables
7
- class HeaderCellComponent < ViewComponent::Base # :nodoc:
8
- include Frontend::Helper
9
- include Katalyst::HtmlAttributes
10
- include Sortable
11
-
12
- delegate :object_name, :collection, :sorting, to: :@table
13
-
14
- def initialize(table, attribute, label: nil, link: {}, width: nil, **html_attributes)
15
- super(**html_attributes)
16
-
17
- @table = table
18
- @attribute = attribute
19
- @value = label
20
- @width = width
21
- @link_attributes = link
22
- end
23
-
24
- def call
25
- tag.th(**html_attributes) do
26
- if sortable?(@attribute)
27
- link_to(value, sort_url(@attribute), **link_attributes)
28
- else
29
- value
30
- end
31
- end
32
- end
33
-
34
- def value
35
- if !@value.nil?
36
- @value
37
- elsif object_name.present?
38
- translation
39
- else
40
- default_value
41
- end
42
- end
43
-
44
- def translation(key = "activerecord.attributes.#{object_name}.#{@attribute}")
45
- translate(key, default: default_value)
46
- end
47
-
48
- def default_value
49
- @attribute.to_s.humanize.capitalize
50
- end
51
-
52
- def inspect
53
- "#<#{self.class.name} attribute: #{@attribute.inspect}, value: #{@value.inspect}>"
54
- end
55
-
56
- # Backwards compatibility with tables 1.0
57
- alias_method :options, :html_attributes=
58
-
59
- private
60
-
61
- def width_class
62
- case @width
63
- when :xs
64
- "width-xs"
65
- when :s
66
- "width-s"
67
- when :m
68
- "width-m"
69
- when :l
70
- "width-l"
71
- else
72
- ""
73
- end
74
- end
75
-
76
- def link_attributes
77
- { data: { turbo_action: "replace" } }.merge_html(@link_attributes)
78
- end
79
-
80
- def default_html_attributes
81
- sort_data.merge(width_data)
82
- end
83
-
84
- def width_data
85
- return {} unless @width
86
-
87
- { class: width_class }
88
- end
89
-
90
- def sort_data
91
- return {} unless sorting&.supports?(collection, @attribute)
92
-
93
- { data: { sort: sorting.status(@attribute) } }
94
- end
95
- end
96
- end
97
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Katalyst
4
- module Tables
5
- module Frontend
6
- # @deprecated Use {Katalyst::TableComponent} instead.
7
- module Helper # :nodoc:
8
- extend ActiveSupport::Concern
9
-
10
- # Generates a url for applying/toggling sort for the given column.
11
- #
12
- # @param sort [String, nil] sort parameter to apply, or nil to remove sorting
13
- # @return [String] URL for toggling column sorting
14
- # @deprecated Use {Katalyst::TablesComponent} instead.
15
- def sort_url_for(sort: nil, default: nil)
16
- # Implementation inspired by pagy's `pagy_url_for` helper.
17
- # Preserve any existing GET parameters
18
- # CAUTION: these parameters are not sanitised
19
- params = if sort && !sort.eql?(default)
20
- request.GET.merge("sort" => sort).except("page")
21
- else
22
- request.GET.except("page", "sort")
23
- end
24
- query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"
25
-
26
- "#{request.path}#{query_string}"
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,38 +0,0 @@
1
- import { Controller } from "@hotwired/stimulus";
2
- import { Turbo } from "@hotwired/turbo-rails";
3
-
4
- export default class TurboCollectionController extends Controller {
5
- static values = {
6
- query: String,
7
- sort: String,
8
- };
9
-
10
- queryValueChanged(query) {
11
- Turbo.navigator.history.replace(this.#url(query));
12
- }
13
-
14
- sortValueChanged(sort) {
15
- document.querySelectorAll(this.#sortSelector).forEach((input) => {
16
- if (input) input.value = sort;
17
- });
18
- }
19
-
20
- get #sortSelector() {
21
- return "input[name='sort']";
22
- }
23
-
24
- #url(query) {
25
- const frame = this.element.closest("turbo-frame");
26
- let url;
27
-
28
- if (frame) {
29
- url = new URL(frame.baseURI);
30
- } else {
31
- url = new URL(window.location.href);
32
- }
33
-
34
- url.search = query;
35
-
36
- return url;
37
- }
38
- }