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 // If the plugin hasn't been initialized yet
32 /* Subscribe to query updates */
33 var channel='/results/' + options.query_uuid + '/changed';
34 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
35 $.subscribe(channel, $this, update_plugin);
36 if (debug) window.console.log('subscribing to ' + channel);
37 $this.data('SimpleList', options);
41 destroy : function( ) {
42 if (debug) console.log("SimpleList.destroy...");
43 return this.each(function(){
44 var $this = $(this), data = $this.data('SimpleList');
45 // xxx not too sure what this is about
46 $(window).unbind('SimpleList');
47 $this.removeData('SimpleList');
50 update : function( content ) {
51 if (debug) console.log("SimpleList.update...");
56 // complexity here is mostly because a datatables-enabled table cannot
57 // be updated in a "normal" way using .html()
58 function update_plugin(e, rows) {
59 // e.data is what we passed in second argument to subscribe
60 // so here it is the jquery object attached to the plugin <div>
61 var $plugindiv=e.data;
62 // locate the <table> element; with datatables in the way,
63 // this might not be a direct son of the div-plugin
64 var $table=$plugindiv.find("table.simplelist").first();
65 // also we may or may not have a header
66 var $tbody=$table.find("tbody.simplelist").first();
67 var use_datatables = $table.hasClass("with-datatables");
68 if (debug) console.log($plugindiv.attr('id') + " udt= " + use_datatables);
70 // clear the spinning wheel: look up an ancestor that has the need-spin class
71 // do this before we might return
72 $plugindiv.closest('.need-spin').spin(false);
74 if (rows.length == 0) {
75 if (use_datatables) datatables_set_message ($table, $tbody, unfold.warning("No result"));
76 else regular_set_message ($table, $tbody, unfold.warning("No result"));
79 if (typeof rows[0].error != 'undefined') {
80 var error="ERROR: " + rows[0].error;
81 if (use_datatables) datatables_set_message ($table, $tbody, unfold.error(error));
82 else regular_set_message ($table, $tbody, unfold.error(error));
85 var options = $plugindiv.data().SimpleList;
86 if (use_datatables) datatables_update_table ($table,$tbody,rows,options.key);
87 else regular_update_table ($table,$tbody,rows,options.key);
91 // hard-wire a separate presentation depending on the key being used....
92 function cell(key, value) {
93 if (key == 'slice_hrn') {
94 return "<i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + "</a>";
95 } else if (key == 'network_hrn') {
96 return "<i class='icon-play-circle'></i>" + value ;
102 function regular_set_message ($table, $tbody, message) {
103 $tbody.html("<tr><td>"+message+"</td></tr>");
106 function regular_update_table ($table, $tbody, rows, key) {
107 if (debug) console.log('regular_update_table ' + rows.length + " rows");
108 var html=$.map(rows, function (row) { return html_row ( cell (key, row[key])); }).join();
112 function datatables_set_message ($table, $tbody, message) {
113 $table.dataTable().fnClearTable();
114 $table.dataTable().fnAddData( [ message ] );
115 $table.dataTable().fnDraw();
118 function datatables_update_table ($table, $tbody, rows, key) {
119 if (debug) console.log('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. A returned array will be flattened into the resulting array.
123 // this is wrong indeed so let's work around that
124 $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ cell (key,row[key]) ]] }) );
125 $table.dataTable().fnDraw();
128 function html_row (cell) { return "<tr><td class='simplelist'>"+cell+"</td></tr>"; }