ab320e5efbe9e9aae554011da0d4240aa6283323
[unfold.git] / plugins / static / js / simplelist.js
1 /**
2  * MySlice SimpleList plugin
3  * URL: http://trac.myslice.info
4  * Description: display simple lists like slices or testbeds
5  * Author: The MySlice Team
6  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
7  * License: GPLv3
8  */
9
10 simplelist_debug=false;
11 //simplelist_debug=true;
12
13 (function($){
14
15     $.fn.SimpleList = function( method ) {
16         /* Method calling logic */
17         if ( methods[method] ) {
18             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
19         } else if ( typeof method === 'object' || ! method ) {
20             return methods.init.apply( this, arguments );
21         } else {
22             $.error( 'Method ' +  method + ' does not exist on jQuery.SimpleList' );
23         }    
24     };
25
26     var methods = {
27         init : function( options ) {
28             return this.each(function(){
29                 var $this = $(this), data = $this.data('SimpleList');
30                 // If the plugin hasn't been initialized yet
31                 if ( ! data ) {
32                     /* Subscribe to query updates */
33                     var channel='/results/' + options.query_uuid + '/changed';
34                     /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
35                     $.subscribe(channel, $this, update_plugin);
36                     if (simplelist_debug) window.console.log('subscribing to ' + channel);
37                     $this.data('SimpleList', {options: options});
38                 }
39             });
40         },
41         destroy : function( ) {
42             if (simplelist_debug) console.log("SimpleList.destroy...");
43             return this.each(function(){
44                 var $this = $(this), data = $this.data('SimpleList');
45                 // xxx not too sure what this is about
46                 $(window).unbind('SimpleList');
47                 $this.removeData('SimpleList');
48             });
49         },
50         update : function( content ) { 
51             if (simplelist_debug) console.log("SimpleList.update...");
52         },
53     }; // methods
54
55     /* Private methods */
56     // complexity here is mostly because a datatables-enabled table cannot
57     // be updated in a "normal" way using .html()
58     function update_plugin(e, rows) {
59         // e.data is what we passed in second argument to subscribe
60         // so here it is the jquery object attached to the plugin <div>
61         var $plugindiv=e.data;
62         // locate the <table> element; with datatables in the way,
63         // this might not be a direct son of the div-plugin
64         var $table=$plugindiv.find("table.simplelist").first();
65         // also we may or may not have a header
66         var $tbody=$table.find("tbody.simplelist").first();
67         var use_datatables = $table.hasClass("with-datatables");
68         if (simplelist_debug) console.log($plugindiv.attr('id') + " udt= " + use_datatables);
69         
70         // clear the spinning wheel: look up an ancestor that has the need-spin class
71         // do this before we might return
72         $plugindiv.closest('.need-spin').spin(false);
73
74         if (rows.length == 0) {
75             if (use_datatables) datatables_set_message ("No result");
76             else                regular_set_message ("No result");
77             return;
78         }
79         if (typeof rows[0].error != 'undefined') {
80             var error="ERROR: " + rows[0].error;
81             if (use_datatables) datatables_set_message (error);
82             else                regular_set_message (error);
83             return;
84         }
85         var options = $plugindiv.data().SimpleList.options;
86         if (use_datatables)     datatables_update_table ($table,$tbody,rows,options.key);
87         else                    regular_update_table ($table,$tbody,rows,options.key);
88
89     }
90
91     // hard-wire a separate presentation depending on the key being used....
92     function cell(key, value) {
93         if (key == 'slice_hrn') {
94             return "<i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + "</a>";
95         } else if (key == 'network_hrn') {
96             return "<i class='icon-play-circle'></i>" + value ;
97         } else {
98             return value;
99         }
100     }
101
102     function regular_set_message ($table, $tbody, message) {
103         $tbody.html("<tr><td>"+message+"</td></tr>");
104     }
105
106     function regular_update_table ($table, $tbody, rows, key) {
107         if (simplelist_debug) console.log('regular_update_table ' + rows.length + " rows");
108         var html=$.map(rows, function (row) { return html_row ( cell (key, row[key])); }).join();
109         $tbody.html(html);
110     }
111     
112     function datatables_set_message ($table, $tbody, message) {
113         $table.dataTable().fnClearTable();
114         $table.dataTable().fnAddData( [ message ] );
115         $table.dataTable().fnDraw();
116     }
117
118     function datatables_update_table ($table, $tbody, rows, key) {
119         if (simplelist_debug) console.log('datatables_update_table ' + rows.length + " rows");
120         $table.dataTable().fnClearTable();
121         // the lambda here returns a [[]] because $.map is kind of broken; as per the doc:
122         // The function can return any value to add to the array. A returned array will be flattened into the resulting array.
123         // this is wrong indeed so let's work around that
124         $table.dataTable().fnAddData( $.map(rows, function (row) { return [[ cell (key,row[key]) ]] }) );
125         $table.dataTable().fnDraw();
126     }   
127     
128     function html_row (cell) { return "<tr><td class='simplelist'>"+cell+"</td></tr>"; }
129     
130 })( jQuery );