Skip to content

Commit

Permalink
Moving jquery event handlers to bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Oct 22, 2023
1 parent 118d4cd commit 703a065
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 26 deletions.
21 changes: 5 additions & 16 deletions _dev/js/theme/components/event/useEvent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import $ from 'jquery';

Check failure on line 1 in _dev/js/theme/components/event/useEvent.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

'$' is defined but never used

Check failure on line 1 in _dev/js/theme/components/event/useEvent.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

'$' is defined but never used
import EventHandler from "bootstrap/js/src/dom/event-handler";

Check failure on line 2 in _dev/js/theme/components/event/useEvent.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

Check failure on line 2 in _dev/js/theme/components/event/useEvent.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Strings must use singlequote

/**
* Function that will help you to add/remove/trigger events
Expand All @@ -13,11 +14,7 @@ const useEvent = () => {
* @param handler {function}
*/
const on = (element, eventName, handlerOrDelegation, handler) => {
if (typeof handlerOrDelegation === 'function') {
$(element).on(eventName, handlerOrDelegation);
} else {
$(element).on(eventName, handlerOrDelegation, handler);
}
EventHandler.on(element, eventName, handlerOrDelegation, handler);
};

/**
Expand All @@ -28,11 +25,7 @@ const useEvent = () => {
* @param handler {function}
*/
const one = (element, eventName, handlerOrDelegation, handler) => {
if (typeof handlerOrDelegation === 'function') {
$(element).one(eventName, handlerOrDelegation);
} else {
$(element).one(eventName, handlerOrDelegation, handler);
}
EventHandler.one(element, eventName, handlerOrDelegation, handler);
};

/**
Expand All @@ -43,11 +36,7 @@ const useEvent = () => {
* @param handler {function}
*/
const off = (element, eventName, handlerOrDelegation, handler) => {
if (typeof handlerOrDelegation === 'function') {
$(element).off(eventName, handlerOrDelegation);
} else {
$(element).off(eventName, handlerOrDelegation, handler);
}
EventHandler.off(element, eventName, handlerOrDelegation, handler);
};

/**
Expand All @@ -57,7 +46,7 @@ const useEvent = () => {
* @param args {object}
*/
const trigger = (element, eventName, args = {}) => {
$(element).trigger(eventName, args);
EventHandler.trigger(element, eventName, args);
};

return {
Expand Down
2 changes: 1 addition & 1 deletion _dev/js/theme/components/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DOMReady(() => {
const createInputFile = () => {
each('.js-file-input', (input) => {
input.addEventListener('change', (event) => {
const target = event.currentTarget;
const target = event.delegateTarget;
const file = (target) ? target.files[0] : null;

if (target && file) {
Expand Down
4 changes: 2 additions & 2 deletions _dev/js/theme/core/cart/handler/cart/addToCartHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const { danger } = useAlertToast();
const addToCartHandler = async (event) => {
event.preventDefault();

const form = event.currentTarget?.form;
const addToCartButton = event.currentTarget;
const form = event.delegateTarget?.form;
const addToCartButton = event.delegateTarget;

const isQuantityInputValid = (input) => {
let validInput = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import deleteFromCartRequest from '../../request/cart/deleteFromCartRequest';
const deleteFromCartHandler = async (event) => {
event.preventDefault();

const button = event.currentTarget;
const button = event.delegateTarget;
const { dataset } = button;
const { idProduct, idProductAttribute, idCustomization = 0 } = dataset;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const codeLinkSubmitHandler = (event) => {
event.preventDefault();

const link = event.currentTarget;
const link = event.delegateTarget;
const input = document.querySelector('[name="discount_name"]');
const form = document.querySelector('.js-voucher-form');
const code = link.dataset?.code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import deleteVoucherFromCartRequest from '../../request/voucher/deleteVoucherFro
const deleteVoucherHandler = async (event) => {
event.preventDefault();

const btn = event.currentTarget;
const btn = event.delegateTarget;
const { dataset } = btn;
const { idDiscount } = dataset;
const payload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import parseToHtml from '../../../../utils/parseToHtml';
const submitVoucherHandler = async (event) => {
event.preventDefault();

const addVoucherForm = event.currentTarget;
const addVoucherForm = event.delegateTarget;
const btn = addVoucherForm.querySelector('[type="submit"]');
const input = addVoucherForm.querySelector('[name="discount_name"]');
const voucherName = input?.value || '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const changeAddressHandler = (event) => {
element.classList.add('selected');
});

const eventTarget = event.currentTarget;
const eventTarget = event.delegateTarget;
const addressErrorElement = document.querySelector(addressError);
const idFailureAddress = addressErrorElement ? addressErrorElement?.id.split('-').pop() : null;
const notValidAddressesVal = document.querySelector(notValidAddresses)?.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ const useBootstrapComponentDynamicImport = (importFiles, {

await handleComponentLoad();

const { currentTarget, type } = e;
const { delegateTarget, type } = e;

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

const { bindEvents, unbindEvents } = useDynamicImportEventsHandler(events, handleEvent);
Expand Down

0 comments on commit 703a065

Please sign in to comment.