2 * Description: display simple lists like slices or testbeds
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
12 $.fn.SimpleList = function( method ) {
13 /* Method calling logic */
14 if ( methods[method] ) {
15 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
16 } else if ( typeof method === 'object' || ! method ) {
17 return methods.init.apply( this, arguments );
19 $.error( 'Method ' + method + ' does not exist on jQuery.SimpleList' );
24 init : function( options ) {
25 return this.each(function(){
26 var $this = $(this), data = $this.data('SimpleList');
27 /* Subscribe to query updates */
28 var channel='/results/' + options.query_uuid + '/changed';
29 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
30 $.subscribe(channel, $this, update_plugin);
31 if (debug) window.messages.debug('subscribing to ' + channel);
32 $this.data('SimpleList', options);
35 destroy : function( ) {
36 if (debug) messages.debug("SimpleList.destroy...");
37 return this.each(function(){
38 var $this = $(this), data = $this.data('SimpleList');
39 // xxx not too sure what this is about
40 $(window).unbind('SimpleList');
41 $this.removeData('SimpleList');
44 update : function( content ) {
45 if (debug) messages.debug("SimpleList.update...");
50 // complexity here is mostly because a datatables-enabled table cannot
51 // be updated in a "normal" way using .html()
52 function update_plugin(e, rows) {
53 // e.data is what we passed in second argument to subscribe
54 // so here it is the jquery object attached to the plugin <div>
55 var $plugindiv=e.data;
56 // locate the <table> element; with datatables in the way,
57 // this might not be a direct son of the div-plugin
58 var $table=$plugindiv.find("table.simplelist").first();
59 // also we may or may not have a header
60 var $tbody=$table.find("tbody.simplelist").first();
61 var use_datatables = $table.hasClass("with-datatables");
62 if (debug) messages.debug($plugindiv.attr('id') + " udt= " + use_datatables);
64 // clear the spinning wheel: look up an ancestor that has the need-spin class
65 // do this before we might return
66 $plugindiv.closest('.need-spin').spin(false);
68 if (rows.length == 0) {
69 if (use_datatables) datatables_set_message ($table, $tbody, unfold.warning("No result"));
70 else regular_set_message ($table, $tbody, unfold.warning("No result"));
73 if (typeof rows[0].error != 'undefined') {
74 var error="ERROR: " + rows[0].error;
75 if (use_datatables) datatables_set_message ($table, $tbody, unfold.error(error));
76 else regular_set_message ($table, $tbody, unfold.error(error));
79 var options = $plugindiv.data().SimpleList;
80 if (use_datatables) datatables_update_table ($table,$tbody,rows,options.key);
81 else regular_update_table ($table,$tbody,rows,options.key);
85 // hard-wire a separate presentation depending on the key being used....
86 function cell(key, value) {
87 if (key == 'slice_hrn') {
88 return "<i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + "</a>";
89 } else if (key == 'network_hrn') {
90 return "<i class='icon-play-circle'></i>" + value ;
96 function regular_set_message ($table, $tbody, message) {
97 $tbody.html("<tr><td>"+message+"</td></tr>");
100 function regular_update_table ($table, $tbody, rows, key) {
101 if (debug) messages.debug('regular_update_table ' + rows.length + " rows");
102 var html=$.map(rows, function (row) { return html_row ( cell (key, row[key])); }).join();
106 function datatables_set_message ($table, $tbody, message) {
107 $table.dataTable().fnClearTable();
108 $table.dataTable().fnAddData( [ message ] );
109 $table.dataTable().fnDraw();
112 function datatables_update_table ($table, $tbody, rows, key) {
113 if (debug) messages.debug('datatables_update_table ' + rows.length + " rows");
114 $table.dataTable().fnClearTable();
115 // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
116 // The function can return any value to add to the array. A returned array will be flattened into the resulting array.
117 // this is wrong indeed so let's work around that
118 $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ cell (key,row[key]) ]] }) );
119 $table.dataTable().fnDraw();
122 function html_row (cell) { return "<tr><td class='simplelist'>"+cell+"</td></tr>"; }