Skip to content

Commit

Permalink
Refactor: Decompose imageCreator into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
astik-dev committed Apr 19, 2024
1 parent 8748b25 commit 162b174
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
42 changes: 1 addition & 41 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,12 @@
import { doc, dqs, dqsa } from "./modules/utils.js";
import imageCreator from "./modules/imageCreator.js";



// script.js

let openClosePermissionProjectPopup = true;



const getFileExtension = filePath => filePath.split('.').pop();

const imageCreator = {

imgBasePath: {
local: "img/",
external: "https://raw.githubusercontent.com/astik-dev/portfolio/content/images/",
},

px1: `img/1x1.png`,

fullPath: function (source, path) {return this.imgBasePath[source] + path},

newWebpPic: function (source, webpPath, fallbackPath, alt, lazy) {
const types = {
jpg: "jpeg",
}

const src = lazy ? "data-src" : "srcset";

const fallbackExt = getFileExtension(fallbackPath);
const fallbackType = types[fallbackExt] || fallbackExt;

return `<picture>
<source type="image/webp" ${src}="${this.fullPath(source, webpPath)}">
<source type="image/${fallbackType}" ${src}="${this.fullPath(source, fallbackPath)}">
${this.newImg(source, fallbackPath, alt, lazy)}
</picture>`;
},

newImg: function (source, path, alt, lazy) {
const fullPath = this.fullPath(source, path);
const src = lazy ? `"${this.px1}" data-src="${fullPath}"` : `"${fullPath}"`;
return `<img src=${src} alt="${alt}">`;
}
}



function toggleBurgerMenu() {
const
headerCont = dqs(".header__container"),
Expand Down
38 changes: 38 additions & 0 deletions src/js/modules/imageCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const getFileExtension = filePath => filePath.split('.').pop();

const imageCreator = {

imgBasePath: {
local: "img/",
external: "https://raw.githubusercontent.com/astik-dev/portfolio/content/images/",
},

px1: `img/1x1.png`,

fullPath: function (source, path) {return this.imgBasePath[source] + path},

newWebpPic: function (source, webpPath, fallbackPath, alt, lazy) {
const types = {
jpg: "jpeg",
}

const src = lazy ? "data-src" : "srcset";

const fallbackExt = getFileExtension(fallbackPath);
const fallbackType = types[fallbackExt] || fallbackExt;

return `<picture>
<source type="image/webp" ${src}="${this.fullPath(source, webpPath)}">
<source type="image/${fallbackType}" ${src}="${this.fullPath(source, fallbackPath)}">
${this.newImg(source, fallbackPath, alt, lazy)}
</picture>`;
},

newImg: function (source, path, alt, lazy) {
const fullPath = this.fullPath(source, path);
const src = lazy ? `"${this.px1}" data-src="${fullPath}"` : `"${fullPath}"`;
return `<img src=${src} alt="${alt}">`;
}
}

export default imageCreator;

0 comments on commit 162b174

Please sign in to comment.