util.js 985 Bytes
export const NODE_KEY = '$treeNodeId';

/**
 * @description: markNodeData
 * @param {*} node
 * @param {*} data
 * @author: renchao
 */
export const markNodeData = function(node, data) {
  if (!data || data[NODE_KEY]) return;
  Object.defineProperty(data, NODE_KEY, {
    value: node.id,
    enumerable: false,
    configurable: false,
    writable: false
  });
};

/**
 * @description: getNodeKey
 * @param {*} key
 * @param {*} data
 * @author: renchao
 */
export const getNodeKey = function(key, data) {
  if (!key) return data[NODE_KEY];
  return data[key];
};

/**
 * @description: findNearestComponent
 * @param {*} element
 * @param {*} componentName
 * @author: renchao
 */
export const findNearestComponent = (element, componentName) => {
  let target = element;
  while (target && target.tagName !== 'BODY') {
    if (target.__vue__ && target.__vue__.$options.name === componentName) {
      return target.__vue__;
    }
    target = target.parentNode;
  }
  return null;
};