utils_ui_find-ancestor-component.js
/**
* @typedef FindAncestorComponentOptions
* @property {number} [maxDepth=50] 최대 탐색 깊이. Default is `50`
*/
/**
* 인수로 넘긴 컴포넌트의 조상 컴포넌트를 탐색하여 조건에 일치하는 컴포넌트를 찾아 반환합니다.
*
* @function findAncestorComponent
* @param {nexacro.Component} comp
* 탐색 시작 컴포넌트
* @param {function(nexacro.Component):boolean} condition
* 조건 검사 함수
* @param {FindAncestorComponentOptions} options
* 추가 옵션
* @returns {nexacro.Component | null}
* 찾은 컴포넌트
* @memberof $f
* @example
* $f.findAncestorComponent(this, (ptr) => ptr instanceof nexacro.Form);
*
*/
export function findAncestorComponent(comp, condition, options) {
const maxDepth = (options && options.maxDepth) || 50;
let currentDepth = 0;
let result = null;
let ptr = comp;
// comp가 넥사크로 컴포넌트가 아님
if (!(comp instanceof nexacro.Component)) {
return null;
}
// condition이 함수가 아님
if (typeof condition !== "function") {
return null;
}
while (currentDepth <= maxDepth) {
// 대상을 찾음
if (condition(ptr)) {
result = ptr;
break;
}
// 부모 객체가 존재하지 않음
if (!ptr.parent) {
break;
}
// 부모 객체로 이어서 탐색
ptr = ptr.parent;
}
return result;
}