Skip to content

Commit

Permalink
Merge pull request #299 from qld-gov-au/fixInit
Browse files Browse the repository at this point in the history
Clean up initilization system of partials. handlebarsHelpers and handlebarsPartials are modules that need to be called handlebarsHelpers(HandleBars)
  • Loading branch information
duttonw authored Jun 24, 2024
2 parents 0a440af + 32a9628 commit 44883ef
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 34 deletions.
7 changes: 4 additions & 3 deletions .esbuild/plugins/qgds-plugin-copy-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ export default function copyPlugin() {
to: ["./dist/sample-data/"],
},
{
from: ["./src/js/handlebars*"],
to: ["./dist/assets/js"],
from: ["./src/js/handlebars.helpers.js"],
to: ["./dist/assets/js/handlebars.helpers.js"],
},
{
from: ["./src/js/handlebars*"],
from: ["./src/js/handlebars.*"],
to: ["./dist/components/"],
},

{ from: ["./src/assets/img/*"], to: ["./dist/assets/img"] },
],
});
Expand Down
6 changes: 5 additions & 1 deletion esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const buildConfig = {
},
{
in: "./src/js/handlebars.init.js",
out: "./components/handlebars.init.bundle",
out: "./components/handlebars.init.min",
},
{
in: "./src/js/handlebars.init.js",
out: "./assets/js/handlebars.init.min",
},
],

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"qld.bootstrap.min.js": "./dist/assets/js/qld.bootstrap.min.js",
"qld.bootstrap.css": "./dist/assets/css/qld.bootstrap.css",
"handlebars.helpers.bundle.js": "./dist/components/handlebars.helpers.bundle.js",
"handlebars.init.js": "./dist/components/handlebars.init.js",
"handlebars.init.min.js": "./dist/components/handlebars.init.min.js",
"bootstrap.min.js": "./dist/assets/js/boostrap.min.js"
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { forGov } from "../breadcrumbs/breadcrumbs.data.json";
import { BreadcrumbsWrapperTest } from "./breadcrumbsWrapper.test.js";
import init from "../../../js/handlebars.init.js";
import Handlebars from "handlebars";
// eslint-disable-next-line no-unused-vars
import init from "../../../js/handlebars.init.js"; //self init's when loaded

const defaultData = { breadcrumbs: forGov };

export default {
title: "!Layout/Components/Breadcrumbs Wrapper",
render: (args) => {
init(Handlebars)

return new BreadcrumbsWrapperTest(args).html;
},
args: defaultData,
Expand Down
8 changes: 4 additions & 4 deletions src/components/bs5/searchInput/search.functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ export async function showSuggestions(value = '', isDefault = false, form) {
<div class="suggestions-category mt-2">
<strong>Suggestions</strong>
<ul class="mt-2">${fetchedSuggestions.slice(0, 4).map(item => {
const highlightedText = item.replace(new RegExp(`(${value})`, 'gi'), '<strong>$1</strong>');
return `<li><a href="#">${highlightedText}</a></li>`;
}).join('')}</ul>
const highlightedText = item.replace(new RegExp(`(${value})`, 'gi'), '<strong>$1</strong>');
return `<li><a href="#">${highlightedText}</a></li>`;
}).join('')}</ul>
</div>`;
dynamicSuggestionsContainer.style.display = 'block';
createPopper(searchInput, suggestions, {
Expand Down Expand Up @@ -190,4 +190,4 @@ export function submitSearchForm(query = '', form) {
}

// Attach the function to the window object to make it globally accessible
window.selectSuggestion = (value, form) => selectSuggestion(value, form);
window.selectSuggestion = (value, form) => selectSuggestion(value, form);
12 changes: 0 additions & 12 deletions src/js/handlebars.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,3 @@ export default function handlebarsHelpers(handlebars) {
return handlebars.helpers.formatDate(dateToFormat);
});
}

//Only load once if Handlebars is available
if(typeof(Handlebars) !== 'undefined') {
this.registedHandlebarsHelpers = undefined;
if (typeof this.registedHandlebarsHelpers === 'undefined') {
// eslint-disable-next-line no-undef
handlebarsHelpers(Handlebars);
this.registedHandlebarsHelpers = true;
}
} else {
console.log("HandleBars is undefined, did not load helpers")
}
21 changes: 11 additions & 10 deletions src/js/handlebars.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Handlebars from "handlebars";
*
* @param {Handlebars} handlebars
*/
let isHandlebarsHelpersAndPartialsRegistered = false;

export default function init(handlebars = Handlebars) {
if (typeof(handlebars) === 'undefined') {
if(typeof(Handlebars) !== 'undefined') {
Expand All @@ -15,19 +17,18 @@ export default function init(handlebars = Handlebars) {
}

if(typeof(handlebars) !== 'undefined') {
//only load once
if (typeof handlebarsHelpers.registeredHandlebarsHelpers === 'undefined') {
handlebarsHelpers(handlebars);
handlebarsHelpers.registeredHandlebarsHelpers = true;
}
//only load once
if (typeof handlebarsPartials.registeredHandlebarsPartials === 'undefined') {
handlebarsPartials(handlebars);
handlebarsPartials.registeredHandlebarsPartials = true;
console.log('partials loaded');
if (!isHandlebarsHelpersAndPartialsRegistered) {
isHandlebarsHelpersAndPartialsRegistered = true;
} else {
console.log("HandleBars Helpers And Partials already loaded, loading again")
}
handlebarsHelpers(handlebars);

handlebarsPartials(handlebars);
} else {
console.log("Handlebars not found, init failed");
}
}

init();

0 comments on commit 44883ef

Please sign in to comment.