UNPKG

1.43 kBJavaScriptView Raw
1import * as is from '../is';
2import Collection from '../collection';
3
4let corefn = ({
5
6 // get a collection
7 // - empty collection on no args
8 // - collection of elements in the graph on selector arg
9 // - guarantee a returned collection when elements or collection specified
10 collection: function( eles, opts ){
11
12 if( is.string( eles ) ){
13 return this.$( eles );
14
15 } else if( is.elementOrCollection( eles ) ){
16 return eles.collection();
17
18 } else if( is.array( eles ) ){
19 if (!opts) {
20 opts = {};
21 }
22 return new Collection( this, eles, opts.unique, opts.removed );
23 }
24
25 return new Collection( this );
26 },
27
28 nodes: function( selector ){
29 let nodes = this.$( function( ele ){
30 return ele.isNode();
31 } );
32
33 if( selector ){
34 return nodes.filter( selector );
35 }
36
37 return nodes;
38 },
39
40 edges: function( selector ){
41 let edges = this.$( function( ele ){
42 return ele.isEdge();
43 } );
44
45 if( selector ){
46 return edges.filter( selector );
47 }
48
49 return edges;
50 },
51
52 // search the graph like jQuery
53 $: function( selector ){
54 let eles = this._private.elements;
55
56 if( selector ){
57 return eles.filter( selector );
58 } else {
59 return eles.spawnSelf();
60 }
61 },
62
63 mutableElements: function(){
64 return this._private.elements;
65 }
66
67});
68
69// aliases
70corefn.elements = corefn.filter = corefn.$;
71
72export default corefn;