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