import js files from joomla as is
[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             jQuery.subscribe('/results/' + options.query_uuid + '/changed', {instance: $this}, update_list);
29
30             /* End of plugin initialization */
31
32             $this.data('SimpleList', {
33                 options: options,
34                 target : $this,
35                 SimpleList : SimpleList
36             });
37
38          }
39        });
40      },
41     destroy : function( ) {
42
43         return this.each(function(){
44             var $this = jQuery(this), data = $this.data('SimpleList');
45             jQuery(window).unbind('SimpleList');
46             data.SimpleList.remove();
47             $this.removeData('SimpleList');
48         })
49
50     },
51 /*
52     reposition : function( ) { // ... },
53     show : function( ) { // ... },
54     hide : function( ) { // ... },
55 */
56     update : function( content ) { }
57   };
58
59     jQuery.fn.SimpleList = function( method ) {
60         /* Method calling logic */
61         if ( methods[method] ) {
62             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
63         } else if ( typeof method === 'object' || ! method ) {
64             return methods.init.apply( this, arguments );
65         } else {
66             jQuery.error( 'Method ' +  method + ' does not exist on jQuery.SimpleList' );
67         }    
68
69     };
70
71     /* Private methods */
72
73     function update_list(e, rows)
74     {
75         if (rows.length == 0) {
76             e.data.instance.html('No result !');
77             return;
78         }
79         if (typeof rows[0].error != 'undefined') {
80             e.data.instance.html('ERROR: ' + rows[0].error);
81             return;
82         }
83         options = e.data.instance.data().SimpleList.options;
84         is_cached = options.query.ts != 'now' ? true : false;
85         e.data.instance.html(myslice_html_ul(rows, options.key, options.value, is_cached)+"<br/>");
86         
87     }
88
89     function myslice_html_li(type, value, is_cached) {
90         var cached = '';
91         //if (is_cached)
92         //    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>';
93         if (type == 'slice_hrn') {
94             return "<li class='icn icn-play'><a href='/view/slice/" + value + "'>" + value + cached + "</a></li>";
95         } else if (type == 'network_hrn') {
96             return "<li class='icn icn-play'>" + value + cached + "</li>";
97         } else {
98             return "<li>" + value + "</li>";
99         }
100     }
101
102
103     function myslice_html_ul(data, key, value, is_cached) {
104         var out = "<ul>";
105         for (var i = 0; i < data.length; i++) {
106             out += myslice_html_li(key, data[i][value], is_cached);
107             //out += myslice_html_li(key, myslice_html_a(data[i][key], data[i][value], key), is_cached);
108         }
109         out += "</ul>";
110
111         return out;
112     }
113
114     /*
115     function myslice_async_render_list(data, key, value, is_cached) {
116         // we suppose we only have one column, or we need more precisions
117         var col = [];
118         if (myslice_array_size(data[0]) == 1) {
119             for (var k in data[0]) {
120                 key = k;
121                 value = k;
122             }
123         } else {
124             for (var k in data[0]) {
125                 if (k.substr(-4) == '_hrn') {
126                     key = k;
127                 } else {
128                     value = k;
129                 }
130             }
131         }
132         return myslice_html_ul(data, key, value, is_cached);
133     }
134     */
135
136 })( jQuery );