lazy_loading_page 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/lazy_loading_page/engine.rb +3 -2
- data/lib/lazy_loading_page/version.rb +1 -1
- metadata +18 -7
- data/app/assets/javascripts/lazy_load.js +0 -196
- data/app/assets/stylesheets/lazy_load.css +0 -49
- data/app/helpers/lazy_loading_page/application_helper.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f42ce0f16db8cab516d15e1cb5890dd790ba0ec
|
4
|
+
data.tar.gz: a6843952f4ac0215fd8f74ffca3ba77bd708ebce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 905d70581a1bc15b9a10287319dd8a937eac3bd3c53e675c7814930fc43d61bae3ef828022a6216628ff213b9fa59acc544f97cf9ec6e4f2551792f526a9cf5f
|
7
|
+
data.tar.gz: 7950c3bee4503cb2a729a4037cd6d9e2c66e1211453d3508b340a082981473f6f6473d4b6a459b9900b2fb960cd32e11c6f4d073f9bbefc527c704f9d2edee86
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# lazy_loading_page
|
2
|
-
***Load page lazily as
|
2
|
+
***Load page lazily as and when required***
|
3
3
|
***
|
4
4
|
|
5
5
|
|
6
|
-
lazy_loading_page is a gem that helps to **load page lazily** that is as
|
6
|
+
lazy_loading_page is a gem that helps to **load page lazily** that is as and **when required**. The gem ***reduces page load time*** and ***reduces server ram*** by loading important content of the page in the first call and then by triggering calls to load the remaining page. The gem give **Reactjs** like **functionality** in your ruby-on-rails application.
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -118,3 +118,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
118
118
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
119
119
|
4. Push to the branch (`git push origin my-new-feature`)
|
120
120
|
5. Create a new Pull Request
|
121
|
+
|
122
|
+
## License
|
123
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
124
|
+
|
@@ -1,10 +1,11 @@
|
|
1
|
+
require "lazy_load/rails"
|
1
2
|
module LazyLoadingPage
|
2
3
|
class Engine < ::Rails::Engine
|
3
4
|
isolate_namespace LazyLoadingPage
|
4
5
|
|
5
|
-
initializer '
|
6
|
+
initializer 'lazy_load-rails.action_controller' do |app|
|
6
7
|
ActiveSupport.on_load :action_controller do
|
7
|
-
helper
|
8
|
+
helper LazyLoad::Rails::ApplicationHelper
|
8
9
|
end
|
9
10
|
end
|
10
11
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_loading_page
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jignesh Satam
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: lazy_load-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: sqlite3
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,7 +52,7 @@ dependencies:
|
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
|
-
description: LazyLoadingPage loads
|
55
|
+
description: LazyLoadingPage loads heavy pages lazily that is as and when required.
|
42
56
|
Single heavy request is broken in small requests, so no timeouts and no congestion
|
43
57
|
of requests. The gem reduces page load time and reduces server ram by loading important
|
44
58
|
content of the page in the first call and then by triggering calls to load the remaining
|
@@ -53,9 +67,6 @@ files:
|
|
53
67
|
- README.md
|
54
68
|
- README.rdoc
|
55
69
|
- Rakefile
|
56
|
-
- app/assets/javascripts/lazy_load.js
|
57
|
-
- app/assets/stylesheets/lazy_load.css
|
58
|
-
- app/helpers/lazy_loading_page/application_helper.rb
|
59
70
|
- config/routes.rb
|
60
71
|
- lib/lazy_loading_page.rb
|
61
72
|
- lib/lazy_loading_page/engine.rb
|
@@ -121,7 +132,7 @@ rubyforge_project:
|
|
121
132
|
rubygems_version: 2.6.8
|
122
133
|
signing_key:
|
123
134
|
specification_version: 4
|
124
|
-
summary: LazyLoadingPage loads heay pages lazily that is as
|
135
|
+
summary: LazyLoadingPage loads heay pages lazily that is as and when required. Single
|
125
136
|
heavy request is broken in small requests.
|
126
137
|
test_files:
|
127
138
|
- test/dummy/app/assets/javascripts/application.js
|
@@ -1,196 +0,0 @@
|
|
1
|
-
("turbolinks:load DOMContentLoaded ready".split(" ")).forEach(function(e){
|
2
|
-
if (window.lazyLoaderInitialize === undefined) {
|
3
|
-
document.addEventListener(e, function() {
|
4
|
-
lazyLoad();
|
5
|
-
});
|
6
|
-
window.lazyLoaderInitialize = true;
|
7
|
-
}
|
8
|
-
});
|
9
|
-
|
10
|
-
function lazyLoad(){
|
11
|
-
var $lazy_loaders = document.querySelectorAll(".lazy_load:not([data-later='true']):not([data-loading='started'])");
|
12
|
-
lazyLoadElements($lazy_loaders);
|
13
|
-
}
|
14
|
-
|
15
|
-
function lazyLoadElements(elements){
|
16
|
-
for (var i = 0; i < elements.length; i++) {
|
17
|
-
(function(i){
|
18
|
-
$lazy_loader = elements[i];
|
19
|
-
lazyLoadElement($lazy_loader);
|
20
|
-
})(i);
|
21
|
-
};
|
22
|
-
}
|
23
|
-
|
24
|
-
function delayedLoading(ele){
|
25
|
-
if ((ele instanceof NodeList) || ((window.jQuery !== undefined || window.$ !== undefined) && (ele instanceof jQuery))) {
|
26
|
-
lazyLoadElements(ele);
|
27
|
-
}
|
28
|
-
else if(ele instanceof HTMLElement){
|
29
|
-
lazyLoadElement(ele);
|
30
|
-
}
|
31
|
-
}
|
32
|
-
|
33
|
-
function lazyLoadElement(ele){
|
34
|
-
if (!ele.classList.contains("lazy_load"))
|
35
|
-
return false;
|
36
|
-
var id = ele.getAttribute("data-id");
|
37
|
-
var url = ele.getAttribute("data-url");
|
38
|
-
if (ele.getAttribute("data-loader") != "false")
|
39
|
-
addLoader(ele);
|
40
|
-
var xhttp = new XMLHttpRequest();
|
41
|
-
xhttp.onreadystatechange = function(){ajaxCallback(xhttp, id);};
|
42
|
-
xhttp.open("GET", url, true);
|
43
|
-
if (ele.getAttribute("data-type") == "script")
|
44
|
-
xhttp.setRequestHeader('Accept', 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*');
|
45
|
-
if (ele.getAttribute("data-type") == "json")
|
46
|
-
xhttp.setRequestHeader('Accept', "application/json, text/javascript, */*");
|
47
|
-
xhttp.setRequestHeader("X-CSRF-Token", document.querySelector("[name='csrf-token']").content);
|
48
|
-
xhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
49
|
-
xhttp.send();
|
50
|
-
ele.setAttribute("data-loading", "started");
|
51
|
-
}
|
52
|
-
|
53
|
-
function addLoader(ele){
|
54
|
-
if (document.querySelector("[data-id='"+ ele.getAttribute("data-id") +"'][class='loading-container']"))
|
55
|
-
return false;
|
56
|
-
var loader_img = document.createElement("div");
|
57
|
-
loader_img.setAttribute("class", "loading-container");
|
58
|
-
loader_img.setAttribute("data-id", ele.getAttribute("data-id"));
|
59
|
-
if (["script", "json"].indexOf(ele.getAttribute("data-type")) >= 0)
|
60
|
-
loader_img.setAttribute("data-type", ele.getAttribute("data-type"));
|
61
|
-
if (ele.getAttribute("data-success") != null)
|
62
|
-
loader_img.setAttribute("data-success", ele.getAttribute("data-success"));
|
63
|
-
if (ele.getAttribute("data-failure") != null)
|
64
|
-
loader_img.setAttribute("data-failure", ele.getAttribute("data-failure"));
|
65
|
-
if (ele.getAttribute("data-complete") != null)
|
66
|
-
loader_img.setAttribute("data-complete", ele.getAttribute("data-complete"));
|
67
|
-
var span = document.createElement("span");
|
68
|
-
span.setAttribute("class", "loading-indicator");
|
69
|
-
for(var j=0; j<3; j++){
|
70
|
-
var i = document.createElement("i");
|
71
|
-
span.appendChild(i);
|
72
|
-
}
|
73
|
-
loader_img.appendChild(span);
|
74
|
-
ele.parentElement.replaceChild(loader_img, ele);
|
75
|
-
loader_img.appendChild(ele);
|
76
|
-
}
|
77
|
-
|
78
|
-
function ajaxCallback(xhttp, id) {
|
79
|
-
var elementToReplace = document.querySelectorAll("[data-id='"+id+"']")[0];
|
80
|
-
switch(xhttp.readyState) {
|
81
|
-
case XMLHttpRequest.UNSENT:
|
82
|
-
// code block
|
83
|
-
break;
|
84
|
-
case XMLHttpRequest.OPENED:
|
85
|
-
// code block
|
86
|
-
break;
|
87
|
-
case XMLHttpRequest.HEADERS_RECEIVED:
|
88
|
-
// code block
|
89
|
-
break;
|
90
|
-
case XMLHttpRequest.LOADING:
|
91
|
-
// code block
|
92
|
-
break;
|
93
|
-
case XMLHttpRequest.DONE:
|
94
|
-
requestComplete(xhttp, elementToReplace);
|
95
|
-
callbackFor(elementToReplace, "complete", xhttp);
|
96
|
-
break;
|
97
|
-
default:
|
98
|
-
requestComplete(xhttp, elementToReplace);
|
99
|
-
callbackFor(elementToReplace, "complete", xhttp);
|
100
|
-
break;
|
101
|
-
}
|
102
|
-
}
|
103
|
-
|
104
|
-
function requestComplete(xhttp, elementToReplace){
|
105
|
-
if (xhttp.status == 200) {
|
106
|
-
requestSuccess(xhttp, elementToReplace);
|
107
|
-
callbackFor(elementToReplace, "success", xhttp);
|
108
|
-
}
|
109
|
-
else{
|
110
|
-
requestFailure(xhttp, elementToReplace)
|
111
|
-
callbackFor(elementToReplace, "failure", xhttp);
|
112
|
-
}
|
113
|
-
}
|
114
|
-
|
115
|
-
function requestSuccess(xhttp, elementToReplace){
|
116
|
-
var parentElement = elementToReplace.parentNode;
|
117
|
-
if (elementToReplace.getAttribute("data-type") == "script"){
|
118
|
-
// if(true){
|
119
|
-
javascriptResponseActions(xhttp, parentElement, elementToReplace);
|
120
|
-
}
|
121
|
-
else if(elementToReplace.getAttribute("data-type") == "json"){
|
122
|
-
// JSON.parse(xhttp.response);
|
123
|
-
parentElement.removeChild(elementToReplace);
|
124
|
-
}
|
125
|
-
else{
|
126
|
-
plainResponseActions(xhttp, parentElement, elementToReplace);
|
127
|
-
}
|
128
|
-
lazyLoad();
|
129
|
-
}
|
130
|
-
|
131
|
-
function requestFailure(xhttp, elementToReplace){
|
132
|
-
if (elementToReplace.classList.contains("loading-container"))
|
133
|
-
elementToReplace = elementToReplace.querySelector('.lazy_load')
|
134
|
-
loadElement(elementToReplace);
|
135
|
-
}
|
136
|
-
|
137
|
-
function javascriptResponseActions(xhttp, parentElement, elementToReplace){
|
138
|
-
var newScript = document.createElement("script");
|
139
|
-
newScript.innerHTML = xhttp.responseText;
|
140
|
-
parentElement.insertBefore(newScript, elementToReplace);
|
141
|
-
parentElement.removeChild(elementToReplace);
|
142
|
-
}
|
143
|
-
|
144
|
-
function plainResponseActions(xhttp, parentElement, elementToReplace){
|
145
|
-
var parser = new DOMParser();
|
146
|
-
var doc = parser.parseFromString(xhttp.responseText, "text/html");
|
147
|
-
var newElements = doc.querySelector("body");
|
148
|
-
if (window.$ !== undefined){
|
149
|
-
// if (false){
|
150
|
-
new_ele = $(xhttp.responseText);
|
151
|
-
if (newElements !== null){
|
152
|
-
new_ele = $(newElements.innerHTML);
|
153
|
-
}
|
154
|
-
$(elementToReplace).replaceWith(new_ele);
|
155
|
-
}
|
156
|
-
else{
|
157
|
-
var template = document.createElement("template");
|
158
|
-
if (newElements !== null){
|
159
|
-
template.innerHTML = newElements.innerHTML;
|
160
|
-
}
|
161
|
-
else{
|
162
|
-
template.innerHTML = xhttp.responseText;
|
163
|
-
}
|
164
|
-
var childNodes = template.content.childNodes
|
165
|
-
var newElement = elementToReplace
|
166
|
-
var scriptTags = [];
|
167
|
-
for(var i= childNodes.length-1; i>=0 ; i--){
|
168
|
-
newPreviousElement = childNodes[i];
|
169
|
-
parentElement.insertBefore(newPreviousElement, newElement);
|
170
|
-
if(newPreviousElement.nodeName == "SCRIPT"){
|
171
|
-
scriptTags.push(newPreviousElement.textContent);
|
172
|
-
eval();
|
173
|
-
}
|
174
|
-
newElement = newPreviousElement;
|
175
|
-
}
|
176
|
-
runScripts(scriptTags);
|
177
|
-
parentElement.removeChild(elementToReplace);
|
178
|
-
}
|
179
|
-
}
|
180
|
-
|
181
|
-
function runScripts(scriptTags){
|
182
|
-
scriptTags.forEach( function(scriptTag){
|
183
|
-
eval(scriptTag)
|
184
|
-
});
|
185
|
-
}
|
186
|
-
|
187
|
-
function callbackFor(ele, option, xhttp){
|
188
|
-
var userFunction = ele.getAttribute("data-"+ option);
|
189
|
-
if (userFunction !== null){
|
190
|
-
userFunctionArray = userFunction.split("(");
|
191
|
-
var extractAttributes = userFunctionArray[1].split(")")[0];
|
192
|
-
var attributes = extractAttributes.split(",").map(function(arg){return arg.trim()})
|
193
|
-
var functionName = userFunctionArray[0];
|
194
|
-
eval(functionName).apply(xhttp, attributes);
|
195
|
-
}
|
196
|
-
}
|
@@ -1,49 +0,0 @@
|
|
1
|
-
@-webkit-keyframes bouncedelay {
|
2
|
-
0%, 100%, 80% {
|
3
|
-
-webkit-transform: scale(0);
|
4
|
-
transform: scale(0)
|
5
|
-
}
|
6
|
-
40% {
|
7
|
-
-webkit-transform: scale(1);
|
8
|
-
transform: scale(1)
|
9
|
-
}
|
10
|
-
}
|
11
|
-
@keyframes bouncedelay {
|
12
|
-
0%, 100%, 80% {
|
13
|
-
-webkit-transform: scale(0);
|
14
|
-
transform: scale(0)
|
15
|
-
}
|
16
|
-
40% {
|
17
|
-
-webkit-transform: scale(1);
|
18
|
-
transform: scale(1)
|
19
|
-
}
|
20
|
-
}
|
21
|
-
.loading-indicator{
|
22
|
-
display: inline-block;
|
23
|
-
vertical-align: middle;
|
24
|
-
}
|
25
|
-
.loading-indicator i{
|
26
|
-
position: relative;
|
27
|
-
display: inline-block;
|
28
|
-
width: 8px;
|
29
|
-
height: 8px;
|
30
|
-
background-color: #A6ADAD;
|
31
|
-
border-radius: 50%;
|
32
|
-
line-height: 0;
|
33
|
-
-webkit-transform-origin: center center;
|
34
|
-
transform-origin: center center;
|
35
|
-
-webkit-animation: bouncedelay 1.3s infinite linear;
|
36
|
-
animation: bouncedelay 1.3s infinite linear
|
37
|
-
}
|
38
|
-
.loading-indicator i:nth-child(2){
|
39
|
-
-webkit-animation-delay: .3s;
|
40
|
-
animation-delay: .3s
|
41
|
-
}
|
42
|
-
.loading-indicator i:nth-child(3){
|
43
|
-
-webkit-animation-delay: .6s;
|
44
|
-
animation-delay: .6s
|
45
|
-
}
|
46
|
-
.loading-container {
|
47
|
-
text-align: center;
|
48
|
-
padding: 1em 1em
|
49
|
-
}
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module LazyLoadingPage
|
2
|
-
module ApplicationHelper
|
3
|
-
def lazy_load(url, options = {})
|
4
|
-
options = options.symbolize_keys
|
5
|
-
id = options[:id].presence
|
6
|
-
classes = ("lazy_load " + options[:class] rescue "lazy_load")
|
7
|
-
params = options[:params]
|
8
|
-
data_merge_hash = {}
|
9
|
-
options[:later].present? && (options[:later].to_s.downcase == "true") && (data_merge_hash[:later] = true)
|
10
|
-
options[:loader].present? && (options[:loader].to_s.downcase == "off") && (data_merge_hash[:loader] = false)
|
11
|
-
options[:type].present? && (["script", "json"].include?(options[:type])) && (data_merge_hash[:type] = options[:type])
|
12
|
-
options[:success].present? && data_merge_hash[:success] = options[:success]
|
13
|
-
options[:failure].present? && data_merge_hash[:failure] = options[:failure]
|
14
|
-
options[:complete].present? && data_merge_hash[:complete] = options[:complete]
|
15
|
-
content_tag("input", nil, type:"hidden", id: id, class: classes, data: {id: SecureRandom.uuid, url: url}.merge!(data_merge_hash))
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|