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=false;
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_list);
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_list(e, rows) {
57 // e.data is what we passed in second argument to subscribe
59 // locate the <tbody>, expected layout being
60 // <div class='plugin'> <table> <thead /> <tbody /tbody> </table> </div>
61 // -- or, if we don't have a header --
62 // <div class='plugin'> <table> <tbody /tbody> </table> </div>
63 var $tbody=$this.find("tbody.simplelist").first();
64 if (simplelist_debug) console.log("$tbody goes with "+$tbody.get(0));
66 if (rows.length == 0) {
67 $tbody.html("<tr><td class='simplelist-empty'>No result !</tr></td>");
70 if (typeof rows[0].error != 'undefined') {
71 e.data.html("<tr><td class='simplelist-error'>ERROR: " + rows[0].error + "</td></tr>");
74 var options = e.data.data().SimpleList.options;
75 var is_cached = options.query.timestamp != 'now' ? true : false;
76 // here is where we use 'key' and 'value' from the SimpleList (python) constructor
77 html_code=myslice_html_tbody(rows, options.key, options.value, is_cached);
78 // locate the tbody from the template, set its text
79 $tbody.html(html_code);
80 // clear the spinning wheel
82 if (simplelist_debug) console.log("about to unspin with elt #" + $elt.attr('id') + " class " + $elt.attr('class'));
83 $elt.closest('.need-spin').spin(false);
86 function myslice_html_tbody(data, key, value, is_cached) {
89 for (var i = 0; i < data.length; i++) {
90 out += myslice_html_tr(key, data[i][value], is_cached);
95 function myslice_html_tr(key, value,is_cached) {
96 var cached = is_cached ? "(cached)" : "";
97 if (key == 'slice_hrn') {
98 return "<tr><td class='simplelist'><i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + cached + "</a></td></tr>";
99 } else if (key == 'network_hrn') {
100 return "<tr><td class='simplelist'><i class='icon-play-circle'></i>" + value + cached + "</td></tr>";
102 return "<tr><td class='simplelist'>" + value + "</td></tr>";