(() => {
const { EventManager, utils, constants, actions } = ShopbySkin;
const url = new URL(window.location.href);
const searchParams = new URLSearchParams(url.search);
const boardId = searchParams.get('boardId');
const boardNo = searchParams.get('boardNo');
let shopbyCommonData = {};
EventManager.on('PAGE_LOAD_COMPLETED', (commonData) => {
shopbyCommonData = commonData;
});
const getActionNameWithPairKey = ({ action, target }) => {
const pairKey = target.closest('[pair-key]')?.getAttribute('pair-key');
const actionKeySuffix = pairKey ? `:${pairKey}` : '';
return `${action}${actionKeySuffix}`;
};
const restrictionCauseMessageWithNextUrlMap = {
MEMBER_ONLY: (writable) => {
if (writable) {
return {};
}
return {
modalType: 'MODAL_CONFIRM_OPEN',
message: '로그인하셔야 본 서비스를 이용하실 수 있습니다.',
nextUrl: `${location.origin}${constants.PAGE.SIGN_IN}`,
};
},
SPECIFIC_MEMBERS: (writable) => {
if (writable) {
return {};
}
if (!utils.isSignedIn())
return {
modalType: 'MODAL_CONFIRM_OPEN',
message: '로그인하셔야 본 서비스를 이용하실 수 있습니다.',
nextUrl: `${location.origin}${constants.PAGE.SIGN_IN}`,
};
return {
modalType: 'MODAL_ALERT_OPEN',
message: '글쓰기 권한이 없습니다.',
};
},
};
const openArticleWriteForm = ({ boardName, canSecretPosting, usesAttachment }) => {
const { boardConfigs = [] } = shopbyCommonData.allBoardConfig;
EventManager.fire('OPEN_LAYER_MODAL', {
name: 'article-write-form',
title: boardName,
isFull: false,
data: {
boardId,
boardNo,
canSecretPosting,
usesAttachment,
boardConfigs,
isModify: false,
},
onClose: ({ reason }) => {
if (reason === 'DID_SUBMIT') {
location.reload();
}
},
});
};
const moduleActionHandler = {
WRITE: ({ target }) => {
const authorityType = target.closest('[shopby-authority-type]')?.getAttribute('shopby-authority-type');
const { profile, allBoardConfig } = shopbyCommonData;
const { data: writable } = actions.queryBoardDisplayableStatus({
profile,
boardNo,
boardId,
boardConfigs: allBoardConfig.boardConfigs,
permissionType: constants.BOARD_PERMISSION_CONFIG_TYPE.WRITE_PERMISSION,
});
const { message, modalType, nextUrl } = restrictionCauseMessageWithNextUrlMap[authorityType]?.(writable) ?? {};
if (message) {
EventManager.fire(modalType, {
message: `${message}`,
noticeType: constants.NOTICE_MODAL_TYPE.WARNING,
onConfirm: () => {
if (!nextUrl) {
return;
}
location.href = `${nextUrl}?from=${encodeURIComponent(location.href)}`;
},
});
return;
}
const boardName = document.querySelector('[shopby-board-name]')?.getAttribute('shopby-board-name');
const canSecretPosting = target.closest('[shopby-can-secret-posting]')?.getAttribute('shopby-can-secret-posting');
const usesAttachment = target.closest('[shopby-uses-attachment]')?.getAttribute('shopby-uses-attachment');
openArticleWriteForm({
boardName,
canSecretPosting,
usesAttachment,
});
},
GO_TO_DETAIL: (event) => {
event.preventDefault();
const { target } = event;
const linkEl = target?.href ?? target.closest('[href]');
const { profile, allBoardConfig } = shopbyCommonData;
const { data: readable } = actions.queryBoardDisplayableStatus({
profile,
boardConfigs: allBoardConfig.boardConfigs,
boardNo,
boardId,
permissionType: constants.BOARD_PERMISSION_CONFIG_TYPE.READ_PERMISSION,
});
if (readable) {
location.href = linkEl.href;
} else {
EventManager.fire('MODAL_ALERT_OPEN', {
message: '게시글 읽기 권한이 없습니다.',
noticeType: constants.NOTICE_MODAL_TYPE.WARNING,
});
}
},
MOVE_TO_FIRST_PAGE: ({ target }) => {
const actionName = getActionNameWithPairKey({ action: 'MOVE_TO_FIRST_PAGE', target });
EventManager.fire(actionName);
},
MOVE_TO_PREV_PAGE: ({ target }) => {
const actionName = getActionNameWithPairKey({ action: 'MOVE_TO_PREV_PAGE', target });
EventManager.fire(actionName);
},
MOVE_TO_PAGE: ({ target }) => {
const actionName = getActionNameWithPairKey({ action: 'MOVE_TO_PAGE', target });
const page = target.closest('[shopby-page]')?.getAttribute('shopby-page');
EventManager.fire(actionName, { page });
},
MOVE_TO_NEXT_PAGE: ({ target }) => {
const actionName = getActionNameWithPairKey({ action: 'MOVE_TO_NEXT_PAGE', target });
EventManager.fire(actionName);
},
MOVE_TO_LAST_PAGE: ({ target }) => {
const actionName = getActionNameWithPairKey({ action: 'MOVE_TO_LAST_PAGE', target });
EventManager.fire(actionName);
},
};
document.querySelector('article-list-info')?.addEventListener('click', ({ target }) => {
const action = target.getAttribute('shopby-action');
const boardName = document.querySelector('[shopby-board-name]')?.getAttribute('shopby-board-name');
moduleActionHandler[action]?.({ boardName });
});
document.querySelector('article-total-count-v2')?.addEventListener('click', ({ target }) => {
const action = target.getAttribute('shopby-action');
moduleActionHandler[action]?.({ target });
});
document.querySelector('article-list-v2')?.addEventListener('click', (event) => {
const action =
event.target.getAttribute('shopby-action') ??
event.target.closest('[shopby-action]')?.getAttribute('shopby-action');
moduleActionHandler[action]?.(event);
});
})();