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