2 * MySlice SimpleList plugin
4 * URL: http://www.myslice.info
5 * Description: display simple lists like slices or testbeds
7 * Author: The MySlice Team
8 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
12 simplelist_debug=true;
16 init : function( options ) {
17 return this.each(function(){
19 var data = $this.data('SimpleList');
20 /* create an empty DOM object */
21 var SimpleList = $('<div />', { text : $this.attr('title') });
22 // If the plugin hasn't been initialized yet
24 /* Subscribe to query updates */
25 var channel='/results/' + options.query_uuid + '/changed';
26 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
27 $.subscribe(channel, $this, update_plugin);
28 if (simplelist_debug) window.console.log('subscribing to ' + channel);
29 $this.data('SimpleList', {options: options, SimpleList : SimpleList});
33 destroy : function( ) {
34 return this.each(function(){
35 var $this = $(this), data = $this.data('SimpleList');
36 $(window).unbind('SimpleList');
37 data.SimpleList.remove();
38 $this.removeData('SimpleList');
41 update : function( content ) { }
44 $.fn.SimpleList = function( method ) {
45 /* Method calling logic */
46 if ( methods[method] ) {
47 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
48 } else if ( typeof method === 'object' || ! method ) {
49 return methods.init.apply( this, arguments );
51 $.error( 'Method ' + method + ' does not exist on jQuery.SimpleList' );
56 function update_plugin(e, rows) {
57 // e.data is what we passed in second argument to subscribe
58 // so here it is the jquery object attached to the plugin <div>
60 // locate the <table> element; with datatables in the way,
61 // this might not be a direct son of the div-plugin
62 var $table=$this.find("table.simplelist").first();
63 // also we may or may not have a header
64 var $tbody=$table.find("tbody.simplelist").first();
65 var use_datatables = $table.hasClass("with-datatables");
66 if (simplelist_debug) console.log($this.attr('id') + " udt= " + use_datatables);
68 // clear the spinning wheel: look up an ancestor that has the need-spin class
69 // do this before we might return
70 $this.closest('.need-spin').spin(false);
72 if (rows.length == 0) {
73 if (use_datatables) datatables_set_message ("No result");
74 else regular_set_message ("No result");
77 if (typeof rows[0].error != 'undefined') {
78 var error="ERROR: " + rows[0].error;
79 if (use_datatables) datatables_set_message (error);
80 else regular_set_message (error);
83 var options = e.data.data().SimpleList.options;
84 if (use_datatables) datatables_update_table ($table,$tbody,rows,options.key);
85 else regular_update_table ($table,$tbody,rows,options.key);
89 // hard-wire a separate presentation depending on the key being used....
90 function cell(key, value) {
91 if (key == 'slice_hrn') {
92 return "<i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + "</a>";
93 } else if (key == 'network_hrn') {
94 return "<i class='icon-play-circle'></i>" + value ;
100 function regular_set_message ($table, $tbody, message) {
101 $tbody.html("<tr><td>"+message+"</td></tr>");
104 function regular_update_table ($table, $tbody, rows, key) {
105 console.log('regular_update_table ' + rows.length + " rows");
106 var html=$.map(rows, function (row) { return html_row ( cell (key, row[key])); }).join();
107 console.log("html="+html);
111 function datatables_set_message ($table, $tbody, message) {
112 $table.dataTable().fnClearTable();
113 $table.dataTable().fnAddData( [ message ] );
114 $table.dataTable().fnDraw();
117 function datatables_update_table ($table, $tbody, rows, key) {
118 console.log('datatables_update_table ' + rows.length + " rows");
119 $table.dataTable().fnClearTable();
120 // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
121 // The function can return any value to add to the array. A returned array will be flattened into the resulting array.
122 // this is wrong indeed so let's work around that
123 $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ cell (key,row[key]) ]] }) );
124 $table.dataTable().fnDraw();
127 function html_row (cell) { return "<tr><td class='simplelist'>"+cell+"</td></tr>"; }