@antv/g6
Version:
graph visualization frame work
88 lines (79 loc) • 2.03 kB
JavaScript
/**
* @fileOverview extend G.Group
* @author huangtonger@aliyun.com
* @ignore
*/
var G = require('@antv/g');
var Util = require('../../util/');
var Mixin = function Mixin() {};
Util.augment(Mixin, {
/**
* find element by className
* @param {String} className class name
* @return {Array} rst
*/
findByClass: function findByClass(className) {
var rst = [];
this.deepEach(function (child) {
if (child.hasClass(className)) {
rst.push(child);
}
});
return rst;
},
/**
* Check contains the specified class
* @param {String} className class name
* @return {Boolean} boolean
*/
hasClass: function hasClass(className) {
var clasees = this.get('class');
if (clasees && clasees.indexOf(className) !== -1) {
return true;
}
return false;
},
/**
* traverse child node
* @param {function} callback callback
* @param {boolean} runSelf excute self or not
*/
deepEach: function deepEach(callback, runSelf) {
Util.traverseTree(this, callback, function (parent) {
return parent.get('children');
}, runSelf);
},
/**
* radix sort (a stable sort)
*/
sort: function sort() {
var children = this.get('children');
this.set('children', Util.radixSort(children, function (child) {
return child.get('zIndex');
}));
},
/**
* sort by callback
* @param {function} callback callback
*/
sortBy: function sortBy(callback) {
var children = this.get('children');
this.set('children', Util.radixSort(children, callback));
},
/**
* clear inner elements
* @param {boolean} bool if destroy child
* @return {object} this
*/
clear: function clear(bool) {
var children = this.get('children');
bool = bool !== false;
while (children.length !== 0) {
children[children.length - 1].remove(bool);
}
return this;
}
});
Util.mixin(G.canvas.Group, [Mixin]);
Util.mixin(G.svg.Group, [Mixin]);
module.exports = Mixin;