1 /*
  2  * Ext JS Library 2.0 Beta 1
  3  * Copyright(c) 2006-2007, Ext JS, LLC.
  4  * licensing@extjs.com
  5  * 
  6  * http://extjs.com/license
  7  */
  8 
  9 Ext.ux.WindowManager = function(app){
 10 //	this.taskbar = new Ext.ux.TaskBar(app);
 11 //	var taskbar = this.taskbar;
 12 //	
 13 	this.desktopEl = null;
 14 //    var taskbarEl = Ext.get('ux-taskbar');
 15 //    var shortcuts = Ext.get('x-shortcuts');
 16 
 17     var windows = new Ext.WindowGroup();
 18     windows.zseed = 8000; // set lower than default (9000)
 19     var activeWindow;
 20 		
 21     function minimizeWin(win){
 22         win.minimized = true;
 23         win.collapse();
 24     }
 25 
 26     /**
 27      * Sets a window visually active, and all others inactive
 28      * @param {Ext.Window} activeWin
 29      */
 30     this.setActive = function(activeWin) {
 31         var eachWindowFunction = function(win, activeWin) {
 32             if (win == activeWin) {
 33                 // same code as markActive, but without the call to feature.focusComponent
 34                 // since the component already has focus
 35                 if(activeWindow && activeWindow != win){
 36                     markInactive(activeWindow);
 37                 }
 38                 activeWindow = win;
 39                 win.minimized = false;
 40                 
 41                 win.toFront();
 42             } else {
 43                 if (win == activeWindow) {
 44                     activeWindow = null;
 45                 }
 46             }
 47         };
 48         windows.each(eachWindowFunction.createDelegate(this, [activeWin], true));
 49     }
 50 
 51     function markActive(win){
 52         if(activeWindow && activeWindow != win){
 53             markInactive(activeWindow);
 54         }
 55 //        taskbar.setActiveButton(win.taskButton);
 56         activeWindow = win;
 57 //        Ext.fly(win.taskButton.el).addClass('active-win');
 58         win.minimized = false;
 59         feature.focusComponent(win);
 60     }
 61 
 62     function markInactive(win){
 63         if(win == activeWindow){
 64             activeWindow = null;
 65 //            Ext.fly(win.taskButton.el).removeClass('active-win');
 66         }
 67     }
 68 
 69     function removeWin(win){
 70         feature.destroyComponent(win);
 71 //    	taskbar.removeTaskButton(win.taskButton);
 72         layout();
 73     }
 74     
 75     function bringToFront(win){
 76         windows.bringToFront(win);
 77     }
 78 
 79     function layout(){
 80         //this.getDesktop().setHeight(Ext.lib.Dom.getViewHeight()-taskbarEl.getHeight());
 81     }
 82     Ext.EventManager.onWindowResize(layout);
 83 
 84     this.layout = layout;
 85     
 86     this.getDesktop = function() {
 87         if (this.desktopEl == null) this.desktopEl = Ext.get('x-desktop');
 88         return this.desktopEl;
 89     }
 90 
 91     this.createWindow = function(config, cls){
 92     	var win = new (cls||Ext.Window)(
 93             Ext.applyIf(config||{}, {
 94                 manager: windows,
 95                 minimizable: true,
 96                 maximizable: true
 97             })
 98         );
 99         win.render(this.getDesktop());
100         //win.taskButton = taskbar.addTaskButton(win);
101 
102         win.cmenu = new Ext.menu.Menu({
103             items: [
104 
105             ]
106         });
107 
108         //win.animateTarget = win.taskButton.el;
109         
110         win.on({
111         	'activate': {
112         		fn: markActive
113         	},
114         	'beforeshow': {
115         		fn: markActive
116         	},
117         	'deactivate': {
118         		fn: markInactive
119         	},
120         	'minimize': {
121         		fn: minimizeWin
122         	},
123         	'close': {
124         		fn: removeWin
125         	},
126             'resize': {
127                 fn: bringToFront
128             }
129         });
130         
131         layout();
132         return win;
133     };
134 
135     this.getManager = function(){
136         return windows;
137     };
138 
139     this.getWindow = function(id){
140         return windows.get(id);
141     }
142     
143     this.getWinWidth = function(){
144 		var width = Ext.lib.Dom.getViewWidth();
145 		return width < 200 ? 200 : width;
146 	}
147 		
148 	this.getWinHeight = function(){
149 		var height = (Ext.lib.Dom.getViewHeight());//-taskbarEl.getHeight());
150 		return height < 100 ? 100 : height;
151 	}
152 		
153 	this.getWinX = function(width){
154 		return (Ext.lib.Dom.getViewWidth() - width) / 2
155 	}
156 		
157 	this.getWinY = function(height){
158 		return (Ext.lib.Dom.getViewHeight() - height) / 2;
159 	}
160     
161     this.getWindowLayout = function(window) {
162         var windowBox = window.getBox(true);
163         var desktopBox = this.getDesktop().getBox(false, true);
164         // trim values to 2 decimal places
165         var x = parseFloat(String(windowBox.x / desktopBox.width).substr(0, 6));
166         var y = parseFloat(String(windowBox.y / desktopBox.height).substr(0, 6));
167         var width = parseFloat(String(windowBox.width / desktopBox.width).substr(0, 6));
168         var height = parseFloat(String(windowBox.height / desktopBox.height).substr(0, 6));
169         return {id: window.id, x: x, y: y, width: width, height: height};
170     }
171     
172     this.setWindowLayout = function(layout) {
173         var desktopBox = this.getDesktop().getBox(false, true);
174         var desktopWidth = desktopBox.width;
175         var desktopHeight = desktopBox.height;
176         
177         var window = this.getWindow(layout.id);
178         window.setPosition(layout.x * desktopWidth, layout.y * desktopHeight);
179         window.setSize(layout.width * desktopWidth, layout.height * desktopHeight);
180         //Workbench.console.info(layout.id+' layout. h:'+layout.height+', w:'+layout.width+', x:'+layout.x+', y:'+layout.y);
181     }
182     
183     /**
184      * Sets the windows in a tile layout
185      * @param {Array} toolIds An array of tool IDs  
186      */
187     this.setDefaultWindowLayout = function(toolIds) {
188         var windowWidth = Math.floor(Ext.lib.Dom.getViewWidth() / toolIds.length);
189         var windowHeight = this.getDesktop().getHeight() - 30;
190         var count = 0;
191         this.getManager().each(function(window){
192             window.setWidth(windowWidth);
193             window.setHeight(windowHeight);
194             var toolId = window.id.split('-window')[0]; // get the window id minus '-window'
195             var position = toolIds.indexOf(toolId);
196             window.setPosition(windowWidth * position, 0);
197         });
198     }
199 
200     // property for storing window layout values
201     this.windowLayoutValues = [];
202 
203     /**
204      * Gets the window layouts in relative values (0.0000 to 1.0000).
205      * @return {Array} An array of layout objects with id, x, y, width, and height values.
206      */
207     this.getWindowLayouts = function() {
208         this.windowLayoutValues = []; // reset the property
209         this.getManager().each(function(window) {
210             var windowLayout = this.getWindowLayout(window);
211             this.windowLayoutValues.push(windowLayout);
212         }, this);
213         return this.windowLayoutValues;
214     }
215     
216     /**
217      * Sets the layout for windows
218      * @param {Object} layoutArray An array of layout objects
219      * @config {String} id The window ID
220      * @config {Float} x The window's x position (between 0.00 and 1.00)
221      * @config {Float} y The window's y position (between 0.00 and 1.00)
222      * @config {Float} width The window's width (between 0.00 and 1.00)
223      * @config {Float} height The window's height (between 0.00 and 1.00)
224      */
225     this.setWindowLayouts = function(layoutArray) {
226         for (var i = 0; i < layoutArray.length; i++) {
227             var layout = layoutArray[i];
228             this.setWindowLayout(layout);
229         }
230     }
231 
232     layout();
233 };
234