-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.php
141 lines (118 loc) · 3.58 KB
/
Node.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* 节点类
*/
class Node {
/**
* 节点编号
*/
public $id;
/**
* 节点内容
*/
public $text;
/**
* 父节点编号
*/
public $parentId;
/**
* 节点权值
*/
public $weight;
/**
* 父节点引用
*/
public $parentNode;
/**
* 是否可见,默认为true
*/
public $visible = true;
/**
* 孩子节点列表
*/
private $children = null;
/**
* Node constructor.
*/
public function __construct() {
$this->children = new Children();
}
// 先序遍历,拼接JSON字符串
public function toString() {
if ($this->visible) {
$result = '{id : ' . $this->id . ', text : ' . $this->text;
if ($this->children != null && $this->children->getSize() != 0) {
$result .= ', children : ' . $this->children->toString();
} else {
$result .= ', leaf : true';
}
return $result . '}';
} else {
return '';
}
}
// 兄弟节点横向排序
public function sortChildren() {
if ($this->children != null && $this->children->getSize() != 0) {
$this->children->sortChildren();
}
}
// 添加孩子节点
public function addChild($node) {
$this->children->addChild($node);
}
// 先序遍历,构造功能叶子列表
public function initializeLeafList(&$leafList) {
if ($this->children->getSize() == 0) {
$leafList[] = $this;
} else {
$this->children->initializeLeafList($leafList);
}
}
// 先序遍历,设置该节点下的所有功能路径为不可见
public function setTreeNotVisible() {
$this->visible = false;
if ($this->children != null && $this->children->getSize() != 0) {
$this->children->setTreeNotVisible();
}
}
// 先序遍历,设置该节点下的所有功能路径为可见
public function searchTreeNode($keyWord) {
if (strpos($this->text, $keyWord) > -1) {
$this->setTreeVisible();
$this->setRouteVisible();
} else {
if ($this->children != null && $this->children->getSize() != 0) {
$this->children->searchTreeNode($keyWord);
}
}
}
// 设置包含该叶子节点的功能路径可见
public function setTreeVisible() {
$this->visible = true;
if ($this->children != null && $this->children->getSize() != 0) {
$this->children->setTreeVisible();
}
}
// 先序遍历,搜索菜单节点,同时进行功能路径过滤
public function setRouteVisible() {
$this->visible = true;
for ($parentNode = $this->parentNode; $parentNode != null; $parentNode = $parentNode->parentNode) {
$parentNode->visible = true;
}
}
public function increaseRouteWeight() {
$this->weight++;
$this->updateNodeWeightToDB($this);
for ($parentNode = $this->parentNode; $parentNode != null; $parentNode = $parentNode->parentNode) {
$parentNode->weight++;
$this->updateNodeWeightToDB($parentNode);
}
}
// 更新节点权值到数据库
public function updateNodeWeightToDB(Node $node) {
// 暂时不实现,实际应用中需要实现该方法
// 或者用户退出系统时,遍历整棵树,统一更新所有节点的权值到数据库中,应该这样做比较好,一次性统一处理
return true;
}
}