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 QueryUpdaterPlugin = Plugin.extend({
31 /**************************************************************************
33 **************************************************************************/
35 init: function(options, element) {
36 this.classname="queryupdater";
37 this._super(options, element);
41 this.initial = Array();
42 this.selected_resources = Array();
44 this.table = this.elmt('table').dataTable({
45 // the original querytable layout was
46 // sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
47 // however the bottom line with 'showing blabla...' and the navigation widget are not really helpful
48 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
49 // so this does not matter anymore now that the pagination area is turned off
50 // sPaginationType: 'bootstrap',
54 // sScrollX : '100%', // Horizontal scrolling
55 // bSortClasses : false, // Disable style for the sorted column
56 // aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
57 // fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
58 // /* Prevent to loop on click while redrawing table */
59 // $('.ResourceSelectedClose').unbind('click');
60 // /* Handle clicks on close span */
61 // /* Reassociate close click every time the table is redrawn */
62 // $('.ResourceSelectedClose').bind('click', self, self._close_click);
66 // XXX This should not be done at init...
67 this.elmt('update').click(this, this.do_checksla);
68 this.elmt('refresh').click(this, this.do_refresh);
69 this.elmt('reset').click(this, this.do_reset);
70 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
72 this.listen_query(options.query_uuid);
75 /*************************** PLUGIN EVENTS ****************************/
77 /***************************** GUI EVENTS *****************************/
79 do_checksla: function(e) {
80 var username = e.data.options.username;
82 var arrayselectedresources = data.selected_resources;
84 var accepted_sla = Array();
87 // XXX check that the query is not disabled
89 console.log("DATA VALUE: " + data.value);
91 //<p>SLA description</p>
92 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>
93 //<p>SLA description</p>
94 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>
95 var promt = $('#modal-body');
101 promt.append('<p>SLA description</p>');
105 for(var iter = 0; iter < arrayselectedresources.length; iter++){
106 var list = '<li class="wi'+iter+'" name=wi"'+iter+'">'+arrayselectedresources[iter].toLowerCase()+'</li>';
108 if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
110 accepted_sla.push({"wilab2":false});
117 wilabForm += "</ul>";
119 //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>';
123 for(var iter = 0; iter < arrayselectedresources.length; iter++){
124 var list = '<li class="wall'+iter+'" name=wall"'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'</li>';
126 if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
128 accepted_sla.push({"wall2":false});
137 var flagDouble = false;
141 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>');
142 promt.append(wilabForm);
143 promt.append('<br />');
147 //promt.append(wallmessage);
149 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>');
150 promt.append(wallForm);
151 promt.append('<br />');
155 // 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>'
157 if(flagWi || flagVW){
158 $('#sla_dialog').show();
160 $('#slamodal-wilab2').modal('show');
166 var username = e.data.options.username;
167 var urn = data.value;
168 // XXX check that the query is not disabled
171 // XXX check that the query is not disabled
172 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
176 $("#accept_sla_wilab2").click(function(){
177 console.log("SLA ACCEPTED");
178 console.log("With username: " + username);
180 // var promt = $('#modal-body');
181 // var notchecked = true;
182 // for (var i=0;i<50;i++)
184 // var wielement = $('#wi'+i);
185 // var wallElement = $('#wall'+i);
186 // if(wielement != null && !wielement.checked)
188 // notchecked = false;
190 // if(wallElement!= null && !wallElement.checked)
192 // notchecked = false;
199 $.post("/sla/agreements/simplecreate",
200 { "template_id": "iMindsServiceWiLab2",
202 "expiration_time": new Date().toISOString()
204 $.post("/sla/agreements/simplecreate",
205 { "template_id": "iMindsServiceVirtualwall",
207 "expiration_time": new Date().toISOString()
210 $('#slamodal-wilab2').modal('hide');
211 accepted_sla["wilab2"] = true;
213 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
215 $('#modal-body').empty();
218 $("#dismiss_sla_wilab2").click(function(){
219 console.log("SLA NOT ACCEPTED");
220 $('#slamodal-wilab2').modal('hide');
221 $('#modal-body').empty();
225 // this.do_update(e);
228 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
229 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
230 // promt.append(list);
231 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
235 // accepted_sla.push({"wall2":false});
237 // $('#sla_dialog').show();
238 // $('#slamodal-virtualwall').modal('show');
241 // $("#accept_sla_vwall").click(function(){
242 // console.log("SLA ACCEPTED");
243 // console.log("With username: " + username);
245 // $.post("/sla/agreements/simplecreate",
246 // { "template_id": "iMindsServiceVirtualwall",
248 // "expiration_time": new Date()
251 // $('#slamodal-virtualwall').modal('hide');
252 // accepted_sla["wall2"] = true;
255 // $("#dismiss_sla_vwall").click(function(){
256 // console.log("SLA NOT ACCEPTED");
257 // $('#slamodal-vir').modal('hide');
262 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
264 // accepted_sla.push({"wilab2":false});
266 // $('#sla_dialog').show();
267 // $('#slamodal-wilab2').modal('show');
270 // $("#accept_sla_wilab2").click(function(){
271 // console.log("SLA ACCEPTED");
272 // console.log("With username: " + username);
274 // $.post("/sla/agreements/simplecreate",
275 // { "template_id": "iMindsServiceWiLab2",
277 // "expiration_time": new Date()
280 // $('#slamodal-wilab2').modal('hide');
281 // accepted_sla["wilab2"] = true;
284 // $("#dismiss_sla_wilab2").click(function(){
285 // console.log("SLA NOT ACCEPTED");
286 // $('#slamodal-wilab2').modal('hide');
294 // for(var sla in accepted_sla){
295 // if(accepted_sla[sla] == true){
300 // if(count == accepted_sla.length){
301 // this.do_update(e);
306 /************************** GUI MANIPULATION **************************/
308 populate_table: function()
310 // Loop over records and display pending ones
311 manifold.query_store.iter_records(this.options.query_uuid, function (record_key, record) {
315 set_button_state: function(name, state)
317 this.elmt(name).attr('disabled', state ? false : 'disabled');
325 find_row: function(key)
327 // key in third position, column id = 2
330 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
331 return (col._aData[KEY_POS] == key);
334 if (cols.length == 0)
337 throw "Too many same-key rows in ResourceSelected plugin";
342 set_state: function(data)
350 // make sure the change is visible : toggle on the whole plugin
351 // this might have to be made an 'auto-toggle' option of this plugin..
352 // also it might be needed to be a little finer-grained here
354 // XXX we don't want to show automaticaly the pending when a checkbox is checked
357 switch (data.status) {
358 case STATE_SET_IN_PENDING:
361 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
363 case STATE_SET_OUT_PENDING:
366 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
370 // find line and delete it
371 // XXX Naming is incorrect for badge-pending !!!!
372 // XXX What is this badge ?
373 row = this.find_row(data.value);
375 this.table.fnDeleteRow(row.nTr);
376 /* indent was wrong !!
377 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
378 $("#badge-pending").text($("#badge-pending").data('number'));
382 case STATE_SET_IN_SUCCESS:
383 case STATE_SET_OUT_SUCCESS:
386 case STATE_SET_IN_FAILURE:
387 case STATE_SET_OUT_FAILURE:
396 var status = msg + status;
399 // if no, create it, else replace it
400 // XXX it's not just about adding lines, but sometimes removing some
401 // XXX how do we handle status reset ?
403 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
404 if (typeof(data.value) !== "string")
405 data.value = JSON.stringify(data.value);
406 data.selected_resources = this.selected_resources;
407 row = this.find_row(data.value);
416 // XXX second parameter refresh = false can improve performance. todo in querytable also
417 this.table.fnAddData(newline);
418 row = this.find_row(data.value);
420 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
421 $("#badge-pending").text($("#badge-pending").data('number'));
424 // Update row text...
425 this.table.fnUpdate(newline, row.nTr);
428 // Change cell color according to status
430 $(row.nTr).removeClass('add remove')
431 var cls = action.toLowerCase();
433 $(row.nTr).addClass(cls);
437 do_update: function(e) {
440 var username = e.data.options.username;
441 var urn = data.value;
442 // XXX check that the query is not disabled
445 console.log("do_update");
446 // XXX check that the query is not disabled
447 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
449 // how to stop the spinning after the event?
450 // this should be triggered by some on_updatequery_done ?
454 // related buttons are also disabled in the html template
455 do_refresh: function(e)
457 throw 'resource_selected.do_refresh Not implemented';
460 do_reset: function(e)
462 throw 'queryupdater.do_reset Not implemented';
465 do_clear_annotations: function(e)
467 throw 'queryupdater.do_clear_annotations Not implemented';
470 /**************************************************************************
472 **************************************************************************/
474 on_new_record: function(record)
477 // if (not and update) {
479 // initial['resource'], initial['lease'] ?
480 this.initial.push(record);
482 //this.set_record_state(record, RECORD_STATE_ATTACHED);
483 // We simply add to the table
485 // \ this.initial_resources
488 // current_resources \ YES | NO
489 // --------------------+----------+---------
490 // YES | attached | added
498 // +-----------------+--------------+
500 // +-------+ ?G +-------+ +-------+ +---+---+ |
501 // | | -----> | | !G | | | | DA |
502 // | ND | | PG | -----> | D | -----> | PC | <------+ |
503 // | | <----- | | ~G | | C | | | |
504 // +-------+ GE +-------+ +-------+ +-------+ +------+
506 // | DA UE | | ?U | PCA |
508 // +-------+ +-------+ +------+
510 // | AD | <----- | PU | --------+
512 // +-------+ +-------+
517 // Plugins (i) receive state information, (ii) perform actions
520 // ND : No data ?G : Get query
521 // PG : Pending Get !G : Get reply
522 // D : Data present ~G : Get partial reply
523 // PC : Pending changes GE : Get error
524 // PU : Pending update C : Change request
525 // PCA: Pending change with annotation R : Reset request
526 // AD : Annotated data ?U : Update query
528 // ~U : Update partial reply
530 // DA : Delete annotation request
532 // - D -> PU directly if the user chooses 'dynamic update'
533 // - Be careful for updates if partial Get success
535 // ND: No data == initialization state
538 // - on_query_in_progress
539 // NOTE: cannot distinguish get and update here. Is it important ?
541 on_query_in_progress: function()
547 // - on_clear_records (Get)
548 // - on_new_record (shared with AD) XXX
550 // NOTE: cannot distinguish get and update here. Is it important ?
551 // NOTE: Would record key be sufficient for update ?
553 on_clear_records: function()
558 on_query_done: function()
563 // PC : Pending changes
564 // NOTE: record_key could be sufficient
565 on_added_record: function(record)
567 this.set_record_state(record, RECORD_STATE_ADDED);
568 // update pending number
571 on_removed_record: function(record_key)
573 this.set_record_state(RECORD_STATE_REMOVED);
576 // PU : Pending update
577 // - on_query_in_progress (already done)
579 // PCA : Pending change with annotation
580 // NOTE: Manifold will inform the plugin about updates, and thus won't
581 // call new record, even if the same channel UUID is used...
582 // - TODO on_updated_record
583 // - Key and confirmation could be sufficient, or key and record state
584 // XXX move record state to the manifold plugin API
586 on_field_state_changed: function(result)
588 if(result.request == FIELD_REQUEST_ADD){
589 this.selected_resources.push(result.value);
590 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
591 var i = this.selected_resources.indexOf(result.value);
593 this.selected_resources.splice(i,1);
596 this.set_state(result);
599 // XXX we will have the requests for change
600 // XXX + the requests to move into the query cycle = the buttons aforementioned
602 // XXX what happens in case of updates ? not implemented yet
603 // XXX we want resources and leases
604 // listen for SET_ADD and SET_REMOVE for slice query
606 /************************** PRIVATE METHODS ***************************/
608 _close_click: function(e)
612 //jQuery.publish('selected', 'add/'+key_value);
613 // this.parentNode is <td> this.parentNode.parentNode is <tr>
614 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
615 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
616 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
617 var remove_urn = this.id;
618 var current_resources = event.data.instance.current_resources;
619 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
620 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
621 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
624 /******************************** TODO ********************************/
626 update_resources: function(resources, change)
628 messages.debug("update_resources");
629 var my_oTable = this.table.dataTable();
630 var prev_resources = this.current_resources;
633 * The first time the query is advertised, don't do anything. The
634 * component will learn nodes in the slice through the manifest
635 * received through the other subscription
640 var initial = this.initial;
641 //var r_removed = []; //
642 /*-----------------------------------------------------------------------
643 TODO: remove this dirty hack !!!
645 resources = jQuery.map(resources, function(x){
646 if(!('timeslot' in x)){x.timeslot=0;}
650 TODO: handle generic keys instead of specific stuff
654 var initial_urn = $.map(initial, function(x){return x.urn;});
655 var resources_urn = $.map(resources, function(x){return x.urn;});
656 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
657 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
658 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
659 exists = false; // ioi
660 /*-----------------------------------------------------------------------*/
662 my_oTable.fnClearTable();
664 TODO: factorization of this code !!!
666 $.each(r_added, function(i, r) {
667 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
669 // Create the resource objects
674 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
675 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
678 newline.push(type, urn, slot, SPAN); // ioi
679 var line = my_oTable.fnAddData(newline);
680 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
681 nTr.className = type;
683 $.each(r_attached, function(i, r) {
684 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
685 var type = 'attached';
686 // Create the resource objects
691 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
692 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
695 newline.push(type, node, slot, SPAN); // ioi
696 var line = my_oTable.fnAddData(newline);
697 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
698 nTr.className = type;
700 $.each(r_removed, function(i, r) {
701 // The list contains objects
704 var time = r.timeslot;
706 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
707 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
710 newline.push('remove', node, slot, SPAN); // ioi
711 var line = my_oTable.fnAddData(newline);
712 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
713 nTr.className = 'remove';
716 this.current_resources = $.merge(r_attached,r_added);
718 /* Allow the user to update the slice */
719 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
721 }, // update_resources
725 $.plugin('QueryUpdaterPlugin', QueryUpdaterPlugin);