no-op just to be able to trace url subscriptions
[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: Google maps display of geolocated data
6  * Requires: 
7  * Author: The MySlice Team
8  * Copyright: Copyright 2012 UPMC Sorbonne Universités
9  * License: GPLv3
10  */
11
12 (function(jQuery){
13
14   var methods = {
15      init : function( options ) {
16
17        return this.each(function(){
18          
19          var $this = jQuery(this),
20              data = $this.data('SimpleList'), SimpleList = jQuery('<div />', { text : $this.attr('title') });
21          
22          // If the plugin hasn't been initialized yet
23          if ( ! data ) {
24          
25             /* Plugin initialization */
26
27             /* Subscribe to query updates */
28              var url='/results/' + options.query_uuid + '/changed';
29             jQuery.subscribe(url, {instance: $this}, update_list);
30
31             /* End of plugin initialization */
32
33             $this.data('SimpleList', {
34                 options: options,
35                 target : $this,
36                 SimpleList : SimpleList
37             });
38
39          }
40        });
41      },
42     destroy : function( ) {
43
44         return this.each(function(){
45             var $this = jQuery(this), data = $this.data('SimpleList');
46             jQuery(window).unbind('SimpleList');
47             data.SimpleList.remove();
48             $this.removeData('SimpleList');
49         })
50
51     },
52 /*
53     reposition : function( ) { // ... },
54     show : function( ) { // ... },
55     hide : function( ) { // ... },
56 */
57     update : function( content ) { }
58   };
59
60     jQuery.fn.SimpleList = function( method ) {
61         /* Method calling logic */
62         if ( methods[method] ) {
63             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
64         } else if ( typeof method === 'object' || ! method ) {
65             return methods.init.apply( this, arguments );
66         } else {
67             jQuery.error( 'Method ' +  method + ' does not exist on jQuery.SimpleList' );
68         }    
69
70     };
71
72     /* Private methods */
73
74     function update_list(e, rows)
75     {
76         if (rows.length == 0) {
77             e.data.instance.html('No result !');
78             return;
79         }
80         if (typeof rows[0].error != 'undefined') {
81             e.data.instance.html('ERROR: ' + rows[0].error);
82             return;
83         }
84         options = e.data.instance.data().SimpleList.options;
85         is_cached = options.query.ts != 'now' ? true : false;
86         e.data.instance.html(myslice_html_ul(rows, options.key, options.value, is_cached)+"<br/>");
87         
88     }
89
90     function myslice_html_li(type, value, is_cached) {
91         var cached = '';
92         //if (is_cached)
93         //    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>';
94         if (type == 'slice_hrn') {
95             return "<li class='icn icn-play'><a href='/view/slice/" + value + "'>" + value + cached + "</a></li>";
96         } else if (type == 'network_hrn') {
97             return "<li class='icn icn-play'>" + value + cached + "</li>";
98         } else {
99             return "<li>" + value + "</li>";
100         }
101     }
102
103
104     function myslice_html_ul(data, key, value, is_cached) {
105         var out = "<ul>";
106         for (var i = 0; i < data.length; i++) {
107             out += myslice_html_li(key, data[i][value], is_cached);
108             //out += myslice_html_li(key, myslice_html_a(data[i][key], data[i][value], key), is_cached);
109         }
110         out += "</ul>";
111
112         return out;
113     }
114
115     /*
116     function myslice_async_render_list(data, key, value, is_cached) {
117         // we suppose we only have one column, or we need more precisions
118         var col = [];
119         if (myslice_array_size(data[0]) == 1) {
120             for (var k in data[0]) {
121                 key = k;
122                 value = k;
123             }
124         } else {
125             for (var k in data[0]) {
126                 if (k.substr(-4) == '_hrn') {
127                     key = k;
128                 } else {
129                     value = k;
130                 }
131             }
132         }
133         return myslice_html_ul(data, key, value, is_cached);
134     }
135     */
136
137 })( jQuery );