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