]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/Application.js
Make modal window optionally "persistent" (not destroied when hidden, and accumulate...
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / Application.js
1 /* ************************************************************************
2
3 Copyright: 2008 Argeo
4
5 License: LGPL
6
7 Authors: Charles du Jeu
8
9 ************************************************************************ */
10
11 /* ************************************************************************
12
13 #asset(slc/*)
14
15 ************************************************************************ */
16
17 /**
18 * This is the main application class of an Argeo RIA.
19 */
20 qx.Class.define("org.argeo.ria.Application",
21 {
22 extend : qx.application.Standalone,
23
24 statics : {
25 INSTANCE : null
26 },
27
28 properties : {
29 /**
30 * Available perspective detected in the current compilation.
31 */
32 perspectives : {
33 check : "Map",
34 init : {}
35 },
36 /**
37 * Currently layouted perspective label
38 */
39 activePerspectiveName : {
40 check : "String",
41 init : ""
42 },
43 /**
44 * Currently layouted perspective.
45 */
46 activePerspective : {
47 init : null
48 },
49 /**
50 * Basic command associated to the application, applicable to all perspectives.
51 */
52 commandsDefinitions : {
53 init : {
54 "stop" : {
55 label : "Stop",
56 icon : "resource/slc/process-stop.png",
57 shortcut : "Control+s",
58 enabled : false,
59 menu : null,
60 toolbar : "list",
61 callback : function(e){},
62 command : null
63 },
64 "switchperspective" : {
65 label : "Switch Perspective",
66 icon : "resource/slc/view-pane-tree.png",
67 shortcut : "",
68 enabled : true,
69 menu : "View",
70 toolbar : false,
71 submenu : [],
72 submenuCallback : function(commandId){
73 // Defer execution to assure that the submenu is closed
74 // before it is rebuilt.
75 qx.event.Timer.once(function(){
76 org.argeo.ria.Application.INSTANCE.loadPerspective(commandId);
77 }, this, 10);
78 },
79 callback : function(e){},
80 command : null
81 },
82 "log" : {
83 label : "Show Console",
84 icon : "resource/slc/help-contents.png",
85 shortcut : "",
86 enabled : true,
87 menu : "View",
88 menuPosition: "last",
89 toolbar : false,
90 callback : function(e){
91 org.argeo.ria.components.Logger.getInstance().toggle();
92 },
93 command : null
94 },
95 "help" : {
96 label : "About...",
97 icon : "resource/slc/help-about.png",
98 shortcut : "Control+h",
99 enabled : true,
100 menu : "View",
101 toolbar : false,
102 callback : function(e){
103 var win = new org.argeo.ria.components.Modal("About SLC", null, "SLC is a product from Argeo.");
104 win.attachAndShow();
105 },
106 command : null
107 }
108 }
109 }
110 },
111
112 members :
113 {
114 /**
115 * This method contains the initial application code and gets called
116 * during startup of the application
117 */
118 main : function()
119 {
120 // Call super class
121 this.base(arguments);
122 this.self(arguments).INSTANCE = this;
123 this.views = {};
124
125 var viewsManager = org.argeo.ria.components.ViewsManager.getInstance();
126 viewsManager.setApplicationRoot(this.getRoot());
127
128 /*
129 var appli = this;
130 qx.bom.Event.addNativeListener(window, "unload", function(){
131 // TODO : Close perspective if one is open.
132 if(appli.getActivePerspective()){
133 alert(appli.getActivePerspective());
134 appli.getActivePerspective().remove(org.argeo.ria.components.ViewsManager.getInstance());
135 }
136 });
137 */
138 // Enable logging in debug variant
139 if (qx.core.Variant.isSet("qx.debug", "on"))
140 {
141 qx.log.appender.Native;
142 qx.log.appender.Console;
143 }
144 var winLogger = org.argeo.ria.components.Logger.getInstance();
145 this.getRoot().add(winLogger);
146 qx.log.Logger.register(winLogger);
147
148 // Main layout
149 var layout = new qx.ui.layout.VBox();
150 var container = new qx.ui.container.Composite(layout);
151 viewsManager.setViewPanesContainer(container);
152 // Document is the application root
153 this.getRoot().add(container, {left:0,right:0,top:0,bottom:0});
154
155 // Find available perspectives
156 var allPerspectives = {};
157 for(var key in qx.Bootstrap.$$registry){
158 if(qx.Class.hasInterface(qx.Bootstrap.$$registry[key], org.argeo.ria.components.IPerspective)){
159 allPerspectives[key] = qx.Bootstrap.$$registry[key];
160 }
161 }
162 var perspectiveNumber = qx.lang.Object.getLength(allPerspectives);
163 if(!perspectiveNumber){
164 this.error("Cannot find a perspective for startup!");
165 return;
166 }
167 this.setPerspectives(allPerspectives);
168 // Choose startup perspective, delete switch menu if only one perspective.
169 if(perspectiveNumber <= 1){
170 delete this.getCommandsDefinitions()["switchperspective"];
171 this.setActivePerspectiveName(qx.lang.Object.getKeys(allPerspectives)[0]);
172 }
173 else{
174 var startupSetting;
175 try{
176 startupSetting = qx.core.Setting.get("ria.StartupPerspective");
177 }catch(e){}
178 if(startupSetting && allPerspectives[startupSetting]){
179 this.setActivePerspectiveName(startupSetting);
180 }else{
181 this.setActivePerspectiveName(qx.lang.Object.getKeys(allPerspectives)[0]);
182 }
183 this.rebuildPerspectiveMenus();
184 }
185
186 var menuBar = new qx.ui.menubar.MenuBar();
187 var toolbar = new qx.ui.toolbar.ToolBar();
188 var commandManager = org.argeo.ria.event.CommandsManager.getInstance();
189 commandManager.init(this.getCommandsDefinitions());
190 commandManager.createCommands();
191 commandManager.registerMenuBar(menuBar);
192 commandManager.registerToolBar(toolbar);
193 toolbar.setShow("both");
194 commandManager.addToolbarContextMenu(toolbar);
195
196 var stopCommand = commandManager.getCommandById("stop");
197 var serviceManager = org.argeo.ria.remote.RequestManager.getInstance();
198 serviceManager.setStopCommand(stopCommand);
199
200 container.add(menuBar);
201 container.add(toolbar);
202
203 this.loadPerspective();
204 },
205
206 /**
207 * Load a given perspective by its name.
208 * @param perspectiveName {String} Perspective to load
209 */
210 loadPerspective : function(perspectiveName){
211 if(perspectiveName){
212 this.setActivePerspectiveName(perspectiveName);
213 this.rebuildPerspectiveMenus();
214 }else{
215 perspectiveName = this.getActivePerspectiveName();
216 }
217 var viewsManager = org.argeo.ria.components.ViewsManager.getInstance();
218 if(this.getActivePerspective()){
219 this.getActivePerspective().remove(viewsManager);
220 }
221 var allPerspectives = this.getPerspectives();
222 var perspectiveClass = allPerspectives[perspectiveName];
223 if(!perspectiveClass){
224 this.error("Cannot find class for startup perspective : "+perspectiveName);
225 return;
226 }
227 var perspective = new perspectiveClass;
228 perspective.initViewPanes(viewsManager);
229 perspective.initViews(viewsManager);
230 this.setActivePerspective(perspective);
231 },
232
233 /**
234 * After switching perspective, call this function to rebuild menu with the right selected.
235 */
236 rebuildPerspectiveMenus : function(){
237 var switchCommand = this.getCommandsDefinitions()["switchperspective"];
238 switchCommand.submenu = [];
239 var allPerspectives = this.getPerspectives();
240 for(var key in allPerspectives){
241 switchCommand.submenu.push({
242 "label":(allPerspectives[key].LABEL || key)+(key==this.getActivePerspectiveName()?" (current)":""),
243 "icon" :(allPerspectives[key].ICON || null),
244 "commandId":key,
245 "disabled" : (key==this.getActivePerspectiveName()?true:false)
246 });
247 }
248 if(switchCommand.command){ // Command already created : force reload
249 switchCommand.command.clearMenus();
250 switchCommand.command.setMenu(switchCommand.submenu);
251 }
252 },
253
254 /**
255 * Specific action of calling an external URL without triggering the "close()" method
256 * of Application.
257 * @param hrefValue {String} A download url that should reply with specific "attachment" header to avoid leaving the application.
258 */
259 javascriptDownloadLocation: function(hrefValue){
260 this.interruptClose = true;
261 document.location.href = hrefValue;
262 this.interruptClose = false;
263 },
264
265 /**
266 * Called at Application ending (closing the browser).
267 */
268 close : function(){
269 if(this.interruptClose) return ;
270 if(this.getActivePerspective()){
271 this.getActivePerspective().remove(org.argeo.ria.components.ViewsManager.getInstance());
272 }
273 this.base(arguments);
274
275 }
276
277 }
278 });