comment suspicious code
[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 (function($){
13     var methods = {
14         init : function( options ) {
15             return this.each(function(){
16                 var $this = $(this);
17                 var data = $this.data('SimpleList');
18 //              console.log("data" + data);
19 //              looks like $this.attr('title') is undefined..
20 //              console.log('iterating in simplelist.init with data='+data+' and title='+$this.attr('title'));
21                 /* create an empty DOM object */                
22                 var SimpleList = $('<div />', { text : $this.attr('title') });
23                 // If the plugin hasn't been initialized yet
24                 if ( ! data ) {
25                     /* Subscribe to query updates */
26                     var url='/results/' + options.query_uuid + '/changed';
27                     $.subscribe(url, {instance: this}, update_list);
28                     window.console.log('subscribing to ' + url);
29                     $this.data('SimpleList', {options: options, target : this, 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
57     function update_list(e, rows) {
58         if (rows.length == 0) {
59             e.data.instance.html('No result !');
60             return;
61         }
62         if (typeof rows[0].error != 'undefined') {
63             e.data.instance.html('ERROR: ' + rows[0].error);
64             return;
65         }
66         options = e.data.instance.data().SimpleList.options;
67         is_cached = options.query.ts != 'now' ? true : false;
68         e.data.instance.html(myslice_html_ul(rows, options.key, options.value, is_cached)+"<br/>");
69         
70     }
71
72     function myslice_html_li(type, value, is_cached) {
73         var cached = '';
74         //if (is_cached)
75         //    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>';
76         if (type == 'slice_hrn') {
77             return "<li class='icn icn-play'><a href='/view/slice/" + value + "'>" + value + cached + "</a></li>";
78         } else if (type == 'network_hrn') {
79             return "<li class='icn icn-play'>" + value + cached + "</li>";
80         } else {
81             return "<li>" + value + "</li>";
82         }
83     }
84     
85     function myslice_html_ul(data, key, value, is_cached) {
86         var out = "<ul>";
87         for (var i = 0; i < data.length; i++) {
88             out += myslice_html_li(key, data[i][value], is_cached);
89             //out += myslice_html_li(key, myslice_html_a(data[i][key], data[i][value], key), is_cached);
90         }
91         out += "</ul>";
92         return out;
93     }
94     
95 })( jQuery );