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