File size: 2,544 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
let corefn = ({
  notify: function( eventName, eventEles ){
    let _p = this._private;

    if( this.batching() ){
      _p.batchNotifications = _p.batchNotifications || {};

      let eles = _p.batchNotifications[ eventName ] = _p.batchNotifications[ eventName ] || this.collection();

      if( eventEles != null ){
        eles.merge( eventEles );
      }

      return; // notifications are disabled during batching
    }

    if( !_p.notificationsEnabled ){ return; } // exit on disabled

    let renderer = this.renderer();

    // exit if destroy() called on core or renderer in between frames #1499 #1528
    if( this.destroyed() || !renderer ){ return; }

    renderer.notify( eventName, eventEles );
  },

  notifications: function( bool ){
    let p = this._private;

    if( bool === undefined ){
      return p.notificationsEnabled;
    } else {
      p.notificationsEnabled = bool ? true : false;
    }

    return this;
  },

  noNotifications: function( callback ){
    this.notifications( false );
    callback();
    this.notifications( true );
  },

  batching: function(){
    return this._private.batchCount > 0;
  },

  startBatch: function(){
    let _p = this._private;

    if( _p.batchCount == null ){
      _p.batchCount = 0;
    }

    if( _p.batchCount === 0 ){
      _p.batchStyleEles = this.collection();
      _p.batchNotifications = {};
    }

    _p.batchCount++;

    return this;
  },

  endBatch: function(){
    let _p = this._private;

    if( _p.batchCount === 0 ){ return this; }

    _p.batchCount--;

    if( _p.batchCount === 0 ){
      // update style for dirty eles
      _p.batchStyleEles.updateStyle();

      let renderer = this.renderer();

      // notify the renderer of queued eles and event types
      Object.keys( _p.batchNotifications ).forEach( eventName => {
        let eles = _p.batchNotifications[eventName];

        if( eles.empty() ){
          renderer.notify( eventName );
        } else {
          renderer.notify( eventName, eles );
        }
      } );
    }

    return this;
  },

  batch: function( callback ){
    this.startBatch();
    callback();
    this.endBatch();

    return this;
  },

  // for backwards compatibility
  batchData: function( map ){
    let cy = this;

    return this.batch( function(){
      let ids = Object.keys( map );

      for( let i = 0; i < ids.length; i++ ){
        let id = ids[i];
        let data = map[ id ];
        let ele = cy.getElementById( id );

        ele.data( data );
      }
    } );
  }
});

export default corefn;