use class 'need-spin' rather than plugin-toggle to host spins
[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         html_code=myslice_html_ul(rows, options.key, options.value, is_cached)+"<br/>";
68         e.data.html(html_code);
69         var $elt = e.data;
70         if (simplelist_debug) console.log("about to unspin with elt #" + $elt.attr('id') + " class " + $elt.attr('class'));
71         $elt.closest('.need-spin').spin(false);
72     }
73
74     function myslice_html_ul(data, key, value, is_cached) {
75         var out = "<ul>";
76         for (var i = 0; i < data.length; i++) {
77             out += myslice_html_li(key, data[i][value], is_cached);
78         }
79         out += "</ul>";
80         return out;
81     }
82     
83     function myslice_html_li(key, value) {
84         var cached = '';
85         if (key == 'slice_hrn') {
86             return "<li class='icn icn-play'><a href='/slice/" + value + "'>" + value + cached + "</a></li>";
87         } else if (key == 'network_hrn') {
88             return "<li class='icn icn-play'>" + value + cached + "</li>";
89         } else {
90             return "<li>" + value + "</li>";
91         }
92     }
93     
94 })( jQuery );