]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-lib/slc-web/class/org/argeo/slc/web/TestList.js
523b5e9dcd802bf6cf7b845c335a463dfed5f05b
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-lib / slc-web / class / org / argeo / slc / web / TestList.js
1 /**
2 * Basic IView implementation for displaying the test results list, by collection.
3 *
4 * This component creates a Table object and feed it with the results. It adds a collection chooser to its viewPane header.
5 *
6 * It creates the following commands : "loadtestlist", "polllistloading", "opentest", "download", "copytocollection", "deletetest".
7 */
8 qx.Class.define("org.argeo.slc.web.TestList",
9 {
10 extend : qx.ui.container.Composite,
11 implement : [org.argeo.ria.components.IView],
12
13 construct : function(){
14 this.base(arguments, new qx.ui.layout.VBox());
15 var model = new qx.ui.table.model.Simple();
16 model.setColumns(["Test Case", "Date"]);
17 this.table = new qx.ui.table.Table(model, {
18 tableColumnModel: function(obj){
19 return new qx.ui.table.columnmodel.Resize(obj)
20 }
21 });
22 },
23
24 properties :
25 {
26 /**
27 * The viewPane containing this applet.
28 */
29 view : {
30 init : null
31 },
32 /**
33 * The load list of available collection (Map of ids => labels)
34 */
35 collectionList : {
36 init : {},
37 check : "Map"
38 },
39 /**
40 * The current collection id selected.
41 */
42 collectionId:{
43 init : 'My Collection',
44 check : "String"
45 },
46 /**
47 * The applet commands.
48 */
49 commands : {
50 init : {
51 "openexecutionapplet" : {
52 label : "Launcher",
53 icon : "resource/slc/utilities-terminal.png",
54 shortcut : "Control+i",
55 enabled : true,
56 menu : "File",
57 toolbar : "list",
58 callback : function(e){
59 var manager = org.argeo.ria.components.ViewsManager.getInstance();
60 var iView = manager.initIViewClass(org.argeo.slc.ria.LauncherApplet, "applet");
61 iView.load();
62 },
63 command : null
64 },
65 "loadtestlist" : {
66 label : "Load Collection",
67 icon : "resource/slc/view-refresh.png",
68 shortcut : "Control+l",
69 enabled : true,
70 menu : "Collection",
71 toolbar : "collection",
72 callback : function(e){
73 this.loadList();
74 },
75 command : null
76 },
77 "polllistloading" : {
78 label : "Auto load",
79 icon : "resource/slc/document-open-recent.png",
80 shortcut : "Control+l",
81 enabled : true,
82 toggle : true,
83 menu : "Collection",
84 toolbar : "collection",
85 callback : function(event){
86 this.pollListLoading(event.getTarget().getUserData("slc.command.toggleState"));
87 },
88 command : null
89 },
90 /*
91 "copyfullcollection" : {
92 label : "Copy to...",
93 icon : "resource/slc/edit-copy.png",
94 shortcut : null,
95 enabled : false,
96 menu : "Collection",
97 toolbar : "collection",
98 callback : function(e){
99 // Call service to copy
100 },
101 submenu : {},
102 submenuCallback : function(commandId){
103 this.copySelectionToCollection(commandId, "current_collection");
104 },
105 init : function(){
106 // Call at command creation
107 org.argeo.ria.remote.RequestManager.getInstance().addListener("reload", function(event){
108 if(event.getDataType() == "collection" || event.getDataType() == "test_cases"){
109 var testList = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("list").getContent();
110 testList.collectionListToMenu(this);
111 }
112 }, this);
113 },
114 command : null
115 },
116 */
117 "opentest" : {
118 label : "Open",
119 icon : "resource/slc/media-playback-start.png",
120 shortcut : "Control+o",
121 enabled : false,
122 menu : "Selection",
123 toolbar : "selection",
124 callback : function(e){
125 var viewsManager = org.argeo.ria.components.ViewsManager.getInstance();
126 var classObj = org.argeo.slc.ria.Applet;
127 var iView = viewsManager.initIViewClass(classObj, "applet");
128 var xmlNodes = viewsManager.getViewPaneSelection("list").getNodes();
129 iView.load(xmlNodes[0]);
130 },
131 selectionChange : function(viewId, xmlNodes){
132 if(viewId != "list") return;
133 this.setEnabled(false);
134 if(xmlNodes == null || !xmlNodes.length) return;
135 this.setEnabled(true);
136 },
137 command : null
138 },
139 "download" : {
140 label : "Download as...",
141 icon : "resource/slc/go-down.png",
142 shortcut : null,
143 enabled : false,
144 menu : "Selection",
145 toolbar : "selection",
146 callback : function(e){ },
147 command : null,
148 submenu : [
149 {'label':"Xsl", 'icon':'resource/slc/mime-xsl.png', 'commandId':'xsl'},
150 {'label':"Xml", 'icon':'resource/slc/mime-xml.png', 'commandId':'xml'},
151 {'label':"Excel", 'icon':'resource/slc/mime-xls.png', 'commandId':'xls'},
152 {'label':"Pdf", 'icon':'resource/slc/mime-pdf.png', 'commandId':'pdf'}
153 ],
154 submenuCallback : function(commandId){
155 var uuid = this.extractTestUuid();
156 var urls = {
157 xsl : "resultView.xslt",
158 xml : "resultViewXml.xslt",
159 xls : "resultView.xls",
160 pdf : "resultView.pdf"
161 };
162 var url = "../"+urls[commandId]+"?uuid="+uuid;
163 if(commandId == "xls" || commandId == "pdf"){
164 document.location.href = url;
165 }else{
166 var win = window.open(url);
167 }
168 },
169 selectionChange : function(viewId, xmlNodes){
170 if(viewId!="list")return;
171 //this.clearMenus();
172 this.setEnabled(false);
173 if(xmlNodes == null || !xmlNodes.length) return;
174 this.setEnabled(true);
175 }
176 },
177 "copytocollection" : {
178 label : "Copy to...",
179 icon : "resource/slc/edit-copy.png",
180 shortcut : "Control+c",
181 enabled : false,
182 menu : "Selection",
183 toolbar : "selection",
184 callback : function(e){
185 // Call service to copy
186 },
187 submenu : {},
188 submenuCallback : function(commandId){
189 this.copySelectionToCollection(commandId, "current_selection");
190 },
191 init : function(){
192 // Call at command creation
193 org.argeo.ria.remote.RequestManager.getInstance().addListener("reload", function(event){
194 if(event.getDataType() == "collection" || event.getDataType() == "test_cases"){
195 var testList = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("list").getContent();
196 testList.collectionListToMenu(this, true);
197 }
198 }, this);
199 },
200 selectionChange : function(viewId, xmlNodes){
201 if(viewId != "list") return;
202 this.setEnabled(false);
203 if(xmlNodes == null || !xmlNodes.length) return;
204 this.setEnabled(true);
205 },
206 command : null
207 },
208 "deletetest" : {
209 label : "Delete",
210 icon : "resource/slc/edit-delete.png",
211 shortcut : "Control+d",
212 enabled : false,
213 menu : "Selection",
214 toolbar : "selection",
215 callback : function(e){
216
217 var request = org.argeo.slc.ria.SlcApi.getRemoveResultService(this.getCollectionId(), this.extractTestUuid());
218 request.addListener("completed", function(response){
219 this.loadCollections();
220 this.loadList();
221 this.info("Test was successfully deleted");
222 }, this);
223 request.send();
224 },
225 selectionChange : function(viewId, xmlNodes){
226 if(viewId != "list") return;
227 this.setEnabled(false);
228 if(xmlNodes == null || !xmlNodes.length) return;
229 this.setEnabled(true);
230 },
231 command : null
232 }
233 }
234 }
235 },
236
237 members : {
238 init : function(viewPane){
239 this.setView(viewPane);
240 },
241 load : function(data){
242 this.table.set({
243 statusBarVisible: false,
244 showCellFocusIndicator:false,
245 columnVisibilityButtonVisible:false,
246 contextMenu : org.argeo.ria.event.CommandsManager.getInstance().createMenuFromIds(["opentest", "download", "copytocollection", "deletetest"]),
247 decorator : new qx.ui.decoration.Background("#fff")
248 });
249 var viewPane = this.getView();
250 this.table.addListener("dblclick", function(e){
251 org.argeo.ria.event.CommandsManager.getInstance().executeCommand("opentest");
252 }, this);
253 var columnModel = this.table.getTableColumnModel();
254 columnModel.getBehavior().setWidth(0, "60%");
255 var renderer = new org.argeo.slc.web.XmlRenderer();
256 columnModel.setDataCellRenderer(0, renderer);
257 columnModel.setDataCellRenderer(1, renderer);
258
259 this.table.getSelectionManager().getSelectionModel().addListener("changeSelection", function(e){
260 var viewSelection = viewPane.getViewSelection();
261 viewSelection.clear();
262 var selectionModel = this.table.getSelectionManager().getSelectionModel();
263 if(!selectionModel.getSelectedCount()){
264 return;
265 }
266 var ranges = this.table.getSelectionManager().getSelectionModel().getSelectedRanges();
267 var xmlNode = this.table.getTableModel().getRowData(ranges[0].minIndex);
268 viewSelection.addNode(xmlNode);
269 viewPane.setViewSelection(viewSelection);
270 }, this);
271
272 var select = new qx.ui.form.SelectBox();
273 this.getView().addHeaderComponent(select);
274 this.getView().setViewTitle("");
275 this.add(this.table, {flex:1});
276
277 select.addListener("changeValue", this.collectionSelectorListener, this);
278
279 org.argeo.ria.remote.RequestManager.getInstance().addListener("reload", function(event){
280 if(event.getDataType()!="collection") return;
281 select.removeListener("changeValue", this.collectionSelectorListener, this);
282 var collectionList = event.getContent();
283 select.removeAll();
284 for(key in collectionList){
285 var item = new qx.ui.form.ListItem(collectionList[key], "resource/slc/folder.png", key);
286 select.add(item);
287 if(key == this.getCollectionId()){
288 select.setSelected(item);
289 }
290 }
291 this.setCollectionId(select.getSelected().getValue());
292 select.addListener("changeValue", this.collectionSelectorListener, this);
293 }, this);
294
295 },
296
297 /**
298 * Use SlcApi to load the available collections.
299 */
300 loadCollections : function(){
301 var request = org.argeo.slc.ria.SlcApi.getListCollectionsService();
302 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
303 request.addListener("completed", function(response){
304 var xml = response.getContent();
305 var collections = {};
306 var nodes = org.argeo.ria.util.Element.selectNodes(xml, "//slc:ref", NSMap);
307 for(var i=0;i<nodes.length;i++){
308 var collId = qx.xml.Element.getSingleNodeText(nodes[i], ".");
309 collections[collId] = collId;
310 }
311 this.setCollectionList(collections);
312 org.argeo.ria.remote.RequestManager.getInstance().fireReloadEvent("collection", this.getCollectionList());
313 }, this);
314 request.setAsynchronous(false);
315 request.send();
316 },
317
318 /**
319 * Load the results of the currently selected collection.
320 */
321 loadList : function(){
322 var model = this.table.getTableModel();
323 model.removeRows(0, model.getRowCount());
324 var commandManager = org.argeo.ria.event.CommandsManager.getInstance();
325 var request = org.argeo.slc.ria.SlcApi.getListResultsService(this.getCollectionId(), null, [commandManager.getCommandById("loadtestlist"), this.getView()]);
326 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
327 request.addListener("completed", function(response){
328 var xml = response.getContent();
329 this.debug("Successfully loaded XML");
330 var nodes = org.argeo.ria.util.Element.selectNodes(xml, "//slc:result-attributes", NSMap);
331 //model.addRows(nodes);
332
333 for(var i=0; i<nodes.length;i++){
334 model.addRows([nodes[i]]);
335 }
336 }, request);
337 request.send();
338 },
339
340 /**
341 * Enable/disable the automatic reloading of the list.
342 * @param state {Boolean} Whether the automatic reloading must be started or stopped.
343 */
344 pollListLoading : function(state){
345 if(!this.timer){
346 this.timer = new qx.event.Timer(5000);
347 this.timer.addListener("interval", this.loadList, this);
348 }
349 if(state){
350 this.loadList();
351 this.timer.start();
352 }else{
353 this.timer.stop();
354 }
355 },
356
357 /**
358 * Creates a menu gui component from the currently loaded collectionList.
359 * @param command {qx.event.Command} The command on which to attach the created menu.
360 * @param checkSelection {Boolean} Whether at the end, we must check the current viewSelection to enable/disable the command accordingly.
361 */
362 collectionListToMenu : function(command, checkSelection){
363 command.setEnabled(false);
364 command.clearMenus();
365 var collectionList = this.getCollectionList();
366 if(!collectionList) return;
367 var submenus = [];
368 for(var key in collectionList){
369 if(this.getCollectionId() && key == this.getCollectionId()) continue;
370 submenus.push({
371 "label":collectionList[key],
372 "icon":"resource/slc/folder.png",
373 "commandId":key
374 });
375 }
376 submenus.push({'separator':true});
377 submenus.push({"label":"New...", "icon":"resource/slc/folder-new.png", "commandId":"slc.client.create"});
378 command.setMenu(submenus);
379 if(checkSelection){
380 var viewSelection = this.getView().getViewSelection();
381 if(viewSelection.getCount()) command.setEnabled(true);
382 }else{
383 command.setEnabled(true);
384 }
385 },
386 /**
387 * Use SlcApi "addResult" service to add selected results to a given collection.
388 * If collectionId is "slc.client.create", first triggers a modal dialog to enter a new collection name, then retrigger itself with the new id.
389 * @param collectionId {String} The id of the destination collection, or "slc.client.create".
390 * @param selectionType {String} "current_collection"|"current_selection". The first adds the whole collection content to the destination, the second only selected results.
391 */
392 copySelectionToCollection:function(collectionId, selectionType){
393 if(collectionId == "slc.client.create"){
394 var modal = new org.argeo.ria.components.Modal("Create collection", "resource/slc/folder-new.png");
395 modal.makePromptForm("Enter the new collection name", function(value){
396 if(value == ""){
397 alert("Please enter a name for the new collection!");
398 return false;
399 }
400 else {
401 // Create the collection now, then recall the callback with the new name.
402 this.copySelectionToCollection(value, selectionType);
403 return true;
404 }
405 }, this);
406 modal.attachAndShow();
407 return;
408 }
409
410 if(selectionType == "current_collection"){
411 this.error("Not implemented yet!");
412 }else if(selectionType == "current_selection"){
413 // get selection ID
414 var request = org.argeo.slc.ria.SlcApi.getAddResultService(collectionId,this.extractTestUuid());
415 request.addListener("completed", function(response){
416 this.info("Result successfully copied to collection!");
417 this.loadCollections();
418 }, this);
419 request.send();
420 }
421 },
422
423 /**
424 * Utilitary function to extract test unique id from the currently selected node.
425 * @return {String} The test unique id.
426 */
427 extractTestUuid: function(){
428 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
429 var xmlNodes = this.getView().getViewSelection().getNodes();
430 var uuid = qx.dom.Node.getText(org.argeo.ria.util.Element.selectSingleNode(xmlNodes[0], "slc:uuid"));
431 return uuid;
432 },
433
434 /**
435 * Listener of the collection selector (select box added to the viewpane header).
436 * @param event {qx.event.type.Event} The event.
437 */
438 collectionSelectorListener : function(event){
439 this.setCollectionId(event.getData());
440 this.loadList();
441 },
442
443 addScroll : function(){
444 return false;
445 }
446 }
447 });