Skip to content

Commit 191a957

Browse files
committed
Merge pull request #300 from Exodia/crumb-click
add crumb click
2 parents cca4d27 + 62b34ed commit 191a957

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/Crumb.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,35 @@ define(
8989
};
9090

9191

92+
Crumb.prototype.initEvents = function () {
93+
this.helper.addDOMEvent(this.main, 'click', click);
94+
};
95+
96+
function click(e) {
97+
var node = e.target;
98+
var children = lib.getChildren(this.main);
99+
while (node !== this.main) {
100+
if (this.helper.isPart(node, 'node')) {
101+
var index = lib.hasAttribute(node, 'data-index') ? node.getAttribute('data-index')
102+
: getPathIndex(children, node);
103+
var event = this.fire('click', { item: this.path[index] });
104+
event.isDefaultPrevented() && e.preventDefault();
105+
return;
106+
}
107+
108+
node = node.parentNode;
109+
}
110+
}
111+
112+
function getPathIndex(children, node) {
113+
for (var i = children.length - 1; i > -1; i -= 2) {
114+
if (children[i] === node) {
115+
// separator 的插入使得索引要除个2
116+
return i / 2;
117+
}
118+
}
119+
}
120+
92121
/**
93122
* 无链接的文字节点的内容HTML模板
94123
*
@@ -99,7 +128,7 @@ define(
99128
* @type {string}
100129
*/
101130
Crumb.prototype.textNodeTemplate =
102-
'<span class="${classes}">${text}</span>';
131+
'<span class="${classes}" data-index="${index}">${text}</span>';
103132

104133
/**
105134
* 链接节点的内容HTML模板
@@ -112,7 +141,7 @@ define(
112141
* @type {string}
113142
*/
114143
Crumb.prototype.linkNodeTemplate =
115-
'<a class="${classes}" href="${href}">${text}</a>';
144+
'<a class="${classes}" href="${href}" data-index="${index}">${text}</a>';
116145

117146
/**
118147
* 分隔符HTML模板
@@ -155,6 +184,7 @@ define(
155184
var data = {
156185
href: u.escape(node.href),
157186
text: u.escape(node.text),
187+
index: index,
158188
classes: classes.join(' ')
159189
};
160190
return lib.format(template, data);

0 commit comments

Comments
 (0)