|
|
|
|
@ -34,7 +34,19 @@ |
|
|
|
|
|
|
|
|
|
HierarchyPanel.prototype.setSelectedUuid = function (uuid) { |
|
|
|
|
this._selectedUuid = uuid || ''; |
|
|
|
|
var ok = this._expandToUuid(this._selectedUuid); |
|
|
|
|
if (!ok && this._bridge && this._selectedUuid) { |
|
|
|
|
// 树里还没有该节点时拉一份最新层级
|
|
|
|
|
var self = this; |
|
|
|
|
this._bridge.call('hier.getTree').then(function (t) { |
|
|
|
|
self.applyTree(t); |
|
|
|
|
}).catch(function () { |
|
|
|
|
self._render(); |
|
|
|
|
}); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
this._render(); |
|
|
|
|
this._scrollToSelected(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
HierarchyPanel.prototype.applyTree = function (data) { |
|
|
|
|
@ -46,8 +58,35 @@ |
|
|
|
|
this._expanded[key] = true; |
|
|
|
|
this._defaultInited = true; |
|
|
|
|
} |
|
|
|
|
if (this._selectedUuid) this._expandToUuid(this._selectedUuid); |
|
|
|
|
} |
|
|
|
|
this._render(); |
|
|
|
|
if (this._selectedUuid) this._scrollToSelected(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** 展开选中节点的全部祖先,使其在树中可见 @returns {boolean} 是否找到 */ |
|
|
|
|
HierarchyPanel.prototype._expandToUuid = function (uuid) { |
|
|
|
|
if (!uuid || !this._tree || !this._tree.root) return false; |
|
|
|
|
var path = []; |
|
|
|
|
var found = findPath(this._tree.root, uuid, path); |
|
|
|
|
if (!found) return false; |
|
|
|
|
for (var i = 0; i < path.length - 1; i++) { |
|
|
|
|
var key = path[i].uuid || path[i].name || ''; |
|
|
|
|
if (key) this._expanded[key] = true; |
|
|
|
|
} |
|
|
|
|
// 根节点也展开
|
|
|
|
|
var rootKey = this._tree.root.uuid || this._tree.root.name || 'scene'; |
|
|
|
|
this._expanded[rootKey] = true; |
|
|
|
|
return true; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** 把选中项滚入可视区 */ |
|
|
|
|
HierarchyPanel.prototype._scrollToSelected = function () { |
|
|
|
|
if (!this.treeEl || !this._selectedUuid) return; |
|
|
|
|
var el = this.treeEl.querySelector('.pw-tree-item.selected'); |
|
|
|
|
if (el && typeof el.scrollIntoView === 'function') { |
|
|
|
|
el.scrollIntoView({ block: 'nearest' }); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
HierarchyPanel.prototype.mount = function () { |
|
|
|
|
@ -195,6 +234,19 @@ |
|
|
|
|
return false; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** DFS 找 uuid 路径,path 收集祖先+自身 */ |
|
|
|
|
function findPath(node, uuid, path) { |
|
|
|
|
if (!node) return false; |
|
|
|
|
path.push(node); |
|
|
|
|
if (node.uuid === uuid) return true; |
|
|
|
|
var children = node.children || []; |
|
|
|
|
for (var i = 0; i < children.length; i++) { |
|
|
|
|
if (findPath(children[i], uuid, path)) return true; |
|
|
|
|
} |
|
|
|
|
path.pop(); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function hasName(list, name) { |
|
|
|
|
for (var i = 0; i < list.length; i++) if (list[i] === name) return true; |
|
|
|
|
return false; |
|
|
|
|
|