2 * MySlice QueryUpdater plugin
4 * URL: http://www.myslice.info
5 * Description: display of selected resources
7 * Author: The MySlice Team
8 * Copyright: Copyright 2012 UPMC Sorbonne Universités
17 // XXX record selected (multiple selection ?)
18 // XXX record disabled ?
19 // XXX out of sync plugin ?
20 // XXX out of date record ?
23 // criticality of the absence of a handler in a plugin
24 // non-critical only can have switch case
26 // Record state through the query cycle
29 var QueryUpdater = Plugin.extend({
31 init: function(options, element) {
32 this.classname="queryupdater";
33 this._super(options, element);
37 this.initial = Array();
39 this.table = this.elmt('table').dataTable({
40 // the original querytable layout was
41 // sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
42 // however the bottom line with 'showing blabla...' and the navigation widget are not really helpful
43 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
44 // so this does not matter anymore now that the pagination area is turned off
45 // sPaginationType: 'bootstrap',
49 // sScrollX : '100%', // Horizontal scrolling
50 // bSortClasses : false, // Disable style for the sorted column
51 // aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
52 // fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
53 // /* Prevent to loop on click while redrawing table */
54 // $('.ResourceSelectedClose').unbind('click');
55 // /* Handle clicks on close span */
56 // /* Reassociate close click every time the table is redrawn */
57 // $('.ResourceSelectedClose').bind('click', self, self._close_click);
61 // XXX This should not be done at init...
62 this.elmt('update').click(this, this.do_update);
63 this.elmt('refresh').click(this, this.do_refresh);
64 this.elmt('reset').click(this, this.do_reset);
65 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
67 this.listen_query(options.query_uuid);
70 /*************************** PLUGIN EVENTS ****************************/
72 /***************************** GUI EVENTS *****************************/
74 do_update: function(e) {
78 console.log("do_update");
80 // XXX check that the query is not disabled
81 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
83 // how to stop the spinning after the event?
84 // this should be triggered by some on_updatequery_done ?
87 // related buttons are also disabled in the html template
88 do_refresh: function(e)
90 throw 'resource_selected.do_refresh Not implemented';
95 throw 'queryupdater.do_reset Not implemented';
98 do_clear_annotations: function(e)
100 throw 'queryupdater.do_clear_annotations Not implemented';
103 /************************** GUI MANIPULATION **************************/
106 set_button_state: function(name, state)
108 this.elmt(name).attr('disabled', state ? false : 'disabled');
116 find_row: function(key)
118 // key in third position, column id = 2
121 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
122 return (col._aData[KEY_POS] == key);
125 if (cols.length == 0)
128 throw "Too many same-key rows in ResourceSelected plugin";
133 set_state: function(data)
135 console.log("function set_state");
142 // make sure the change is visible : toggle on the whole plugin
143 // this might have to be made an 'auto-toggle' option of this plugin..
144 // also it might be needed to be a little finer-grained here
146 // XXX we don't want to show automaticaly the pending when a checkbox is checked
149 switch(data.request) {
150 case FIELD_REQUEST_ADD_RESET:
151 case FIELD_REQUEST_REMOVE_RESET:
152 // find line and delete it
153 row = this.find_row(data.value);
155 this.table.fnDeleteRow(row.nTr);
156 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
157 $("#badge-pending").text($("#badge-pending").data('number'));
159 case FIELD_REQUEST_CHANGE:
162 case FIELD_REQUEST_ADD:
165 case FIELD_REQUEST_REMOVE:
170 switch(data.status) {
171 case FIELD_REQUEST_PENDING:
173 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
175 case FIELD_REQUEST_SUCCESS:
178 case FIELD_REQUEST_FAILURE:
183 var status = msg + status;
188 // if no, create it, else replace it
189 // XXX it's not just about adding lines, but sometimes removing some
190 // XXX how do we handle status reset ?
191 data.value = JSON.stringify(data.value);
192 row = this.find_row(data.value);
201 // XXX second parameter refresh = false can improve performance. todo in querytable also
202 this.table.fnAddData(newline);
203 row = this.find_row(data.value);
204 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
205 $("#badge-pending").text($("#badge-pending").data('number'));
207 // Update row text...
208 this.table.fnUpdate(newline, row.nTr);
211 // Change cell color according to status
213 $(row.nTr).removeClass('add remove')
214 var cls = action.toLowerCase();
216 $(row.nTr).addClass(cls);
220 /*************************** QUERY HANDLER ****************************/
224 /*************************** RECORD HANDLER ***************************/
226 on_new_record: function(record)
228 console.log("query_updater on_new_record");
231 // if (not and update) {
233 // initial['resource'], initial['lease'] ?
234 this.initial.push(record);
236 //this.set_record_state(record, RECORD_STATE_ATTACHED);
237 // We simply add to the table
239 // \ this.initial_resources
242 // current_resources \ YES | NO
243 // --------------------+----------+---------
244 // YES | attached | added
252 // +-----------------+--------------+
254 // +-------+ ?G +-------+ +-------+ +---+---+ |
255 // | | -----> | | !G | | | | DA |
256 // | ND | | PG | -----> | D | -----> | PC | <------+ |
257 // | | <----- | | ~G | | C | | | |
258 // +-------+ GE +-------+ +-------+ +-------+ +------+
260 // | DA UE | | ?U | PCA |
262 // +-------+ +-------+ +------+
264 // | AD | <----- | PU | --------+
266 // +-------+ +-------+
271 // Plugins (i) receive state information, (ii) perform actions
274 // ND : No data ?G : Get query
275 // PG : Pending Get !G : Get reply
276 // D : Data present ~G : Get partial reply
277 // PC : Pending changes GE : Get error
278 // PU : Pending update C : Change request
279 // PCA: Pending change with annotation R : Reset request
280 // AD : Annotated data ?U : Update query
282 // ~U : Update partial reply
284 // DA : Delete annotation request
286 // - D -> PU directly if the user chooses 'dynamic update'
287 // - Be careful for updates if partial Get success
289 // ND: No data == initialization state
292 // - on_query_in_progress
293 // NOTE: cannot distinguish get and update here. Is it important ?
295 on_query_in_progress: function()
297 messages.debug("queryupdater.on_query_in_progress");
302 // - on_clear_records (Get)
303 // - on_new_record (shared with AD) XXX
305 // NOTE: cannot distinguish get and update here. Is it important ?
306 // NOTE: Would record key be sufficient for update ?
308 on_clear_records: function()
313 on_query_done: function()
315 console.log("on_query_done");
319 // PC : Pending changes
320 // NOTE: record_key could be sufficient
321 on_added_record: function(record)
323 console.log("on_added_record = ",record);
324 this.set_record_state(record, RECORD_STATE_ADDED);
325 // update pending number
328 on_removed_record: function(record_key)
330 console.log("on_removed_record = ",record_key);
331 this.set_record_state(RECORD_STATE_REMOVED);
334 // PU : Pending update
335 // - on_query_in_progress (already done)
337 // PCA : Pending change with annotation
338 // NOTE: Manifold will inform the plugin about updates, and thus won't
339 // call new record, even if the same channel UUID is used...
340 // - TODO on_updated_record
341 // - Key and confirmation could be sufficient, or key and record state
342 // XXX move record state to the manifold plugin API
344 on_field_state_changed: function(result)
346 console.log("on_field_state_changed");
348 messages.debug(result)
349 /* this.set_state(result.request, result.key, result.value, result.status); */
350 this.set_state(result);
353 // XXX we will have the requests for change
354 // XXX + the requests to move into the query cycle = the buttons aforementioned
356 // XXX what happens in case of updates ? not implemented yet
357 // XXX we want resources and leases
358 // listen for SET_ADD and SET_REMOVE for slice query
360 /************************** PRIVATE METHODS ***************************/
362 _close_click: function(e)
366 //jQuery.publish('selected', 'add/'+key_value);
367 // this.parentNode is <td> this.parentNode.parentNode is <tr>
368 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
369 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
370 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
371 var remove_urn = this.id;
372 var current_resources = event.data.instance.current_resources;
373 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
374 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
375 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
378 /******************************** TODO ********************************/
380 update_resources: function(resources, change)
382 messages.debug("update_resources");
383 var my_oTable = this.table.dataTable();
384 var prev_resources = this.current_resources;
387 * The first time the query is advertised, don't do anything. The
388 * component will learn nodes in the slice through the manifest
389 * received through the other subscription
394 var initial = this.initial;
395 //var r_removed = []; //
396 /*-----------------------------------------------------------------------
397 TODO: remove this dirty hack !!!
399 resources = jQuery.map(resources, function(x){
400 if(!('timeslot' in x)){x.timeslot=0;}
404 TODO: handle generic keys instead of specific stuff
408 var initial_urn = $.map(initial, function(x){return x.urn;});
409 var resources_urn = $.map(resources, function(x){return x.urn;});
410 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
411 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
412 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
413 exists = false; // ioi
414 /*-----------------------------------------------------------------------*/
416 my_oTable.fnClearTable();
418 TODO: factorization of this code !!!
420 $.each(r_added, function(i, r) {
421 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
423 // Create the resource objects
428 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
429 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
432 newline.push(type, urn, slot, SPAN); // ioi
433 var line = my_oTable.fnAddData(newline);
434 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
435 nTr.className = type;
437 $.each(r_attached, function(i, r) {
438 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
439 var type = 'attached';
440 // Create the resource objects
445 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
446 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
449 newline.push(type, node, slot, SPAN); // ioi
450 var line = my_oTable.fnAddData(newline);
451 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
452 nTr.className = type;
454 $.each(r_removed, function(i, r) {
455 // The list contains objects
458 var time = r.timeslot;
460 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
461 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
464 newline.push('remove', node, slot, SPAN); // ioi
465 var line = my_oTable.fnAddData(newline);
466 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
467 nTr.className = 'remove';
470 this.current_resources = $.merge(r_attached,r_added);
472 /* Allow the user to update the slice */
473 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
475 }, // update_resources
479 $.plugin('QueryUpdater', QueryUpdater);