Breadcrumb.js
5.12 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
define(function(require) {
var graphic = require('../../util/graphic');
var layout = require('../../util/layout');
var zrUtil = require('zrender/core/util');
var TEXT_PADDING = 8;
var ITEM_GAP = 8;
var ARRAY_LENGTH = 5;
function Breadcrumb(containerGroup, onSelect) {
/**
* @private
* @type {module:zrender/container/Group}
*/
this.group = new graphic.Group();
containerGroup.add(this.group);
/**
* @private
* @type {Function}
*/
this._onSelect = onSelect || zrUtil.noop;
}
Breadcrumb.prototype = {
constructor: Breadcrumb,
render: function (seriesModel, api, targetNode) {
var model = seriesModel.getModel('breadcrumb');
var thisGroup = this.group;
thisGroup.removeAll();
if (!model.get('show') || !targetNode) {
return;
}
var normalStyleModel = model.getModel('itemStyle.normal');
// var emphasisStyleModel = model.getModel('itemStyle.emphasis');
var textStyleModel = normalStyleModel.getModel('textStyle');
var layoutParam = {
pos: {
left: model.get('left'),
right: model.get('right'),
top: model.get('top'),
bottom: model.get('bottom')
},
box: {
width: api.getWidth(),
height: api.getHeight()
},
emptyItemWidth: model.get('emptyItemWidth'),
totalWidth: 0,
renderList: []
};
this._prepare(
model, targetNode, layoutParam, textStyleModel
);
this._renderContent(
model, targetNode, layoutParam, normalStyleModel, textStyleModel
);
layout.positionGroup(thisGroup, layoutParam.pos, layoutParam.box);
},
/**
* Prepare render list and total width
* @private
*/
_prepare: function (model, targetNode, layoutParam, textStyleModel) {
for (var node = targetNode; node; node = node.parentNode) {
var text = node.getModel().get('name');
var textRect = textStyleModel.getTextRect(text);
var itemWidth = Math.max(
textRect.width + TEXT_PADDING * 2,
layoutParam.emptyItemWidth
);
layoutParam.totalWidth += itemWidth + ITEM_GAP;
layoutParam.renderList.push({node: node, text: text, width: itemWidth});
}
},
/**
* @private
*/
_renderContent: function (
model, targetNode, layoutParam, normalStyleModel, textStyleModel
) {
// Start rendering.
var lastX = 0;
var emptyItemWidth = layoutParam.emptyItemWidth;
var height = model.get('height');
var availableSize = layout.getAvailableSize(layoutParam.pos, layoutParam.box);
var totalWidth = layoutParam.totalWidth;
var renderList = layoutParam.renderList;
for (var i = renderList.length - 1; i >= 0; i--) {
var item = renderList[i];
var itemWidth = item.width;
var text = item.text;
// Hdie text and shorten width if necessary.
if (totalWidth > availableSize.width) {
totalWidth -= itemWidth - emptyItemWidth;
itemWidth = emptyItemWidth;
text = '';
}
this.group.add(new graphic.Polygon({
shape: {
points: makeItemPoints(
lastX, 0, itemWidth, height,
i === renderList.length - 1, i === 0
)
},
style: zrUtil.defaults(
normalStyleModel.getItemStyle(),
{
lineJoin: 'bevel',
text: text,
textFill: textStyleModel.getTextColor(),
textFont: textStyleModel.getFont()
}
),
z: 10,
onclick: zrUtil.bind(this._onSelect, this, item.node)
}));
lastX += itemWidth + ITEM_GAP;
}
},
/**
* @override
*/
remove: function () {
this.group.removeAll();
}
};
function makeItemPoints(x, y, itemWidth, itemHeight, head, tail) {
var points = [
[head ? x : x - ARRAY_LENGTH, y],
[x + itemWidth, y],
[x + itemWidth, y + itemHeight],
[head ? x : x - ARRAY_LENGTH, y + itemHeight]
];
!tail && points.splice(2, 0, [x + itemWidth + ARRAY_LENGTH, y + itemHeight / 2]);
!head && points.push([x, y + itemHeight / 2]);
return points;
}
return Breadcrumb;
});