1 | import * as is from '../is';
|
2 | import * as util from '../util';
|
3 | import Collection from '../collection';
|
4 | import Element from '../collection/element';
|
5 |
|
6 | let corefn = {
|
7 | add: function( opts ){
|
8 |
|
9 | let elements;
|
10 | let cy = this;
|
11 |
|
12 |
|
13 | if( is.elementOrCollection( opts ) ){
|
14 | let eles = opts;
|
15 |
|
16 | if( eles._private.cy === cy ){
|
17 | elements = eles.restore();
|
18 |
|
19 | } else {
|
20 | let jsons = [];
|
21 |
|
22 | for( let i = 0; i < eles.length; i++ ){
|
23 | let ele = eles[ i ];
|
24 | jsons.push( ele.json() );
|
25 | }
|
26 |
|
27 | elements = new Collection( cy, jsons );
|
28 | }
|
29 | }
|
30 |
|
31 |
|
32 | else if( is.array( opts ) ){
|
33 | let jsons = opts;
|
34 |
|
35 | elements = new Collection( cy, jsons );
|
36 | }
|
37 |
|
38 |
|
39 | else if( is.plainObject( opts ) && (is.array( opts.nodes ) || is.array( opts.edges )) ){
|
40 | let elesByGroup = opts;
|
41 | let jsons = [];
|
42 |
|
43 | let grs = [ 'nodes', 'edges' ];
|
44 | for( let i = 0, il = grs.length; i < il; i++ ){
|
45 | let group = grs[ i ];
|
46 | let elesArray = elesByGroup[ group ];
|
47 |
|
48 | if( is.array( elesArray ) ){
|
49 |
|
50 | for( let j = 0, jl = elesArray.length; j < jl; j++ ){
|
51 | let json = util.extend( { group: group }, elesArray[ j ] );
|
52 |
|
53 | jsons.push( json );
|
54 | }
|
55 | }
|
56 | }
|
57 |
|
58 | elements = new Collection( cy, jsons );
|
59 | }
|
60 |
|
61 |
|
62 | else {
|
63 | let json = opts;
|
64 | elements = (new Element( cy, json )).collection();
|
65 | }
|
66 |
|
67 | return elements;
|
68 | },
|
69 |
|
70 | remove: function( collection ){
|
71 | if( is.elementOrCollection( collection ) ){
|
72 |
|
73 | } else if( is.string( collection ) ){
|
74 | let selector = collection;
|
75 | collection = this.$( selector );
|
76 | }
|
77 |
|
78 | return collection.remove();
|
79 | }
|
80 | };
|
81 |
|
82 | export default corefn;
|