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) {
77 var username = e.data.options.username;
79 // XXX check that the query is not disabled
81 console.log("DATA VALUE: " + data.value);
83 if (data.value.toLowerCase().indexOf("iminds") >= 0){
85 $('#sla_dialog').show();
86 $('#slamodal').modal('show');
88 $(document).ready(function() {
89 $("#accept_sla").click(function(){
90 console.log("SLA ACCEPTED");
91 console.log("With username: " + username);
92 if(urn.toLowerCase().indexOf("wall2") >= 0){
93 $.post("/sla/agreements/simplecreate",
94 { "template_id": "iMindsServiceVirtualwall",
96 "expiration_time": new Date() // jgarcia: FIXME
98 } else if(urn.toLowerCase().indexOf("wilab2") >= 0){
99 $.post("/sla/agreements/simplecreate",
100 { "template_id":"iMindsServiceWiLab2",
102 "expiration_time": new Date() // jgarcia: FIXME
105 $('#slamodal').modal('hide');
107 console.log("Executing do_update after sending SLA");
108 // XXX check that the query is not disabled
109 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
111 // how to stop the spinning after the event?
112 // this should be triggered by some on_updatequery_done ?
116 $(document).ready(function() {
117 $("#dismiss_sla").click(function(){
118 console.log("SLA NOT ACCEPTED");
119 $('#slamodal').modal('hide');
125 console.log("do_update");
126 // XXX check that the query is not disabled
127 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
129 // how to stop the spinning after the event?
130 // this should be triggered by some on_updatequery_done ?
135 // related buttons are also disabled in the html template
136 do_refresh: function(e)
138 throw 'resource_selected.do_refresh Not implemented';
141 do_reset: function(e)
143 throw 'queryupdater.do_reset Not implemented';
146 do_clear_annotations: function(e)
148 throw 'queryupdater.do_clear_annotations Not implemented';
151 /************************** GUI MANIPULATION **************************/
154 set_button_state: function(name, state)
156 this.elmt(name).attr('disabled', state ? false : 'disabled');
164 find_row: function(key)
166 // key in third position, column id = 2
169 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
170 return (col._aData[KEY_POS] == key);
173 if (cols.length == 0)
176 throw "Too many same-key rows in ResourceSelected plugin";
181 set_state: function(data)
183 console.log("function set_state");
190 // make sure the change is visible : toggle on the whole plugin
191 // this might have to be made an 'auto-toggle' option of this plugin..
192 // also it might be needed to be a little finer-grained here
194 // XXX we don't want to show automaticaly the pending when a checkbox is checked
197 switch(data.request) {
198 case FIELD_REQUEST_ADD_RESET:
199 case FIELD_REQUEST_REMOVE_RESET:
200 // find line and delete it
201 row = this.find_row(data.value);
203 this.table.fnDeleteRow(row.nTr);
204 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
205 $("#badge-pending").text($("#badge-pending").data('number'));
207 case FIELD_REQUEST_CHANGE:
210 case FIELD_REQUEST_ADD:
213 case FIELD_REQUEST_REMOVE:
218 switch(data.status) {
219 case FIELD_REQUEST_PENDING:
221 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
223 case FIELD_REQUEST_SUCCESS:
226 case FIELD_REQUEST_FAILURE:
231 var status = msg + status;
236 // if no, create it, else replace it
237 // XXX it's not just about adding lines, but sometimes removing some
238 // XXX how do we handle status reset ?
239 data.value = JSON.stringify(data.value);
240 row = this.find_row(data.value);
249 // XXX second parameter refresh = false can improve performance. todo in querytable also
250 this.table.fnAddData(newline);
251 row = this.find_row(data.value);
252 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
253 $("#badge-pending").text($("#badge-pending").data('number'));
255 // Update row text...
256 this.table.fnUpdate(newline, row.nTr);
259 // Change cell color according to status
261 $(row.nTr).removeClass('add remove')
262 var cls = action.toLowerCase();
264 $(row.nTr).addClass(cls);
268 /*************************** QUERY HANDLER ****************************/
272 /*************************** RECORD HANDLER ***************************/
274 on_new_record: function(record)
276 console.log("query_updater on_new_record");
279 // if (not and update) {
281 // initial['resource'], initial['lease'] ?
282 this.initial.push(record);
284 //this.set_record_state(record, RECORD_STATE_ATTACHED);
285 // We simply add to the table
287 // \ this.initial_resources
290 // current_resources \ YES | NO
291 // --------------------+----------+---------
292 // YES | attached | added
300 // +-----------------+--------------+
302 // +-------+ ?G +-------+ +-------+ +---+---+ |
303 // | | -----> | | !G | | | | DA |
304 // | ND | | PG | -----> | D | -----> | PC | <------+ |
305 // | | <----- | | ~G | | C | | | |
306 // +-------+ GE +-------+ +-------+ +-------+ +------+
308 // | DA UE | | ?U | PCA |
310 // +-------+ +-------+ +------+
312 // | AD | <----- | PU | --------+
314 // +-------+ +-------+
319 // Plugins (i) receive state information, (ii) perform actions
322 // ND : No data ?G : Get query
323 // PG : Pending Get !G : Get reply
324 // D : Data present ~G : Get partial reply
325 // PC : Pending changes GE : Get error
326 // PU : Pending update C : Change request
327 // PCA: Pending change with annotation R : Reset request
328 // AD : Annotated data ?U : Update query
330 // ~U : Update partial reply
332 // DA : Delete annotation request
334 // - D -> PU directly if the user chooses 'dynamic update'
335 // - Be careful for updates if partial Get success
337 // ND: No data == initialization state
340 // - on_query_in_progress
341 // NOTE: cannot distinguish get and update here. Is it important ?
343 on_query_in_progress: function()
345 messages.debug("queryupdater.on_query_in_progress");
350 // - on_clear_records (Get)
351 // - on_new_record (shared with AD) XXX
353 // NOTE: cannot distinguish get and update here. Is it important ?
354 // NOTE: Would record key be sufficient for update ?
356 on_clear_records: function()
361 on_query_done: function()
363 console.log("on_query_done");
367 // PC : Pending changes
368 // NOTE: record_key could be sufficient
369 on_added_record: function(record)
371 console.log("on_added_record = ",record);
372 this.set_record_state(record, RECORD_STATE_ADDED);
373 // update pending number
376 on_removed_record: function(record_key)
378 console.log("on_removed_record = ",record_key);
379 this.set_record_state(RECORD_STATE_REMOVED);
382 // PU : Pending update
383 // - on_query_in_progress (already done)
385 // PCA : Pending change with annotation
386 // NOTE: Manifold will inform the plugin about updates, and thus won't
387 // call new record, even if the same channel UUID is used...
388 // - TODO on_updated_record
389 // - Key and confirmation could be sufficient, or key and record state
390 // XXX move record state to the manifold plugin API
392 on_field_state_changed: function(result)
394 console.log("on_field_state_changed");
396 messages.debug(result)
397 /* this.set_state(result.request, result.key, result.value, result.status); */
398 this.set_state(result);
401 // XXX we will have the requests for change
402 // XXX + the requests to move into the query cycle = the buttons aforementioned
404 // XXX what happens in case of updates ? not implemented yet
405 // XXX we want resources and leases
406 // listen for SET_ADD and SET_REMOVE for slice query
408 /************************** PRIVATE METHODS ***************************/
410 _close_click: function(e)
414 //jQuery.publish('selected', 'add/'+key_value);
415 // this.parentNode is <td> this.parentNode.parentNode is <tr>
416 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
417 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
418 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
419 var remove_urn = this.id;
420 var current_resources = event.data.instance.current_resources;
421 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
422 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
423 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
426 /******************************** TODO ********************************/
428 update_resources: function(resources, change)
430 messages.debug("update_resources");
431 var my_oTable = this.table.dataTable();
432 var prev_resources = this.current_resources;
435 * The first time the query is advertised, don't do anything. The
436 * component will learn nodes in the slice through the manifest
437 * received through the other subscription
442 var initial = this.initial;
443 //var r_removed = []; //
444 /*-----------------------------------------------------------------------
445 TODO: remove this dirty hack !!!
447 resources = jQuery.map(resources, function(x){
448 if(!('timeslot' in x)){x.timeslot=0;}
452 TODO: handle generic keys instead of specific stuff
456 var initial_urn = $.map(initial, function(x){return x.urn;});
457 var resources_urn = $.map(resources, function(x){return x.urn;});
458 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
459 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
460 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
461 exists = false; // ioi
462 /*-----------------------------------------------------------------------*/
464 my_oTable.fnClearTable();
466 TODO: factorization of this code !!!
468 $.each(r_added, function(i, r) {
469 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
471 // Create the resource objects
476 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
477 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
480 newline.push(type, urn, slot, SPAN); // ioi
481 var line = my_oTable.fnAddData(newline);
482 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
483 nTr.className = type;
485 $.each(r_attached, function(i, r) {
486 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
487 var type = 'attached';
488 // Create the resource objects
493 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
494 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
497 newline.push(type, node, slot, SPAN); // ioi
498 var line = my_oTable.fnAddData(newline);
499 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
500 nTr.className = type;
502 $.each(r_removed, function(i, r) {
503 // The list contains objects
506 var time = r.timeslot;
508 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
509 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
512 newline.push('remove', node, slot, SPAN); // ioi
513 var line = my_oTable.fnAddData(newline);
514 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
515 nTr.className = 'remove';
518 this.current_resources = $.merge(r_attached,r_added);
520 /* Allow the user to update the slice */
521 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
523 }, // update_resources
527 $.plugin('QueryUpdater', QueryUpdater);