13693261870
2022-09-16 354b3dbfbffb3df45212a2a44dbbf48b4acc2594
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='Ext-container-Monitor'>/**
</span> * This is a utility class for being able to track all items of a particular type
 * inside any level at a container. This can be used in favour of bubbling add/remove events
 * which can add a large perf cost when implemented globally
 * @private
 */
Ext.define('Ext.container.Monitor', {
<span id='Ext-container-Monitor-property-target'>    target: null,
</span><span id='Ext-container-Monitor-property-selector'>    selector: '',
</span>    
<span id='Ext-container-Monitor-property-scope'>    scope: null,
</span><span id='Ext-container-Monitor-property-addHandler'>    addHandler: null,
</span><span id='Ext-container-Monitor-property-removeHandler'>    removeHandler: null,
</span>    
<span id='Ext-container-Monitor-property-disabled'>    disabled: 0,
</span>    
<span id='Ext-container-Monitor-method-constructor'>    constructor: function(config){
</span>        Ext.apply(this, config);
    },
    
<span id='Ext-container-Monitor-method-bind'>    bind: function(target){
</span>        var me = this;
        
        me.target = target;
        target.on('beforedestroy', me.disable, me);
        me.onContainerAdd(target);
    },
    
<span id='Ext-container-Monitor-method-unbind'>    unbind: function() {
</span>        var me = this,
            target = me.target;
            
        if (target) {
            target.un('beforedestroy', me.disable, me);
        }
        me.items = null;
    },
    
<span id='Ext-container-Monitor-method-disable'>    disable: function(){
</span>        ++this.disabled;    
    },
    
<span id='Ext-container-Monitor-method-enable'>    enable: function(){
</span>        if (this.disabled &gt; 0) {
            --this.disabled;
        }
    },
    
<span id='Ext-container-Monitor-method-handleAdd'>    handleAdd: function(ct, comp) {
</span>        if (!this.disabled) {
            if (comp.is(this.selector)) {
                this.onItemAdd(comp.ownerCt, comp);
            }
        
            if (comp.isQueryable) {
                this.onContainerAdd(comp);
            }
        }
    },
    
<span id='Ext-container-Monitor-method-onItemAdd'>    onItemAdd: function(ct, comp){
</span>        var me = this,
            items = me.items,
            handler = me.addHandler;
            
        if (!me.disabled) {
            if (handler) {
                handler.call(me.scope || comp, comp);
            }
            if (items) {
                items.add(comp);
            }
        }
    },
    
<span id='Ext-container-Monitor-method-onItemRemove'>    onItemRemove: function(ct, comp){
</span>        var me = this,
            items = me.items,
            handler = me.removeHandler;
            
        if (!me.disabled) {
            if (handler) {
                handler.call(me.scope || comp, comp);
            }
            if (items) {
                items.remove(comp);
            }
        }
    },
    
<span id='Ext-container-Monitor-method-onContainerAdd'>    onContainerAdd: function(ct, preventChildren) {
</span>        var me = this,
            items, len,
            handleAdd = me.handleAdd,
            handleRemove = me.handleRemove,
            i, comp;
        
        if (ct.isContainer) {
            ct.on('add', handleAdd, me);
            ct.on('dockedadd', handleAdd, me);
            ct.on('remove', handleRemove, me);
            ct.on('dockedremove', handleRemove, me);
        }
        
        // Means we've been called by a parent container so the selector
        // matchers have already been processed
        if (preventChildren !== true) {
            items = ct.query(me.selector);
            for (i = 0, len = items.length; i &lt; len; ++i) {
                comp = items[i];
                me.onItemAdd(comp.ownerCt, comp);
            }
        }
         
        items = ct.query('container');
        for (i = 0, len = items.length; i &lt; len; ++i) {
            me.onContainerAdd(items[i], true);
        }
        
    },
    
<span id='Ext-container-Monitor-method-handleRemove'>    handleRemove: function(ct, comp) {
</span>        var me = this;
            
        // During a destroy we don't want to maintain any of this information,
        // so typically we'll be disabled here
        if (!me.disabled) {
            if (comp.is(me.selector)) {
                me.onItemRemove(ct, comp);
            }
        
            if (comp.isQueryable) {
                me.onContainerRemove(ct, comp);
            }
        }
    },
    
<span id='Ext-container-Monitor-method-onContainerRemove'>    onContainerRemove: function(ct, comp){
</span>        var me = this,
            items, i, len, item;
            
        // If it's not a container, it means it's a queryable that isn't a container.
        // For example a button with a menu
        if (!comp.isDestroyed &amp;&amp; !comp.destroying &amp;&amp; comp.isContainer) {
            me.removeCtListeners(comp);
            
            items = comp.query(me.selector);
            for (i = 0, len = items.length; i &lt; len; ++i) {
                item = items[i];
                me.onItemRemove(item.ownerCt, item);
            }
            
            items = comp.query('container');
            for (i = 0, len = items.length; i &lt; len; ++i) {
                me.removeCtListeners(items[i]);
            }
        } else {
            // comp destroying, or we need to invalidate the collection
            me.invalidateItems();
        }
    },
    
<span id='Ext-container-Monitor-method-removeCtListeners'>    removeCtListeners: function(comp){
</span>        var me = this;
        comp.un('add', me.handleAdd, me);
        comp.un('dockedadd', me.handleAdd, me);
        comp.un('remove', me.handleRemove, me);
        comp.un('dockedremove', me.handleRemove, me);
    },
    
<span id='Ext-container-Monitor-method-getItems'>    getItems: function(){
</span>        var me = this,
            items = me.items;
            
        if (!items) {
            items = me.items = new Ext.util.MixedCollection();
            items.addAll(me.target.query(me.selector));
        }
        return items;
    },
    
<span id='Ext-container-Monitor-method-invalidateItems'>    invalidateItems: function(){
</span>        this.items = null;
    }
});
</pre>
</body>
</html>