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