use SliceList rather than SimpleList
[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         if (rows.length == 0) {
58             e.data.html('No result !');
59             return;
60         }
61         if (typeof rows[0].error != 'undefined') {
62             e.data.html('ERROR: ' + rows[0].error);
63             return;
64         }
65         var options = e.data.data().SimpleList.options;
66         var is_cached = options.query.timestamp != 'now' ? true : false;
67         // here is where we use 'key' and 'value' from the SimpleList (python) constructor
68         html_code=myslice_html_ul(rows, options.key, options.value, is_cached)+"<br/>";
69         e.data.html(html_code);
70         var $elt = e.data;
71         if (simplelist_debug) console.log("about to unspin with elt #" + $elt.attr('id') + " class " + $elt.attr('class'));
72         $elt.closest('.need-spin').spin(false);
73     }
74
75     function myslice_html_ul(data, key, value, is_cached) {
76         var out = "<ul>";
77         for (var i = 0; i < data.length; i++) {
78             out += myslice_html_li(key, data[i][value], is_cached);
79         }
80         out += "</ul>";
81         return out;
82     }
83     
84     function myslice_html_li(key, value,is_cached) {
85         var cached = is_cached ? "(cached)" : "";
86         if (key == 'slice_hrn') {
87             return "<li class='slice-hrn'><i class='icon-play-circle'></i><a href='/slice/" + value + "'>" + value + cached + "</a></li>";
88         } else if (key == 'network_hrn') {
89             return "<li class='network-hrn'><i class='icon-play-circle'></i>" + value + cached + "</li>";
90         } else {
91             return "<li>" + value + "</li>";
92         }
93     }
94     
95 })( jQuery );