modules_common_replace-haksa-text.js
import { traverseComponentTreeWithCallback } from "../../utils/ui/traverse-component-tree-with-callback";
import { isMobile } from "../../adapters/ezworks/com-api/util";
/**
* 컴포넌트 내의 텍스트 중 "학년"을 "학기차"로, "대학"을 "대학원"으로 변경하는 함수입니다.
*
* @function replaceHaksaText
* @param {nexacro.Div} targetComp
* 텍스트를 변경할 대상 Div 컴포넌트
* @param {Object} [options={}]
* 옵션 객체
* @param {boolean} [options.force=false]
* true인 경우 업무코드와 관계없이 강제로 텍스트를 변경합니다
* @memberof $f
* @example
* // 기본 사용
* $f.replaceHaksaText(this.div_main);
*
* // 강제 변경
* $f.replaceHaksaText(this.div_main, { force: true });
*
*/
export function replaceHaksaText(targetComp, options) {
// 업무 상세 코드 조회
const taskDtlCd = $comp
?.getWorkInfoDataset?.(targetComp)
?.getColumn?.(0, "TASK_DTL_CD");
const { force = false } = options || {};
// 업무코드가 없고 강제실행이 아닌 경우 종료
if (!taskDtlCd && !force) {
return false;
}
// 업무코드가 'D'로 시작하지 않고 강제실행이 아닌 경우 종료
const taskCd = taskDtlCd.substring(0, 1);
if (taskCd !== "D" && !force) {
return false;
}
// 컴포넌트 트리를 순회하며 텍스트 변경
traverseComponentTreeWithCallback(targetComp, (comp, e) => {
// 텍스트 변경 제외 대상인 경우 하위 순회 중단
if (comp.ignoreReplaceText === "true") {
e.stopChildNodeTraversal();
return;
}
// Static 컴포넌트의 텍스트 변경
if (comp instanceof nexacro.Static) {
comp.text = replaceHaksaString(String(comp.text));
}
// Grid 컴포넌트의 텍스트 변경
else if (comp instanceof nexacro.Grid) {
replaceTextInGrid(comp, "head");
// 모바일의 경우 추가로 body 영역도 변환
if (isMobile()) {
replaceTextInGrid(comp, "body");
}
}
});
return true;
}
const searchRegexp = /(학년|대학)$/;
const replacer = (match) => {
switch (match) {
case "학년":
return "학기차";
case "대학":
return "대학원";
}
};
/**
* 주어진 문자열에서 searchRegexp와 일치하는 부분을 replacer로 대체합니다.
*
* @param {string} str 변환하고자 하는 입력 문자열
* @returns {string} 변환된 문자열
* @access private
*/
function replaceHaksaString(str) {
return str.replace(searchRegexp, replacer);
}
/**
* 지정된 유형의 셀에서 텍스트를 치환하는 메서드입니다.
*
* @param {Object} comp 텍스트 치환 작업이 수행될 컴포넌트 객체
* @param {string} type 텍스트 치환 작업이 수행될 셀의 유형
* @returns {void} 반환값이 없습니다
* @access private
*/
function replaceTextInGrid(comp, type) {
const count = comp.getCellCount(type);
for (let i = 0; i < count; i++) {
const text = comp.getCellProperty(type, i, "text");
if (text) {
comp.setCellProperty(type, i, "text", replaceHaksaString(text));
}
}
}