UNPKG

3.14 kBJavaScriptView Raw
1import * as is from '../is' ;
2import zIndexSort from './zsort' ;
3import * as util from '../util';
4
5let elesfn = ({
6 forEach: function( fn, thisArg ){
7 if( is.fn( fn ) ){
8 let N = this.length;
9
10 for( let i = 0; i < N; i++ ){
11 let ele = this[ i ];
12 let ret = thisArg ? fn.apply( thisArg, [ ele, i, this ] ) : fn( ele, i, this );
13
14 if( ret === false ){ break; } // exit each early on return false
15 }
16 }
17
18 return this;
19 },
20
21 toArray: function(){
22 let array = [];
23
24 for( let i = 0; i < this.length; i++ ){
25 array.push( this[ i ] );
26 }
27
28 return array;
29 },
30
31 slice: function( start, end ){
32 let array = [];
33 let thisSize = this.length;
34
35 if( end == null ){
36 end = thisSize;
37 }
38
39 if( start == null ){
40 start = 0;
41 }
42
43 if( start < 0 ){
44 start = thisSize + start;
45 }
46
47 if( end < 0 ){
48 end = thisSize + end;
49 }
50
51 for( let i = start; i >= 0 && i < end && i < thisSize; i++ ){
52 array.push( this[ i ] );
53 }
54
55 return this.spawn( array );
56 },
57
58 size: function(){
59 return this.length;
60 },
61
62 eq: function( i ){
63 return this[ i ] || this.spawn();
64 },
65
66 first: function(){
67 return this[0] || this.spawn();
68 },
69
70 last: function(){
71 return this[ this.length - 1 ] || this.spawn();
72 },
73
74 empty: function(){
75 return this.length === 0;
76 },
77
78 nonempty: function(){
79 return !this.empty();
80 },
81
82 sort: function( sortFn ){
83 if( !is.fn( sortFn ) ){
84 return this;
85 }
86
87 let sorted = this.toArray().sort( sortFn );
88
89 return this.spawn( sorted );
90 },
91
92 sortByZIndex: function(){
93 return this.sort( zIndexSort );
94 },
95
96 zDepth: function(){
97 let ele = this[0];
98 if( !ele ){ return undefined; }
99
100 // let cy = ele.cy();
101 let _p = ele._private;
102 let group = _p.group;
103
104 if( group === 'nodes' ){
105 let depth = _p.data.parent ? ele.parents().size() : 0;
106
107 if( !ele.isParent() ){
108 return util.MAX_INT - 1; // childless nodes always on top
109 }
110
111 return depth;
112 } else {
113 let src = _p.source;
114 let tgt = _p.target;
115 let srcDepth = src.zDepth();
116 let tgtDepth = tgt.zDepth();
117
118 return Math.max( srcDepth, tgtDepth, 0 ); // depth of deepest parent
119 }
120 }
121});
122
123elesfn.each = elesfn.forEach;
124
125const defineSymbolIterator = () => {
126 const typeofUndef = typeof undefined;
127 const isIteratorSupported = typeof Symbol != typeofUndef && typeof Symbol.iterator != typeofUndef; // eslint-disable-line no-undef
128
129 if (isIteratorSupported) {
130 elesfn[Symbol.iterator] = function() { // eslint-disable-line no-undef
131 let entry = { value: undefined, done: false };
132 let i = 0;
133 let length = this.length;
134
135 return {
136 next: () => {
137 if ( i < length ) {
138 entry.value = this[i++];
139 } else {
140 entry.value = undefined;
141 entry.done = true;
142 }
143
144 return entry;
145 },
146 [Symbol.iterator]: function() { // eslint-disable-line no-undef
147 return this;
148 }
149 };
150 };
151 }
152};
153defineSymbolIterator();
154
155export default elesfn;