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