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 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 // XXX check that the query is not disabled
168 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
172 $("#accept_sla_wilab2").click(function(){
173 console.log("SLA ACCEPTED");
174 console.log("With username: " + username);
176 // var promt = $('#modal-body');
177 // var notchecked = true;
178 // for (var i=0;i<50;i++)
180 // var wielement = $('#wi'+i);
181 // var wallElement = $('#wall'+i);
182 // if(wielement != null && !wielement.checked)
184 // notchecked = false;
186 // if(wallElement!= null && !wallElement.checked)
188 // notchecked = false;
195 $.post("/sla/agreements/simplecreate",
196 { "template_id": "iMindsServiceWiLab2",
198 "expiration_time": new Date().toISOString()
200 $.post("/sla/agreements/simplecreate",
201 { "template_id": "iMindsServiceVirtualwall",
203 "expiration_time": new Date().toISOString()
206 $('#slamodal-wilab2').modal('hide');
207 accepted_sla["wilab2"] = true;
209 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
211 $('#modal-body').empty();
214 $("#dismiss_sla_wilab2").click(function(){
215 console.log("SLA NOT ACCEPTED");
216 $('#slamodal-wilab2').modal('hide');
217 $('#modal-body').empty();
221 // this.do_update(e);
224 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
225 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
226 // promt.append(list);
227 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
231 // accepted_sla.push({"wall2":false});
233 // $('#sla_dialog').show();
234 // $('#slamodal-virtualwall').modal('show');
237 // $("#accept_sla_vwall").click(function(){
238 // console.log("SLA ACCEPTED");
239 // console.log("With username: " + username);
241 // $.post("/sla/agreements/simplecreate",
242 // { "template_id": "iMindsServiceVirtualwall",
244 // "expiration_time": new Date()
247 // $('#slamodal-virtualwall').modal('hide');
248 // accepted_sla["wall2"] = true;
251 // $("#dismiss_sla_vwall").click(function(){
252 // console.log("SLA NOT ACCEPTED");
253 // $('#slamodal-vir').modal('hide');
258 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
260 // accepted_sla.push({"wilab2":false});
262 // $('#sla_dialog').show();
263 // $('#slamodal-wilab2').modal('show');
266 // $("#accept_sla_wilab2").click(function(){
267 // console.log("SLA ACCEPTED");
268 // console.log("With username: " + username);
270 // $.post("/sla/agreements/simplecreate",
271 // { "template_id": "iMindsServiceWiLab2",
273 // "expiration_time": new Date()
276 // $('#slamodal-wilab2').modal('hide');
277 // accepted_sla["wilab2"] = true;
280 // $("#dismiss_sla_wilab2").click(function(){
281 // console.log("SLA NOT ACCEPTED");
282 // $('#slamodal-wilab2').modal('hide');
290 // for(var sla in accepted_sla){
291 // if(accepted_sla[sla] == true){
296 // if(count == accepted_sla.length){
297 // this.do_update(e);
302 do_update: function(e) {
305 var username = e.data.options.username;
306 var urn = data.value;
307 // XXX check that the query is not disabled
310 console.log("do_update");
311 // XXX check that the query is not disabled
312 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
314 // how to stop the spinning after the event?
315 // this should be triggered by some on_updatequery_done ?
319 // related buttons are also disabled in the html template
320 do_refresh: function(e)
322 throw 'resource_selected.do_refresh Not implemented';
325 do_reset: function(e)
327 throw 'queryupdater.do_reset Not implemented';
330 do_clear_annotations: function(e)
332 throw 'queryupdater.do_clear_annotations Not implemented';
335 /************************** GUI MANIPULATION **************************/
338 set_button_state: function(name, state)
340 this.elmt(name).attr('disabled', state ? false : 'disabled');
348 find_row: function(key)
350 // key in third position, column id = 2
353 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
354 return (col._aData[KEY_POS] == key);
357 if (cols.length == 0)
360 throw "Too many same-key rows in ResourceSelected plugin";
365 set_state: function(data)
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.status) {
381 case STATE_SET_IN_PENDING:
384 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
386 case STATE_SET_OUT_PENDING:
389 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
393 // find line and delete it
394 // XXX Naming is incorrect for badge-pending !!!!
395 // XXX What is this badge ?
396 row = this.find_row(data.value);
398 this.table.fnDeleteRow(row.nTr);
399 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
400 $("#badge-pending").text($("#badge-pending").data('number'));
403 case STATE_SET_IN_SUCCESS:
404 case STATE_SET_OUT_SUCCESS:
407 case STATE_SET_IN_FAILURE:
408 case STATE_SET_OUT_FAILURE:
417 var status = msg + status;
420 // if no, create it, else replace it
421 // XXX it's not just about adding lines, but sometimes removing some
422 // XXX how do we handle status reset ?
424 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
425 if (typeof(data.value) !== "string")
426 data.value = JSON.stringify(data.value);
427 data.selected_resources = this.selected_resources;
428 row = this.find_row(data.value);
437 // XXX second parameter refresh = false can improve performance. todo in querytable also
438 this.table.fnAddData(newline);
439 row = this.find_row(data.value);
440 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
441 $("#badge-pending").text($("#badge-pending").data('number'));
443 // Update row text...
444 this.table.fnUpdate(newline, row.nTr);
447 // Change cell color according to status
449 $(row.nTr).removeClass('add remove')
450 var cls = action.toLowerCase();
452 $(row.nTr).addClass(cls);
456 /*************************** QUERY HANDLER ****************************/
460 /*************************** RECORD HANDLER ***************************/
462 on_new_record: function(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()
552 // PC : Pending changes
553 // NOTE: record_key could be sufficient
554 on_added_record: function(record)
556 this.set_record_state(record, RECORD_STATE_ADDED);
557 // update pending number
560 on_removed_record: function(record_key)
562 this.set_record_state(RECORD_STATE_REMOVED);
565 // PU : Pending update
566 // - on_query_in_progress (already done)
568 // PCA : Pending change with annotation
569 // NOTE: Manifold will inform the plugin about updates, and thus won't
570 // call new record, even if the same channel UUID is used...
571 // - TODO on_updated_record
572 // - Key and confirmation could be sufficient, or key and record state
573 // XXX move record state to the manifold plugin API
575 on_field_state_changed: function(result)
577 if(result.request == FIELD_REQUEST_ADD){
578 this.selected_resources.push(result.value);
579 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
580 var i = this.selected_resources.indexOf(result.value);
582 this.selected_resources.splice(i,1);
585 messages.debug(result)
587 this.set_state(result);
590 // XXX we will have the requests for change
591 // XXX + the requests to move into the query cycle = the buttons aforementioned
593 // XXX what happens in case of updates ? not implemented yet
594 // XXX we want resources and leases
595 // listen for SET_ADD and SET_REMOVE for slice query
597 /************************** PRIVATE METHODS ***************************/
599 _close_click: function(e)
603 //jQuery.publish('selected', 'add/'+key_value);
604 // this.parentNode is <td> this.parentNode.parentNode is <tr>
605 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
606 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
607 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
608 var remove_urn = this.id;
609 var current_resources = event.data.instance.current_resources;
610 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
611 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
612 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
615 /******************************** TODO ********************************/
617 update_resources: function(resources, change)
619 messages.debug("update_resources");
620 var my_oTable = this.table.dataTable();
621 var prev_resources = this.current_resources;
624 * The first time the query is advertised, don't do anything. The
625 * component will learn nodes in the slice through the manifest
626 * received through the other subscription
631 var initial = this.initial;
632 //var r_removed = []; //
633 /*-----------------------------------------------------------------------
634 TODO: remove this dirty hack !!!
636 resources = jQuery.map(resources, function(x){
637 if(!('timeslot' in x)){x.timeslot=0;}
641 TODO: handle generic keys instead of specific stuff
645 var initial_urn = $.map(initial, function(x){return x.urn;});
646 var resources_urn = $.map(resources, function(x){return x.urn;});
647 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
648 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
649 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
650 exists = false; // ioi
651 /*-----------------------------------------------------------------------*/
653 my_oTable.fnClearTable();
655 TODO: factorization of this code !!!
657 $.each(r_added, function(i, r) {
658 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
660 // Create the resource objects
665 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
666 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
669 newline.push(type, urn, slot, SPAN); // ioi
670 var line = my_oTable.fnAddData(newline);
671 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
672 nTr.className = type;
674 $.each(r_attached, function(i, r) {
675 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
676 var type = 'attached';
677 // Create the resource objects
682 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
683 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
686 newline.push(type, node, slot, SPAN); // ioi
687 var line = my_oTable.fnAddData(newline);
688 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
689 nTr.className = type;
691 $.each(r_removed, function(i, r) {
692 // The list contains objects
695 var time = r.timeslot;
697 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
698 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
701 newline.push('remove', node, slot, SPAN); // ioi
702 var line = my_oTable.fnAddData(newline);
703 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
704 nTr.className = 'remove';
707 this.current_resources = $.merge(r_attached,r_added);
709 /* Allow the user to update the slice */
710 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
712 }, // update_resources
716 $.plugin('QueryUpdaterPlugin', QueryUpdaterPlugin);