myslice.ini possible themes = onelab, fed4fire, fibre, smartfire
[myslice.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         /* Optional parameter warning_msg */
20         if(options.warning_msg != "unknown-setting-warning_msg"){
21             this.warning_msg = options.warning_msg;
22         }else{
23             this.warning_msg = "No Result";
24         }
25         }, 
26
27         on_query_in_progress: function() {
28             messages.debug("on_query_in_progress");
29             this.spin(true);
30         },
31
32         on_query_done: function() {
33                 this._display_table();
34             this.unspin();
35             console.log("query simple list done");
36             console.log(this);
37             console.log(this.buffered_records);
38         },
39         
40         on_new_record: function(record) {
41             this.buffered_records.push(record);
42         },
43
44     /* Private methods */
45
46         _display_table: function() {
47             var self=this;
48         var $plugindiv = this.elmt();
49         var options = this.options;
50         // locate the <table> element; with datatables in the way,
51         // this might not be a direct son of the div-plugin
52         var $table = $plugindiv.find("table."+this.classname).first();
53         // also we may or may not have a header
54         var $tbody = $table.find("tbody."+this.classname).first();
55         var use_datatables = $table.hasClass("with-datatables");
56             var rows=self.buffered_records;
57             self.buffered_records=[];
58         if (debug){
59                     messages.debug($plugindiv.attr('id') + " udt= " + use_datatables + " rows="+rows.length);
60             }
61         // check if rows contains results related to the object key
62         object_key = this.options.key.split('.');
63         if (rows.length == 0 || rows[0][object_key[0]].length == 0) {
64                 if (use_datatables){
65                 this._datatables_set_message ($table, $tbody, unfold.warning(self.warning_msg));
66                 }else{
67                     this._regular_set_message ($table, $tbody, unfold.warning(self.warning_msg));
68             }
69                     return;
70         }
71
72         if (typeof rows[0].error != 'undefined') {
73                     var error="ERROR: " + rows[0].error;
74                     if (use_datatables){
75                 this._datatables_set_message ($table, $tbody, unfold.error(error));
76                     }else{
77                     this._regular_set_message ($table, $tbody, unfold.error(error));
78             }
79                     return;
80         }
81
82             if (use_datatables){
83                     this._datatables_update_table($table, $tbody, rows, options.key);
84             }else{
85                     this._regular_update_table($table, $tbody, rows, options.key, this.classname);
86         }
87         },
88
89         _regular_set_message: function ($table, $tbody, message) {
90             $tbody.html("<tr><td>"+message+"</td></tr>");
91         },
92
93         _regular_update_table: function ($table, $tbody, rows, key, classname) {
94             if (debug)
95                 messages.debug('regular_update_table ' + rows.length + " rows" + 
96                                " key=" + key + " classname=" + this.classname);
97             var self=this;
98             var html=$.map(rows, function (row) {
99                 var value = row;
100                 $.each(key.split('.'), function(i, k) {
101                     if ($.isArray(value)) {
102                         value = $.map(value, function(val, i) { return val[k]});
103                     } else {
104                         value = value[k];
105                     }
106                 });
107                 if ($.isArray(value)) {
108                     return $.map(value, function(val, i) { 
109                         return self._html_row ( self._cell (key, val), this.classname); 
110                     });
111                 } else {
112                     return self._html_row ( self._cell (key, value), this.classname);
113                 }
114             }).join();
115             $tbody.html(html);
116         },
117     
118         _datatables_set_message: function ($table, $tbody, message) {
119             $table.dataTable().fnClearTable();
120             $table.dataTable().fnAddData( [ message ] );
121             $table.dataTable().fnDraw();
122         },
123
124         _datatables_update_table: function ($table, $tbody, rows, key) {
125             if (debug) messages.debug('datatables_update_table ' + rows.length + " rows");
126             $table.dataTable().fnClearTable();
127             // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
128             // The function can return any value to add to the array. 
129             // A returned array will be flattened into the resulting array.
130             // this is wrong indeed so let's work around that
131             var self=this;
132             $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ self._cell (key,row[key]) ]] }) );
133             $table.dataTable().fnDraw();
134         },
135     
136         _html_row: function (cell, classname) { 
137             return "<tr><td class='"+classname+"'>"+cell+"</td></tr>"; 
138         },
139     
140         // hard-wire a separate presentation depending on the key being used....
141         _cell: function (key, value) {
142             if (key == 'slice.slice_hrn') {
143                 return "<i class='icon-play-circle'></i><a href='/portal/slice/" + value + "'>" + value + "</a>";
144             } else if (key == 'platform') {
145                 return "<i class='icon-play-circle'></i><a href='/portal/platform/" + value + "'>" + value + "</a>";
146             } else {
147                 return value;
148             }
149         },
150     });
151
152     $.plugin('SimpleList', SimpleList);
153
154 })( jQuery );