9c3752ba0fd3806cd0a8292b6c03f4f7606dc29d
[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 //              console.log("data" + data);
21 //              looks like $this.attr('title') is undefined..
22 //              console.log('iterating in simplelist.init with data='+data+' and title='+$this.attr('title'));
23                 /* create an empty DOM object */                
24                 var SimpleList = $('<div />', { text : $this.attr('title') });
25                 // If the plugin hasn't been initialized yet
26                 if ( ! data ) {
27                     /* Subscribe to query updates */
28                     var url='/results/' + options.query_uuid + '/changed';
29                     $.subscribe(url, {instance: $this}, update_list);
30                     if (simplelist_debug) window.console.log('subscribing to ' + url);
31                     $this.data('SimpleList', {options: options, target : this, SimpleList : SimpleList});
32                 }
33             });
34         },
35         destroy : function( ) {
36             return this.each(function(){
37                 var $this = $(this), data = $this.data('SimpleList');
38                 $(window).unbind('SimpleList');
39                 data.SimpleList.remove();
40                 $this.removeData('SimpleList');
41             })
42     },
43         update : function( content ) { }
44     };
45
46     $.fn.SimpleList = function( method ) {
47         /* Method calling logic */
48         if ( methods[method] ) {
49             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
50         } else if ( typeof method === 'object' || ! method ) {
51             return methods.init.apply( this, arguments );
52         } else {
53             $.error( 'Method ' +  method + ' does not exist on jQuery.SimpleList' );
54         }    
55     };
56
57     /* Private methods */
58     function update_list(e, rows) {
59         if (rows.length == 0) {
60             e.data.instance.html('No result !');
61             return;
62         }
63         if (typeof rows[0].error != 'undefined') {
64             e.data.instance.html('ERROR: ' + rows[0].error);
65             return;
66         }
67         options = e.data.instance.data().SimpleList.options;
68         is_cached = options.query.ts != 'now' ? true : false;
69         html_code=myslice_html_ul(rows, options.key, options.value, is_cached)+"<br/>";
70         e.data.instance.html(html_code);
71         
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             //out += myslice_html_li(key, myslice_html_a(data[i][key], data[i][value], key), is_cached);
79         }
80         out += "</ul>";
81         return out;
82     }
83     
84     function myslice_html_li(type, value, is_cached) {
85         var cached = '';
86         //if (is_cached)
87         //    cached='<div class="cache"><span><b>Cached information from the database</b><br/>Timestamp: XX/XX/XX XX:XX:XX<br/><br/><i>Refresh in progress...</i></span></div>';
88         if (type == 'slice_hrn') {
89             return "<li class='icn icn-play'><a href='/slice/" + value + "'>" + value + cached + "</a></li>";
90         } else if (type == 'network_hrn') {
91             return "<li class='icn icn-play'>" + value + cached + "</li>";
92         } else {
93             return "<li>" + value + "</li>";
94         }
95     }
96     
97 })( jQuery );