2 * Description: display simple lists like slices or testbeds
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
12 var SimpleList = Plugin.extend ({
14 init: function (options, element) {
15 this._super (options, element);
16 this.buffered_records=[];
17 this.listen_query(options.query_uuid);
20 on_query_in_progress: function() {
21 messages.debug("on_query_in_progress");
25 on_query_done: function() {
26 this._display_table();
30 on_new_record: function(record) {
31 this.buffered_records.push(record);
36 _display_table: function() {
38 var $plugindiv = this.elmt();
39 var options = this.options;
40 var classname=options.classname;
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."+classname).first();
44 // also we may or may not have a header
45 var $tbody = $table.find("tbody."+classname).first();
46 var use_datatables = $table.hasClass("with-datatables");
47 var rows=self.buffered_records;
48 self.buffered_records=[];
50 messages.debug($plugindiv.attr('id') + " udt= " + use_datatables + " rows="+rows.length);
52 if (rows.length == 0) {
54 this._datatables_set_message ($table, $tbody, unfold.warning("No result"));
56 this._regular_set_message ($table, $tbody, unfold.warning("No result"));
60 if (typeof rows[0].error != 'undefined') {
61 var error="ERROR: " + rows[0].error;
63 this._datatables_set_message ($table, $tbody, unfold.error(error));
65 this._regular_set_message ($table, $tbody, unfold.error(error));
70 this._datatables_update_table($table, $tbody, rows, options.key);
72 this._regular_update_table($table, $tbody, rows, options.key, classname);
75 _regular_set_message: function ($table, $tbody, message) {
76 $tbody.html("<tr><td>"+message+"</td></tr>");
79 _regular_update_table: function ($table, $tbody, rows, key, classname) {
81 messages.debug('regular_update_table ' + rows.length + " rows" + " key=" + key + " classname=" + classname);
83 var html=$.map(rows, function (row) {
85 $.each(key.split('.'), function(i, k) {
86 if ($.isArray(value)) {
87 value = $.map(value, function(val, i) { return val[k]});
92 if ($.isArray(value)) {
93 return $.map(value, function(val, i) {
94 return self._html_row ( self._cell (key, val), classname);
97 return self._html_row ( self._cell (key, value), classname);
103 _datatables_set_message: function ($table, $tbody, message) {
104 $table.dataTable().fnClearTable();
105 $table.dataTable().fnAddData( [ message ] );
106 $table.dataTable().fnDraw();
109 _datatables_update_table: function ($table, $tbody, rows, key) {
110 if (debug) messages.debug('datatables_update_table ' + rows.length + " rows");
111 $table.dataTable().fnClearTable();
112 // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
113 // The function can return any value to add to the array. A returned array will be flattened into the resulting array.
114 // this is wrong indeed so let's work around that
116 $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ self._cell (key,row[key]) ]] }) );
117 $table.dataTable().fnDraw();
120 _html_row: function (cell, classname) {
121 return "<tr><td class='"+classname+"'>"+cell+"</td></tr>";
124 // hard-wire a separate presentation depending on the key being used....
125 _cell: function (key, value) {
126 if (key == 'slice.slice_hrn') {
127 return "<i class='icon-play-circle'></i><a href='/portal/slice/" + value + "'>" + value + "</a>";
128 } else if (key == 'platform') {
129 return "<i class='icon-play-circle'></i><a href='/portal/platform/" + value + "'>" + value + "</a>";
136 $.plugin('SimpleList', SimpleList);