2 * Description: associate with a Get query, maintains the 'Update' query that records pending changes
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
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 );
16 $.error( 'Method ' + method + ' does not exist on $.Updater' );
21 init : function( options ) {
22 return this.each(function(){
24 var updater = new Updater (options);
25 $this.data('Updater',updater);
26 /* Subscribe to query updates */
27 var results_channel = '/results/' + options.query_uuid + '/changed';
28 $.subscribe(results_channel, function (e,rows) { updater.update_slice (e,rows); } );
32 destroy : function( ) {
33 return this.each(function(){
35 $(window).unbind('Updater');
36 data.Updater.remove();
37 $this.removeData('Updater');
41 show : function( content ) { }
44 function Updater (options) {
46 // xxx should try to locate update_query first, in case we have several Updaters
48 // however the mental model behind the global manifold object for now is
49 // to unambiguously find a query based on its query_uuid, which in the joomla
50 // implementation wouldn't fly
51 // we keep this for a later improvement
52 var query=manifold.find_query (options.query_uuid);
53 // very rough way of filling this for now
55 new ManifoldQuery ("update", query.subject, null, query.filters,
58 undefined, /* unique */
59 query.query_uuid, /* tmp */
60 undefined, undefined /* maybe some day I'll get that one */);
62 this.arm_button = function () {
63 $('#updater-' + this.options.plugin_uuid).click(this, this.submit_update_request);
65 this.submit_update_request = function (e) {
66 var update_query = e.data.update_query;
67 $.publish("messages:debug","Updater.submit_update_request " + update_query.__repr());
68 // xxx here - we need a valid query_uuid so the results will make it
69 manifold.asynchroneous_exec ( [ {'query_uuid': xxx, 'id': null}]);
70 // looks like a previous attempt to disable the button while the query is flying
71 //$('#updateslice-' + options.plugin_uuid).prop('disabled', true);
77 function update_resources(e, resources, change) {
78 data = e.data.instance.data().Slices;
80 data.update_query.params['resource'] = resources
81 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
84 function update_leases(e, leases, change) {
85 data = e.data.instance.data().Slices;
87 data.update_query.params['lease'] = leases
88 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
91 function update_slice(e, rows, query) {
92 /* This function is called twice : get and update */
94 var data = e.data.instance.data().Slices;
96 /* Update placeholders and trigger subqueries updates */
97 if (rows.length == 0) {
104 if (data.update_query == null) {
105 data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
107 /* In case of update the list of resources and leases should be updated accordingly */
110 $.each(slice, function(key, value) {
111 if (typeof value == 'string') {
112 $('#myslice__' + key).html(value);
116 /* TODO avoid repetitions + made this code generic and plugin-independent */
118 if (query.method == 'update') {
119 // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
120 query.analyzed_subqueries();
123 /* NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
124 * le query plan et obtenir toutes les infos, par contre on ne peut pas
125 * savoir d'avance quels parametres ont été accordés, changés, etc.
126 * Dans le cas général, ca pourrait affecter le query plan...
127 * Par contre on n'a pas d'information sur toutes les resources, mais
128 * uniquement celles dans la liste. Comment gérer ?
131 /* Inform child plugins about their respective parts of the results */
133 var r_subq = query.analyzed_query.subqueries['resource'];
134 var l_subq = query.analyzed_query.subqueries['lease'];
135 $.publish('/results/' + r_subq.uuid + '/changed', [slice['resource'], r_subq]);
136 $.publish('/results/' + l_subq.uuid + '/changed', [slice['lease'], l_subq]);
138 /* Subscribe to get notifications from child plugins */
139 if (!data.child_subscribe) {
140 $.subscribe('/update-set/' + r_subq.uuid, {instance: e.data.instance}, update_resources);
141 $.subscribe('/update-set/' + l_subq.uuid, {instance: e.data.instance}, update_leases);
142 data.child_subscribe = true