UNPKG

2.54 kBJavaScriptView Raw
1let corefn = ({
2 notify: function( eventName, eventEles ){
3 let _p = this._private;
4
5 if( this.batching() ){
6 _p.batchNotifications = _p.batchNotifications || {};
7
8 let eles = _p.batchNotifications[ eventName ] = _p.batchNotifications[ eventName ] || this.collection();
9
10 if( eventEles != null ){
11 eles.merge( eventEles );
12 }
13
14 return; // notifications are disabled during batching
15 }
16
17 if( !_p.notificationsEnabled ){ return; } // exit on disabled
18
19 let renderer = this.renderer();
20
21 // exit if destroy() called on core or renderer in between frames #1499 #1528
22 if( this.destroyed() || !renderer ){ return; }
23
24 renderer.notify( eventName, eventEles );
25 },
26
27 notifications: function( bool ){
28 let p = this._private;
29
30 if( bool === undefined ){
31 return p.notificationsEnabled;
32 } else {
33 p.notificationsEnabled = bool ? true : false;
34 }
35
36 return this;
37 },
38
39 noNotifications: function( callback ){
40 this.notifications( false );
41 callback();
42 this.notifications( true );
43 },
44
45 batching: function(){
46 return this._private.batchCount > 0;
47 },
48
49 startBatch: function(){
50 let _p = this._private;
51
52 if( _p.batchCount == null ){
53 _p.batchCount = 0;
54 }
55
56 if( _p.batchCount === 0 ){
57 _p.batchStyleEles = this.collection();
58 _p.batchNotifications = {};
59 }
60
61 _p.batchCount++;
62
63 return this;
64 },
65
66 endBatch: function(){
67 let _p = this._private;
68
69 if( _p.batchCount === 0 ){ return this; }
70
71 _p.batchCount--;
72
73 if( _p.batchCount === 0 ){
74 // update style for dirty eles
75 _p.batchStyleEles.updateStyle();
76
77 let renderer = this.renderer();
78
79 // notify the renderer of queued eles and event types
80 Object.keys( _p.batchNotifications ).forEach( eventName => {
81 let eles = _p.batchNotifications[eventName];
82
83 if( eles.empty() ){
84 renderer.notify( eventName );
85 } else {
86 renderer.notify( eventName, eles );
87 }
88 } );
89 }
90
91 return this;
92 },
93
94 batch: function( callback ){
95 this.startBatch();
96 callback();
97 this.endBatch();
98
99 return this;
100 },
101
102 // for backwards compatibility
103 batchData: function( map ){
104 let cy = this;
105
106 return this.batch( function(){
107 let ids = Object.keys( map );
108
109 for( let i = 0; i < ids.length; i++ ){
110 let id = ids[i];
111 let data = map[ id ];
112 let ele = cy.getElementById( id );
113
114 ele.data( data );
115 }
116 } );
117 }
118});
119
120export default corefn;