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_table);
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_table(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 <tbody>, expected layout being
61 // <div class='plugin'> <table> <thead /> <tbody /tbody> </table> </div>
62 // -- or, if we don't have a header --
63 // <div class='plugin'> <table> <tbody /tbody> </table> </div>
64 var $tbody=$this.find("tbody.simplelist").first();
65 if (simplelist_debug) console.log("$tbody goes with "+$tbody.get(0));
67 if (rows.length == 0) {
68 $tbody.html("<tr><td class='simplelist-empty'>No result !</tr></td>");
71 if (typeof rows[0].error != 'undefined') {
72 e.data.html("<tr><td class='simplelist-error'>ERROR: " + rows[0].error + "</td></tr>");
75 var options = e.data.data().SimpleList.options;
76 var is_cached = options.query.timestamp != 'now' ? true : false;
77 // here is where we use 'key' and 'value' from the SimpleList (python) constructor
78 html_code=myslice_html_tbody(rows, options.key, options.value, is_cached);
79 // locate the tbody from the template, set its text
80 $tbody.html(html_code);
82 // clear the spinning wheel: look up an ancestor that has the need-spin class
83 $this.closest('.need-spin').spin(false);
85 // in case we run in datatables mode
86 // xxx this is not working yet
87 // http://datatables.net/forums/discussion/14556/can039t-get-my-table-to-refresh-properly#Item_1
88 // http://live.datatables.net/ufihuc/2/edit#javascript,html
89 // most likely when using datatables we'll have to change the contents using some other way..
90 var datatables_table=$this.find("table.with-datatables");
91 if (simplelist_debug) console.log ("running fnDraw() on " + datatables_table.length + " items");
92 // only try this if needed as datatables might not be loaded at all
93 if (datatables_table.length >0) datatables_table.dataTable().fnDraw();
97 function myslice_html_tbody(data, key, value, is_cached) {
100 for (var i = 0; i < data.length; i++) {
101 out += myslice_html_tr(key, data[i][value], is_cached);
106 function myslice_html_tr(key, value,is_cached) {
107 // could not understand what sense this 'cache' stuff could actually make
108 // var cached = is_cached ? "(cached)" : "";
110 if (key == 'slice_hrn') {
111 return "<tr><td class='simplelist'><i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + cached + "</a></td></tr>";
112 } else if (key == 'network_hrn') {
113 return "<tr><td class='simplelist'><i class='icon-play-circle'></i>" + value + cached + "</td></tr>";
115 return "<tr><td class='simplelist'>" + value + "</td></tr>";