@@ -21,7 +21,6 @@ import {
21
21
debounce ,
22
22
omit ,
23
23
result ,
24
- merge ,
25
24
camelCase ,
26
25
cloneDeep ,
27
26
invoke ,
@@ -46,6 +45,7 @@ import * as connectionPoints from '../connectionPoints/index.mjs';
46
45
import * as anchors from '../anchors/index.mjs' ;
47
46
48
47
import $ from '../mvc/Dom/index.mjs' ;
48
+ import { GridLayer } from './layers/GridLayer.mjs' ;
49
49
50
50
const sortingTypes = {
51
51
NONE : 'sorting-none' ,
@@ -83,6 +83,8 @@ const defaultHighlighting = {
83
83
} ;
84
84
85
85
const defaultLayers = [ {
86
+ name : LayersNames . GRID ,
87
+ } , {
86
88
name : LayersNames . BACK ,
87
89
} , {
88
90
name : LayersNames . CELLS ,
@@ -395,7 +397,6 @@ export const Paper = View.extend({
395
397
// Layers (SVGGroups)
396
398
this . _layers = { } ;
397
399
398
- this . setGrid ( options . drawGrid ) ;
399
400
this . cloneOptions ( ) ;
400
401
this . render ( ) ;
401
402
this . _setDimensions ( ) ;
@@ -556,15 +557,6 @@ export const Paper = View.extend({
556
557
position : 'absolute' ,
557
558
inset : 0
558
559
}
559
- } , {
560
- namespaceURI : ns . xhtml ,
561
- tagName : 'div' ,
562
- className : addClassNamePrefix ( 'paper-grid' ) ,
563
- selector : 'grid' ,
564
- style : {
565
- position : 'absolute' ,
566
- inset : 0
567
- }
568
560
} , {
569
561
namespaceURI : ns . svg ,
570
562
tagName : 'svg' ,
@@ -629,7 +621,7 @@ export const Paper = View.extend({
629
621
}
630
622
631
623
if ( options . drawGrid ) {
632
- this . drawGrid ( ) ;
624
+ this . setGrid ( options . drawGrid ) ;
633
625
}
634
626
635
627
return this ;
@@ -640,11 +632,20 @@ export const Paper = View.extend({
640
632
V ( this . svg ) . prepend ( V . createSVGStyle ( css ) ) ;
641
633
} ,
642
634
635
+ createLayer ( name ) {
636
+ switch ( name ) {
637
+ case LayersNames . GRID :
638
+ return new GridLayer ( { name, paper : this , patterns : this . constructor . gridPatterns } ) ;
639
+ default :
640
+ return new PaperLayer ( { name } ) ;
641
+ }
642
+ } ,
643
+
643
644
renderLayers : function ( layers = defaultLayers ) {
644
645
this . removeLayers ( ) ;
645
646
// TODO: Layers to be read from the graph `layers` attribute
646
647
layers . forEach ( ( { name, sorted } ) => {
647
- const layerView = new PaperLayer ( { name } ) ;
648
+ const layerView = this . createLayer ( name ) ;
648
649
this . layers . appendChild ( layerView . el ) ;
649
650
this . _layers [ name ] = layerView ;
650
651
} ) ;
@@ -681,10 +682,6 @@ export const Paper = View.extend({
681
682
682
683
update : function ( ) {
683
684
684
- if ( this . options . drawGrid ) {
685
- this . drawGrid ( ) ;
686
- }
687
-
688
685
if ( this . _background ) {
689
686
this . updateBackgroundImage ( this . _background ) ;
690
687
}
@@ -1810,7 +1807,7 @@ export const Paper = View.extend({
1810
1807
}
1811
1808
1812
1809
const { options } = this ;
1813
- const { origin, drawGrid } = options ;
1810
+ const { origin } = options ;
1814
1811
1815
1812
// setter
1816
1813
tx || ( tx = 0 ) ;
@@ -1828,11 +1825,6 @@ export const Paper = View.extend({
1828
1825
origin . y = oy ;
1829
1826
1830
1827
this . trigger ( 'translate' , ox , oy ) ;
1831
-
1832
- if ( drawGrid ) {
1833
- this . drawGrid ( ) ;
1834
- }
1835
-
1836
1828
return this ;
1837
1829
} ,
1838
1830
@@ -2639,155 +2631,17 @@ export const Paper = View.extend({
2639
2631
} ,
2640
2632
2641
2633
setGridSize : function ( gridSize ) {
2642
-
2643
2634
const { options } = this ;
2644
2635
options . gridSize = gridSize ;
2645
-
2646
2636
if ( options . drawGrid && ! options . drawGridSize ) {
2647
2637
// Do not redraw the grid if the `drawGridSize` is set.
2648
- this . drawGrid ( ) ;
2649
- }
2650
-
2651
- return this ;
2652
- } ,
2653
-
2654
- clearGrid : function ( ) {
2655
-
2656
- const { childNodes } = this ;
2657
- if ( childNodes && childNodes . grid ) {
2658
- childNodes . grid . style . backgroundImage = '' ;
2638
+ this . getLayerView ( LayersNames . GRID ) . renderGrid ( ) ;
2659
2639
}
2660
2640
return this ;
2661
2641
} ,
2662
2642
2663
- _getGridRefs : function ( ) {
2664
-
2665
- if ( ! this . _gridCache ) {
2666
-
2667
- this . _gridCache = {
2668
- root : V ( 'svg' , { width : '100%' , height : '100%' } , V ( 'defs' ) ) ,
2669
- patterns : { } ,
2670
- add : function ( id , vel ) {
2671
- V ( this . root . node . childNodes [ 0 ] ) . append ( vel ) ;
2672
- this . patterns [ id ] = vel ;
2673
- this . root . append ( V ( 'rect' , { width : '100%' , height : '100%' , fill : 'url(#' + id + ')' } ) ) ;
2674
- } ,
2675
- get : function ( id ) {
2676
- return this . patterns [ id ] ;
2677
- } ,
2678
- exist : function ( id ) {
2679
- return this . patterns [ id ] !== undefined ;
2680
- }
2681
- } ;
2682
- }
2683
-
2684
- return this . _gridCache ;
2685
- } ,
2686
-
2687
2643
setGrid : function ( drawGrid ) {
2688
-
2689
- this . clearGrid ( ) ;
2690
-
2691
- this . _gridCache = null ;
2692
- this . _gridSettings = [ ] ;
2693
-
2694
- var optionsList = Array . isArray ( drawGrid ) ? drawGrid : [ drawGrid || { } ] ;
2695
- optionsList . forEach ( function ( item ) {
2696
- this . _gridSettings . push . apply ( this . _gridSettings , this . _resolveDrawGridOption ( item ) ) ;
2697
- } , this ) ;
2698
- return this ;
2699
- } ,
2700
-
2701
- _resolveDrawGridOption : function ( opt ) {
2702
-
2703
- var namespace = this . constructor . gridPatterns ;
2704
- if ( isString ( opt ) && Array . isArray ( namespace [ opt ] ) ) {
2705
- return namespace [ opt ] . map ( function ( item ) {
2706
- return assign ( { } , item ) ;
2707
- } ) ;
2708
- }
2709
-
2710
- var options = opt || { args : [ { } ] } ;
2711
- var isArray = Array . isArray ( options ) ;
2712
- var name = options . name ;
2713
-
2714
- if ( ! isArray && ! name && ! options . markup ) {
2715
- name = 'dot' ;
2716
- }
2717
-
2718
- if ( name && Array . isArray ( namespace [ name ] ) ) {
2719
- var pattern = namespace [ name ] . map ( function ( item ) {
2720
- return assign ( { } , item ) ;
2721
- } ) ;
2722
-
2723
- var args = Array . isArray ( options . args ) ? options . args : [ options . args || { } ] ;
2724
-
2725
- defaults ( args [ 0 ] , omit ( opt , 'args' ) ) ;
2726
- for ( var i = 0 ; i < args . length ; i ++ ) {
2727
- if ( pattern [ i ] ) {
2728
- assign ( pattern [ i ] , args [ i ] ) ;
2729
- }
2730
- }
2731
- return pattern ;
2732
- }
2733
-
2734
- return isArray ? options : [ options ] ;
2735
- } ,
2736
-
2737
- drawGrid : function ( opt ) {
2738
-
2739
- const gridSize = this . options . drawGridSize || this . options . gridSize ;
2740
- if ( gridSize <= 1 ) {
2741
- return this . clearGrid ( ) ;
2742
- }
2743
-
2744
- var localOptions = Array . isArray ( opt ) ? opt : [ opt ] ;
2745
-
2746
- var ctm = this . matrix ( ) ;
2747
- var refs = this . _getGridRefs ( ) ;
2748
-
2749
- this . _gridSettings . forEach ( function ( gridLayerSetting , index ) {
2750
-
2751
- var id = 'pattern_' + index ;
2752
- var options = merge ( gridLayerSetting , localOptions [ index ] , {
2753
- sx : ctm . a || 1 ,
2754
- sy : ctm . d || 1 ,
2755
- ox : ctm . e || 0 ,
2756
- oy : ctm . f || 0
2757
- } ) ;
2758
-
2759
- options . width = gridSize * ( ctm . a || 1 ) * ( options . scaleFactor || 1 ) ;
2760
- options . height = gridSize * ( ctm . d || 1 ) * ( options . scaleFactor || 1 ) ;
2761
-
2762
- if ( ! refs . exist ( id ) ) {
2763
- refs . add ( id , V ( 'pattern' , { id : id , patternUnits : 'userSpaceOnUse' } , V ( options . markup ) ) ) ;
2764
- }
2765
-
2766
- var patternDefVel = refs . get ( id ) ;
2767
-
2768
- if ( isFunction ( options . update ) ) {
2769
- options . update ( patternDefVel . node . childNodes [ 0 ] , options ) ;
2770
- }
2771
-
2772
- var x = options . ox % options . width ;
2773
- if ( x < 0 ) x += options . width ;
2774
-
2775
- var y = options . oy % options . height ;
2776
- if ( y < 0 ) y += options . height ;
2777
-
2778
- patternDefVel . attr ( {
2779
- x : x ,
2780
- y : y ,
2781
- width : options . width ,
2782
- height : options . height
2783
- } ) ;
2784
- } ) ;
2785
-
2786
- var patternUri = new XMLSerializer ( ) . serializeToString ( refs . root . node ) ;
2787
- patternUri = 'url(data:image/svg+xml;base64,' + btoa ( patternUri ) + ')' ;
2788
-
2789
- this . childNodes . grid . style . backgroundImage = patternUri ;
2790
-
2644
+ this . getLayerView ( LayersNames . GRID ) . setGrid ( drawGrid ) ;
2791
2645
return this ;
2792
2646
} ,
2793
2647
@@ -3250,10 +3104,10 @@ export const Paper = View.extend({
3250
3104
color : '#AAAAAA' ,
3251
3105
thickness : 1 ,
3252
3106
markup : 'rect' ,
3253
- update : function ( el , opt ) {
3107
+ render : function ( el , opt ) {
3254
3108
V ( el ) . attr ( {
3255
- width : opt . thickness * opt . sx ,
3256
- height : opt . thickness * opt . sy ,
3109
+ width : opt . thickness ,
3110
+ height : opt . thickness ,
3257
3111
fill : opt . color
3258
3112
} ) ;
3259
3113
}
@@ -3262,16 +3116,21 @@ export const Paper = View.extend({
3262
3116
color : '#AAAAAA' ,
3263
3117
thickness : 1 ,
3264
3118
markup : 'rect' ,
3265
- update : function ( el , opt ) {
3266
- var size = opt . sx <= 1 ? opt . thickness * opt . sx : opt . thickness ;
3267
- V ( el ) . attr ( { width : size , height : size , fill : opt . color } ) ;
3119
+ render : function ( el , opt ) {
3120
+ V ( el ) . attr ( { fill : opt . color } ) ;
3121
+ } ,
3122
+ update : function ( el , opt , paper ) {
3123
+ const { sx, sy } = paper . scale ( ) ;
3124
+ const width = sx <= 1 ? opt . thickness : opt . thickness / sx ;
3125
+ const height = sy <= 1 ? opt . thickness : opt . thickness / sy ;
3126
+ V ( el ) . attr ( { width, height } ) ;
3268
3127
}
3269
3128
} ] ,
3270
3129
mesh : [ {
3271
3130
color : '#AAAAAA' ,
3272
3131
thickness : 1 ,
3273
3132
markup : 'path' ,
3274
- update : function ( el , opt ) {
3133
+ render : function ( el , opt ) {
3275
3134
3276
3135
var d ;
3277
3136
var width = opt . width ;
@@ -3291,7 +3150,7 @@ export const Paper = View.extend({
3291
3150
color : '#AAAAAA' ,
3292
3151
thickness : 1 ,
3293
3152
markup : 'path' ,
3294
- update : function ( el , opt ) {
3153
+ render : function ( el , opt ) {
3295
3154
3296
3155
var d ;
3297
3156
var width = opt . width ;
@@ -3311,7 +3170,7 @@ export const Paper = View.extend({
3311
3170
thickness : 3 ,
3312
3171
scaleFactor : 4 ,
3313
3172
markup : 'path' ,
3314
- update : function ( el , opt ) {
3173
+ render : function ( el , opt ) {
3315
3174
3316
3175
var d ;
3317
3176
var width = opt . width ;
0 commit comments