flex_infinite_scroll 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c06e8d3f9cc52fcfb5f27ecf04f9aa527fe9d8a68605ab27c8655b9e59a110f6
4
+ data.tar.gz: e38bd3e917d5cf558d7c501d450c1331e7bb9ba7e64ab6b34fc5703907a9195e
5
+ SHA512:
6
+ metadata.gz: 3c289900ff552c532db39185bb4405b2500de066160ee5c4752e2fab2855660be01233d5d16e9e4d4ccbd01bfa5c5e87dd4b6c03bae5b2b0d431e61ae3ca59db
7
+ data.tar.gz: 7b9c06824b812f18b52454a520051b0a9d5b1e3b4c5be807b5311a972a7ee44dcd50b6c40026fb547e0f3db5b9e678efe50d3da927defe02db9d2b9591d87199
@@ -0,0 +1,17 @@
1
+ # Infinite scroll module
2
+ module FlexInfiniteScroll
3
+ class Engine < ::Rails::Engine; end
4
+ extend ActiveSupport::Concern
5
+ def infinite_scroll(page = 1, page_size = (ENV['FIS_PAGE_SIZE'] || 20))
6
+ page = page.to_i if page.class == String
7
+ offset_skip = (page - 1) * page_size
8
+ total_page = (count / page_size.to_f).ceil
9
+ {
10
+ data: offset(offset_skip).limit(page_size),
11
+ total_page: total_page,
12
+ prev_page: (page == 1 ? nil : page - 1),
13
+ current_page: page,
14
+ next_page: (page == total_page ? nil : page + 1)
15
+ }
16
+ end
17
+ end
@@ -0,0 +1,86 @@
1
+ // Flex Infinite Scroll initialization
2
+
3
+ $.fn.extend({
4
+ flexInfiniteScroll: function(config = {}) {
5
+ // Config preparing
6
+ var startPage = config.startPage || 1;
7
+ var nextPage = startPage + 1;
8
+ var dataUrl = config.url;
9
+ var scrollContainer = this[0];
10
+ var queryParams = config.queryParams;
11
+ var dataProcess = config.dataProcess;
12
+ var initialLoad = config.initialLoad;
13
+ var loadMargin = config.loadMargin || 50;
14
+ var beforeAction = config.beforeAction;
15
+ var afterAction = config.afterAction;
16
+ var targetContainer = config.targetContainer || scrollContainer ;
17
+ var eventTarget;
18
+ if (config.lastPage) nextPage = 'last';
19
+ scrollContainer.dataset.fisNextPage = nextPage;
20
+ scrollContainer.dataset.fisUrl = dataUrl;
21
+ scrollContainer.dataset.fisLoading = 0;
22
+
23
+ function getData(page = 1){
24
+ // Check for loading process and last page
25
+ if (scrollContainer.dataset.fisLoading === '1' || page == 'last') return false
26
+ scrollContainer.dataset.fisLoading = 1;
27
+ // before load action
28
+ if (beforeAction) beforeAction;
29
+ $.ajax({
30
+ url: scrollContainer.dataset.fisUrl,
31
+ // custom query params
32
+ data: (queryParams || {page: page}),
33
+ dataType: 'json',
34
+ type: 'GET',
35
+ success: function(json) {
36
+
37
+ if (dataProcess){
38
+ // custom user processor
39
+ dataProcess(json,$(targetContainer));
40
+ } else {
41
+ // default data processor
42
+ $(json.data).appendTo($(targetContainer));
43
+ }
44
+ scrollContainer.dataset.fisNextPage = json.next_page || 'last';
45
+ scrollContainer.dataset.fisLoading = 0;
46
+ if (scrollNotApear()) {
47
+ // check if on initial load not enough elements on screen
48
+ getData(scrollContainer.dataset.fisNextPage)
49
+ }
50
+ // after load action
51
+ if (afterAction) afterAction;
52
+ },
53
+ error: function() {
54
+ scrollContainer.dataset.fisLoading = 0;
55
+ }
56
+ });
57
+ }
58
+
59
+ // intial load
60
+ if (initialLoad) {
61
+ getData(startPage)
62
+ } else if (scrollNotApear()) {
63
+ // check if on initial load not enough elements on screen
64
+ getData(scrollContainer.dataset.fisNextPage)
65
+ }
66
+
67
+ function scrollNotApear(){
68
+ var containerSize=$(scrollContainer).innerHeight();
69
+ var scrollSize=scrollContainer.scrollHeight;
70
+ return (scrollSize - containerSize - (loadMargin * 1.1) < 0)
71
+ }
72
+
73
+ // check if body scroll
74
+ eventTarget = (scrollContainer.localName == 'body' ? window : scrollContainer);
75
+
76
+ $(eventTarget).on('scroll', () => {
77
+ var scrollTop=$(eventTarget).scrollTop();
78
+ var containerSize=$(scrollContainer).innerHeight();
79
+ var scrollSize=scrollContainer.scrollHeight;
80
+ if (scrollTop + containerSize > scrollSize - loadMargin && scrollContainer.dataset.fisNextPage != 'last'){
81
+ getData(scrollContainer.dataset.fisNextPage);
82
+ };
83
+ })
84
+
85
+ }
86
+ })
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flex_infinite_scroll
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Ignatov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: belmek@me.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/flex_infinite_scroll.rb
20
+ - vendor/assets/javascript/flex_infinite_scroll.js
21
+ homepage: https://github.com/aignatov-bio/flex_infinite_scroll
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.7.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Add infinite scrolling to models and views
45
+ test_files: []