UNPKG

2.01 kBJavaScriptView Raw
1/**
2 * Elements are drawn in a specific order based on compound depth (low to high), the element type (nodes above edges),
3 * and z-index (low to high). These styles affect how this applies:
4 *
5 * z-compound-depth: May be `bottom | orphan | auto | top`. The first drawn is `bottom`, then `orphan` which is the
6 * same depth as the root of the compound graph, followed by the default value `auto` which draws in order from
7 * root to leaves of the compound graph. The last drawn is `top`.
8 * z-index-compare: May be `auto | manual`. The default value is `auto` which always draws edges under nodes.
9 * `manual` ignores this convention and draws based on the `z-index` value setting.
10 * z-index: An integer value that affects the relative draw order of elements. In general, an element with a higher
11 * `z-index` will be drawn on top of an element with a lower `z-index`.
12 */
13import * as util from '../util';
14
15let zIndexSort = function( a, b ){
16 let cy = a.cy();
17 let hasCompoundNodes = cy.hasCompoundNodes();
18
19 function getDepth(ele){
20 let style = ele.pstyle( 'z-compound-depth' );
21 if ( style.value === 'auto' ){
22 return hasCompoundNodes ? ele.zDepth() : 0;
23 } else if ( style.value === 'bottom' ){
24 return -1;
25 } else if ( style.value === 'top' ){
26 return util.MAX_INT;
27 }
28 // 'orphan'
29 return 0;
30 }
31 let depthDiff = getDepth(a) - getDepth(b);
32 if ( depthDiff !== 0 ){
33 return depthDiff;
34 }
35
36 function getEleDepth(ele){
37 let style = ele.pstyle( 'z-index-compare' );
38 if ( style.value === 'auto' ){
39 return ele.isNode() ? 1 : 0;
40 }
41 // 'manual'
42 return 0;
43 }
44 let eleDiff = getEleDepth(a) - getEleDepth(b);
45 if ( eleDiff !== 0 ){
46 return eleDiff;
47 }
48
49 let zDiff = a.pstyle( 'z-index' ).value - b.pstyle( 'z-index' ).value;
50 if ( zDiff !== 0 ){
51 return zDiff;
52 }
53 // compare indices in the core (order added to graph w/ last on top)
54 return a.poolIndex() - b.poolIndex();
55};
56
57export default zIndexSort;