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.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;
23 this.warning_msg = "No Result";
27 on_query_in_progress: function() {
28 messages.debug("on_query_in_progress");
32 on_query_done: function() {
33 this._display_table();
35 console.log("query simple list done");
37 console.log(this.buffered_records);
40 on_new_record: function(record) {
41 this.buffered_records.push(record);
46 _display_table: function() {
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=[];
59 messages.debug($plugindiv.attr('id') + " udt= " + use_datatables + " rows="+rows.length);
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) {
65 this._datatables_set_message ($table, $tbody, unfold.warning(self.warning_msg));
67 this._regular_set_message ($table, $tbody, unfold.warning(self.warning_msg));
72 if (typeof rows[0].error != 'undefined') {
73 var error="ERROR: " + rows[0].error;
75 this._datatables_set_message ($table, $tbody, unfold.error(error));
77 this._regular_set_message ($table, $tbody, unfold.error(error));
83 this._datatables_update_table($table, $tbody, rows, options.key);
85 this._regular_update_table($table, $tbody, rows, options.key, this.classname);
89 _regular_set_message: function ($table, $tbody, message) {
90 $tbody.html("<tr><td>"+message+"</td></tr>");
93 _regular_update_table: function ($table, $tbody, rows, key, classname) {
95 messages.debug('regular_update_table ' + rows.length + " rows" +
96 " key=" + key + " classname=" + this.classname);
98 var html=$.map(rows, function (row) {
100 $.each(key.split('.'), function(i, k) {
101 if ($.isArray(value)) {
102 value = $.map(value, function(val, i) { return val[k]});
107 if ($.isArray(value)) {
108 return $.map(value, function(val, i) {
109 return self._html_row ( self._cell (key, val), this.classname);
112 return self._html_row ( self._cell (key, value), this.classname);
118 _datatables_set_message: function ($table, $tbody, message) {
119 $table.dataTable().fnClearTable();
120 $table.dataTable().fnAddData( [ message ] );
121 $table.dataTable().fnDraw();
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
132 $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ self._cell (key,row[key]) ]] }) );
133 $table.dataTable().fnDraw();
136 _html_row: function (cell, classname) {
137 return "<tr><td class='"+classname+"'>"+cell+"</td></tr>";
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>";
152 $.plugin('SimpleList', SimpleList);