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);
214 $("#dismiss_sla_wilab2").click(function(){
215 console.log("SLA NOT ACCEPTED");
216 $('#slamodal-wilab2').modal('hide');
220 // this.do_update(e);
223 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
224 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
225 // promt.append(list);
226 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
230 // accepted_sla.push({"wall2":false});
232 // $('#sla_dialog').show();
233 // $('#slamodal-virtualwall').modal('show');
236 // $("#accept_sla_vwall").click(function(){
237 // console.log("SLA ACCEPTED");
238 // console.log("With username: " + username);
240 // $.post("/sla/agreements/simplecreate",
241 // { "template_id": "iMindsServiceVirtualwall",
243 // "expiration_time": new Date()
246 // $('#slamodal-virtualwall').modal('hide');
247 // accepted_sla["wall2"] = true;
250 // $("#dismiss_sla_vwall").click(function(){
251 // console.log("SLA NOT ACCEPTED");
252 // $('#slamodal-vir').modal('hide');
257 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
259 // accepted_sla.push({"wilab2":false});
261 // $('#sla_dialog').show();
262 // $('#slamodal-wilab2').modal('show');
265 // $("#accept_sla_wilab2").click(function(){
266 // console.log("SLA ACCEPTED");
267 // console.log("With username: " + username);
269 // $.post("/sla/agreements/simplecreate",
270 // { "template_id": "iMindsServiceWiLab2",
272 // "expiration_time": new Date()
275 // $('#slamodal-wilab2').modal('hide');
276 // accepted_sla["wilab2"] = true;
279 // $("#dismiss_sla_wilab2").click(function(){
280 // console.log("SLA NOT ACCEPTED");
281 // $('#slamodal-wilab2').modal('hide');
289 // for(var sla in accepted_sla){
290 // if(accepted_sla[sla] == true){
295 // if(count == accepted_sla.length){
296 // this.do_update(e);
301 do_update: function(e) {
304 var username = e.data.options.username;
305 var urn = data.value;
306 // XXX check that the query is not disabled
309 console.log("do_update");
310 // XXX check that the query is not disabled
311 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
313 // how to stop the spinning after the event?
314 // this should be triggered by some on_updatequery_done ?
318 // related buttons are also disabled in the html template
319 do_refresh: function(e)
321 throw 'resource_selected.do_refresh Not implemented';
324 do_reset: function(e)
326 throw 'queryupdater.do_reset Not implemented';
329 do_clear_annotations: function(e)
331 throw 'queryupdater.do_clear_annotations Not implemented';
334 /************************** GUI MANIPULATION **************************/
337 set_button_state: function(name, state)
339 this.elmt(name).attr('disabled', state ? false : 'disabled');
347 find_row: function(key)
349 // key in third position, column id = 2
352 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
353 return (col._aData[KEY_POS] == key);
356 if (cols.length == 0)
359 throw "Too many same-key rows in ResourceSelected plugin";
364 set_state: function(data)
366 console.log("function set_state");
373 // make sure the change is visible : toggle on the whole plugin
374 // this might have to be made an 'auto-toggle' option of this plugin..
375 // also it might be needed to be a little finer-grained here
377 // XXX we don't want to show automaticaly the pending when a checkbox is checked
380 switch(data.request) {
381 case FIELD_REQUEST_ADD_RESET:
382 case FIELD_REQUEST_REMOVE_RESET:
383 // find line and delete it
384 row = this.find_row(data.value);
386 this.table.fnDeleteRow(row.nTr);
387 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
388 $("#badge-pending").text($("#badge-pending").data('number'));
390 case FIELD_REQUEST_CHANGE:
393 case FIELD_REQUEST_ADD:
396 case FIELD_REQUEST_REMOVE:
401 switch(data.status) {
402 case FIELD_REQUEST_PENDING:
404 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
406 case FIELD_REQUEST_SUCCESS:
409 case FIELD_REQUEST_FAILURE:
414 var status = msg + status;
419 // if no, create it, else replace it
420 // XXX it's not just about adding lines, but sometimes removing some
421 // XXX how do we handle status reset ?
422 data.value = JSON.stringify(data.value);
423 data.selected_resources = this.selected_resources;
424 row = this.find_row(data.value);
433 // XXX second parameter refresh = false can improve performance. todo in querytable also
434 this.table.fnAddData(newline);
435 row = this.find_row(data.value);
436 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
437 $("#badge-pending").text($("#badge-pending").data('number'));
439 // Update row text...
440 this.table.fnUpdate(newline, row.nTr);
443 // Change cell color according to status
445 $(row.nTr).removeClass('add remove')
446 var cls = action.toLowerCase();
448 $(row.nTr).addClass(cls);
452 /*************************** QUERY HANDLER ****************************/
456 /*************************** RECORD HANDLER ***************************/
458 on_new_record: function(record)
460 console.log("query_updater on_new_record");
463 // if (not and update) {
465 // initial['resource'], initial['lease'] ?
466 this.initial.push(record);
468 //this.set_record_state(record, RECORD_STATE_ATTACHED);
469 // We simply add to the table
471 // \ this.initial_resources
474 // current_resources \ YES | NO
475 // --------------------+----------+---------
476 // YES | attached | added
484 // +-----------------+--------------+
486 // +-------+ ?G +-------+ +-------+ +---+---+ |
487 // | | -----> | | !G | | | | DA |
488 // | ND | | PG | -----> | D | -----> | PC | <------+ |
489 // | | <----- | | ~G | | C | | | |
490 // +-------+ GE +-------+ +-------+ +-------+ +------+
492 // | DA UE | | ?U | PCA |
494 // +-------+ +-------+ +------+
496 // | AD | <----- | PU | --------+
498 // +-------+ +-------+
503 // Plugins (i) receive state information, (ii) perform actions
506 // ND : No data ?G : Get query
507 // PG : Pending Get !G : Get reply
508 // D : Data present ~G : Get partial reply
509 // PC : Pending changes GE : Get error
510 // PU : Pending update C : Change request
511 // PCA: Pending change with annotation R : Reset request
512 // AD : Annotated data ?U : Update query
514 // ~U : Update partial reply
516 // DA : Delete annotation request
518 // - D -> PU directly if the user chooses 'dynamic update'
519 // - Be careful for updates if partial Get success
521 // ND: No data == initialization state
524 // - on_query_in_progress
525 // NOTE: cannot distinguish get and update here. Is it important ?
527 on_query_in_progress: function()
529 messages.debug("queryupdater.on_query_in_progress");
534 // - on_clear_records (Get)
535 // - on_new_record (shared with AD) XXX
537 // NOTE: cannot distinguish get and update here. Is it important ?
538 // NOTE: Would record key be sufficient for update ?
540 on_clear_records: function()
545 on_query_done: function()
547 console.log("on_query_done");
551 // PC : Pending changes
552 // NOTE: record_key could be sufficient
553 on_added_record: function(record)
555 console.log("on_added_record = ",record);
556 this.set_record_state(record, RECORD_STATE_ADDED);
557 // update pending number
560 on_removed_record: function(record_key)
562 console.log("on_removed_record = ",record_key);
563 this.set_record_state(RECORD_STATE_REMOVED);
566 // PU : Pending update
567 // - on_query_in_progress (already done)
569 // PCA : Pending change with annotation
570 // NOTE: Manifold will inform the plugin about updates, and thus won't
571 // call new record, even if the same channel UUID is used...
572 // - TODO on_updated_record
573 // - Key and confirmation could be sufficient, or key and record state
574 // XXX move record state to the manifold plugin API
576 on_field_state_changed: function(result)
578 console.log("on_field_state_changed");
580 if(result.request == FIELD_REQUEST_ADD){
581 this.selected_resources.push(result.value);
582 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
583 var i = this.selected_resources.indexOf(result.value);
585 this.selected_resources.splice(i,1);
588 console.log("Resources: " + self.selected_resources);
589 messages.debug(result)
590 /* this.set_state(result.request, result.key, result.value, result.status); */
591 this.set_state(result);
594 // XXX we will have the requests for change
595 // XXX + the requests to move into the query cycle = the buttons aforementioned
597 // XXX what happens in case of updates ? not implemented yet
598 // XXX we want resources and leases
599 // listen for SET_ADD and SET_REMOVE for slice query
601 /************************** PRIVATE METHODS ***************************/
603 _close_click: function(e)
607 //jQuery.publish('selected', 'add/'+key_value);
608 // this.parentNode is <td> this.parentNode.parentNode is <tr>
609 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
610 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
611 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
612 var remove_urn = this.id;
613 var current_resources = event.data.instance.current_resources;
614 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
615 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
616 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
619 /******************************** TODO ********************************/
621 update_resources: function(resources, change)
623 messages.debug("update_resources");
624 var my_oTable = this.table.dataTable();
625 var prev_resources = this.current_resources;
628 * The first time the query is advertised, don't do anything. The
629 * component will learn nodes in the slice through the manifest
630 * received through the other subscription
635 var initial = this.initial;
636 //var r_removed = []; //
637 /*-----------------------------------------------------------------------
638 TODO: remove this dirty hack !!!
640 resources = jQuery.map(resources, function(x){
641 if(!('timeslot' in x)){x.timeslot=0;}
645 TODO: handle generic keys instead of specific stuff
649 var initial_urn = $.map(initial, function(x){return x.urn;});
650 var resources_urn = $.map(resources, function(x){return x.urn;});
651 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
652 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
653 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
654 exists = false; // ioi
655 /*-----------------------------------------------------------------------*/
657 my_oTable.fnClearTable();
659 TODO: factorization of this code !!!
661 $.each(r_added, function(i, r) {
662 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
664 // Create the resource objects
669 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
670 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
673 newline.push(type, urn, slot, SPAN); // ioi
674 var line = my_oTable.fnAddData(newline);
675 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
676 nTr.className = type;
678 $.each(r_attached, function(i, r) {
679 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
680 var type = 'attached';
681 // Create the resource objects
686 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
687 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
690 newline.push(type, node, slot, SPAN); // ioi
691 var line = my_oTable.fnAddData(newline);
692 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
693 nTr.className = type;
695 $.each(r_removed, function(i, r) {
696 // The list contains objects
699 var time = r.timeslot;
701 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
702 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
705 newline.push('remove', node, slot, SPAN); // ioi
706 var line = my_oTable.fnAddData(newline);
707 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
708 nTr.className = 'remove';
711 this.current_resources = $.merge(r_attached,r_added);
713 /* Allow the user to update the slice */
714 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
716 }, // update_resources
720 $.plugin('QueryUpdater', QueryUpdater);