2 * MySlice SimpleList plugin
3 * URL: http://trac.myslice.info
4 * Description: display simple lists like slices or testbeds
5 * Author: The MySlice Team
6 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
15 $.fn.SimpleList = function( method ) {
16 /* Method calling logic */
17 if ( methods[method] ) {
18 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
19 } else if ( typeof method === 'object' || ! method ) {
20 return methods.init.apply( this, arguments );
22 $.error( 'Method ' + method + ' does not exist on jQuery.SimpleList' );
27 init : function( options ) {
28 return this.each(function(){
29 var $this = $(this), data = $this.data('SimpleList');
30 /* Subscribe to query updates */
31 var channel='/results/' + options.query_uuid + '/changed';
32 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
33 $.subscribe(channel, $this, update_plugin);
34 if (debug) window.console.log('subscribing to ' + channel);
35 $this.data('SimpleList', options);
38 destroy : function( ) {
39 if (debug) console.log("SimpleList.destroy...");
40 return this.each(function(){
41 var $this = $(this), data = $this.data('SimpleList');
42 // xxx not too sure what this is about
43 $(window).unbind('SimpleList');
44 $this.removeData('SimpleList');
47 update : function( content ) {
48 if (debug) console.log("SimpleList.update...");
53 // complexity here is mostly because a datatables-enabled table cannot
54 // be updated in a "normal" way using .html()
55 function update_plugin(e, rows) {
56 // e.data is what we passed in second argument to subscribe
57 // so here it is the jquery object attached to the plugin <div>
58 var $plugindiv=e.data;
59 // locate the <table> element; with datatables in the way,
60 // this might not be a direct son of the div-plugin
61 var $table=$plugindiv.find("table.simplelist").first();
62 // also we may or may not have a header
63 var $tbody=$table.find("tbody.simplelist").first();
64 var use_datatables = $table.hasClass("with-datatables");
65 if (debug) console.log($plugindiv.attr('id') + " udt= " + use_datatables);
67 // clear the spinning wheel: look up an ancestor that has the need-spin class
68 // do this before we might return
69 $plugindiv.closest('.need-spin').spin(false);
71 if (rows.length == 0) {
72 if (use_datatables) datatables_set_message ($table, $tbody, unfold.warning("No result"));
73 else regular_set_message ($table, $tbody, unfold.warning("No result"));
76 if (typeof rows[0].error != 'undefined') {
77 var error="ERROR: " + rows[0].error;
78 if (use_datatables) datatables_set_message ($table, $tbody, unfold.error(error));
79 else regular_set_message ($table, $tbody, unfold.error(error));
82 var options = $plugindiv.data().SimpleList;
83 if (use_datatables) datatables_update_table ($table,$tbody,rows,options.key);
84 else regular_update_table ($table,$tbody,rows,options.key);
88 // hard-wire a separate presentation depending on the key being used....
89 function cell(key, value) {
90 if (key == 'slice_hrn') {
91 return "<i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + "</a>";
92 } else if (key == 'network_hrn') {
93 return "<i class='icon-play-circle'></i>" + value ;
99 function regular_set_message ($table, $tbody, message) {
100 $tbody.html("<tr><td>"+message+"</td></tr>");
103 function regular_update_table ($table, $tbody, rows, key) {
104 if (debug) console.log('regular_update_table ' + rows.length + " rows");
105 var html=$.map(rows, function (row) { return html_row ( cell (key, row[key])); }).join();
109 function datatables_set_message ($table, $tbody, message) {
110 $table.dataTable().fnClearTable();
111 $table.dataTable().fnAddData( [ message ] );
112 $table.dataTable().fnDraw();
115 function datatables_update_table ($table, $tbody, rows, key) {
116 if (debug) console.log('datatables_update_table ' + rows.length + " rows");
117 $table.dataTable().fnClearTable();
118 // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
119 // The function can return any value to add to the array. A returned array will be flattened into the resulting array.
120 // this is wrong indeed so let's work around that
121 $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ cell (key,row[key]) ]] }) );
122 $table.dataTable().fnDraw();
125 function html_row (cell) { return "<tr><td class='simplelist'>"+cell+"</td></tr>"; }