254fb22635964456e35718848d4348ecbc0a3708
[unfold.git] / plugins / lists / static / js / simplelist.js
1 /**
2  * Description: display simple lists like slices or testbeds
3  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 (function($){
8
9     var debug=false;
10     debug=true
11
12     var SimpleList = Plugin.extend ({
13
14         init: function (options, element) {
15             this.classname=options.classname;
16             this._super (options, element);
17             this.buffered_records=[];
18             this.listen_query(options.query_uuid);
19         }, 
20
21         on_query_in_progress: function() {
22             messages.debug("on_query_in_progress");
23             this.spin(true);
24         },
25
26         on_query_done: function() {
27             this._display_table();
28             this.unspin();
29         },
30         
31         on_new_record: function(record) {
32             this.buffered_records.push(record);
33         },
34
35     /* Private methods */
36
37         _display_table: function() {
38             var self=this;
39             var $plugindiv = this.elmt();
40             var options = this.options;
41             // locate the <table> element; with datatables in the way,
42             // this might not be a direct son of the div-plugin
43             var $table = $plugindiv.find("table."+this.classname).first();
44             // also we may or may not have a header
45             var $tbody = $table.find("tbody."+this.classname).first();
46             var use_datatables = $table.hasClass("with-datatables");
47             var rows=self.buffered_records;
48             self.buffered_records=[];
49             if (debug) 
50                 messages.debug($plugindiv.attr('id') + " udt= " + use_datatables + " rows="+rows.length);
51         
52             if (rows.length == 0) {
53                 if (use_datatables)
54                     this._datatables_set_message ($table, $tbody, unfold.warning("No result"));
55                 else
56                     this._regular_set_message ($table, $tbody, unfold.warning("No result"));
57                 return;
58             }
59
60             if (typeof rows[0].error != 'undefined') {
61                 var error="ERROR: " + rows[0].error;
62                 if (use_datatables) 
63                     this._datatables_set_message ($table, $tbody, unfold.error(error));
64                 else
65                     this._regular_set_message ($table, $tbody, unfold.error(error));
66                 return;
67             }
68
69             if (use_datatables) 
70                 this._datatables_update_table($table, $tbody, rows, options.key);
71             else
72                 this._regular_update_table($table, $tbody, rows, options.key, this.classname);
73         },
74
75         _regular_set_message: function ($table, $tbody, message) {
76             $tbody.html("<tr><td>"+message+"</td></tr>");
77         },
78
79         _regular_update_table: function ($table, $tbody, rows, key, classname) {
80             if (debug)
81                 messages.debug('regular_update_table ' + rows.length + " rows" + 
82                                " key=" + key + " classname=" + this.classname);
83             var self=this;
84             var html=$.map(rows, function (row) {
85                 var value = row;
86                 $.each(key.split('.'), function(i, k) {
87                     if ($.isArray(value)) {
88                         value = $.map(value, function(val, i) { return val[k]});
89                     } else {
90                         value = value[k];
91                     }
92                 });
93                 if ($.isArray(value)) {
94                     return $.map(value, function(val, i) { 
95                         return self._html_row ( self._cell (key, val), this.classname); 
96                     });
97                 } else {
98                     return self._html_row ( self._cell (key, value), this.classname);
99                 }
100             }).join();
101             $tbody.html(html);
102         },
103     
104         _datatables_set_message: function ($table, $tbody, message) {
105             $table.dataTable().fnClearTable();
106             $table.dataTable().fnAddData( [ message ] );
107             $table.dataTable().fnDraw();
108         },
109
110         _datatables_update_table: function ($table, $tbody, rows, key) {
111             if (debug) messages.debug('datatables_update_table ' + rows.length + " rows");
112             $table.dataTable().fnClearTable();
113             // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
114             // The function can return any value to add to the array. 
115             // A returned array will be flattened into the resulting array.
116             // this is wrong indeed so let's work around that
117             var self=this;
118             $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ self._cell (key,row[key]) ]] }) );
119             $table.dataTable().fnDraw();
120         },
121     
122         _html_row: function (cell, classname) { 
123             return "<tr><td class='"+classname+"'>"+cell+"</td></tr>"; 
124         },
125     
126         // hard-wire a separate presentation depending on the key being used....
127         _cell: function (key, value) {
128             if (key == 'slice.slice_hrn') {
129                 return "<i class='icon-play-circle'></i><a href='/portal/slice/" + value + "'>" + value + "</a>";
130             } else if (key == 'platform') {
131                 return "<i class='icon-play-circle'></i><a href='/portal/platform/" + value + "'>" + value + "</a>";
132             } else {
133                 return value;
134             }
135         },
136     });
137
138     $.plugin('SimpleList', SimpleList);
139
140 })( jQuery );