import { CustomData, ID } from '../types'; import { dataItemIdKey, dataItemNameKey, dataItemUseCaptureKey } from './dataAttributes'; export type HTMLItemInfo = { /** ID элемента, заданый через data-атрибут data-item-id */ actionElementId: ID; /** Порядковый номер элемента в блоке, внутри которого он находится. -1, если элемент не найден в блоке */ actionElementIndex: number; /** Имя блока элемента, заданое через data-атрибут data-item-name */ actionElementName?: string; /** Параметр, отвечающий за то, на какой фазе отлавливать событие */ eventUseCapture: boolean; }; export const getItemInfo = ( block: B | null, targetItem: T ): HTMLItemInfo & Partial => { const allItems: Element[] = block ? Array.from(block.querySelectorAll(`[${dataItemIdKey}]`)) : []; const actionElementId = targetItem.getAttribute(dataItemIdKey) || ''; const actionElementIndex = allItems.findIndex((item) => item === targetItem); const actionElementName = targetItem.getAttribute(dataItemNameKey) || targetItem.innerText; const eventUseCapture = targetItem.getAttribute(dataItemUseCaptureKey) === 'true'; const data = targetItem.getAttribute('data-json'); let jsonData: Partial = {}; try { jsonData = data ? JSON.parse(data) : jsonData; /* eslint-disable-next-line no-empty */ } finally { } return { actionElementId, actionElementIndex: actionElementIndex < 0 ? -1 : actionElementIndex + 1, actionElementName, eventUseCapture, ...jsonData, }; };