1 Ext.ns('Gilbert.lib.app')
4 Gilbert.lib.app.Desktop = Ext.extend(Ext.Panel, {
6 constructor: function(application, config) {
7 var application = this.application = application;
8 Gilbert.lib.app.Desktop.superclass.constructor.call(this, Ext.applyIf(config||{}, {
12 bodyStyle: 'background: none;',
19 Gilbert.lib.app.MainMenu = Ext.extend(Ext.Toolbar, {
21 constructor: function(application, config) {
22 var application = this.application = application;
23 Gilbert.lib.app.MainMenu.superclass.constructor.call(this, Ext.applyIf(config||{}, {
29 text: '<span style="font-weight: bolder; text-transform: uppercase;">Gilbert</span>',
41 Gilbert.lib.app.TaskBar = Ext.extend(Ext.Toolbar, {
43 constructor: function(application, config) {
44 var application = this.application = application;
45 Gilbert.lib.app.TaskBar.superclass.constructor.call(this, Ext.applyIf(config||{}, {
51 new Ext.ux.ToolbarReorderer({
52 defaultReorderable: true,
58 get_button_for_window: function(win) {
59 return this.find('represented_window', win)[0];
62 default_button_handler: function(button) {
63 var win = button.represented_window;
64 if (this.active_window === win) {
71 register_window: function(win) {
72 win.on('show', this.window_shown, this);
73 win.on('hide', this.window_hidden, this);
74 win.on('minimize', this.window_minimize, this);
75 win.on('deactivate', this.window_deactivated, this);
76 win.on('activate', this.window_activated, this);
77 win.on('titlechange', this.window_titlechanged, this);
78 win.on('iconchange', this.window_iconchanged, this);
80 var button = new Ext.Button({
88 button.represented_window = win;
89 button.setHandler(this.default_button_handler, this);
93 win.on('destroy', this.window_destroyed, this);
96 window_destroyed: function(win) {
97 this.remove(this.get_button_for_window(win));
98 this.application.do_layout();
101 window_shown: function(win) {
102 if (this.minimizing_window !== win) {
103 this.get_button_for_window(win).show();
104 this.application.do_layout();
108 window_hidden: function(win) {
109 if (this.minimizing_window !== win) {
110 this.get_button_for_window(win).hide();
111 this.application.do_layout();
115 window_minimize: function(win) {
116 var button = this.get_button_for_window(win);
118 this.minimizing_window = win;
119 win.hide(button.el, function () {
120 this.minimizing_window = undefined;
122 win.minimized = true;
123 button.setText('<i>'+win.title+'</i>');
124 button.setHandler(function (button) {
125 var win = button.represented_window;
127 win.minimized = false;
128 button.setText(win.title);
129 button.setHandler(this.default_button_handler, this);
131 this.minimizing_window = win;
132 win.show(button.el, function () {
133 this.minimizing_window = undefined;
139 window_deactivated: function(win) {
140 var button = this.get_button_for_window(win);
141 button.toggle(false);
142 button.setText(win.title);
144 if (this.active_window === win) {
145 this.active_window = undefined;
149 window_activated: function(win) {
150 var button = this.get_button_for_window(win);
152 button.setText('<b>'+win.title+'</b>');
154 this.active_window = win;
157 window_titlechanged: function(win) {
158 var button = this.get_button_for_window(win);
160 button.setText('<i>'+win.title+'</i>');
162 button.setText(win.title);
166 window_iconchanged: function(win) {
167 var button = this.get_button_for_window(win);
168 button.setIconClass(win.iconCls);
174 Gilbert.lib.app.Application = Ext.extend(Ext.util.Observable, {
176 constructor: function (config) {
181 Ext.apply(this, config, {
182 renderTo: Ext.getBody(),
185 Gilbert.lib.app.Application.superclass.constructor.call(this);
189 'model_registered': true,
190 'plugin_registered': true,
191 'window_created': true,
194 Ext.onReady(this.pre_init.createDelegate(this));
197 pre_init: function () {
200 Gilbert.api.plugins.auth.get_preference('gilbert.background', function (background) {
202 outer.renderTo.setStyle('background', background);
205 Gilbert.api.plugins.auth.get_preference('gilbert.theme', function (theme) {
207 outer._set_theme(theme);
213 _set_theme: function(theme) {
214 var link_element = document.getElementById('gilbert.theme.' + theme);
216 Ext.each(document.getElementsByClassName('gilbert.theme'), function (theme_element) {
217 if (theme_element != link_element) {
218 theme_element.disabled = true;
220 theme_element.disabled = false;
227 Ext.QuickTips.init();
229 var desktop = this.desktop = new Gilbert.lib.app.Desktop();
230 var mainmenu = this.mainmenu = new Gilbert.lib.app.MainMenu(this);
231 var taskbar = this.taskbar = new Gilbert.lib.app.TaskBar(this);
232 var viewport = this.viewport = new Ext.Viewport({
233 renderTo: this.renderTo,
242 var windows = this.windows = new Ext.WindowGroup();
244 Ext.Direct.on('exception', function (exception) {
245 if (exception.code == Ext.Direct.exceptions.TRANSPORT) {
246 if (exception.xhr.status == 403) {
247 window.alert('You have been unexpectedly logged out.');
248 window.location.reload(true);
251 if (exception.html) {
252 var win = this.create_window({
258 html_source: exception.html,
259 }, Gilbert.lib.ui.HTMLWindow);
264 var initial_plugins = this.plugins;
267 Ext.iterate(initial_plugins, function (name, plugin, plugins) {
268 this.register_plugin(name, plugin);
273 this.renderTo.on('contextmenu', Ext.emptyFn, null, {preventDefault: true});
275 window.onbeforeunload = function (event) {
276 var notice = 'You will lose all unsaved changes and windows.';
277 var event = event || window.event;
279 event.returnValue = notice;
285 this.fireEvent('ready', this);
288 create_window: function(config, cls) {
289 var win = new(cls||Ext.Window)(Ext.applyIf(config||{},{
290 renderTo: this.desktop.el,
291 manager: this.windows,
293 constrainHeader: true,
296 win.render(this.desktop.el);
298 win.on('show', function () {
299 this.mainmenu.hide();
303 win.on('hide', function () {
305 this.mainmenu.show();
308 win.on('close', function () {
310 this.mainmenu.show();
314 win.setPosition(Math.floor(Math.random() * ((this.desktop.getInnerWidth() - win.width) - 1)), Math.floor(Math.random() * ((this.desktop.getInnerHeight() - win.height) - 1)));
316 this.taskbar.register_window(win);
317 this.fireEvent('window_created', win);
321 do_layout: function() {
322 this.mainmenu.doLayout();
323 this.taskbar.doLayout();
324 this.viewport.doLayout();
327 register_plugin: function (name, plugin) {
328 if (plugin.init(this) != false) {
329 this.plugins[name] = plugin;
330 this.fireEvent('plugin_registered', name, plugin, this);
334 get_plugin: function (name) {
335 return this.plugins[name];
338 register_model: function (app_label, name, model) {
339 if (!this.models[app_label]) {
340 this.models[app_label] = {};
342 this.models[app_label][name] = model;
343 this.fireEvent('model_registered', name, model, this);
346 get_model: function (app_label, name) {
347 if (!this.models[app_label]) {
350 return this.models[app_label][name];