simplelist is datatables-aware
[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_plugin);
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_plugin(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 <table> element; with datatables in the way,
61         // this might not be a direct son of the div-plugin
62         var $table=$this.find("table.simplelist").first();
63         // also we may or may not have a header
64         var $tbody=$table.find("tbody.simplelist").first();
65         var use_datatables = $table.hasClass("with-datatables");
66         if (simplelist_debug) console.log($this.attr('id') + " udt= " + use_datatables);
67         
68         // clear the spinning wheel: look up an ancestor that has the need-spin class
69         // do this before we might return
70         $this.closest('.need-spin').spin(false);
71
72         if (rows.length == 0) {
73             if (use_datatables) datatables_set_message ("No result");
74             else                regular_set_message ("No result");
75             return;
76         }
77         if (typeof rows[0].error != 'undefined') {
78             var error="ERROR: " + rows[0].error;
79             if (use_datatables) datatables_set_message (error);
80             else                regular_set_message (error);
81             return;
82         }
83         var options = e.data.data().SimpleList.options;
84         if (use_datatables)     datatables_update_table ($table,$tbody,rows,options.key);
85         else                    regular_update_table ($table,$tbody,rows,options.key);
86
87     }
88
89     // hard-wire a separate presentation depending on the key being used....
90     function cell(key, value) {
91         if (key == 'slice_hrn') {
92             return "<i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + "</a>";
93         } else if (key == 'network_hrn') {
94             return "<i class='icon-play-circle'></i>" + value ;
95         } else {
96             return value;
97         }
98     }
99
100     function regular_set_message ($table, $tbody, message) {
101         $tbody.html("<tr><td>"+message+"</td></tr>");
102     }
103
104     function regular_update_table ($table, $tbody, rows, key) {
105         console.log('regular_update_table ' + rows.length + " rows");
106         var html=$.map(rows, function (row) { return html_row ( cell (key, row[key])); }).join();
107         console.log("html="+html);
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         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 );