File size: 1,940 Bytes
bc20498 |
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 |
import * as is from '../is';
import * as util from '../util';
import Collection from '../collection';
import Element from '../collection/element';
let corefn = {
add: function( opts ){
let elements;
let cy = this;
// add the elements
if( is.elementOrCollection( opts ) ){
let eles = opts;
if( eles._private.cy === cy ){ // same instance => just restore
elements = eles.restore();
} else { // otherwise, copy from json
let jsons = [];
for( let i = 0; i < eles.length; i++ ){
let ele = eles[ i ];
jsons.push( ele.json() );
}
elements = new Collection( cy, jsons );
}
}
// specify an array of options
else if( is.array( opts ) ){
let jsons = opts;
elements = new Collection( cy, jsons );
}
// specify via opts.nodes and opts.edges
else if( is.plainObject( opts ) && (is.array( opts.nodes ) || is.array( opts.edges )) ){
let elesByGroup = opts;
let jsons = [];
let grs = [ 'nodes', 'edges' ];
for( let i = 0, il = grs.length; i < il; i++ ){
let group = grs[ i ];
let elesArray = elesByGroup[ group ];
if( is.array( elesArray ) ){
for( let j = 0, jl = elesArray.length; j < jl; j++ ){
let json = util.extend( { group: group }, elesArray[ j ] );
jsons.push( json );
}
}
}
elements = new Collection( cy, jsons );
}
// specify options for one element
else {
let json = opts;
elements = (new Element( cy, json )).collection();
}
return elements;
},
remove: function( collection ){
if( is.elementOrCollection( collection ) ){
// already have right ref
} else if( is.string( collection ) ){
let selector = collection;
collection = this.$( selector );
}
return collection.remove();
}
};
export default corefn;
|