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