turbo-scroll 0.1.3 → 0.1.5

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: 32d21797cebb4fe6b6f5dc02130949f2d83ba899d1d30d41bbcc1e90cca76a02
4
- data.tar.gz: 334e100f9fa988dc2bac5467470d6a33634e00ccfafd488b4a61a6d8e6000983
3
+ metadata.gz: 48ff30ee0ad80ac7aed3f9ff1a8b7207a1d3f441171f5e3c90ddd5da90d62bbc
4
+ data.tar.gz: dbe358c917c234b0bc297915263e01d73c4c1cb1d1af421bce3e7f37824bf0e9
5
5
  SHA512:
6
- metadata.gz: aeb2e54b0e4777b84053e154be686cba079ae7278661128c1bc34d8bab4d1b3cd910d2d1d4ca0d5c17d2b69c2e06f2dedf21934276638f13a4088a109ad7570c
7
- data.tar.gz: eea9623e8f432b508fb7dd7e236c620cc414a0f016a41928a0c30bd82b3e7e950866c1ed75a351eb9a739e268e24c2858436ec11e0e321a9264e83e78d41214e
6
+ metadata.gz: 2f9b0c1718d30644218ed882926cebe99f07aa5ce1c43e17cdd0499f67f19a2c7e72250faac07813f528d9b7715f068f553482ab6205e3d99e8f367928c0278c
7
+ data.tar.gz: f6e3825ea5446660f7e14452112968b56af036541f501d5224c27d3399f84f5e681ee0e8f856baca44701775a7ab251253301adef9be0b3cb9741643d0749784
data/README.md CHANGED
@@ -35,13 +35,33 @@ end
35
35
 
36
36
  ### index.html.erb|slim
37
37
 
38
- In your index page, make sure you have a DOM element with ID `infinite`
38
+ The simplest is to use the `turbo_scroll_auto` helper and
39
+ nest inside your initial infinite content. This will wrap the
40
+ content with an element with id #infinite that will be
41
+ used to append new content to for infinite scrolling.
42
+
43
+
44
+ ```slim
45
+ = turbo_scroll_auto page: @articles.next_page_index
46
+ - @articles.each do |article|
47
+ = article
48
+ ```
49
+
50
+ Alternatively, in your index page, make sure you have a DOM element with ID `infinite`
39
51
  and render inside of it your initial page content.
40
52
 
41
53
  At the bottom of your page, add the infinite scrolling loader
42
- by calling the `turbo_scroll_loader` helper and passing the next page index
54
+ by calling the `turbo_scroll_auto` helper and passing the next page index
43
55
  if a next page is present.
44
56
 
57
+ ```slim
58
+ #infinite
59
+ - @articles.each do |article|
60
+ = article
61
+
62
+ = turbo_scroll_auto page: @articles.next_page_index
63
+ ```
64
+
45
65
  This gem currently assumes that the page parameter is called `page`, so in
46
66
  your controller make sure you use that parameter for selecting
47
67
  the records for a given page.
@@ -51,23 +71,11 @@ When the loader component becomes visible, it will do 2 things
51
71
  - append the next page to the #infinite dom id
52
72
  - update the loader to load the next page (when present)
53
73
 
54
- #### Slim Example
55
-
56
- ```slim
57
- #infinite
58
- / render your page fragment here in whatever structure you desire
59
- / and extract it into a partial or a component to avoid repition, if desired.
60
- - @articles.each do |article|
61
- = article
62
-
63
- = turbo_scroll_loader(page: @articles.next_page_index)
64
- ```
65
-
66
74
  If you want to use a different ID, you'll have to pass it on in turbo_stream response.
67
75
 
68
76
  ### index.turbo_stream.erb|slim
69
77
 
70
- Your turbo_stream response can use the `turbo_scroll_update` helper to
78
+ Your turbo_stream response can use the `turbo_scroll_auto_stream` helper to
71
79
  append the next page content and update the current loader with a
72
80
  loader for the next page.
73
81
 
@@ -75,27 +83,43 @@ When using the [next-pageable](https://github.com/allcrux/next-pageable) gem
75
83
  the next_page_index is already present on the collection when a next page exists.
76
84
 
77
85
  ```slim
78
- = turbo_scroll_update page: @articles.next_page_index
79
- / render your page fragment here in whatever structure you desire
80
- / and extract it into a partial or a component to avoid repition, if desired.
86
+ = turbo_scroll_auto_stream page: @articles.next_page_index
81
87
  - @articles.each do |article|
82
88
  = article
83
89
  ```
84
90
 
91
+ ### More variant (no auto loading, just simple 'more' loading)
92
+
93
+ ```slim
94
+ = turbo_scroll_more page: @articles.next_page_index
95
+ = render(Articles::Row.with_collection(@articles))
96
+ ```
97
+
98
+ Your turbo_stream response can use the `turbo_scroll_more_stream` helper to
99
+ append the next page content and update the current more loader with a
100
+ more loader for the next page.
101
+
102
+ articles\index.turbo_stream.slim
103
+
104
+ ```slim
105
+ = turbo_scroll_more_stream page: @articles.next_page_index
106
+ = render Articles::Row.with_collection(@articles)
107
+ ```
108
+
85
109
  ### An HTML table alternative for table layouts using CSS grids
86
110
 
87
111
  As HTML is pretty picky on the tags allowed inside 'table', 'tr', 'td', etc you
88
112
  can consider using CSS grid as an alternative.
89
113
 
90
114
  ```css
91
- .articles-table {
115
+ .article.row {
92
116
  display: grid;
93
117
  grid-template-columns: minmax(0, 2fr) minmax(0, 2fr) minmax(0, 8fr) minmax(0, 2fr) minmax(0, 1fr) 3em;
94
118
  max-width: 100%;
95
119
  width: 100%;
96
120
  }
97
121
 
98
- .articles-table .col {
122
+ .article.row .col {
99
123
  height: 2.75rem;
100
124
  display: flex;
101
125
  align-items: center;
@@ -104,34 +128,43 @@ can consider using CSS grid as an alternative.
104
128
  padding-right: 0.5rem;
105
129
  }
106
130
 
107
- .articles-table .col-striped:nth-child(12n+7),
108
- .articles-table .col-striped:nth-child(12n+8),
109
- .articles-table .col-striped:nth-child(12n+9),
110
- .articles-table .col-striped:nth-child(12n+10),
111
- .articles-table .col-striped:nth-child(12n+11),
112
- .articles-table .col-striped:nth-child(12n+12) {
131
+ .article.row .col-head {
132
+ font-weight: bolder;
133
+ }
134
+
135
+ .article.row .col-filter {
136
+ padding-left: 0rem;
137
+ padding-right: 0rem;
138
+ }
139
+
140
+ .article.row .align-right {
141
+ justify-content: right;
142
+ }
143
+
144
+ .article.row.striped:nth-child(2n+1) {
113
145
  background-color: #EEEEEE;
114
146
  }
115
147
  ```
116
148
 
117
149
  which would go hand in hand with this partial for a record row
118
150
 
119
- ```css
120
- .col.col-striped = article.articlenumber
121
- .col.col-striped = article.barcode
122
- .col.col-striped = article.description
123
- .col.col-striped = article.supplier
124
- .col.col-striped.align-right = article.price.print
125
- .col.col-striped.align-right
126
- a.btn.btn-sm.btn-secondary href=edit_article_path(article)
127
- i.bi.bi-pencil
151
+ ```slim
152
+ .article.row.striped
153
+ .col = article.articlenumber
154
+ .col = article.barcode
155
+ .col = article.description
156
+ .col = article.supplier
157
+ .col.align-right = article.price.print
158
+ .col.align-right
159
+ a.btn.btn-sm.btn-secondary href=edit_article_path(article)
160
+ i.bi.bi-pencil
128
161
  ```
129
162
 
130
163
 
131
164
  ### Using a different DOM ID
132
165
 
133
166
  In case you want or need to use a different DOM ID you
134
- can pass it on as an extra param to the `turbo_scroll_update` helper.
167
+ can pass it on as an extra param to the `turbo_scroll_auto_stream` helper.
135
168
 
136
169
  The below example illustrates this for the case where your
137
170
  DOM ID is `#scroll`.
@@ -139,17 +172,15 @@ DOM ID is `#scroll`.
139
172
  index.html.slim
140
173
 
141
174
  ```slim
142
- #scroll
175
+ = turbo_scroll_auto page: @articles.next_page_index, id: :scroll
143
176
  - @articles.each do |article|
144
177
  = article
145
-
146
- = turbo_scroll_loader(page: @articles.next_page_index)
147
178
  ```
148
179
 
149
180
  index.turbo_stream.slim
150
181
 
151
182
  ```slim
152
- = turbo_scroll_update page: @articles.next_page_index, infinite_dom_id: :scroll
183
+ = turbo_scroll_auto_stream page: @articles.next_page_index, infinite_dom_id: :scroll
153
184
  - @articles.each do |article|
154
185
  = article
155
186
  ```
data/lib/locales/en.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  en:
3
3
  click_here: Click <a href="%{next_page_path}">here</a> to force load the next page.
4
+ more: More
data/lib/locales/fr.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  fr:
3
3
  click_here: Cliquez <a href="%{next_page_path}">ici</a> pour charger la page suivante.
4
+ more: Plus
data/lib/locales/nl.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  nl:
3
3
  click_here: Klik <a href="%{next_page_path}">hier</a> voor de volgende pagina.
4
+ more: Meer
data/lib/locales/ro.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  ro:
3
3
  click_here: Faceți clic <a href="%{next_page_path}">aici</a> pentru a încărca pagina următoare.
4
+ more: More
data/lib/locales/vn.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  vn:
3
3
  click_here: Nhấp vào <a href="%{next_page_path}">đây</a> để tải trang tiếp theo.
4
+ more: Xem thêm
@@ -0,0 +1,5 @@
1
+ = turbo_frame_tag loader_dom_id, src: next_page_stream_path, loading: "lazy", target: "_top"
2
+ .page-loading
3
+ p == I18n.t(".click_here", next_page_path: next_page_path)
4
+ - if loading_indicator
5
+ = render(TurboScroll::SpinLoader.new())
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "turbo-rails"
4
+
5
+ class TurboScroll::Auto < ViewComponent::Base
6
+ include Turbo::FramesHelper
7
+
8
+ attr_reader :page, :loader_dom_id, :loading_indicator
9
+
10
+ def initialize(page:, loader_dom_id: :loader, loading_indicator: true)
11
+ @page = page
12
+ @loader_dom_id = loader_dom_id
13
+ @loading_indicator = loading_indicator
14
+ end
15
+
16
+ def query_params
17
+ @query_params ||=
18
+ request.query_parameters.except(:page, :raw, :format)
19
+ end
20
+
21
+ def next_page_path
22
+ url_for(page: page, **query_params)
23
+ end
24
+
25
+ def next_page_stream_path
26
+ url_for(format: :turbo_stream, page: page, **query_params)
27
+ end
28
+
29
+ def render?
30
+ page
31
+ end
32
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ = turbo_stream.append(infinite_dom_id)
2
+ = content
3
+
4
+ = turbo_stream.replace(loader_dom_id)
5
+ = render(TurboScroll::Auto.new(page: page, \
6
+ loader_dom_id: loader_dom_id, \
7
+ loading_indicator: loading_indicator))
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "turbo-rails"
4
+
5
+ class TurboScroll::AutoStream < ViewComponent::Base
6
+ include Turbo::FramesHelper
7
+ include Turbo::StreamsHelper
8
+
9
+ attr_reader :page, :loader_dom_id, :infinite_dom_id, :loading_indicator
10
+
11
+ def initialize(page:, loader_dom_id: :loader, loading_indicator:, infinite_dom_id: :infinite)
12
+ @page = page
13
+ @infinite_dom_id = infinite_dom_id
14
+ @loading_indicator = loading_indicator
15
+ @loader_dom_id = loader_dom_id
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ turbo-frame id=loader_dom_id
2
+ a.more href=next_page_path onclick="more('#{next_page_stream_path}')" =I18n.t(".more")
3
+
4
+ javascript:
5
+ function more(url) {
6
+ event.preventDefault();
7
+ fetch(url)
8
+ .then(response => response.text())
9
+ .then(html => Turbo.renderStreamMessage(html))
10
+ .catch(error => console.warn('Something went wrong.', error));
11
+ }
12
+
@@ -2,14 +2,14 @@
2
2
 
3
3
  require "turbo-rails"
4
4
 
5
- class TurboScroll::Loader < ViewComponent::Base
5
+ class TurboScroll::More < ViewComponent::Base
6
6
  include Turbo::FramesHelper
7
7
 
8
- attr_reader :page, :loading_indicator
8
+ attr_reader :page, :loader_dom_id
9
9
 
10
- def initialize(page:, loading_indicator: true)
10
+ def initialize(page:, loader_dom_id: :loader)
11
11
  @page = page
12
- @loading_indicator = loading_indicator
12
+ @loader_dom_id = loader_dom_id
13
13
  end
14
14
 
15
15
  def query_params
@@ -0,0 +1,5 @@
1
+ = turbo_stream.append(infinite_dom_id)
2
+ = content
3
+
4
+ = turbo_stream.replace(loader_dom_id)
5
+ = render(TurboScroll::More.new(page: page, loader_dom_id: loader_dom_id))
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "turbo-rails"
4
+
5
+ class TurboScroll::MoreStream < ViewComponent::Base
6
+ include Turbo::FramesHelper
7
+ include Turbo::StreamsHelper
8
+
9
+ attr_reader :page, :loader_dom_id, :infinite_dom_id
10
+
11
+ def initialize(page:, loader_dom_id: :loader, infinite_dom_id: :infinite)
12
+ @page = page
13
+ @loader_dom_id = loader_dom_id
14
+ @infinite_dom_id = infinite_dom_id
15
+ end
16
+ end
@@ -1,9 +1,13 @@
1
1
  module TurboScroll
2
2
  class Railtie < ::Rails::Railtie
3
3
  initializer "turbo-scrolls.load_components" do
4
- require_relative "update"
5
- require_relative "loader"
6
- require_relative "spin_loader"
4
+ ActiveSupport.on_load(:action_view) do
5
+ require_relative "auto"
6
+ require_relative "auto_stream"
7
+ require_relative "more"
8
+ require_relative "more_stream"
9
+ require_relative "spin_loader"
10
+ end
7
11
  end
8
12
  initializer "turbo-scrolls.view_helpers" do |app|
9
13
  ActiveSupport.on_load(:action_view) do
@@ -3,13 +3,7 @@
3
3
  require "turbo-rails"
4
4
 
5
5
  class TurboScroll::SpinLoader < ViewComponent::Base
6
- attr_reader :loading_indicator
7
-
8
- def initialize(loading_indicator: true)
9
- @loading_indicator = loading_indicator
10
- end
11
-
12
- def render?
13
- loading_indicator
6
+ #
7
+ def initialize()
14
8
  end
15
9
  end
@@ -1,3 +1,3 @@
1
1
  module TurboScroll
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/turbo-scroll.rb CHANGED
@@ -3,19 +3,50 @@ require "turbo-scroll/railtie" if defined?(Rails::Railtie)
3
3
 
4
4
  module TurboScroll
5
5
  module ViewHelpers
6
- def turbo_scroll_loader(page:, loading_indicator: true)
7
- render(TurboScroll::Loader.new(page: page, loading_indicator: loading_indicator))
6
+
7
+ #
8
+ def turbo_scroll_auto(page:, loader_dom_id: :loader, loading_indicator: true, id: :infinite, &block)
9
+ if block_given?
10
+ safe_concat(%{<div id="#{id}">})
11
+ concat(capture(&block))
12
+ safe_concat("</div>")
13
+ end
14
+ render(TurboScroll::Auto.new(page: page, loader_dom_id: loader_dom_id, loading_indicator: loading_indicator))
15
+ end
16
+
17
+ #
18
+ def turbo_scroll_more(page:, loader_dom_id: :loader, id: :infinite, &block)
19
+ if block_given?
20
+ safe_concat(%{<div id="#{id}">})
21
+ concat(capture(&block))
22
+ safe_concat("</div>")
23
+ end
24
+ render(TurboScroll::More.new(page: page, loader_dom_id: loader_dom_id))
8
25
  end
9
26
 
10
- def turbo_scroll_update(page:, infinite_dom_id: :infinite, loading_indicator: true, &block)
27
+ #
28
+ def turbo_scroll_auto_stream(page:, loader_dom_id: :loader, infinite_dom_id: :infinite, loading_indicator: true, &block)
11
29
  render(
12
- TurboScroll::Update.new(
30
+ TurboScroll::AutoStream.new(
13
31
  page: page,
32
+ loader_dom_id: loader_dom_id,
14
33
  infinite_dom_id: infinite_dom_id,
15
34
  loading_indicator: loading_indicator
16
35
  ),
17
36
  &block
18
37
  )
19
38
  end
39
+
40
+ #
41
+ def turbo_scroll_more_stream(page:, loader_dom_id: :loader, infinite_dom_id: :infinite, &block)
42
+ render(
43
+ TurboScroll::MoreStream.new(
44
+ page: page,
45
+ loader_dom_id: loader_dom_id,
46
+ infinite_dom_id: infinite_dom_id
47
+ ),
48
+ &block
49
+ )
50
+ end
20
51
  end
21
52
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo-scroll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koen handekyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-19 00:00:00.000000000 Z
11
+ date: 2022-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: turbo-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: view_component
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: slim
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '4'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '4'
69
69
  description: TurboScroll is a minimalistic gem that provides infinite scrolling for
@@ -82,16 +82,19 @@ files:
82
82
  - lib/locales/nl.yml
83
83
  - lib/locales/ro.yml
84
84
  - lib/locales/vn.yml
85
- - lib/tasks/turbo_scroll_tasks.rake
86
85
  - lib/turbo-scroll.rb
87
- - lib/turbo-scroll/loader.html.slim
88
- - lib/turbo-scroll/loader.rb
89
- - lib/turbo-scroll/loader.scss
86
+ - lib/turbo-scroll/auto.html.slim
87
+ - lib/turbo-scroll/auto.rb
88
+ - lib/turbo-scroll/auto.scss
89
+ - lib/turbo-scroll/auto_stream.html.slim
90
+ - lib/turbo-scroll/auto_stream.rb
91
+ - lib/turbo-scroll/more.html.slim
92
+ - lib/turbo-scroll/more.rb
93
+ - lib/turbo-scroll/more_stream.html.slim
94
+ - lib/turbo-scroll/more_stream.rb
90
95
  - lib/turbo-scroll/railtie.rb
91
96
  - lib/turbo-scroll/spin_loader.rb
92
97
  - lib/turbo-scroll/spin_loader.slim
93
- - lib/turbo-scroll/update.html.slim
94
- - lib/turbo-scroll/update.rb
95
98
  - lib/turbo-scroll/version.rb
96
99
  homepage: https://github.com/allcrux/turbo-scroll
97
100
  licenses:
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :turbo-scroll do
3
- # # Task goes here
4
- # end
@@ -1,4 +0,0 @@
1
- = turbo_frame_tag :infinite_loader, src: next_page_stream_path, loading: "lazy", target: "_top"
2
- .page-loading
3
- p == I18n.t(".click_here", next_page_path: next_page_path)
4
- = render(TurboScroll::SpinLoader.new(loading_indicator: loading_indicator))
@@ -1,5 +0,0 @@
1
- = turbo_stream.append(infinite_dom_id)
2
- = content
3
-
4
- = turbo_stream.replace(:infinite_loader)
5
- = render(TurboScroll::Loader.new(page: page, loading_indicator: loading_indicator))
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "turbo-rails"
4
-
5
- class TurboScroll::Update < ViewComponent::Base
6
- include Turbo::FramesHelper
7
- include Turbo::StreamsHelper
8
-
9
- attr_reader :page, :infinite_dom_id, :loading_indicator
10
-
11
- def initialize(page:, loading_indicator:, infinite_dom_id: :infinite)
12
- @page = page
13
- @infinite_dom_id = infinite_dom_id
14
- @loading_indicator = loading_indicator
15
- end
16
- end