2 * Description: associate with a Get query, maintains the 'Update' query that records pending changes
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
7 // xxx this is ongoing work, very rough, and not working at all yet
11 $.fn.Updater = function ( method ) {
12 /* Method calling logic */
13 if ( methods[method] ) {
14 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
15 } else if ( typeof method === 'object' || ! method ) {
16 return methods.init.apply( this, arguments );
18 $.error( 'Method ' + method + ' does not exist on $.Updater' );
23 init : function( options ) {
24 return this.each(function(){
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); } );
33 destroy : function( ) {
34 return this.each(function(){
36 $(window).unbind('Updater');
37 data.Updater.remove();
38 $this.removeData('Updater');
42 show : function( content ) { }
45 function Updater (options) {
47 // xxx should try to locate update_query first, in case we have several Updaters
49 // however the mental model behind the global manifold object for now is
50 // to unambiguously find a query based on its query_uuid, which in the joomla
51 // implementation wouldn't fly
52 // we keep this for a later improvement
53 var query=manifold.find_query (options.query_uuid);
54 messages.info("retrieved query " + query.__repr());
55 // very rough way of filling this for now
57 new ManifoldQuery ("update", query.subject, null, query.filters,
60 undefined, /* unique */
62 undefined, undefined /* maybe some day I'll get that one */);
63 manifold.insert_query (this.update_query);
64 // arm button once document is loaded
65 (function(updater) {$(document).ready(function(){updater.arm_button()})})(this);
67 this.arm_button = function () {
68 $('#updater-' + this.options.plugin_uuid).click(this, this.submit_update_request);
70 this.submit_update_request = function (e) {
71 messages.debug("submit_update_request");
72 var query_uuid = e.data.options.query_uuid;
73 var update_query = e.data.update_query;
74 messages.debug("Updater.submit_update_request " + update_query.__repr());
75 // actually send the Update query, but publish results as if coming from the original query
76 manifold.asynchroneous_exec ( [ {'query_uuid': update_query.query_uuid, 'publish_uuid' : query_uuid} ]);
77 // looks like a previous attempt to disable the button while the query is flying
78 //$('#updateslice-' + options.plugin_uuid).prop('disabled', true);
81 update_resources = function (e, resources, change) {
82 data = e.data.instance.data().Slices;
84 data.update_query.params['resource'] = resources
85 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
88 update_leases = function (e, leases, change) {
89 data = e.data.instance.data().Slices;
91 data.update_query.params['lease'] = leases
92 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
95 update_slice = function (e, rows, query) {
97 /* This function is called twice : get and update */
100 var data = e.data.instance.data().Slices;
102 /* Update placeholders and trigger subqueries updates */
103 if (rows.length == 0) {
110 if (data.update_query == null) {
111 data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
113 /* In case of update the list of resources and leases should be updated accordingly */
116 $.each(slice, function(key, value) {
117 if (typeof value == 'string') {
118 $('#myslice__' + key).html(value);
122 /* TODO avoid repetitions + made this code generic and plugin-independent */
124 if (query.method == 'update') {
125 // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
126 query.analyzed_subqueries();
129 /* NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
130 * le query plan et obtenir toutes les infos, par contre on ne peut pas
131 * savoir d'avance quels parametres ont été accordés, changés, etc.
132 * Dans le cas général, ca pourrait affecter le query plan...
133 * Par contre on n'a pas d'information sur toutes les resources, mais
134 * uniquement celles dans la liste. Comment gérer ?
137 /* Inform child plugins about their respective parts of the results */
139 var r_subq = query.analyzed_query.subqueries['resource'];
140 var l_subq = query.analyzed_query.subqueries['lease'];
141 $.publish('/results/' + r_subq.uuid + '/changed', [slice['resource'], r_subq]);
142 $.publish('/results/' + l_subq.uuid + '/changed', [slice['lease'], l_subq]);
144 /* Subscribe to get notifications from child plugins */
145 if (!data.child_subscribe) {
146 $.subscribe('/update-set/' + r_subq.uuid, {instance: e.data.instance}, update_resources);
147 $.subscribe('/update-set/' + l_subq.uuid, {instance: e.data.instance}, update_leases);
148 data.child_subscribe = true