Skip to content

Commit

Permalink
Linting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Oct 16, 2023
1 parent 41edf41 commit 3564065
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 104 deletions.
2 changes: 1 addition & 1 deletion _dev/js/checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function setUpCheckout() {
on(prestashop.selectors.checkout.giftCheckbox, 'change', ({ target }) => {
const isChecked = target.checked;
const giftBlock = document.querySelector('#gift');
const collapseInstance = bootstrap.Collapse.getOrCreateInstance(giftBlock);
const collapseInstance = window.bootstrap.Collapse.getOrCreateInstance(giftBlock);

collapseInstance.toggle(isChecked);
});
Expand Down
3 changes: 1 addition & 2 deletions _dev/js/theme/components/dynamic-bootstrap-components.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import $ from 'jquery';
import DOMReady from "../utils/DOMReady";
import DOMReady from '../utils/DOMReady';
import useBootstrapComponentDynamicImport from '../utils/dynamicImports/useBootstrapComponentDynamicImport';

DOMReady(() => {
Expand Down
206 changes: 105 additions & 101 deletions _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const useBootstrapComponentDynamicImport = (importFiles, {
if (!instancesMap.has(element)) {
instancesMap.set(element, instance);
}
}
};

const getInstanceFromMap = (element) => {
if (!instancesMap.has(element)) {
return null;
}

return instancesMap.get(element);
}
};

if (!componentName) {
throw new Error('Component name is required');
Expand All @@ -38,86 +38,19 @@ const useBootstrapComponentDynamicImport = (importFiles, {
} catch (e) {
return false;
}
}

const handleComponentLoad = async () => {
if (filesLoaded) {
return;
}

unbindEvents();
await loadFiles();
onLoad();
executeCallStack();
};

const getOrCreateInstance = (element) => {
const pluginInstance = getComponentInstance(element);

if (pluginInstance) {
return pluginInstance.proxyInstance;
}

const proxyInstance = new ComponentObjectConstructorFunction(element);

setInstanceInMap(element, proxyInstance);

return proxyInstance;
}

const getComponentInstance = (element) => getInstanceFromMap(element);

const proxyFactory = (pluginInstance) => {
const pluginObject = {};
const proxyHandler = {
get(target, prop, receiver) {
return (...args) => {
if (!filesLoaded) {
pluginInstance.instanceMethodCall.push({
prop,
args,
});

handleComponentLoad();
}

if (pluginInstance.componentInstance !== null) {
pluginInstance.componentInstance[prop](...args);
}

return receiver;
};
}
}

return new Proxy(pluginObject, proxyHandler)
}

const ComponentObjectConstructorFunction = function(args) {
const pluginInstance = {
args,
instanceMethodCall: [],
componentInstance: null,
}

pluginInstance.proxyInstance = proxyFactory(pluginInstance);

callStack.push(pluginInstance);

return pluginInstance.proxyInstance;
};

ComponentObjectConstructorFunction.getOrCreateInstance = getOrCreateInstance;
ComponentObjectConstructorFunction.getInstance = getComponentInstance;
const loadFiles = () => {
filesLoaded = true;

const handleJQueryPluginCall = function(args) {
jQueryCallStack.push({
elem: this,
args,
return Promise.all(importFiles()).then((files) => {
files.forEach((file) => {
if (file.default) {
window.bootstrap[componentName] = file.default;
}
});
});

handleComponentLoad();
}
};

const executeCallStack = () => {
callStack.forEach(({ args, instanceMethodCall, componentInstance }, i) => {
Expand All @@ -127,8 +60,8 @@ const useBootstrapComponentDynamicImport = (importFiles, {

setInstanceInMap(componentInstance._element, componentInstance);

instanceMethodCall.forEach(({ prop, args }) => {
componentInstance[prop](...args);
instanceMethodCall.forEach(({ prop, methodArgs }) => {
componentInstance[prop](...methodArgs);
});
});

Expand All @@ -139,32 +72,17 @@ const useBootstrapComponentDynamicImport = (importFiles, {
}
};

window.bootstrap = window.bootstrap || {};
window.bootstrap[componentName] = ComponentObjectConstructorFunction;

if (isJQueryEnabled()) {
window.jQuery.fn[getJQueryComponentName()] = handleJQueryPluginCall;
}

const handleEvent = async (e) => {
e.preventDefault();

// DISABLE FOR NOW BEFORE REFACTORING
/* eslint-disable */
await handleComponentLoad();
/* eslint-enable */

const { currentTarget, type } = e;

currentTarget.dispatchEvent(new Event(type));
}

const loadFiles = () => {
filesLoaded = true;

return Promise.all(importFiles()).then((files) => {
files.forEach((file) => {
if (file.default) {
window.bootstrap[componentName] = file.default;
}
});
});
};

const unbindEvents = () => {
Expand All @@ -180,7 +98,7 @@ const useBootstrapComponentDynamicImport = (importFiles, {
document,
name,
selector,
handleEvent
handleEvent,
);
});
};
Expand All @@ -198,11 +116,97 @@ const useBootstrapComponentDynamicImport = (importFiles, {
document,
name,
selector,
handleEvent
handleEvent,
);
});
};

const handleComponentLoad = async () => {
if (filesLoaded) {
return;
}

unbindEvents();
await loadFiles();
onLoad();
executeCallStack();
};

const proxyFactory = (pluginInstance) => {
const pluginObject = {};
const proxyHandler = {
get(target, prop, receiver) {
return (...args) => {
if (!filesLoaded) {
pluginInstance.instanceMethodCall.push({
prop,
args,
});

handleComponentLoad();
}

if (pluginInstance.componentInstance !== null) {
pluginInstance.componentInstance[prop](...args);
}

return receiver;
};
},
};

return new Proxy(pluginObject, proxyHandler);
};

const getComponentInstance = (element) => getInstanceFromMap(element);

function ComponentObjectConstructorFunction(args) {
const pluginInstance = {
args,
instanceMethodCall: [],
componentInstance: null,
};

pluginInstance.proxyInstance = proxyFactory(pluginInstance);

callStack.push(pluginInstance);

return pluginInstance.proxyInstance;
}

const getOrCreateInstance = (element) => {
const pluginInstance = getComponentInstance(element);

if (pluginInstance) {
return pluginInstance.proxyInstance;
}

const proxyInstance = new ComponentObjectConstructorFunction(element);

setInstanceInMap(element, proxyInstance);

return proxyInstance;
};

ComponentObjectConstructorFunction.getOrCreateInstance = getOrCreateInstance;
ComponentObjectConstructorFunction.getInstance = getComponentInstance;

const handleJQueryPluginCall = (args) => {
jQueryCallStack.push({
elem: this,
args,
});

handleComponentLoad();
};

window.bootstrap = window.bootstrap || {};
window.bootstrap[componentName] = ComponentObjectConstructorFunction;

if (isJQueryEnabled()) {
window.jQuery.fn[getJQueryComponentName()] = handleJQueryPluginCall;
}

const init = () => {
bindEvents();
};
Expand Down

0 comments on commit 3564065

Please sign in to comment.