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' );
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 // xxx - here -- here -- xxx
35 $('#updater-' + options.plugin_uuid).click({instance: $this}, function (e) {
36 var data = e.data.instance.data().Slices;
37 tophat_async_exec([{'query': data.update_query, 'id': null}]);
38 //$('#updateslice-' + options.plugin_uuid).prop('disabled', true);
41 /* End of plugin initialization */
43 $(this).data('Slices', {
47 child_subscribe: false, /* Are we listening for children updates */
54 destroy : function( ) {
56 return this.each(function(){
57 var $this = $(this), data = $this.data('Slices');
58 $(window).unbind('Slices');
60 $this.removeData('Slices');
65 show : function( content ) { }
70 function update_resources(e, resources, change)
72 data = e.data.instance.data().Slices;
74 data.update_query.params['resource'] = resources
75 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
78 function update_leases(e, leases, change)
80 data = e.data.instance.data().Slices;
82 data.update_query.params['lease'] = leases
83 $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
86 function update_slice(e, rows, query) {
87 /* This function is called twice : get and update */
89 var data = e.data.instance.data().Slices;
91 /* Update placeholders and trigger subqueries updates */
92 if (rows.length == 0) {
99 if (data.update_query == null) {
100 data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
102 /* In case of update the list of resources and leases should be updated accordingly */
105 $.each(slice, function(key, value) {
106 if (typeof value == 'string') {
107 $('#myslice__' + key).html(value);
111 /* TODO avoid repetitions + made this code generic and plugin-independent */
113 if (query.method == 'update') {
114 // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
115 query.analyzed_subqueries();
118 /* NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
119 * le query plan et obtenir toutes les infos, par contre on ne peut pas
120 * savoir d'avance quels parametres ont été accordés, changés, etc.
121 * Dans le cas général, ca pourrait affecter le query plan...
122 * Par contre on n'a pas d'information sur toutes les resources, mais
123 * uniquement celles dans la liste. Comment gérer ?
126 /* Inform child plugins about their respective parts of the results */
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]);
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