Project

General

Profile

Download (8.29 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / Module_DatasetExplorerOchart_sitools2v3 / OChartModel.js @ 20ff13ec

1

    
2
Ext.namespace('sitools.user.view.modules.datasetExplorerOchart');
3

    
4
Ext.define( 'sitools.user.view.modules.datasetExplorerOchart.OChartModel', {
5
    extend: 'Ext.selection.DataViewModel',
6

    
7
    requires: [
8
        'Ext.util.KeyNav',
9
        'Ext.util.KeyMap'
10
    ],
11

    
12
    destroy: function(){
13
        var me = this;
14
        if(me.keyMap){
15
            me.keyMap.destroy();
16
        }
17

    
18
        me.callParent(arguments);
19
    },
20

    
21
    /**
22
     * Overwrites model refresh so it handles a Tree Store
23
     */
24
    refresh: function(){
25
        var me = this,
26
            store = me.store,
27
            rec,
28
            toBeSelected = [],
29
            toBeReAdded = [],
30
            oldSelections = me.getSelection(),
31
            len = oldSelections.length,
32
            view = me.view,
33
            selection,
34
            change,
35
            i = 0,
36
            lastFocused = me.getLastFocused();
37

    
38
        // Not been bound yet.
39
        if( !store ){
40
            return;
41
        }
42

    
43
        // Add currently records to the toBeSelected list if present in the Store
44
        // If they are not present, and pruneRemoved is false, we must still retain the record
45
        for( ; i < len; i++ ){
46
            selection = oldSelections[i];
47
            if( view.getNode( selection ) ){
48
                toBeSelected.push( selection );
49
            }
50

    
51
            // Selected records no longer represented in Store must be retained
52
            else if( !me.pruneRemoved ){
53
                // See if a record by the same ID exists. If so, select it
54
                rec = store.getById( selection.getId() );
55
                if( rec ){
56
                    toBeSelected.push( rec );
57
                }
58
                // If it does not exist, we have to re-add it to the selection
59
                else{
60
                    toBeReAdded.push( selection )
61
                }
62
            }
63

    
64
            // In single select mode, only one record may be selected
65
            if( me.mode === 'SINGLE' && toBeReAdded.length ){
66
                break;
67
            }
68
        }
69

    
70
        // there was a change from the old selected and
71
        // the new selection
72
        if( me.selected.getCount() != (toBeSelected.length + toBeReAdded.length) ){
73
            change = true;
74
        }
75

    
76
        me.clearSelections();
77

    
78
        if( view.getNode( lastFocused ) ){
79
            // restore the last focus but suppress restoring focus
80
            me.setLastFocused( lastFocused, true );
81
        }
82

    
83
        if( toBeSelected.length ){
84
            // perform the selection again
85
            me.doSelect( toBeSelected, false, true );
86
        }
87

    
88
        // If some of the selections were not present in the Store, but pruneRemoved is false, we must add them back
89
        if( toBeReAdded.length ){
90
            me.selected.addAll( toBeReAdded );
91

    
92
            // No records reselected.
93
            if( !me.lastSelected ){
94
                me.lastSelected = toBeReAdded[toBeReAdded.length - 1];
95
            }
96
        }
97

    
98
        me.maybeFireSelectionChange( change );
99
    },
100

    
101
    /**
102
     * Overwrites model deselectAll so it handles a Tree Store
103
     */
104
    deselectAll: function( suppressEvent ){
105
        var me = this,
106
            selections = me.getSelection(),
107
            selIndexes = {},
108
            store = me.store,
109
            start = selections.length,
110
            i, l, rec;
111

    
112
        // Cache selection records' indexes first to avoid
113
        // looking them up on every sort comparison below.
114
        // We can't rely on store.indexOf being fast because
115
        // for whatever reason the Store in question may force
116
        // sequential index lookup, which will result in O(n^2)
117
        // sort performance below.
118
        for( i = 0, l = selections.length; i < l; i++ ){
119
            rec = selections[i];
120

    
121
            selIndexes[rec.internalId] = rec.get( 'index' );
122
        }
123

    
124
        // Sort the selections so that the events fire in
125
        // a predictable order like selectAll
126
        selections = Ext.Array.sort( selections, function( r1, r2 ){
127
            var idx1 = selIndexes[r1.internalId],
128
                idx2 = selIndexes[r2.internalId];
129

    
130
            // Don't check for equality since indexes will be unique
131
            return idx1 < idx2 ? -1 : 1;
132
        } );
133

    
134
        if(me.suspendChanges) me.suspendChanges();
135
        me.doDeselect( selections, suppressEvent );
136
        if(me.resumeChanges) me.resumeChanges();
137
        // fire selection change only if the number of selections differs
138
        if( !suppressEvent ){
139
            me.maybeFireSelectionChange( me.getSelection().length !== start );
140
        }
141
    },
142

    
143
    initKeyNav: function( view ){
144
        var me = this;
145

    
146
        if( !view.rendered ){
147
            view.on( {
148
                render: Ext.Function.bind( me.initKeyNav, me, [view] ),
149
                single: true
150
            } );
151
            return;
152
        }
153

    
154
        view.el.set( {
155
            tabIndex: -1
156
        } );
157

    
158
        me.keyNav = new Ext.util.KeyNav( {
159
            target           : view.el,
160
            ignoreInputFields: true,
161
            down             : Ext.pass( me.onNavKey, ['down'], me ),
162
            right            : Ext.pass( me.onNavKey, ['right'], me ),
163
            left             : Ext.pass( me.onNavKey, ['left'], me ),
164
            up               : Ext.pass( me.onNavKey, ['up'], me ),
165
            scope            : me
166
        } );
167

    
168
        me.keyMap = Ext.create('Ext.util.KeyMap',  {
169
            target : view.el,
170
            binding : [
171
                {
172
                    key: Ext.EventObject.NUM_PLUS,
173
                    ctrl: false,
174
                    shift: false,
175
                    fn: me.onKeyExpand,
176
                    scope: me
177
                }, {
178
                    key: Ext.EventObject.NUM_MINUS,
179
                    ctrl: false,
180
                    shift: false,
181
                    fn: me.onKeyCollapse,
182
                    scope: me
183
                }
184
            ]
185
        });
186
    },
187

    
188
    onNavKey: function( direction ){
189
        var me = this,
190
            view = me.view,
191
            store = view.store,
192
            selected = me.getLastSelected(),
193
            root = view.getRootNode(),
194
            record, node;
195

    
196
        if( !selected ){
197
            if(!root) return;
198

    
199
            selected = root;
200
            if(!view.rootVisible){
201
                selected = root.firstChild;
202
            }
203

    
204
            if(selected){
205
                me.select(selected);
206
            }
207

    
208
            return;
209
        }
210

    
211
        direction = direction || 'right';
212
        switch( direction ){
213
            case 'left':
214
                record = selected.previousSibling;
215
                break;
216
            case 'right':
217
                record = selected.nextSibling;
218
                break;
219
            case 'up':
220
                if(selected == root){
221
                    record = null;
222
                    break;
223
                }
224

    
225
                record = selected.parentNode;
226

    
227
                if(!view.rootVisible && record == root){
228
                    record = null;
229
                }
230
                break;
231
            case 'down':
232
                record = selected.firstChild;
233
                if(!record && !selected.isLeaf()){
234
                    record = selected;
235
                }
236
                break;
237
        }
238

    
239
        if(!record) return;
240

    
241
        if(direction == 'down' && !selected.isExpanded()){
242
            selected.expand(false, function(rec){
243
                me.select(record);
244
                view.hideTools();
245
                if(Ext.versions.extjs.isLessThan( '4.2.0' )){
246
                    view.focusNode(record);
247
                }
248
            }, me);
249
        }
250
        else{
251
            me.select(record);
252
            view.hideTools();
253
            if(Ext.versions.extjs.isLessThan( '4.2.0' )){
254
                view.focusNode(record);
255
            }
256
        }
257
    },
258

    
259
    onKeyExpand: function(){
260
        var me = this,
261
            view = me.view,
262
            selected = me.getSelection(),
263
            len = selected.length,
264
            i;
265

    
266
        for(i = 0; i < len; ++i){
267
            selected[i].expand();
268
        }
269
    },
270

    
271
    onKeyCollapse: function(){
272
        var me = this,
273
            view = me.view,
274
            selected = me.getSelection(),
275
            len = selected.length,
276
            i;
277

    
278
        for(i = 0; i < len; ++i){
279
            selected[i].collapse();
280
        }
281
    },
282

    
283
    onContainerClick: function() {
284
        if(!this.view.dragging && this.deselectOnContainerClick) {
285
            this.deselectAll();
286
        }
287
    }
288
} );
289