Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable turning lazyloading on/off in settings.yml #433

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions _data/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ web:
# Set 'inject' to false to disable SVG injection.
svg:
inject: true
# -----------------
# Images management
# -----------------
images:
lazyload: true

# ----------------------------------------------------------
# Epub settings
Expand Down
1 change: 1 addition & 0 deletions assets/js/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ layout: null
---

{% include_relative polyfills.js %}
{% include_relative settings.js %}
{% include_relative locales.js %}
{% include_relative mark-parents.js %}

Expand Down
6 changes: 4 additions & 2 deletions assets/js/lazyload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*jslint browser */
/*globals window, IntersectionObserver, SVGInject */
/*globals window, IntersectionObserver, SVGInject, settings */

// Add observer
var ebImageObserverConfig = {
Expand All @@ -26,8 +26,10 @@ function ebLazyLoadImages() {
.call(document.querySelectorAll('[data-src], [data-srcset]'));

// If IntersectionObserver is supported,
// and lazyloading is on in settings.yml (loaded in settings.js),
// create a new one that will use it on all the lazyImages.
if (window.hasOwnProperty('IntersectionObserver')) {
if (window.hasOwnProperty('IntersectionObserver')
&& settings.web.images.lazyload === true) {
var lazyImageObserver = new IntersectionObserver(function
(entries, lazyImageObserver) {
entries.forEach(function (entry) {
Expand Down
21 changes: 21 additions & 0 deletions assets/js/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Load settings.yml into a settings array.

// Fetch specific values from settings.yml and
// convert them into a Javascript object called settings.
// Note that some YAML keys use hyphens, which are invalid JS.
// So to use them as variables, use square brackets and quotes,
// e.g. search['search-placeholder'].
// NB: The generated settings load in client-side Javascript, so
// do not include any settings that should not be publicly available.

// Create default settings object
var settings = {
web: {
images: {
lazyload: true
}
}
};

// Override default settings from settings.yml
settings.web.images.lazyload = {{ site.data.settings.web.images.lazyload }};