uses messages:debug rather than console.log
[myslice.git] / plugins / updater / updater.js
1 /**
2  * Description: associate with a Get query, maintains the 'Update' query that records pending changes
3  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 ( function ( $ ) {
8     
9     $.fn.Updater = function( method ) {
10         /* Method calling logic */
11         if ( methods[method] ) {
12             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
13         } else if ( typeof method === 'object' || ! method ) {
14             return methods.init.apply( this, arguments );
15         } else {
16             $.error( 'Method ' +  method + ' does not exist on $.Updater' );
17         }    
18         
19     };
20     
21     var methods = {
22         
23         init : function( options ) {
24             return this.each(function(){
25                 var $this = $(this);
26                 var updater = new Updater (options);
27                 $this.data('Updater',updater);
28                 /* Subscribe to query updates */
29                 var results_channel = '/results/' + options.query_uuid + '/changed';
30                 $.subscribe(results_channel, function (e,rows) { updater.update_slice (e,rows); } );
31
32
33         // xxx - here -- here -- xxx
34
35             $('#updater-' + options.plugin_uuid).click({instance: $this}, function (e) {
36                 var data = e.data.instance.data().Slices;
37                 tophat_async_exec([{'query': data.update_query, 'id': null}]);
38                 //$('#updateslice-' + options.plugin_uuid).prop('disabled', true);
39             });
40
41             /* End of plugin initialization */
42
43             $(this).data('Slices', {
44                 options: options,
45                 target : $this,
46                 update_query : null,
47                 child_subscribe: false, /* Are we listening for children updates */
48                 Slices : Slices
49             });
50
51          }
52        });
53      },
54     destroy : function( ) {
55
56         return this.each(function(){
57             var $this = $(this), data = $this.data('Slices');
58             $(window).unbind('Slices');
59             data.Slices.remove();
60             $this.removeData('Slices');
61         })
62
63     },
64
65     show : function( content ) { }
66   };
67
68     /* Private methods */
69
70     function update_resources(e, resources, change)
71     {
72         data = e.data.instance.data().Slices;
73
74         data.update_query.params['resource'] = resources
75         $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
76     }
77
78     function update_leases(e, leases, change)
79     {
80         data = e.data.instance.data().Slices;
81
82         data.update_query.params['lease'] = leases
83         $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
84     }
85
86     function update_slice(e, rows, query) {
87         /* This function is called twice : get and update */
88
89         var data = e.data.instance.data().Slices;
90
91         /* Update placeholders and trigger subqueries updates */
92         if (rows.length == 0) {
93             alert("no result");
94             return;
95         }
96         var slice = rows[0];
97
98         /* for get */
99         if (data.update_query == null) {
100             data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
101         }
102         /* In case of update the list of resources and leases should be updated accordingly */
103
104         /* only for get ? */
105         $.each(slice, function(key, value) {
106             if (typeof value == 'string') {
107                 $('#myslice__' + key).html(value);
108             }
109         });
110
111         /* TODO avoid repetitions + made this code generic and plugin-independent */
112
113         if (query.method == 'update') {
114             // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
115             query.analyzed_subqueries();
116         }
117
118         /* NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
119          * le query plan et obtenir toutes les infos, par contre on ne peut pas
120          * savoir d'avance quels parametres ont été accordés, changés, etc.
121          * Dans le cas général, ca pourrait affecter le query plan...
122          * Par contre on n'a pas d'information sur toutes les resources, mais
123          * uniquement celles dans la liste. Comment gérer ?
124          */
125
126         /* Inform child plugins about their respective parts of the results */
127         /* Only for get */
128         var r_subq = query.analyzed_query.subqueries['resource'];
129         var l_subq = query.analyzed_query.subqueries['lease'];
130         $.publish('/results/' + r_subq.uuid + '/changed', [slice['resource'], r_subq]);
131         $.publish('/results/' + l_subq.uuid + '/changed', [slice['lease'],    l_subq]);
132
133         /* Subscribe to get notifications from child plugins */
134         if (!data.child_subscribe) {
135             $.subscribe('/update-set/' + r_subq.uuid, {instance: e.data.instance}, update_resources);
136             $.subscribe('/update-set/' + l_subq.uuid, {instance: e.data.instance}, update_leases);
137             data.child_subscribe = true
138         }
139
140     }
141
142 })( jQuery );
143