2c05345eb2253cffefee7e8eded0697b32de4c78
[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=true;
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_table);
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_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>
59         var $this=e.data;
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));
66
67         if (rows.length == 0) {
68             $tbody.html("<tr><td class='simplelist-empty'>No result !</tr></td>");
69             return;
70         }
71         if (typeof rows[0].error != 'undefined') {
72             e.data.html("<tr><td class='simplelist-error'>ERROR: " + rows[0].error + "</td></tr>");
73             return;
74         }
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);
81
82         // clear the spinning wheel: look up an ancestor that has the need-spin class
83         $this.closest('.need-spin').spin(false);
84
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();
94
95     }
96
97     function myslice_html_tbody(data, key, value, is_cached) {
98 //      return $.map (...)
99         var out = "";
100         for (var i = 0; i < data.length; i++) {
101             out += myslice_html_tr(key, data[i][value], is_cached);
102         }
103         return out;
104     }
105     
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)" : "";
109         var 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>";
114         } else {
115             return "<tr><td class='simplelist'>" + value + "</td></tr>";
116         }
117     }
118     
119 })( jQuery );