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