Skip to content
This repository was archived by the owner on Nov 22, 2021. It is now read-only.

Commit 519b9b7

Browse files
author
Ke, Mingze
committed
Added filter by tags
1 parent a226e4c commit 519b9b7

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

code.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ Code.getPage = function () {
8282
return page;
8383
};
8484

85+
Code.getTags = function () {
86+
var tags = Code.getStringParamFromUrl('tags', '').trim();
87+
if (!tags.length) {
88+
tags = '*';
89+
}
90+
tags = tags.split(/\s*,\s*/);
91+
return tags;
92+
};
93+
8594
Code.getDemoPage = function () {
8695
var area = document.querySelector('#demo-area.show');
8796
if (area) {
@@ -635,6 +644,44 @@ Code.renderContent = function() {
635644
}
636645
};
637646

647+
Code.filterXML = function (toolboxXML, property, values) {
648+
var categories = slice.call(toolboxXML.querySelectorAll('category'));
649+
categories.forEach(function (cate) {
650+
if (cate.getAttribute(property) !== null) {
651+
filterTag(cate);
652+
}
653+
});
654+
655+
var blocks = slice.call(toolboxXML.querySelectorAll('block'));
656+
blocks.forEach(function (block) {
657+
if (block.getAttribute(property) !== null) {
658+
filterTag(block);
659+
}
660+
});
661+
662+
function filterTag(node) {
663+
var vals = node.getAttribute(property).trim().split(/\s*,\s*/);
664+
vals = vals.filter(function (tag) {
665+
return values.indexOf('*') !== -1 || values.indexOf(tag) !== -1;
666+
});
667+
if (!vals.length) {
668+
Code.pruneNode(node);
669+
}
670+
}
671+
672+
return toolboxXML;
673+
};
674+
675+
Code.pruneNode = function (node) {
676+
if (node && node.parentElement) {
677+
var parent = node.parentElement;
678+
parent.removeChild(node);
679+
if (!parent.children.length) {
680+
Code.pruneNode(parent);
681+
}
682+
}
683+
};
684+
638685
Code.getToolBox = function (toolboxXML) {
639686
var categories = slice.call(toolboxXML.querySelectorAll('category')).map(function (e) {
640687
return e.id;
@@ -1401,7 +1448,7 @@ Promise.all([
14011448
})
14021449
]).then(function (values) {
14031450
Code.renderPage(values[0].body.innerHTML);
1404-
Code.init(Code.getToolBox(values[1].body.firstChild));
1451+
Code.init(Code.getToolBox(Code.filterXML(values[1].body.firstChild, 'tags', Code.getTags())));
14051452
Code.loadDemoArea();
14061453
Code.loadGa();
14071454
Code.ga();

0 commit comments

Comments
 (0)