Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / desktop / js / App.js
1 /*!
2  * Ext JS Library 4.0
3  * Copyright(c) 2006-2011 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.com/license
6  */
7
8 Ext.define('Ext.ux.desktop.App', {
9     mixins: {
10         observable: 'Ext.util.Observable'
11     },
12
13     requires: [
14         'Ext.container.Viewport',
15
16         'Ext.ux.desktop.Desktop'
17     ],
18
19     isReady: false,
20     modules: null,
21     useQuickTips: true,
22
23     constructor: function (config) {
24         var me = this;
25         me.addEvents(
26             'ready',
27             'beforeunload'
28         );
29
30         me.mixins.observable.constructor.call(this, config);
31
32         if (Ext.isReady) {
33             Ext.Function.defer(me.init, 10, me);
34         } else {
35             Ext.onReady(me.init, me);
36         }
37     },
38
39     init: function() {
40         var me = this, desktopCfg;
41
42         if (me.useQuickTips) {
43             Ext.QuickTips.init();
44         }
45
46         me.modules = me.getModules();
47         if (me.modules) {
48             me.initModules(me.modules);
49         }
50
51         desktopCfg = me.getDesktopConfig();
52         me.desktop = new Ext.ux.desktop.Desktop(desktopCfg);
53
54         me.viewport = new Ext.container.Viewport({
55             layout: 'fit',
56             items: [ me.desktop ]
57         });
58
59         Ext.EventManager.on(window, 'beforeunload', me.onUnload, me);
60
61         me.isReady = true;
62         me.fireEvent('ready', me);
63     },
64
65     /**
66      * This method returns the configuration object for the Desktop object. A derived
67      * class can override this method, call the base version to build the config and
68      * then modify the returned object before returning it.
69      */
70     getDesktopConfig: function () {
71         var me = this, cfg = {
72             app: me,
73             taskbarConfig: me.getTaskbarConfig()
74         };
75
76         Ext.apply(cfg, me.desktopConfig);
77         return cfg;
78     },
79
80     getModules: Ext.emptyFn,
81
82     /**
83      * This method returns the configuration object for the Start Button. A derived
84      * class can override this method, call the base version to build the config and
85      * then modify the returned object before returning it.
86      */
87     getStartConfig: function () {
88         var me = this, cfg = {
89             app: me,
90             menu: []
91         };
92
93         Ext.apply(cfg, me.startConfig);
94
95         Ext.each(me.modules, function (module) {
96             if (module.launcher) {
97                 cfg.menu.push(module.launcher);
98             }
99         });
100
101         return cfg;
102     },
103
104     /**
105      * This method returns the configuration object for the TaskBar. A derived class
106      * can override this method, call the base version to build the config and then
107      * modify the returned object before returning it.
108      */
109     getTaskbarConfig: function () {
110         var me = this, cfg = {
111             app: me,
112             startConfig: me.getStartConfig()
113         };
114
115         Ext.apply(cfg, me.taskbarConfig);
116         return cfg;
117     },
118
119     initModules : function(modules) {
120         var me = this;
121         Ext.each(modules, function (module) {
122             module.app = me;
123         });
124     },
125
126     getModule : function(name) {
127         var ms = this.modules;
128         for (var i = 0, len = ms.length; i < len; i++) {
129             var m = ms[i];
130             if (m.id == name || m.appType == name) {
131                 return m;
132             }
133         }
134         return null;
135     },
136
137     onReady : function(fn, scope) {
138         if (this.isReady) {
139             fn.call(scope, this);
140         } else {
141             this.on({
142                 ready: fn,
143                 scope: scope,
144                 single: true
145             });
146         }
147     },
148
149     getDesktop : function() {
150         return this.desktop;
151     },
152
153     onUnload : function(e) {
154         if (this.fireEvent('beforeunload', this) === false) {
155             e.stopEvent();
156         }
157     }
158 });