~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/air/src/NativeWindow.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 0.20
 
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/**
 
10
 * @class Ext.air.NativeWindow
 
11
 * @extends Ext.air.NativeObservable
 
12
 * 
 
13
 * Wraps the AIR NativeWindow class to give an Ext friendly API. <br/><br/>This class also adds 
 
14
 * automatic state management (position and size) for the window (by id) and it can be used 
 
15
 * for easily creating "minimize to system tray" for the main window in your application.<br/><br/>
 
16
 * 
 
17
 * Note: Many of the config options for this class can only be applied to NEW windows. Passing 
 
18
 * in an existing instance of a window along with those config options will have no effect.
 
19
 * 
 
20
 * @constructor
 
21
 * @param {Object} config
 
22
 */
 
23
Ext.air.NativeWindow = function(config){
 
24
        Ext.apply(this, config);
 
25
        
 
26
        /**
 
27
         * @type String
 
28
         */
 
29
        this.id = this.id || Ext.uniqueId();
 
30
        
 
31
        this.addEvents(
 
32
                /**
 
33
                 * @event close
 
34
                 * @param {Object} e The air event object
 
35
                 */
 
36
                'close', 
 
37
                /**
 
38
                 * @event closing
 
39
                 * @param {Object} e The air event object
 
40
                 */
 
41
                'closing',
 
42
                /**
 
43
                 * @event move
 
44
                 * @param {Object} e The air event object
 
45
                 */
 
46
                'move',
 
47
                /**
 
48
                 * @event moving
 
49
                 * @param {Object} e The air event object
 
50
                 */
 
51
                'moving',
 
52
                /**
 
53
                 * @event resize
 
54
                 * @param {Object} e The air event object
 
55
                 */
 
56
                'resize',
 
57
                /**
 
58
                 * @event resizing
 
59
                 * @param {Object} e The air event object
 
60
                 */
 
61
                'resizing',
 
62
                /**
 
63
                 * @event displayStateChange
 
64
                 * @param {Object} e The air event object
 
65
                 */
 
66
                'displayStateChange',
 
67
                /**
 
68
                 * @event displayStateChanging
 
69
                 * @param {Object} e The air event object
 
70
                 */
 
71
                'displayStateChanging'
 
72
        );
 
73
        
 
74
        Ext.air.NativeWindow.superclass.constructor.call(this);
 
75
        
 
76
        if(!this.instance){
 
77
                var options = new air.NativeWindowInitOptions();
 
78
                options.systemChrome = this.chrome;
 
79
                options.type = this.type;
 
80
                options.resizable = this.resizable;
 
81
                options.minimizable = this.minimizable;
 
82
                options.maximizable = this.maximizable;
 
83
                options.transparent = this.transparent;
 
84
                
 
85
                this.loader = window.runtime.flash.html.HTMLLoader.createRootWindow(false, options, false);
 
86
                this.loader.load(new air.URLRequest(this.file));
 
87
        
 
88
                this.instance = this.loader.window.nativeWindow;
 
89
        }else{
 
90
                this.loader = this.instance.stage.getChildAt(0);
 
91
        }
 
92
        
 
93
        var provider = Ext.state.Manager;
 
94
        var b = air.Screen.mainScreen.visibleBounds;
 
95
        
 
96
        var state = provider.get(this.id) || {};
 
97
        provider.set(this.id, state);
 
98
                
 
99
        var win = this.instance;
 
100
        
 
101
        var width = Math.max(state.width || this.width, 100);
 
102
        var height = Math.max(state.height || this.height, 100);
 
103
        
 
104
        var centerX = b.x + ((b.width/2)-(width/2));
 
105
        var centerY = b.y + ((b.height/2)-(height/2));
 
106
        
 
107
        var x = !Ext.isEmpty(state.x, false) ? state.x : (!Ext.isEmpty(this.x, false) ? this.x : centerX);
 
108
        var y = !Ext.isEmpty(state.y, false) ? state.y : (!Ext.isEmpty(this.y, false) ? this.y : centerY);
 
109
        
 
110
        win.width = width;
 
111
        win.height = height;
 
112
        win.x = x;
 
113
        win.y = y;
 
114
        
 
115
        win.addEventListener('move', function(){
 
116
                if(win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) {
 
117
                        state.x = win.x;
 
118
                        state.y = win.y;
 
119
                }
 
120
        });     
 
121
        win.addEventListener('resize', function(){
 
122
                if (win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) {
 
123
                        state.width = win.width;
 
124
                        state.height = win.height;
 
125
                }
 
126
        });
 
127
        
 
128
        Ext.air.NativeWindowManager.register(this);
 
129
        this.on('close', this.unregister, this);
 
130
        
 
131
        /**
 
132
         * @cfg {Boolean} minimizeToTray 
 
133
         * True to enable minimizing to the system tray. Note: this should only be applied
 
134
         * to the primary window in your application. A trayIcon is required.
 
135
         */
 
136
        if(this.minimizeToTray){
 
137
                this.initMinimizeToTray(this.trayIcon, this.trayMenu);
 
138
        }
 
139
        
 
140
};
 
141
 
 
142
Ext.extend(Ext.air.NativeWindow, Ext.air.NativeObservable, {
 
143
        
 
144
        /**
 
145
         * @cfg {air.NativeWindow} instance 
 
146
         * The native window instance to wrap. If undefined, a new window will be created.
 
147
         */
 
148
        
 
149
        /**
 
150
         * @cfg {String} trayIcon 
 
151
         * The icon to display when minimized in the system tray
 
152
         */
 
153
        /**
 
154
         * @cfg {NativeMenu} trayMenu 
 
155
         * Menu to display when the tray icon is right clicked
 
156
         */
 
157
        /**
 
158
         * @cfg {String} trayTip 
 
159
         * Tooltip for the tray icon
 
160
         */     
 
161
        
 
162
        /**
 
163
         * @cfg {String} chrome 
 
164
         * The native window chrome (defaults to 'standard', can also be 'none').
 
165
         */
 
166
        chrome: 'standard', // can also be none
 
167
        /**
 
168
         * @cfg {String} type 
 
169
         * The native window type - normal, utility or lightweight. (defaults to normal)
 
170
         */
 
171
        type: 'normal', // can be normal, utility or lightweight
 
172
        /**
 
173
         * @cfg {Number} width
 
174
         */
 
175
        width:600,
 
176
        /**
 
177
         * @cfg {Number} height 
 
178
         */
 
179
        height:400,
 
180
        /**
 
181
         * @cfg {Boolean} resizable 
 
182
         */
 
183
        resizable: true,
 
184
        /**
 
185
         * @cfg {Boolean} minimizable 
 
186
         */
 
187
        minimizable: true,
 
188
        /**
 
189
         * @cfg {Boolean} maximizable 
 
190
         */
 
191
        maximizable: true,
 
192
        /**
 
193
         * @cfg {Boolean} transparent
 
194
         */
 
195
        transparent: false,
 
196
        
 
197
        /**
 
198
         * Returns the air.NativeWindow instance
 
199
         * @return air.NativeWindow
 
200
         */
 
201
        getNative : function(){
 
202
                return this.instance;
 
203
        },
 
204
        
 
205
        /**
 
206
         * Returns the x/y coordinates for centering the windw on the screen
 
207
         * @return {x: Number, y: Number}
 
208
         */
 
209
        getCenterXY : function(){
 
210
                var b = air.Screen.mainScreen.visibleBounds;
 
211
                return {
 
212
                        x: b.x + ((b.width/2)-(this.width/2)),
 
213
                        y: b.y + ((b.height/2)-(this.height/2))
 
214
                };
 
215
        },
 
216
        
 
217
        /**
 
218
         * Shows the window
 
219
         */
 
220
        show :function(){
 
221
                if(this.trayed){
 
222
                        Ext.air.SystemTray.hideIcon();
 
223
                        this.trayed = false;
 
224
                }
 
225
                this.instance.visible = true;
 
226
        },
 
227
        
 
228
        /**
 
229
         * Shows and activates the window
 
230
         */
 
231
        activate : function(){
 
232
                this.show();
 
233
                this.instance.activate();
 
234
        },
 
235
        
 
236
        /**
 
237
         * Hides the window
 
238
         */
 
239
        hide :function(){
 
240
                this.instance.visible = false;
 
241
        },
 
242
        
 
243
        /**
 
244
         * Closes the window
 
245
         */
 
246
        close : function(){
 
247
                this.instance.close();  
 
248
        },
 
249
        
 
250
        /**
 
251
         * Returns true if this window is minimized
 
252
         * @return Boolean
 
253
         */
 
254
        isMinimized :function(){
 
255
                return this.instance.displayState == air.NativeWindowDisplayState.MINIMIZED;
 
256
        },
 
257
        
 
258
        /**
 
259
         * Returns true if this window is maximized
 
260
         * @return Boolean
 
261
         */
 
262
        isMaximized :function(){
 
263
                return this.instance.displayState == air.NativeWindowDisplayState.MAXIMIZED;
 
264
        },
 
265
        
 
266
        /**
 
267
         * Moves the window to the passed xy and y coordinates
 
268
         * @param {Number} x
 
269
         * @param {Number} y
 
270
         */
 
271
        moveTo : function(x, y){
 
272
                this.x = this.instance.x = x;
 
273
                this.y = this.instance.y = y;   
 
274
        },
 
275
        
 
276
        /**
 
277
         * @param {Number} width
 
278
         * @param {Number} height
 
279
         */
 
280
        resize : function(width, height){
 
281
                this.width = this.instance.width = width;
 
282
                this.height = this.instance.height = height;    
 
283
        },
 
284
        
 
285
        unregister : function(){
 
286
                Ext.air.NativeWindowManager.unregister(this);
 
287
        },
 
288
        
 
289
        initMinimizeToTray : function(icon, menu){
 
290
                var tray = Ext.air.SystemTray;
 
291
                
 
292
                tray.setIcon(icon, this.trayTip);
 
293
                this.on('displayStateChanging', function(e){
 
294
                        if(e.afterDisplayState == 'minimized'){
 
295
                                e.preventDefault();
 
296
                                this.hide();
 
297
                                tray.showIcon();
 
298
                                this.trayed = true;
 
299
                        }
 
300
                }, this);
 
301
                
 
302
                tray.on('click', function(){
 
303
                        this.activate();
 
304
                }, this);
 
305
                
 
306
                if(menu){
 
307
                        tray.setMenu(menu);
 
308
                }
 
309
        }
 
310
});
 
311
 
 
312
/**
 
313
 * Returns the first opened window in your application
 
314
 * @return air.NativeWindow
 
315
 * @static
 
316
 */
 
317
Ext.air.NativeWindow.getRootWindow = function(){
 
318
        return air.NativeApplication.nativeApplication.openedWindows[0];
 
319
};
 
320
 
 
321
/**
 
322
 * Returns the javascript "window" object of the first opened window in your application
 
323
 * @return Window
 
324
 * @static
 
325
 */
 
326
Ext.air.NativeWindow.getRootHtmlWindow = function(){
 
327
        return Ext.air.NativeWindow.getRootWindow().stage.getChildAt(0).window;
 
328
};
 
329
 
 
330
/**
 
331
 * @class Ext.air.NativeWindowGroup
 
332
 * 
 
333
 * A collection of NativeWindows.
 
334
 */
 
335
Ext.air.NativeWindowGroup = function(){
 
336
    var list = {};
 
337
 
 
338
    return {
 
339
                /**
 
340
                 * @param {Object} win
 
341
                 */
 
342
        register : function(win){
 
343
            list[win.id] = win;
 
344
        },
 
345
 
 
346
        /**
 
347
                 * @param {Object} win
 
348
                 */
 
349
        unregister : function(win){
 
350
            delete list[win.id];
 
351
        },
 
352
 
 
353
        /**
 
354
                 * @param {String} id
 
355
                 */
 
356
        get : function(id){
 
357
            return list[id];
 
358
        },
 
359
 
 
360
        /**
 
361
                 * Closes all windows
 
362
                 */
 
363
        closeAll : function(){
 
364
            for(var id in list){
 
365
                if(list.hasOwnProperty(id)){
 
366
                    list[id].close();
 
367
                }
 
368
            }
 
369
        },
 
370
 
 
371
        /**
 
372
         * Executes the specified function once for every window in the group, passing each
 
373
         * window as the only parameter. Returning false from the function will stop the iteration.
 
374
         * @param {Function} fn The function to execute for each item
 
375
         * @param {Object} scope (optional) The scope in which to execute the function
 
376
         */
 
377
        each : function(fn, scope){
 
378
            for(var id in list){
 
379
                if(list.hasOwnProperty(id)){
 
380
                    if(fn.call(scope || list[id], list[id]) === false){
 
381
                        return;
 
382
                    }
 
383
                }
 
384
            }
 
385
        }
 
386
    };
 
387
};
 
388
 
 
389
/**
 
390
 * @class Ext.air.NativeWindowManager
 
391
 * @extends Ext.air.NativeWindowGroup
 
392
 * 
 
393
 * Collection of all NativeWindows created.
 
394
 * 
 
395
 * @singleton
 
396
 */
 
397
Ext.air.NativeWindowManager = new Ext.air.NativeWindowGroup();
 
 
b'\\ No newline at end of file'