UNPKG

@formily/core

Version:

English | [简体中文](./README.zh-cn.md)

246 lines (245 loc) 9.16 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var shared_1 = require("@formily/shared"); var FormGraph = (function (_super) { __extends(FormGraph, _super); function FormGraph(props) { if (props === void 0) { props = {}; } var _this = _super.call(this) || this; _this.refrences = {}; _this.nodes = {}; _this.size = 0; _this.buffer = []; _this.matchStrategy = props.matchStrategy; return _this; } FormGraph.prototype.select = function (path, eacher) { var pattern = shared_1.FormPath.parse(path); if (!eacher) { var node = this.get(pattern); if (node) { return node; } } for (var nodePath in this.nodes) { var node = this.nodes[nodePath]; if (shared_1.isFn(this.matchStrategy) ? this.matchStrategy(pattern, nodePath) : pattern.match(nodePath)) { if (shared_1.isFn(eacher)) { var result = eacher(node, shared_1.FormPath.parse(nodePath)); if (result === false) { return node; } } else { return node; } } } }; FormGraph.prototype.get = function (path) { return this.nodes[shared_1.FormPath.parse(path).toString()]; }; FormGraph.prototype.selectParent = function (path) { var selfPath = shared_1.FormPath.parse(path); var parentPath = shared_1.FormPath.parse(path).parent(); if (selfPath.toString() === parentPath.toString()) return undefined; return this.get(parentPath); }; FormGraph.prototype.selectChildren = function (path) { var _this = this; var ref = this.refrences[shared_1.FormPath.parse(path).toString()]; if (ref && ref.children) { return shared_1.reduce(ref.children, function (buf, path) { return buf.concat(_this.get(path)).concat(_this.selectChildren(path)); }, []); } return []; }; FormGraph.prototype.exist = function (path) { return !!this.get(shared_1.FormPath.parse(path)); }; FormGraph.prototype.eachChildren = function (path, selector, eacher, recursion) { var _this = this; if (selector === void 0) { selector = true; } if (eacher === void 0) { eacher = true; } if (recursion === void 0) { recursion = true; } if (shared_1.isFn(path)) { recursion = selector; eacher = path; path = ''; selector = '*'; } if (shared_1.isFn(selector)) { recursion = eacher; eacher = selector; selector = '*'; } var ref = this.refrences[shared_1.FormPath.parse(path).toString()]; if (ref && ref.children) { return shared_1.each(ref.children, function (path) { if (shared_1.isFn(eacher)) { var node = _this.get(path); if (node && (shared_1.isFn(_this.matchStrategy) ? _this.matchStrategy(selector, path) : shared_1.FormPath.parse(selector).match(path))) { eacher(node, path); if (recursion) { _this.eachChildren(path, selector, eacher, recursion); } } } }); } }; FormGraph.prototype.eachParent = function (path, eacher) { var selfPath = shared_1.FormPath.parse(path); var ref = this.refrences[selfPath.toString()]; if (shared_1.isFn(eacher)) { if (ref && ref.parent) { var node = this.get(ref.parent.path); this.eachParent(ref.parent.path, eacher); eacher(node, ref.parent.path); } } }; FormGraph.prototype.eachParentAndChildren = function (path, eacher) { var selfPath = shared_1.FormPath.parse(path); var node = this.get(selfPath); if (!node) return; this.eachParent(selfPath, eacher); if (shared_1.isFn(eacher)) { eacher(node, selfPath); this.eachChildren(selfPath, eacher); } }; FormGraph.prototype.getLatestParent = function (path) { var selfPath = shared_1.FormPath.parse(path); var parentPath = shared_1.FormPath.parse(path).parent(); if (selfPath.toString() === parentPath.toString()) return undefined; if (this.refrences[parentPath.toString()]) return { ref: this.refrences[parentPath.toString()], path: shared_1.FormPath.parse(parentPath.toString()) }; return this.getLatestParent(parentPath); }; FormGraph.prototype.map = function (mapper) { return shared_1.map(this.nodes, mapper); }; FormGraph.prototype.reduce = function (reducer, initial) { return shared_1.reduce(this.nodes, reducer, initial); }; FormGraph.prototype.appendNode = function (path, node) { var _this = this; var selfPath = shared_1.FormPath.parse(path); var parentPath = selfPath.parent(); var parentRef = this.refrences[parentPath.toString()]; var selfRef = { path: selfPath, children: [] }; if (this.get(selfPath)) return; this.nodes[selfPath.toString()] = node; this.refrences[selfPath.toString()] = selfRef; this.size++; if (parentRef) { parentRef.children.push(selfPath); selfRef.parent = parentRef; } else { var latestParent = this.getLatestParent(selfPath); if (latestParent) { latestParent.ref.children.push(selfPath); selfRef.parent = latestParent.ref; this.buffer.push({ path: selfPath, ref: selfRef, latestParent: latestParent }); } } this.buffer.forEach(function (_a, index) { var path = _a.path, ref = _a.ref, latestParent = _a.latestParent; if (path.parent().match(selfPath) || (selfPath.includes(latestParent.path) && path.includes(selfPath) && selfPath.toString() !== path.toString())) { selfRef.children.push(path); ref.parent = selfRef; latestParent.ref.children.splice(latestParent.ref.children.indexOf(path), 1); _this.buffer.splice(index, 1); } }); this.notify({ type: 'GRAPH_NODE_DID_MOUNT', payload: selfRef }); }; FormGraph.prototype.remove = function (path) { var _this = this; var selfPath = shared_1.FormPath.parse(path); var selfRef = this.refrences[selfPath.toString()]; if (!selfRef) return; this.notify({ type: 'GRAPH_NODE_WILL_UNMOUNT', payload: selfRef }); if (selfRef.children) { selfRef.children.forEach(function (path) { _this.remove(path); }); } this.buffer = this.buffer.filter(function (_a) { var ref = _a.ref; return selfRef !== ref; }); delete this.nodes[selfPath.toString()]; delete this.refrences[selfPath.toString()]; this.size--; if (selfRef.parent) { selfRef.parent.children.forEach(function (path, index) { if (path.match(selfPath)) { selfRef.parent.children.splice(index, 0); } }); } }; FormGraph.prototype.replace = function (path, node) { var selfPath = shared_1.FormPath.parse(path); var selfRef = this.refrences[selfPath.toString()]; if (!selfRef) return; this.notify({ type: 'GRAPH_NODE_WILL_UNMOUNT', payload: selfRef }); this.nodes[selfPath.toString()] = node; this.notify({ type: 'GRAPH_NODE_DID_MOUNT', payload: selfRef }); }; return FormGraph; }(shared_1.Subscribable)); exports.FormGraph = FormGraph;