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 /* indent was wrong !!
400 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
401 $("#badge-pending").text($("#badge-pending").data('number'));
405 case STATE_SET_IN_SUCCESS:
406 case STATE_SET_OUT_SUCCESS:
409 case STATE_SET_IN_FAILURE:
410 case STATE_SET_OUT_FAILURE:
419 var status = msg + status;
422 // if no, create it, else replace it
423 // XXX it's not just about adding lines, but sometimes removing some
424 // XXX how do we handle status reset ?
426 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
427 if (typeof(data.value) !== "string")
428 data.value = JSON.stringify(data.value);
429 data.selected_resources = this.selected_resources;
430 row = this.find_row(data.value);
439 // XXX second parameter refresh = false can improve performance. todo in querytable also
440 this.table.fnAddData(newline);
441 row = this.find_row(data.value);
443 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
444 $("#badge-pending").text($("#badge-pending").data('number'));
447 // Update row text...
448 this.table.fnUpdate(newline, row.nTr);
451 // Change cell color according to status
453 $(row.nTr).removeClass('add remove')
454 var cls = action.toLowerCase();
456 $(row.nTr).addClass(cls);
460 /*************************** QUERY HANDLER ****************************/
464 /*************************** RECORD HANDLER ***************************/
466 on_new_record: function(record)
469 // if (not and update) {
471 // initial['resource'], initial['lease'] ?
472 this.initial.push(record);
474 //this.set_record_state(record, RECORD_STATE_ATTACHED);
475 // We simply add to the table
477 // \ this.initial_resources
480 // current_resources \ YES | NO
481 // --------------------+----------+---------
482 // YES | attached | added
490 // +-----------------+--------------+
492 // +-------+ ?G +-------+ +-------+ +---+---+ |
493 // | | -----> | | !G | | | | DA |
494 // | ND | | PG | -----> | D | -----> | PC | <------+ |
495 // | | <----- | | ~G | | C | | | |
496 // +-------+ GE +-------+ +-------+ +-------+ +------+
498 // | DA UE | | ?U | PCA |
500 // +-------+ +-------+ +------+
502 // | AD | <----- | PU | --------+
504 // +-------+ +-------+
509 // Plugins (i) receive state information, (ii) perform actions
512 // ND : No data ?G : Get query
513 // PG : Pending Get !G : Get reply
514 // D : Data present ~G : Get partial reply
515 // PC : Pending changes GE : Get error
516 // PU : Pending update C : Change request
517 // PCA: Pending change with annotation R : Reset request
518 // AD : Annotated data ?U : Update query
520 // ~U : Update partial reply
522 // DA : Delete annotation request
524 // - D -> PU directly if the user chooses 'dynamic update'
525 // - Be careful for updates if partial Get success
527 // ND: No data == initialization state
530 // - on_query_in_progress
531 // NOTE: cannot distinguish get and update here. Is it important ?
533 on_query_in_progress: function()
535 messages.debug("queryupdater.on_query_in_progress");
540 // - on_clear_records (Get)
541 // - on_new_record (shared with AD) XXX
543 // NOTE: cannot distinguish get and update here. Is it important ?
544 // NOTE: Would record key be sufficient for update ?
546 on_clear_records: function()
551 on_query_done: function()
556 // PC : Pending changes
557 // NOTE: record_key could be sufficient
558 on_added_record: function(record)
560 this.set_record_state(record, RECORD_STATE_ADDED);
561 // update pending number
564 on_removed_record: function(record_key)
566 this.set_record_state(RECORD_STATE_REMOVED);
569 // PU : Pending update
570 // - on_query_in_progress (already done)
572 // PCA : Pending change with annotation
573 // NOTE: Manifold will inform the plugin about updates, and thus won't
574 // call new record, even if the same channel UUID is used...
575 // - TODO on_updated_record
576 // - Key and confirmation could be sufficient, or key and record state
577 // XXX move record state to the manifold plugin API
579 on_field_state_changed: function(result)
581 if(result.request == FIELD_REQUEST_ADD){
582 this.selected_resources.push(result.value);
583 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
584 var i = this.selected_resources.indexOf(result.value);
586 this.selected_resources.splice(i,1);
589 messages.debug(result)
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('QueryUpdaterPlugin', QueryUpdaterPlugin);