plugin.updater: fixed typos
[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         /************************** PRIVATE METHODS ***************************/
53
54         /******************************** TODO ********************************/
55
56         /*
57             query_failed: function (e, code, output) 
58         {
59             var plugindiv=e.data;
60             var updater=plugindiv.data('Updater');
61                 $('#updater-' + updater.options.plugin_uuid).removeAttr('disabled');
62             // just as a means to deom how to retrieve the stuff passed on the channel
63             if (debug)
64                 messages.debug("retrieved error code " + code + " and output " + output);
65         },
66             
67         update_resources: function (e, resources, change)
68         {
69                 data = e.data.instance.data().Slices;
70
71                 data.update_query.params['resource'] = resources
72                 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
73         },
74
75         update_leases: function (e, leases, change) 
76         {
77             data = e.data.instance.data().Slices;
78             
79             data.update_query.params['lease'] = leases
80             $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
81         },
82         
83         query_completed: function (e, rows, query)
84         {
85
86             // This function is called twice : get and update 
87             messages.info("updater.query_completed - not implemented yet");
88             return;
89           
90             var data = e.data.instance.data().Slices;
91           
92             // Update placeholders and trigger subqueries updates 
93             if (rows.length == 0) {
94             alert("no result");
95             return;
96             }
97             var slice = rows[0];
98           
99             // for get
100             if (data.update_query == null) {
101                 data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
102             }
103             // In case of update the list of resources and leases should be updated accordingly
104           
105             // only for get ?
106             $.each(slice, function(key, value) {
107             if (typeof value == 'string') {
108                 $('#myslice__' + key).html(value);
109             }
110             });
111           
112             // TODO avoid repetitions + made this code generic and plugin-independent 
113             
114             if (query.method == 'update') {
115                 // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
116                 query.analyzed_subqueries();
117             }
118           
119             // NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
120             // le query plan et obtenir toutes les infos, par contre on ne peut pas
121             // savoir d'avance quels parametres ont été accordés, changés, etc.
122             // Dans le cas général, ca pourrait affecter le query plan...
123             // Par contre on n'a pas d'information sur toutes les resources, mais
124             // uniquement celles dans la liste. Comment gérer ?
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     });
143
144     $.plugin('Updater', Updater);
145
146 })( jQuery );