This repository was archived by the owner on Jul 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Add JS Loaders #123
Open
lucasvallenet
wants to merge
4
commits into
master
Choose a base branch
from
feature/js-loaders
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add JS Loaders #123
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
110fb2e
Loaders (wip)
lucasvallenet d70ff62
Add config.js / Update app.js with js class / Add loaders.js
lucasvallenet f714cd3
Add config image selectors and css class / Refactor app.js
lucasvallenet cc8ff1f
Add config vars / Reformat app.js and add explicite comments
lucasvallenet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,97 @@ | ||
| import modular from 'modujs'; | ||
| import * as modules from './modules'; | ||
| import globals from './globals'; | ||
| import { html } from './utils/environment'; | ||
|
|
||
| const app = new modular({ | ||
| modules: modules | ||
| }); | ||
|
|
||
| window.onload = (event) => { | ||
| const $style = document.getElementById('main-css'); | ||
|
|
||
| if ($style) { | ||
| if ($style.isLoaded) { | ||
| init(); | ||
| } else { | ||
| $style.addEventListener('load', (event) => { | ||
| init(); | ||
| }); | ||
| import modular from 'modujs' | ||
| import * as modules from './modules' | ||
| import globals from './globals' | ||
| import { fontsLoader, preloadImages, styleSheetsLoader } from './utils/loaders' | ||
| import { html } from './utils/environment' | ||
| import config from './config' | ||
|
|
||
| class App { | ||
| constructor() { | ||
| console.log(`${this.constructor.name}:constructor`) | ||
|
|
||
| this.options = { | ||
| fonts: [ | ||
| // { name: '<font-name>', style: '<font-style>', weight: '<font-weight>' } | ||
| ], | ||
| preloadImage: [ | ||
| 'img[data-preload]' | ||
| ], | ||
| styleSheets: [ | ||
| '#main-css' | ||
| ] | ||
| } | ||
| } else { | ||
| console.warn('The "main-css" stylesheet not found'); | ||
|
|
||
| // Create app | ||
| this.app = new modular({ | ||
| modules: modules | ||
| }) | ||
|
|
||
| // // Add custom events | ||
| // this.addCustomEvents() | ||
|
|
||
| // Set vars | ||
| this.setVars() | ||
|
|
||
| // Window events | ||
| window.addEventListener('load', () => this.load()) | ||
| } | ||
|
|
||
| load() { | ||
| console.log(`${this.constructor.name}:load`) | ||
|
|
||
| // Font load | ||
| const fontLoad = new Promise(resolve => { | ||
| fontsLoader(this.options.fonts, () => { | ||
| html.classList.add(config.CSS_CLASS.FONTS_LOADED) | ||
| resolve() | ||
| }) | ||
| }) | ||
|
|
||
| // Image preload | ||
| const imagePreload = new Promise(resolve => { | ||
| preloadImages(this.options.preloadImage, () => { | ||
| html.classList.add(config.CSS_CLASS.IMAGES_PRELOADED) | ||
| resolve() | ||
| }) | ||
| }) | ||
|
|
||
| // Stylesheets load | ||
| const styleSheetLoad = new Promise(resolve => styleSheetsLoader(this.options.styleSheets, () => resolve())) | ||
|
|
||
| Promise.all([ | ||
| fontLoad, | ||
| imagePreload, | ||
| styleSheetLoad, | ||
| ]).then(() => { | ||
| this.init() | ||
| }).catch(e => { | ||
| console.log(e) | ||
| }) | ||
| } | ||
| }; | ||
|
|
||
| function init() { | ||
| globals(); | ||
| init() { | ||
| console.log(`${this.constructor.name}:init`) | ||
|
|
||
| app.init(app); | ||
| // Init globals | ||
| globals() | ||
|
|
||
| html.classList.add('is-loaded'); | ||
| html.classList.add('is-ready'); | ||
| html.classList.remove('is-loading'); | ||
| // Init modular app | ||
| this.app.init(this.app) | ||
|
|
||
| // Update classes | ||
| html.classList.add(config.CSS_CLASS.LOADED) | ||
| html.classList.add(config.CSS_CLASS.READY) | ||
| html.classList.remove(config.CSS_CLASS.LOADING) | ||
| } | ||
|
|
||
| addCustomEvents() { | ||
| console.log(`${this.constructor.name}:addCustomEvents`) | ||
| } | ||
|
|
||
| setVars() { | ||
| html.style.setProperty('--vh-initial', `${0.01 * html.clientHeight}px`) | ||
| } | ||
| } | ||
|
|
||
| // Instanciate app | ||
| new App() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| const env = process.env.NODE_ENV | ||
|
|
||
| export default config = Object.freeze({ | ||
| // Environments | ||
| ENV: env, | ||
| IS_PROD: env === 'production', | ||
| IS_DEV: env === 'development', | ||
|
|
||
| // CSS class names | ||
| CSS_CLASS: { | ||
| LOADING: 'is-loading', | ||
| READY: 'is-ready', | ||
| LOADED: 'is-loaded', | ||
| FONTS_LOADED: 'has-fonts-loaded', | ||
| IMAGES_PRELOADED: 'has-images-preloaded', | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,87 +1,19 @@ | ||
| const LAZY_LOADED_IMAGES = [] | ||
|
|
||
| export function loadImage(url, options = {}) { | ||
| return new Promise((resolve, reject) => { | ||
| const $img = new Image(); | ||
|
|
||
| if (options.crossOrigin) { | ||
| $img.crossOrigin = options.crossOrigin; | ||
| } | ||
|
|
||
| const loadCallback = () => { | ||
| resolve({ | ||
| element: $img, | ||
| ...getImageMetadata($img), | ||
| }); | ||
| } | ||
|
|
||
| if($img.decode) { | ||
| $img.src = url | ||
| $img.decode().then(loadCallback).catch(e => { | ||
| reject(e) | ||
| }) | ||
| } else { | ||
| $img.onload = loadCallback | ||
| $img.onerror = (e) => { | ||
| reject(e); | ||
| }; | ||
| $img.src = url | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export function getImageMetadata($img) { | ||
| return { | ||
| url: $img.src, | ||
| width: $img.naturalWidth, | ||
| height: $img.naturalHeight, | ||
| ratio: $img.naturalWidth / $img.naturalHeight, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Lazy load the given image. | ||
| * Get an image meta data | ||
| * | ||
| * @param {HTMLImageElement} $el - The image element. | ||
| * @param {?string} url - The URI to lazy load into $el. | ||
| * If falsey, the value of the `data-src` attribute on $el will be used as the URI. | ||
| * @param {?function} callback - A function to call when the image is loaded. | ||
| * @param {HTMLImageElement} $img - The image element. | ||
| * @return {object} The given image meta data | ||
| */ | ||
| export async function lazyLoadImage($el, url, callback) { | ||
| let src = url ? url : $el.dataset.src | ||
|
|
||
| let loadedImage = LAZY_LOADED_IMAGES.find(image => image.url === src) | ||
|
|
||
| if (!loadedImage) { | ||
| loadedImage = await loadImage(src) | ||
|
|
||
| if (!loadedImage.url) { | ||
| return; | ||
| } | ||
|
|
||
| LAZY_LOADED_IMAGES.push(loadedImage) | ||
| } | ||
|
|
||
| if($el.src === src) { | ||
| return | ||
| } | ||
|
|
||
| if ($el.tagName === 'IMG') { | ||
| $el.src = loadedImage.url; | ||
| } else { | ||
| $el.style.backgroundImage = `url(${loadedImage.url})`; | ||
| } | ||
|
|
||
| requestAnimationFrame(() => { | ||
| let lazyParent = $el.closest('.c-lazy'); | ||
| const getImageMetadata = $img => ({ | ||
| url: $img.src, | ||
| width: $img.naturalWidth, | ||
| height: $img.naturalHeight, | ||
| ratio: $img.naturalWidth / $img.naturalHeight, | ||
| }) | ||
|
|
||
| if(lazyParent) { | ||
| lazyParent.classList.add('-lazy-loaded') | ||
| lazyParent.style.backgroundImage = '' | ||
| } | ||
|
|
||
| $el.classList.add('-lazy-loaded') | ||
|
|
||
| callback?.() | ||
| }) | ||
| export { | ||
| getImageMetadata, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.