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();
38 this.selected_resources = Array();
40 this.table = this.elmt('table').dataTable({
41 // the original querytable layout was
42 // sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
43 // however the bottom line with 'showing blabla...' and the navigation widget are not really helpful
44 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
45 // so this does not matter anymore now that the pagination area is turned off
46 // sPaginationType: 'bootstrap',
50 // sScrollX : '100%', // Horizontal scrolling
51 // bSortClasses : false, // Disable style for the sorted column
52 // aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
53 // fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
54 // /* Prevent to loop on click while redrawing table */
55 // $('.ResourceSelectedClose').unbind('click');
56 // /* Handle clicks on close span */
57 // /* Reassociate close click every time the table is redrawn */
58 // $('.ResourceSelectedClose').bind('click', self, self._close_click);
62 // XXX This should not be done at init...
63 this.elmt('update').click(this, this.do_checksla);
64 this.elmt('refresh').click(this, this.do_refresh);
65 this.elmt('reset').click(this, this.do_reset);
66 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
68 this.listen_query(options.query_uuid);
71 /*************************** PLUGIN EVENTS ****************************/
73 /***************************** GUI EVENTS *****************************/
75 do_checksla: function(e) {
76 var username = e.data.options.username;
78 var arrayselectedresources = data.selected_resources;
80 var accepted_sla = Array();
82 // XXX check that the query is not disabled
84 console.log("DATA VALUE: " + data.value);
86 //<p>SLA description</p>
87 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>
88 //<p>SLA description</p>
89 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>
90 var promt = $('#modal-body');
97 var wimessage = '<p>SLA description</p><p>to be deployed</p>';
98 for(var iter = 0; iter < arrayselectedresources.length; iter++){
99 var list = '<p class="wi'+iter+'" name=wi"'+iter+'">'+arrayselectedresources[iter].toLowerCase()+'</p><br>';
102 if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
104 accepted_sla.push({"wilab2":false});
112 //var wallmessage = '<p>SLA description</p><p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>';
115 for(var iter = 0; iter < arrayselectedresources.length; iter++){
116 var list = '<class="wall'+iter+'" name=wall"'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'</p><br>';
118 if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
120 accepted_sla.push({"wall2":false});
127 promt.append(wimessage);
128 var flagDouble = false;
132 promt.append(wilabForm);
136 //promt.append(wallmessage);
138 promt.append(wallForm);
142 // var wimessage = '<p>SLA description</p><p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>'
144 $('#sla_dialog').show();
145 $('#slamodal-wilab2').modal('show');
148 $("#accept_sla_wilab2").click(function(){
149 console.log("SLA ACCEPTED");
150 console.log("With username: " + username);
152 // var promt = $('#modal-body');
153 // var notchecked = true;
154 // for (var i=0;i<50;i++)
156 // var wielement = $('#wi'+i);
157 // var wallElement = $('#wall'+i);
158 // if(wielement != null && !wielement.checked)
160 // notchecked = false;
162 // if(wallElement!= null && !wallElement.checked)
164 // notchecked = false;
171 $.post("/sla/agreements/simplecreate",
172 { "template_id": "iMindsServiceWiLab2",
174 "expiration_time": new Date().toISOString()
176 $.post("/sla/agreements/simplecreate",
177 { "template_id": "iMindsServiceVirtualwall",
179 "expiration_time": new Date().toISOString()
182 $('#slamodal-wilab2').modal('hide');
183 accepted_sla["wilab2"] = true;
189 $("#dismiss_sla_wilab2").click(function(){
190 console.log("SLA NOT ACCEPTED");
191 $('#slamodal-wilab2').modal('hide');
194 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
195 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
196 // promt.append(list);
197 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
201 // accepted_sla.push({"wall2":false});
203 // $('#sla_dialog').show();
204 // $('#slamodal-virtualwall').modal('show');
207 // $("#accept_sla_vwall").click(function(){
208 // console.log("SLA ACCEPTED");
209 // console.log("With username: " + username);
211 // $.post("/sla/agreements/simplecreate",
212 // { "template_id": "iMindsServiceVirtualwall",
214 // "expiration_time": new Date()
217 // $('#slamodal-virtualwall').modal('hide');
218 // accepted_sla["wall2"] = true;
221 // $("#dismiss_sla_vwall").click(function(){
222 // console.log("SLA NOT ACCEPTED");
223 // $('#slamodal-vir').modal('hide');
228 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
230 // accepted_sla.push({"wilab2":false});
232 // $('#sla_dialog').show();
233 // $('#slamodal-wilab2').modal('show');
236 // $("#accept_sla_wilab2").click(function(){
237 // console.log("SLA ACCEPTED");
238 // console.log("With username: " + username);
240 // $.post("/sla/agreements/simplecreate",
241 // { "template_id": "iMindsServiceWiLab2",
243 // "expiration_time": new Date()
246 // $('#slamodal-wilab2').modal('hide');
247 // accepted_sla["wilab2"] = true;
250 // $("#dismiss_sla_wilab2").click(function(){
251 // console.log("SLA NOT ACCEPTED");
252 // $('#slamodal-wilab2').modal('hide');
260 // for(var sla in accepted_sla){
261 // if(accepted_sla[sla] == true){
266 // if(count == accepted_sla.length){
267 // this.do_update(e);
272 do_update: function(e) {
275 var username = e.data.options.username;
276 var urn = data.value;
277 // XXX check that the query is not disabled
280 console.log("do_update");
281 // XXX check that the query is not disabled
282 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
284 // how to stop the spinning after the event?
285 // this should be triggered by some on_updatequery_done ?
289 // related buttons are also disabled in the html template
290 do_refresh: function(e)
292 throw 'resource_selected.do_refresh Not implemented';
295 do_reset: function(e)
297 throw 'queryupdater.do_reset Not implemented';
300 do_clear_annotations: function(e)
302 throw 'queryupdater.do_clear_annotations Not implemented';
305 /************************** GUI MANIPULATION **************************/
308 set_button_state: function(name, state)
310 this.elmt(name).attr('disabled', state ? false : 'disabled');
318 find_row: function(key)
320 // key in third position, column id = 2
323 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
324 return (col._aData[KEY_POS] == key);
327 if (cols.length == 0)
330 throw "Too many same-key rows in ResourceSelected plugin";
335 set_state: function(data)
337 console.log("function set_state");
344 // make sure the change is visible : toggle on the whole plugin
345 // this might have to be made an 'auto-toggle' option of this plugin..
346 // also it might be needed to be a little finer-grained here
348 // XXX we don't want to show automaticaly the pending when a checkbox is checked
351 switch(data.request) {
352 case FIELD_REQUEST_ADD_RESET:
353 case FIELD_REQUEST_REMOVE_RESET:
354 // find line and delete it
355 row = this.find_row(data.value);
357 this.table.fnDeleteRow(row.nTr);
358 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
359 $("#badge-pending").text($("#badge-pending").data('number'));
361 case FIELD_REQUEST_CHANGE:
364 case FIELD_REQUEST_ADD:
367 case FIELD_REQUEST_REMOVE:
372 switch(data.status) {
373 case FIELD_REQUEST_PENDING:
375 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
377 case FIELD_REQUEST_SUCCESS:
380 case FIELD_REQUEST_FAILURE:
385 var status = msg + status;
390 // if no, create it, else replace it
391 // XXX it's not just about adding lines, but sometimes removing some
392 // XXX how do we handle status reset ?
393 data.value = JSON.stringify(data.value);
394 data.selected_resources = this.selected_resources;
395 row = this.find_row(data.value);
404 // XXX second parameter refresh = false can improve performance. todo in querytable also
405 this.table.fnAddData(newline);
406 row = this.find_row(data.value);
407 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
408 $("#badge-pending").text($("#badge-pending").data('number'));
410 // Update row text...
411 this.table.fnUpdate(newline, row.nTr);
414 // Change cell color according to status
416 $(row.nTr).removeClass('add remove')
417 var cls = action.toLowerCase();
419 $(row.nTr).addClass(cls);
423 /*************************** QUERY HANDLER ****************************/
427 /*************************** RECORD HANDLER ***************************/
429 on_new_record: function(record)
431 console.log("query_updater on_new_record");
434 // if (not and update) {
436 // initial['resource'], initial['lease'] ?
437 this.initial.push(record);
439 //this.set_record_state(record, RECORD_STATE_ATTACHED);
440 // We simply add to the table
442 // \ this.initial_resources
445 // current_resources \ YES | NO
446 // --------------------+----------+---------
447 // YES | attached | added
455 // +-----------------+--------------+
457 // +-------+ ?G +-------+ +-------+ +---+---+ |
458 // | | -----> | | !G | | | | DA |
459 // | ND | | PG | -----> | D | -----> | PC | <------+ |
460 // | | <----- | | ~G | | C | | | |
461 // +-------+ GE +-------+ +-------+ +-------+ +------+
463 // | DA UE | | ?U | PCA |
465 // +-------+ +-------+ +------+
467 // | AD | <----- | PU | --------+
469 // +-------+ +-------+
474 // Plugins (i) receive state information, (ii) perform actions
477 // ND : No data ?G : Get query
478 // PG : Pending Get !G : Get reply
479 // D : Data present ~G : Get partial reply
480 // PC : Pending changes GE : Get error
481 // PU : Pending update C : Change request
482 // PCA: Pending change with annotation R : Reset request
483 // AD : Annotated data ?U : Update query
485 // ~U : Update partial reply
487 // DA : Delete annotation request
489 // - D -> PU directly if the user chooses 'dynamic update'
490 // - Be careful for updates if partial Get success
492 // ND: No data == initialization state
495 // - on_query_in_progress
496 // NOTE: cannot distinguish get and update here. Is it important ?
498 on_query_in_progress: function()
500 messages.debug("queryupdater.on_query_in_progress");
505 // - on_clear_records (Get)
506 // - on_new_record (shared with AD) XXX
508 // NOTE: cannot distinguish get and update here. Is it important ?
509 // NOTE: Would record key be sufficient for update ?
511 on_clear_records: function()
516 on_query_done: function()
518 console.log("on_query_done");
522 // PC : Pending changes
523 // NOTE: record_key could be sufficient
524 on_added_record: function(record)
526 console.log("on_added_record = ",record);
527 this.set_record_state(record, RECORD_STATE_ADDED);
528 // update pending number
531 on_removed_record: function(record_key)
533 console.log("on_removed_record = ",record_key);
534 this.set_record_state(RECORD_STATE_REMOVED);
537 // PU : Pending update
538 // - on_query_in_progress (already done)
540 // PCA : Pending change with annotation
541 // NOTE: Manifold will inform the plugin about updates, and thus won't
542 // call new record, even if the same channel UUID is used...
543 // - TODO on_updated_record
544 // - Key and confirmation could be sufficient, or key and record state
545 // XXX move record state to the manifold plugin API
547 on_field_state_changed: function(result)
549 console.log("on_field_state_changed");
551 if(result.request == FIELD_REQUEST_ADD){
552 this.selected_resources.push(result.value);
553 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
554 var i = this.selected_resources.indexOf(result.value);
556 this.selected_resources.splice(i,1);
559 console.log("Resources: " + self.selected_resources);
560 messages.debug(result)
561 /* this.set_state(result.request, result.key, result.value, result.status); */
562 this.set_state(result);
565 // XXX we will have the requests for change
566 // XXX + the requests to move into the query cycle = the buttons aforementioned
568 // XXX what happens in case of updates ? not implemented yet
569 // XXX we want resources and leases
570 // listen for SET_ADD and SET_REMOVE for slice query
572 /************************** PRIVATE METHODS ***************************/
574 _close_click: function(e)
578 //jQuery.publish('selected', 'add/'+key_value);
579 // this.parentNode is <td> this.parentNode.parentNode is <tr>
580 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
581 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
582 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
583 var remove_urn = this.id;
584 var current_resources = event.data.instance.current_resources;
585 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
586 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
587 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
590 /******************************** TODO ********************************/
592 update_resources: function(resources, change)
594 messages.debug("update_resources");
595 var my_oTable = this.table.dataTable();
596 var prev_resources = this.current_resources;
599 * The first time the query is advertised, don't do anything. The
600 * component will learn nodes in the slice through the manifest
601 * received through the other subscription
606 var initial = this.initial;
607 //var r_removed = []; //
608 /*-----------------------------------------------------------------------
609 TODO: remove this dirty hack !!!
611 resources = jQuery.map(resources, function(x){
612 if(!('timeslot' in x)){x.timeslot=0;}
616 TODO: handle generic keys instead of specific stuff
620 var initial_urn = $.map(initial, function(x){return x.urn;});
621 var resources_urn = $.map(resources, function(x){return x.urn;});
622 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
623 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
624 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
625 exists = false; // ioi
626 /*-----------------------------------------------------------------------*/
628 my_oTable.fnClearTable();
630 TODO: factorization of this code !!!
632 $.each(r_added, function(i, r) {
633 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
635 // Create the resource objects
640 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
641 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
644 newline.push(type, urn, slot, SPAN); // ioi
645 var line = my_oTable.fnAddData(newline);
646 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
647 nTr.className = type;
649 $.each(r_attached, function(i, r) {
650 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
651 var type = 'attached';
652 // Create the resource objects
657 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
658 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
661 newline.push(type, node, slot, SPAN); // ioi
662 var line = my_oTable.fnAddData(newline);
663 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
664 nTr.className = type;
666 $.each(r_removed, function(i, r) {
667 // The list contains objects
670 var time = r.timeslot;
672 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
673 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
676 newline.push('remove', node, slot, SPAN); // ioi
677 var line = my_oTable.fnAddData(newline);
678 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
679 nTr.className = 'remove';
682 this.current_resources = $.merge(r_attached,r_added);
684 /* Allow the user to update the slice */
685 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
687 }, // update_resources
691 $.plugin('QueryUpdater', QueryUpdater);