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();
83 // XXX check that the query is not disabled
85 console.log("DATA VALUE: " + data.value);
87 //<p>SLA description</p>
88 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>
89 //<p>SLA description</p>
90 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>
91 var promt = $('#modal-body');
97 promt.append('<p>SLA description</p>');
101 for(var iter = 0; iter < arrayselectedresources.length; iter++){
102 var list = '<li class="wi'+iter+'" name=wi"'+iter+'">'+arrayselectedresources[iter].toLowerCase()+'</li>';
104 if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
106 accepted_sla.push({"wilab2":false});
113 wilabForm += "</ul>";
115 //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>';
119 for(var iter = 0; iter < arrayselectedresources.length; iter++){
120 var list = '<li class="wall'+iter+'" name=wall"'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'</li>';
122 if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
124 accepted_sla.push({"wall2":false});
133 var flagDouble = false;
137 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>');
138 promt.append(wilabForm);
139 promt.append('<br />');
143 //promt.append(wallmessage);
145 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>');
146 promt.append(wallForm);
147 promt.append('<br />');
151 // 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>'
153 if(flagWi || flagVW){
154 $('#sla_dialog').show();
156 $('#slamodal-wilab2').modal('show');
162 var username = e.data.options.username;
163 var urn = data.value;
164 // XXX check that the query is not disabled
167 console.log("do_update");
168 // XXX check that the query is not disabled
169 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
173 $("#accept_sla_wilab2").click(function(){
174 console.log("SLA ACCEPTED");
175 console.log("With username: " + username);
177 // var promt = $('#modal-body');
178 // var notchecked = true;
179 // for (var i=0;i<50;i++)
181 // var wielement = $('#wi'+i);
182 // var wallElement = $('#wall'+i);
183 // if(wielement != null && !wielement.checked)
185 // notchecked = false;
187 // if(wallElement!= null && !wallElement.checked)
189 // notchecked = false;
196 $.post("/sla/agreements/simplecreate",
197 { "template_id": "iMindsServiceWiLab2",
199 "expiration_time": new Date().toISOString()
201 $.post("/sla/agreements/simplecreate",
202 { "template_id": "iMindsServiceVirtualwall",
204 "expiration_time": new Date().toISOString()
207 $('#slamodal-wilab2').modal('hide');
208 accepted_sla["wilab2"] = true;
210 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
212 $('#modal-body').empty();
215 $("#dismiss_sla_wilab2").click(function(){
216 console.log("SLA NOT ACCEPTED");
217 $('#slamodal-wilab2').modal('hide');
218 $('#modal-body').empty();
222 // this.do_update(e);
225 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
226 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
227 // promt.append(list);
228 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
232 // accepted_sla.push({"wall2":false});
234 // $('#sla_dialog').show();
235 // $('#slamodal-virtualwall').modal('show');
238 // $("#accept_sla_vwall").click(function(){
239 // console.log("SLA ACCEPTED");
240 // console.log("With username: " + username);
242 // $.post("/sla/agreements/simplecreate",
243 // { "template_id": "iMindsServiceVirtualwall",
245 // "expiration_time": new Date()
248 // $('#slamodal-virtualwall').modal('hide');
249 // accepted_sla["wall2"] = true;
252 // $("#dismiss_sla_vwall").click(function(){
253 // console.log("SLA NOT ACCEPTED");
254 // $('#slamodal-vir').modal('hide');
259 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
261 // accepted_sla.push({"wilab2":false});
263 // $('#sla_dialog').show();
264 // $('#slamodal-wilab2').modal('show');
267 // $("#accept_sla_wilab2").click(function(){
268 // console.log("SLA ACCEPTED");
269 // console.log("With username: " + username);
271 // $.post("/sla/agreements/simplecreate",
272 // { "template_id": "iMindsServiceWiLab2",
274 // "expiration_time": new Date()
277 // $('#slamodal-wilab2').modal('hide');
278 // accepted_sla["wilab2"] = true;
281 // $("#dismiss_sla_wilab2").click(function(){
282 // console.log("SLA NOT ACCEPTED");
283 // $('#slamodal-wilab2').modal('hide');
291 // for(var sla in accepted_sla){
292 // if(accepted_sla[sla] == true){
297 // if(count == accepted_sla.length){
298 // this.do_update(e);
303 do_update: function(e) {
306 var username = e.data.options.username;
307 var urn = data.value;
308 // XXX check that the query is not disabled
311 console.log("do_update");
312 // XXX check that the query is not disabled
313 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
315 // how to stop the spinning after the event?
316 // this should be triggered by some on_updatequery_done ?
320 // related buttons are also disabled in the html template
321 do_refresh: function(e)
323 throw 'resource_selected.do_refresh Not implemented';
326 do_reset: function(e)
328 throw 'queryupdater.do_reset Not implemented';
331 do_clear_annotations: function(e)
333 throw 'queryupdater.do_clear_annotations Not implemented';
336 /************************** GUI MANIPULATION **************************/
339 set_button_state: function(name, state)
341 this.elmt(name).attr('disabled', state ? false : 'disabled');
349 find_row: function(key)
351 // key in third position, column id = 2
354 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
355 return (col._aData[KEY_POS] == key);
358 if (cols.length == 0)
361 throw "Too many same-key rows in ResourceSelected plugin";
366 set_state: function(data)
368 console.log("function set_state");
375 // make sure the change is visible : toggle on the whole plugin
376 // this might have to be made an 'auto-toggle' option of this plugin..
377 // also it might be needed to be a little finer-grained here
379 // XXX we don't want to show automaticaly the pending when a checkbox is checked
382 switch(data.request) {
383 case FIELD_REQUEST_ADD_RESET:
384 case FIELD_REQUEST_REMOVE_RESET:
385 // find line and delete it
386 row = this.find_row(data.value);
388 this.table.fnDeleteRow(row.nTr);
389 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
390 $("#badge-pending").text($("#badge-pending").data('number'));
392 case FIELD_REQUEST_CHANGE:
395 case FIELD_REQUEST_ADD:
398 case FIELD_REQUEST_REMOVE:
403 switch(data.status) {
404 case FIELD_REQUEST_PENDING:
406 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
408 case FIELD_REQUEST_SUCCESS:
411 case FIELD_REQUEST_FAILURE:
416 var status = msg + status;
421 // if no, create it, else replace it
422 // XXX it's not just about adding lines, but sometimes removing some
423 // XXX how do we handle status reset ?
424 data.value = JSON.stringify(data.value);
425 data.selected_resources = this.selected_resources;
426 row = this.find_row(data.value);
435 // XXX second parameter refresh = false can improve performance. todo in querytable also
436 this.table.fnAddData(newline);
437 row = this.find_row(data.value);
438 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
439 $("#badge-pending").text($("#badge-pending").data('number'));
441 // Update row text...
442 this.table.fnUpdate(newline, row.nTr);
445 // Change cell color according to status
447 $(row.nTr).removeClass('add remove')
448 var cls = action.toLowerCase();
450 $(row.nTr).addClass(cls);
454 /*************************** QUERY HANDLER ****************************/
458 /*************************** RECORD HANDLER ***************************/
460 on_new_record: function(record)
462 console.log("query_updater on_new_record");
465 // if (not and update) {
467 // initial['resource'], initial['lease'] ?
468 this.initial.push(record);
470 //this.set_record_state(record, RECORD_STATE_ATTACHED);
471 // We simply add to the table
473 // \ this.initial_resources
476 // current_resources \ YES | NO
477 // --------------------+----------+---------
478 // YES | attached | added
486 // +-----------------+--------------+
488 // +-------+ ?G +-------+ +-------+ +---+---+ |
489 // | | -----> | | !G | | | | DA |
490 // | ND | | PG | -----> | D | -----> | PC | <------+ |
491 // | | <----- | | ~G | | C | | | |
492 // +-------+ GE +-------+ +-------+ +-------+ +------+
494 // | DA UE | | ?U | PCA |
496 // +-------+ +-------+ +------+
498 // | AD | <----- | PU | --------+
500 // +-------+ +-------+
505 // Plugins (i) receive state information, (ii) perform actions
508 // ND : No data ?G : Get query
509 // PG : Pending Get !G : Get reply
510 // D : Data present ~G : Get partial reply
511 // PC : Pending changes GE : Get error
512 // PU : Pending update C : Change request
513 // PCA: Pending change with annotation R : Reset request
514 // AD : Annotated data ?U : Update query
516 // ~U : Update partial reply
518 // DA : Delete annotation request
520 // - D -> PU directly if the user chooses 'dynamic update'
521 // - Be careful for updates if partial Get success
523 // ND: No data == initialization state
526 // - on_query_in_progress
527 // NOTE: cannot distinguish get and update here. Is it important ?
529 on_query_in_progress: function()
531 messages.debug("queryupdater.on_query_in_progress");
536 // - on_clear_records (Get)
537 // - on_new_record (shared with AD) XXX
539 // NOTE: cannot distinguish get and update here. Is it important ?
540 // NOTE: Would record key be sufficient for update ?
542 on_clear_records: function()
547 on_query_done: function()
549 console.log("on_query_done");
553 // PC : Pending changes
554 // NOTE: record_key could be sufficient
555 on_added_record: function(record)
557 console.log("on_added_record = ",record);
558 this.set_record_state(record, RECORD_STATE_ADDED);
559 // update pending number
562 on_removed_record: function(record_key)
564 console.log("on_removed_record = ",record_key);
565 this.set_record_state(RECORD_STATE_REMOVED);
568 // PU : Pending update
569 // - on_query_in_progress (already done)
571 // PCA : Pending change with annotation
572 // NOTE: Manifold will inform the plugin about updates, and thus won't
573 // call new record, even if the same channel UUID is used...
574 // - TODO on_updated_record
575 // - Key and confirmation could be sufficient, or key and record state
576 // XXX move record state to the manifold plugin API
578 on_field_state_changed: function(result)
580 console.log("on_field_state_changed");
582 if(result.request == FIELD_REQUEST_ADD){
583 this.selected_resources.push(result.value);
584 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
585 var i = this.selected_resources.indexOf(result.value);
587 this.selected_resources.splice(i,1);
590 console.log("Resources: " + self.selected_resources);
591 messages.debug(result)
592 /* this.set_state(result.request, result.key, result.value, result.status); */
593 this.set_state(result);
596 // XXX we will have the requests for change
597 // XXX + the requests to move into the query cycle = the buttons aforementioned
599 // XXX what happens in case of updates ? not implemented yet
600 // XXX we want resources and leases
601 // listen for SET_ADD and SET_REMOVE for slice query
603 /************************** PRIVATE METHODS ***************************/
605 _close_click: function(e)
609 //jQuery.publish('selected', 'add/'+key_value);
610 // this.parentNode is <td> this.parentNode.parentNode is <tr>
611 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
612 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
613 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
614 var remove_urn = this.id;
615 var current_resources = event.data.instance.current_resources;
616 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
617 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
618 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
621 /******************************** TODO ********************************/
623 update_resources: function(resources, change)
625 messages.debug("update_resources");
626 var my_oTable = this.table.dataTable();
627 var prev_resources = this.current_resources;
630 * The first time the query is advertised, don't do anything. The
631 * component will learn nodes in the slice through the manifest
632 * received through the other subscription
637 var initial = this.initial;
638 //var r_removed = []; //
639 /*-----------------------------------------------------------------------
640 TODO: remove this dirty hack !!!
642 resources = jQuery.map(resources, function(x){
643 if(!('timeslot' in x)){x.timeslot=0;}
647 TODO: handle generic keys instead of specific stuff
651 var initial_urn = $.map(initial, function(x){return x.urn;});
652 var resources_urn = $.map(resources, function(x){return x.urn;});
653 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
654 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
655 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
656 exists = false; // ioi
657 /*-----------------------------------------------------------------------*/
659 my_oTable.fnClearTable();
661 TODO: factorization of this code !!!
663 $.each(r_added, function(i, r) {
664 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
666 // Create the resource objects
671 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
672 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
675 newline.push(type, urn, slot, SPAN); // ioi
676 var line = my_oTable.fnAddData(newline);
677 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
678 nTr.className = type;
680 $.each(r_attached, function(i, r) {
681 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
682 var type = 'attached';
683 // Create the resource objects
688 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
689 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
692 newline.push(type, node, slot, SPAN); // ioi
693 var line = my_oTable.fnAddData(newline);
694 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
695 nTr.className = type;
697 $.each(r_removed, function(i, r) {
698 // The list contains objects
701 var time = r.timeslot;
703 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
704 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
707 newline.push('remove', node, slot, SPAN); // ioi
708 var line = my_oTable.fnAddData(newline);
709 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
710 nTr.className = 'remove';
713 this.current_resources = $.merge(r_attached,r_added);
715 /* Allow the user to update the slice */
716 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
718 }, // update_resources
722 $.plugin('QueryUpdater', QueryUpdater);