rewrite simplelist to use a table (thus datatable-ready) instead of <ul> -- ctd
[unfold.git] / plugins / static / js / simplelist.js
1 /**
2  * MySlice SimpleList plugin
3  * Version: 0.1.0
4  * URL: http://www.myslice.info
5  * Description: display simple lists like slices or testbeds
6  * Requires: 
7  * Author: The MySlice Team
8  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
9  * License: GPLv3
10  */
11
12 simplelist_debug=false;
13
14 (function($){
15     var methods = {
16         init : function( options ) {
17             return this.each(function(){
18                 var $this = $(this);
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
23                 if ( ! data ) {
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});
30                 }
31             });
32         },
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');
39             })
40     },
41         update : function( content ) { }
42     };
43
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 );
50         } else {
51             $.error( 'Method ' +  method + ' does not exist on jQuery.SimpleList' );
52         }    
53     };
54
55     /* Private methods */
56     function update_list(e, rows) {
57         // e.data is what we passed in second argument to subscribe
58         var $this=e.data;
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));
65
66         if (rows.length == 0) {
67             $tbody.html("<tr><td class='simplelist-empty'>No result !</tr></td>");
68             return;
69         }
70         if (typeof rows[0].error != 'undefined') {
71             e.data.html("<tr><td class='simplelist-error'>ERROR: " + rows[0].error + "</td></tr>");
72             return;
73         }
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
81         var $elt = e.data;
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);
84     }
85
86     function myslice_html_tbody(data, key, value, is_cached) {
87 //      return $.map (...)
88         var out = "";
89         for (var i = 0; i < data.length; i++) {
90             out += myslice_html_tr(key, data[i][value], is_cached);
91         }
92         return out;
93     }
94     
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>";
101         } else {
102             return "<tr><td class='simplelist'>" + value + "</td></tr>";
103         }
104     }
105     
106 })( jQuery );