]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/main/webapp/argeo-ria-lib/slc-web/class/org/argeo/slc/web/TestList.js
Create Argeo SLC RIA project
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / 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 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 : "resource/slc/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 : "resource/slc/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 : "resource/slc/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 : "resource/slc/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 : "resource/slc/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':'resource/slc/mime-xsl.png', 'commandId':'xsl'},
142 {'label':"Xml", 'icon':'resource/slc/mime-xml.png', 'commandId':'xml'},
143 {'label':"Excel", 'icon':'resource/slc/mime-xls.png', 'commandId':'xls'},
144 {'label':"Pdf", 'icon':'resource/slc/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 "copytocollection" : {
170 label : "Copy to...",
171 icon : "resource/slc/edit-copy.png",
172 shortcut : "Control+c",
173 enabled : false,
174 menu : "Selection",
175 toolbar : "selection",
176 callback : function(e){
177 // Call service to copy
178 },
179 submenu : {},
180 submenuCallback : function(commandId){
181 this.copySelectionToCollection(commandId, "current_selection");
182 },
183 init : function(){
184 // Call at command creation
185 org.argeo.ria.remote.RequestManager.getInstance().addListener("reload", function(event){
186 if(event.getDataType() == "collection" || event.getDataType() == "test_cases"){
187 var testList = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("list").getContent();
188 testList.collectionListToMenu(this, true);
189 }
190 }, this);
191 },
192 selectionChange : function(viewId, xmlNodes){
193 if(viewId != "list") return;
194 this.setEnabled(false);
195 if(xmlNodes == null || !xmlNodes.length) return;
196 this.setEnabled(true);
197 },
198 command : null
199 },
200 "deletetest" : {
201 label : "Delete",
202 icon : "resource/slc/edit-delete.png",
203 shortcut : "Control+d",
204 enabled : false,
205 menu : "Selection",
206 toolbar : "selection",
207 callback : function(e){
208 var modal = new org.argeo.ria.components.Modal("Confirm", null);
209 modal.addConfirm("Are you sure you want to delete<br> test " + this.extractTestUuid() + "?");
210 modal.addListener("ok", function(){
211 var request = org.argeo.slc.ria.SlcApi.getRemoveResultService(this.getCollectionId(), this.extractTestUuid());
212 request.addListener("completed", function(response){
213 this.loadCollections();
214 this.loadList();
215 this.info("Test was successfully deleted");
216 }, this);
217 request.send();
218 }, this);
219 modal.attachAndShow();
220 },
221 selectionChange : function(viewId, xmlNodes){
222 if(viewId != "list") return;
223 this.setEnabled(false);
224 if(xmlNodes == null || !xmlNodes.length) return;
225 this.setEnabled(true);
226 },
227 command : null
228 }
229 }
230 }
231 },
232
233 members : {
234 init : function(viewPane, data){
235 this.setView(viewPane);
236 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
237 },
238 load : function(){
239 this.table.set({
240 statusBarVisible: false,
241 showCellFocusIndicator:false,
242 columnVisibilityButtonVisible:false,
243 contextMenu : org.argeo.ria.event.CommandsManager.getInstance().createMenuFromIds(["opentest", "download", "copytocollection", "deletetest"]),
244 decorator : new qx.ui.decoration.Background("#fff")
245 });
246 var viewPane = this.getView();
247 this.table.addListener("dblclick", function(e){
248 org.argeo.ria.event.CommandsManager.getInstance().executeCommand("opentest");
249 }, this);
250 var columnModel = this.table.getTableColumnModel();
251 columnModel.getBehavior().setWidth(0, "60%");
252 var renderer = new org.argeo.slc.web.XmlRenderer();
253 columnModel.setDataCellRenderer(0, renderer);
254 columnModel.setDataCellRenderer(1, renderer);
255
256 this.table.getSelectionManager().getSelectionModel().addListener("changeSelection", function(e){
257 var viewSelection = this.getViewSelection();
258 viewSelection.clear();
259 var selectionModel = this.table.getSelectionManager().getSelectionModel();
260 if(!selectionModel.getSelectedCount()){
261 return;
262 }
263 var ranges = this.table.getSelectionManager().getSelectionModel().getSelectedRanges();
264 var xmlNode = this.table.getTableModel().getRowData(ranges[0].minIndex);
265 viewSelection.addNode(xmlNode);
266 //viewPane.setViewSelection(viewSelection);
267 }, this);
268
269 var select = new qx.ui.form.SelectBox();
270 this.getView().addHeaderComponent(select);
271 this.getView().setViewTitle("");
272 this.add(this.table, {flex:1});
273
274 select.addListener("changeValue", this.collectionSelectorListener, this);
275
276 org.argeo.ria.remote.RequestManager.getInstance().addListener("reload", function(event){
277 if(event.getDataType()!="collection") return;
278 select.removeListener("changeValue", this.collectionSelectorListener, this);
279 var collectionList = event.getContent();
280 select.removeAll();
281 for(key in collectionList){
282 var item = new qx.ui.form.ListItem(collectionList[key], "resource/slc/folder.png", key);
283 select.add(item);
284 if(key == this.getCollectionId()){
285 select.setSelected(item);
286 }
287 }
288 if(qx.lang.Object.getLength(collectionList)){
289 this.setCollectionId(select.getSelected().getValue());
290 }
291 select.addListener("changeValue", this.collectionSelectorListener, this);
292 }, this);
293
294 },
295
296 /**
297 * Use SlcApi to load the available collections.
298 */
299 loadCollections : function(){
300 var request = org.argeo.slc.ria.SlcApi.getListCollectionsService();
301 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
302 request.addListener("completed", function(response){
303 var xml = response.getContent();
304 var collections = {};
305 var nodes = org.argeo.ria.util.Element.selectNodes(xml, "//slc:ref", NSMap);
306 for(var i=0;i<nodes.length;i++){
307 var collId = qx.xml.Element.getSingleNodeText(nodes[i], ".");
308 collections[collId] = collId;
309 }
310 this.setCollectionList(collections);
311 org.argeo.ria.remote.RequestManager.getInstance().fireReloadEvent("collection", this.getCollectionList());
312 }, this);
313 request.setAsynchronous(false);
314 request.send();
315 },
316
317 /**
318 * Load the results of the currently selected collection.
319 */
320 loadList : function(){
321 var model = this.table.getTableModel();
322 model.removeRows(0, model.getRowCount());
323 var commandManager = org.argeo.ria.event.CommandsManager.getInstance();
324 var request = org.argeo.slc.ria.SlcApi.getListResultsService(this.getCollectionId(), null, [commandManager.getCommandById("loadtestlist"), this.getView()]);
325 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
326 request.addListener("completed", function(response){
327 var xml = response.getContent();
328 this.debug("Successfully loaded XML");
329 var nodes = org.argeo.ria.util.Element.selectNodes(xml, "//slc:result-attributes", NSMap);
330 //model.addRows(nodes);
331
332 for(var i=0; i<nodes.length;i++){
333 model.addRows([nodes[i]]);
334 }
335 }, request);
336 request.send();
337 },
338
339 /**
340 * Enable/disable the automatic reloading of the list.
341 * @param state {Boolean} Whether the automatic reloading must be started or stopped.
342 */
343 pollListLoading : function(state){
344 if(!this.timer){
345 this.timer = new qx.event.Timer(5000);
346 this.timer.addListener("interval", this.loadList, this);
347 }
348 if(state){
349 this.loadList();
350 this.timer.start();
351 }else{
352 this.timer.stop();
353 }
354 },
355
356 /**
357 * Creates a menu gui component from the currently loaded collectionList.
358 * @param command {qx.event.Command} The command on which to attach the created menu.
359 * @param checkSelection {Boolean} Whether at the end, we must check the current viewSelection to enable/disable the command accordingly.
360 */
361 collectionListToMenu : function(command, checkSelection){
362 command.setEnabled(false);
363 command.clearMenus();
364 var collectionList = this.getCollectionList();
365 if(!collectionList) return;
366 var submenus = [];
367 for(var key in collectionList){
368 if(this.getCollectionId() && key == this.getCollectionId()) continue;
369 submenus.push({
370 "label":collectionList[key],
371 "icon":"resource/slc/folder.png",
372 "commandId":key
373 });
374 }
375 submenus.push({'separator':true});
376 submenus.push({"label":"New...", "icon":"resource/slc/folder-new.png", "commandId":"slc.client.create"});
377 command.setMenu(submenus);
378 if(checkSelection){
379 var viewSelection = this.getView().getViewSelection();
380 if(viewSelection.getCount()) command.setEnabled(true);
381 }else{
382 command.setEnabled(true);
383 }
384 },
385 /**
386 * Use SlcApi "addResult" service to add selected results to a given collection.
387 * If collectionId is "slc.client.create", first triggers a modal dialog to enter a new collection name, then retrigger itself with the new id.
388 * @param collectionId {String} The id of the destination collection, or "slc.client.create".
389 * @param selectionType {String} "current_collection"|"current_selection". The first adds the whole collection content to the destination, the second only selected results.
390 */
391 copySelectionToCollection:function(collectionId, selectionType){
392 if(collectionId == "slc.client.create"){
393 var modal = new org.argeo.ria.components.Modal("Create collection", "resource/slc/folder-new.png");
394 modal.makePromptForm("Enter the new collection name", function(value){
395 if(value == ""){
396 alert("Please enter a name for the new collection!");
397 return false;
398 }
399 else {
400 // Create the collection now, then recall the callback with the new name.
401 this.copySelectionToCollection(value, selectionType);
402 return true;
403 }
404 }, this);
405 modal.attachAndShow();
406 return;
407 }
408
409 var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus();
410 if(currentFocus.getViewId() == "applet"){
411 alert("Should copy data from the applet - command was " + collectionId);
412 return;
413 }
414
415 if(selectionType == "current_collection"){
416 this.error("Not implemented yet!");
417 }else if(selectionType == "current_selection"){
418 // get selection ID
419 var request = org.argeo.slc.ria.SlcApi.getAddResultService(collectionId,this.extractTestUuid());
420 request.addListener("completed", function(response){
421 this.info("Result successfully copied to collection!");
422 this.loadCollections();
423 }, this);
424 request.send();
425 }
426 },
427
428 /**
429 * Utilitary function to extract test unique id from the currently selected node.
430 * @return {String} The test unique id.
431 */
432 extractTestUuid: function(){
433 var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
434 var xmlNodes = this.getView().getViewSelection().getNodes();
435 var uuid = qx.dom.Node.getText(org.argeo.ria.util.Element.selectSingleNode(xmlNodes[0], "slc:uuid"));
436 return uuid;
437 },
438
439 /**
440 * Listener of the collection selector (select box added to the viewpane header).
441 * @param event {qx.event.type.Event} The event.
442 */
443 collectionSelectorListener : function(event){
444 this.setCollectionId(event.getData());
445 this.loadList();
446 },
447
448 addScroll : function(){
449 return false;
450 },
451
452 close : function(){
453 if(this.timer){
454 this.pollListLoading(false);
455 }
456 }
457
458 }
459 });