d14d5d8b5a3dc08f84a01827c975ed6d4e1cb309
[myslice.git] / plugins / updater / static / js / updater.js
1 /**
2  * Description: Manage query updates
3  * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 (function( $ ) {
8     
9     var debug=false;
10 //    debug=true
11
12     var Updater = Plugin.extend({
13
14         init: function(options, element)
15         {
16             this._super(options, element);
17             
18             this.listen_query(options.query_uuid);
19         },
20
21         
22         /*************************** PLUGIN EVENTS ****************************/
23
24         /***************************** GUI EVENTS *****************************/
25
26         arm_button: function()
27         {
28                 this.el('updater').click(this, this.submit_update_request);
29         },
30
31         submit_update_request: function (e) 
32         {
33             var self = e.data;
34
35             manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
36
37         },
38
39         /************************** GUI MANIPULATION **************************/
40
41         disable_update_button: function()
42         {
43             this.el('updater').attr('disabled', 'disabled');
44         },
45
46         /*************************** QUERY HANDLER ****************************/
47
48         /*************************** RECORD HANDLER ***************************/
49
50         /************************** PRIVATE METHODS ***************************/
51
52         /******************************** TODO ********************************/
53
54         /*
55             query_failed: function (e, code, output) 
56         {
57             var plugindiv=e.data;
58             var updater=plugindiv.data('Updater');
59                 $('#updater-' + updater.options.plugin_uuid).removeAttr('disabled');
60             // just as a means to deom how to retrieve the stuff passed on the channel
61             if (debug)
62                 messages.debug("retrieved error code " + code + " and output " + output);
63         },
64             
65         update_resources: function (e, resources, change)
66         {
67                 data = e.data.instance.data().Slices;
68
69                 data.update_query.params['resource'] = resources
70                 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
71         },
72
73         update_leases: function (e, leases, change) 
74         {
75             data = e.data.instance.data().Slices;
76             
77             data.update_query.params['lease'] = leases
78             $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
79         },
80         
81         query_completed: function (e, rows, query)
82         {
83
84             // This function is called twice : get and update 
85             messages.info("updater.query_completed - not implemented yet");
86             return;
87           
88             var data = e.data.instance.data().Slices;
89           
90             // Update placeholders and trigger subqueries updates 
91             if (rows.length == 0) {
92             alert("no result");
93             return;
94             }
95             var slice = rows[0];
96           
97             // for get
98             if (data.update_query == null) {
99                 data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
100             }
101             // In case of update the list of resources and leases should be updated accordingly
102           
103             // only for get ?
104             $.each(slice, function(key, value) {
105             if (typeof value == 'string') {
106                 $('#myslice__' + key).html(value);
107             }
108             });
109           
110             // TODO avoid repetitions + made this code generic and plugin-independent 
111             
112             if (query.method == 'update') {
113                 // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
114                 query.analyzed_subqueries();
115             }
116           
117             // NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
118             // le query plan et obtenir toutes les infos, par contre on ne peut pas
119             // savoir d'avance quels parametres ont été accordés, changés, etc.
120             // Dans le cas général, ca pourrait affecter le query plan...
121             // Par contre on n'a pas d'information sur toutes les resources, mais
122             // uniquement celles dans la liste. Comment gérer ?
123             
124             // Inform child plugins about their respective parts of the results 
125             // Only for get 
126             var r_subq = query.analyzed_query.subqueries['resource'];
127             var l_subq = query.analyzed_query.subqueries['lease'];
128             $.publish('/results/' + r_subq.uuid + '/changed', [slice['resource'], r_subq]);
129             $.publish('/results/' + l_subq.uuid + '/changed', [slice['lease'],    l_subq]);
130             
131             // Subscribe to get notifications from child plugins
132             if (!data.child_subscribe) {
133                 $.subscribe('/update-set/' + r_subq.uuid, {instance: e.data.instance}, update_resources);
134                 $.subscribe('/update-set/' + l_subq.uuid, {instance: e.data.instance}, update_leases);
135                 data.child_subscribe = true
136             }
137             
138         }
139         */
140     });
141
142     $.plugin('Updater', Updater);
143
144 })( jQuery );