Skip to content

Commit

Permalink
Added lazy render nikolalsvk#34
Browse files Browse the repository at this point in the history
  • Loading branch information
naveed-ahmad committed Jul 30, 2019
1 parent 41c6821 commit beb3734
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
21 changes: 21 additions & 0 deletions app/views/render_async/_lazy_load.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var lazyLoaded = function(element) {
element.setAttribute("data-loaded", true)
}

var isLazyLoaded = function (element) {
return "true" === element.getAttribute("data-loaded")
};
var _lazyListener = function () {
var observer = new IntersectionObserver(function (entries, observer) {
entries.forEach(function (entry) {
if(0 < entry.intersectionRatio || entry.isIntersecting){
if(!isLazyLoaded(entry.target)){
_listener();
observer.unobserve(entry.target);
}
}
})
}, {rootMargin: '0px', threshold: 0});

observer.observe(document.getElementById("<%= container_id %>"))
}
2 changes: 2 additions & 0 deletions app/views/render_async/_render_async.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
error_event_name: error_event_name,
retry_count: retry_count,
interval: interval,
lazy_load: lazy_load,
turbolinks: RenderAsync.configuration.turbolinks } %>
<% else %>
<%= render partial: 'render_async/request_vanilla',
Expand All @@ -34,6 +35,7 @@
error_event_name: error_event_name,
retry_count: retry_count,
interval: interval,
lazy_load: lazy_load,
turbolinks: RenderAsync.configuration.turbolinks } %>
<% end %>
<% end %>
Expand Down
31 changes: 23 additions & 8 deletions app/views/render_async/_request_jquery.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ if (window.jQuery) {
return;
}
<% end %>

var _listener = function(currentRetryCount) {
var headers = <%= headers.to_json.html_safe %>;
var csrfTokenElement = document.querySelector('meta[name="csrf-token"]')
Expand All @@ -18,7 +17,9 @@ if (window.jQuery) {
data: "<%= escape_javascript(data.to_s.html_safe) %>",
headers: headers
}).done(function(response) {
<% if interval %>
lazyLoaded(document.getElementById("<%= container_id %>"));

<% if interval %>
$("#<%= container_id %>").empty();
$("#<%= container_id %>").append(response);
<% else %>
Expand Down Expand Up @@ -73,16 +74,30 @@ if (window.jQuery) {
}
<% end %>

<% if lazy_load %>
<%= render partial: 'render_async/lazy_load',
formats: [:js],
locals: { container_id: container_id } %>
<% end %>

<% if turbolinks %>
$(document).one('turbolinks:load', _listener);
<% if lazy_load %>
$(document).one('turbolinks:load', _lazyListener);
<% else %>
$(document).one('turbolinks:load', _listener);
<% end %>
<% elsif interval %>
var _intervalFunction = function() {
_listener();
setInterval(_listener, <%= interval %>);
}
var _intervalFunction = function() {
_listener();
setInterval(_listener, <%= interval %>);
}
$(document).ready(_intervalFunction);
<% else %>
$(document).ready(_listener);
<% if lazy_load %>
$(document).ready(_lazyListener);
<% else %>
$(document).ready(_listener);
<% end %>
<% end %>
}(jQuery));
} else {
Expand Down
20 changes: 18 additions & 2 deletions app/views/render_async/_request_vanilla.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
if (request.readyState === 4) {
if (request.status >= SUCCESS && request.status < ERROR) {
var container = document.getElementById('<%= container_id %>');
lazyLoaded(container);

<% if interval %>
container.innerHTML = request.response;
<% else %>
Expand Down Expand Up @@ -86,10 +88,20 @@
}
<% end %>

<% if lazy_load %>
<%= render partial: 'render_async/lazy_load',
formats: [:js],
locals: { container_id: container_id } %>
<% end %>

<% if turbolinks %>
document.addEventListener("turbolinks:load", function (e) {
e.target.removeEventListener(e.type, arguments.callee);
_listener.call(this);
<% if lazy_load %>
_lazyListener();
<% else %>
_listener.call(this);
<% end %>
});
<% elsif interval %>
var _intervalFunction = function() {
Expand All @@ -98,6 +110,10 @@
}
document.addEventListener("DOMContentLoaded", _intervalFunction);
<% else %>
document.addEventListener("DOMContentLoaded", _listener);
<% if lazy_load %>
document.addEventListener("DOMContentLoaded", _lazyListener);
<% else %>
document.addEventListener("DOMContentLoaded", _listener);
<% end %>
<% end %>
})();
4 changes: 3 additions & 1 deletion lib/render_async/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def render_async(path, options = {}, &placeholder)
error_event_name = options.delete(:error_event_name)
retry_count = options.delete(:retry_count) || 0
interval = options.delete(:interval)
lazy_load = options.delete(:lazy_load) || false

render 'render_async/render_async', html_element_name: html_element_name,
container_id: container_id,
Expand All @@ -44,7 +45,8 @@ def render_async(path, options = {}, &placeholder)
error_message: error_message,
error_event_name: error_event_name,
retry_count: retry_count,
interval: interval
interval: interval,
lazy_load: lazy_load
end

private
Expand Down

0 comments on commit beb3734

Please sign in to comment.