]> 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
QX 1.1 Migration
[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 = org.argeo.slc.ria.SlcApi.DEFAULT_CONTEXT + "/"+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.setMenuDef(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 if(appletView.contentExists(this.getCurrentOpenInstanceId())){
276 appletView.closeCurrent();
277 }
278 this.setCurrentOpenInstanceId(null);
279 }
280 this.loadCollections();
281 this.loadList();
282 this.info("Test was successfully deleted");
283 }, this);
284 request.send();
285 }, this);
286 modal.attachAndShow();
287 },
288 selectionChange : function(viewId, xmlNodes){
289 if(viewId != "list") return;
290 this.setEnabled(false);
291 if(xmlNodes == null || !xmlNodes.length) return;
292 this.setEnabled(true);
293 },
294 command : null
295 }
296 }
297 }
298 },
299
300 members : {
301 init : function(viewPane, data){
302 this.setView(viewPane);
303 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
304 },
305 load : function(){
306 this.table.set({
307 statusBarVisible: false,
308 showCellFocusIndicator:false,
309 columnVisibilityButtonVisible:false,
310 contextMenu : org.argeo.ria.event.CommandsManager.getInstance().createMenuFromIds(["opentest", "download", "copytocollection", "deletetest"]),
311 decorator : new qx.ui.decoration.Background("#fff")
312 });
313 var viewPane = this.getView();
314 this.table.addListener("dblclick", function(e){
315 var pref = this.getRiaPreferenceValue("slc.web.TestList.DefaultAction");
316 if(pref == "Open"){
317 org.argeo.ria.event.CommandsManager.getInstance().executeCommand("opentest");
318 }else{
319 var uuid = this.extractTestUuid();
320 var url = "../resultView.xslt?uuid="+uuid;
321 alert('Should download : '+url);
322 }
323 }, this);
324 var columnModel = this.table.getTableColumnModel();
325 columnModel.getBehavior().setWidth(0, "60%");
326 var renderer = new org.argeo.slc.web.XmlRenderer();
327 columnModel.setDataCellRenderer(0, renderer);
328 columnModel.setDataCellRenderer(1, renderer);
329
330 this.table.getSelectionManager().getSelectionModel().addListener("changeSelection", function(e){
331 var viewSelection = this.getViewSelection();
332 viewSelection.clear();
333 var selectionModel = this.table.getSelectionManager().getSelectionModel();
334 if(!selectionModel.getSelectedCount()){
335 return;
336 }
337 var ranges = this.table.getSelectionManager().getSelectionModel().getSelectedRanges();
338 var xmlNode = this.table.getTableModel().getRowData(ranges[0].minIndex);
339 viewSelection.addNode(xmlNode);
340 //viewPane.setViewSelection(viewSelection);
341 }, this);
342
343 var select = new qx.ui.form.SelectBox();
344 this.getView().addHeaderComponent(select);
345 this.getView().setViewTitle("");
346 this.add(this.table, {flex:1});
347
348 select.addListener("changeSelection", this.collectionSelectorListener, this);
349
350 org.argeo.ria.remote.RequestManager.getInstance().addListener("reload", function(event){
351 if(event.getDataType()!="collection") return;
352 select.removeListener("changeSelection", this.collectionSelectorListener, this);
353 var collectionList = event.getContent();
354 select.removeAll();
355 for(key in collectionList){
356 var item = new qx.ui.form.ListItem(collectionList[key], "org/argeo/slc/ria/folder.png", key);
357 select.add(item);
358 if(key == this.getCollectionId()){
359 select.setSelection([item]);
360 }
361 }
362 if(qx.lang.Object.getLength(collectionList)){
363 var selection = select.getSelection();
364 var value = selection[0].getModel();
365 this.setCollectionId(value);
366 }
367 select.addListener("changeSelection", this.collectionSelectorListener, this);
368 }, this);
369
370 },
371
372 /**
373 * Use SlcApi to load the available collections.
374 */
375 loadCollections : function(){
376 var request = org.argeo.slc.ria.SlcApi.getListCollectionsService();
377 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
378 request.addListener("completed", function(response){
379 var xml = response.getContent();
380 var collections = {};
381 var nodes = org.argeo.ria.util.Element.selectNodes(xml, "//slc:ref", NSMap);
382 for(var i=0;i<nodes.length;i++){
383 var collId = qx.xml.Element.getSingleNodeText(nodes[i], ".");
384 collections[collId] = collId;
385 }
386 this.setCollectionList(collections);
387 org.argeo.ria.remote.RequestManager.getInstance().fireReloadEvent("collection", this.getCollectionList());
388 }, this);
389 request.setAsynchronous(false);
390 request.send();
391 },
392
393 /**
394 * Load the results of the currently selected collection.
395 */
396 loadList : function(){
397 var model = this.table.getTableModel();
398 model.removeRows(0, model.getRowCount());
399 var commandManager = org.argeo.ria.event.CommandsManager.getInstance();
400 var request = org.argeo.slc.ria.SlcApi.getListResultsService(this.getCollectionId(), null, [commandManager.getCommandById("loadtestlist"), this.getView()]);
401 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
402 request.addListener("completed", function(response){
403 var xml = response.getContent();
404 this.debug("Successfully loaded XML");
405 var nodes = org.argeo.ria.util.Element.selectNodes(xml, "//slc:result-attributes", NSMap);
406 //model.addRows(nodes);
407
408 for(var i=0; i<nodes.length;i++){
409 model.addRows([nodes[i]]);
410 }
411 }, request);
412 request.send();
413 },
414
415 /**
416 * Enable/disable the automatic reloading of the list.
417 * @param state {Boolean} Whether the automatic reloading must be started or stopped.
418 */
419 pollListLoading : function(state){
420 if(!this.timer){
421 this.timer = new qx.event.Timer(5000);
422 this.timer.addListener("interval", this.loadList, this);
423 }
424 if(state){
425 this.loadList();
426 this.timer.start();
427 }else{
428 this.timer.stop();
429 }
430 },
431
432 /**
433 * Creates a menu gui component from the currently loaded collectionList.
434 * @param command {qx.ui.core.Command} The command on which to attach the created menu.
435 * @param checkSelection {Boolean} Whether at the end, we must check the current viewSelection to enable/disable the command accordingly.
436 */
437 collectionListToMenu : function(command, checkSelection){
438 command.setEnabled(false);
439 command.clearMenus();
440 var collectionList = this.getCollectionList();
441 if(!collectionList) return;
442 var submenus = [];
443 for(var key in collectionList){
444 if(this.getCollectionId() && key == this.getCollectionId()) continue;
445 submenus.push({
446 "label":collectionList[key],
447 "icon":"org/argeo/slc/ria/folder.png",
448 "commandId":key
449 });
450 }
451 submenus.push({'separator':true});
452 submenus.push({"label":"New...", "icon":"org/argeo/slc/ria/folder-new.png", "commandId":"slc.client.create"});
453 command.setMenuDef(submenus);
454 if(checkSelection){
455 var viewSelection = this.getView().getViewSelection();
456 if(viewSelection.getCount()) command.setEnabled(true);
457 }else{
458 command.setEnabled(true);
459 }
460 },
461 /**
462 * Use SlcApi "addResult" service to add selected results to a given collection.
463 * If collectionId is "slc.client.create", first triggers a modal dialog to enter a new collection name, then retrigger itself with the new id.
464 * @param collectionId {String} The id of the destination collection, or "slc.client.create".
465 * @param selectionType {String} "current_collection"|"current_selection". The first adds the whole collection content to the destination, the second only selected results.
466 */
467 copySelectionToCollection:function(collectionId, selectionType){
468 if(collectionId == "slc.client.create"){
469 var modal = new org.argeo.ria.components.Modal("Create collection", "org/argeo/slc/ria/folder-new.png");
470 modal.makePromptForm("Enter the new collection name", function(value){
471 if(value == ""){
472 alert("Please enter a name for the new collection!");
473 return false;
474 }
475 else {
476 // Create the collection now, then recall the callback with the new name.
477 this.copySelectionToCollection(value, selectionType);
478 return true;
479 }
480 }, this);
481 modal.attachAndShow();
482 return;
483 }
484
485 var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus();
486 if(currentFocus.getViewId() == "applet"){
487 alert("Should copy data from the applet - command was " + collectionId);
488 return;
489 }
490
491 if(selectionType == "current_collection"){
492 this.error("Not implemented yet!");
493 }else if(selectionType == "current_selection"){
494 // get selection ID
495 var request = org.argeo.slc.ria.SlcApi.getAddResultService(collectionId,this.extractTestUuid());
496 request.addListener("completed", function(response){
497 this.info("Result successfully copied to collection!");
498 this.loadCollections();
499 }, this);
500 request.send();
501 }
502 },
503
504 /**
505 * Utilitary function to extract test unique id from the currently selected node.
506 * @return {String} The test unique id.
507 */
508 extractTestUuid: function(){
509 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
510 var xmlNodes = this.getView().getViewSelection().getNodes();
511 var uuid = qx.dom.Node.getText(org.argeo.ria.util.Element.selectSingleNode(xmlNodes[0], "slc:uuid"));
512 return uuid;
513 },
514
515 /**
516 * Listener of the collection selector (select box added to the viewpane header).
517 * @param event {qx.event.type.Event} The event.
518 */
519 collectionSelectorListener : function(event){
520 var selection = event.getData();
521 this.setCollectionId(selection[0].getModel());
522 this.loadList();
523 },
524
525 addScroll : function(){
526 return false;
527 },
528
529 close : function(){
530 if(this.timer){
531 this.pollListLoading(false);
532 }
533 }
534
535 }
536 });