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);
36 this.table = this.elmt('table').dataTable({
37 // the original querytable layout was
38 // sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
39 // however the bottom line with 'showing blabla...' and the navigation widget are not really helpful
40 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
41 // so this does not matter anymore now that the pagination area is turned off
42 // sPaginationType: 'bootstrap',
46 // sScrollX : '100%', // Horizontal scrolling
47 // bSortClasses : false, // Disable style for the sorted column
48 // aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
49 // fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
50 // /* Prevent to loop on click while redrawing table */
51 // $('.ResourceSelectedClose').unbind('click');
52 // /* Handle clicks on close span */
53 // /* Reassociate close click every time the table is redrawn */
54 // $('.ResourceSelectedClose').bind('click', self, self._close_click);
58 // XXX This should not be done at init...
59 this.elmt('update').click(this, this.do_update);
60 this.elmt('refresh').click(this, this.do_refresh);
61 this.elmt('reset').click(this, this.do_reset);
62 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
64 this.listen_query(options.query_uuid);
67 /*************************** PLUGIN EVENTS ****************************/
69 /***************************** GUI EVENTS *****************************/
71 do_update: function(e) {
73 var username = e.data.options.username;
75 // XXX check that the query is not disabled
77 console.log("DATA VALUE: " + data.value);
79 if (data.value.toLowerCase().indexOf("iminds") >= 0){
81 $('#sla_dialog').show();
82 $('#slamodal').modal('show');
84 $(document).ready(function() {
85 $("#accept_sla").click(function(){
86 console.log("SLA ACCEPTED");
87 console.log("With username: " + username);
88 if(urn.toLowerCase().indexOf("wall2") >= 0){
89 $.post("/sla/agreements/simplecreate",
90 { "template_id": "iMindsServiceVirtualwall",
92 "expiration_time": new Date() // jgarcia: FIXME
94 } else if(urn.toLowerCase().indexOf("wilab2") >= 0){
95 $.post("/sla/agreements/simplecreate",
96 { "template_id":"iMindsServiceWiLab2",
98 "expiration_time": new Date() // jgarcia: FIXME
101 $('#slamodal').modal('hide');
102 console.log("Executing raise_event after sending SLA");
103 // manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
107 $(document).ready(function() {
108 $("#dismiss_sla").click(function(){
109 console.log("SLA NOT ACCEPTED");
110 $('#slamodal').modal('hide');
115 console.log("Executing raise_event");
116 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
120 // related buttons are also disabled in the html template
121 do_refresh: function(e)
123 throw 'resource_selected.do_refresh Not implemented';
126 do_reset: function(e)
128 throw 'queryupdater.do_reset Not implemented';
131 do_clear_annotations: function(e)
133 throw 'queryupdater.do_clear_annotations Not implemented';
136 /************************** GUI MANIPULATION **************************/
139 set_button_state: function(name, state)
141 this.elmt(name).attr('disabled', state ? false : 'disabled');
149 find_row: function(key)
151 // key in third position, column id = 2
154 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
155 return (col._aData[KEY_POS] == key);
158 if (cols.length == 0)
161 throw "Too many same-key rows in ResourceSelected plugin";
166 set_state: function(data)
174 // make sure the change is visible : toggle on the whole plugin
175 // this might have to be made an 'auto-toggle' option of this plugin..
176 // also it might be needed to be a little finer-grained here
178 // XXX we don't want to show automaticaly the pending when a checkbox is checked
181 switch(data.request) {
182 case FIELD_REQUEST_ADD_RESET:
183 case FIELD_REQUEST_REMOVE_RESET:
184 // find line and delete it
185 row = this.find_row(data.value);
187 this.table.fnDeleteRow(row.nTr);
188 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
189 $("#badge-pending").text($("#badge-pending").data('number'));
191 case FIELD_REQUEST_CHANGE:
194 case FIELD_REQUEST_ADD:
197 case FIELD_REQUEST_REMOVE:
202 switch(data.status) {
203 case FIELD_REQUEST_PENDING:
205 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
207 case FIELD_REQUEST_SUCCESS:
210 case FIELD_REQUEST_FAILURE:
215 var status = msg + status;
220 // if no, create it, else replace it
221 // XXX it's not just about adding lines, but sometimes removing some
222 // XXX how do we handle status reset ?
223 data.value = JSON.stringify(data.value);
224 row = this.find_row(data.value);
233 // XXX second parameter refresh = false can improve performance. todo in querytable also
234 this.table.fnAddData(newline);
235 row = this.find_row(data.value);
236 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
237 $("#badge-pending").text($("#badge-pending").data('number'));
239 // Update row text...
240 this.table.fnUpdate(newline, row.nTr);
243 // Change cell color according to status
245 $(row.nTr).removeClass('add remove')
246 var cls = action.toLowerCase();
248 $(row.nTr).addClass(cls);
252 /*************************** QUERY HANDLER ****************************/
256 /*************************** RECORD HANDLER ***************************/
258 on_new_record: function(record)
260 // if (not and update) {
262 // initial['resource'], initial['lease'] ?
263 this.initial.push(record.urn);
265 // We simply add to the table
267 // \ this.initial_resources
270 // current_resources \ YES | NO
271 // --------------------+----------+---------
272 // YES | attached | added
280 // +-----------------+--------------+
282 // +-------+ ?G +-------+ +-------+ +---+---+ |
283 // | | -----> | | !G | | | | DA |
284 // | ND | | PG | -----> | D | -----> | PC | <------+ |
285 // | | <----- | | ~G | | C | | | |
286 // +-------+ GE +-------+ +-------+ +-------+ +------+
288 // | DA UE | | ?U | PCA |
290 // +-------+ +-------+ +------+
292 // | AD | <----- | PU | --------+
294 // +-------+ +-------+
299 // Plugins (i) receive state information, (ii) perform actions
302 // ND : No data ?G : Get query
303 // PG : Pending Get !G : Get reply
304 // D : Data present ~G : Get partial reply
305 // PC : Pending changes GE : Get error
306 // PU : Pending update C : Change request
307 // PCA: Pending change with annotation R : Reset request
308 // AD : Annotated data ?U : Update query
310 // ~U : Update partial reply
312 // DA : Delete annotation request
314 // - D -> PU directly if the user chooses 'dynamic update'
315 // - Be careful for updates if partial Get success
317 // ND: No data == initialization state
320 // - on_query_in_progress
321 // NOTE: cannot distinguish get and update here. Is it important ?
323 on_query_in_progress: function()
325 messages.debug("queryupdater.on_query_in_progress");
330 // - on_clear_records (Get)
331 // - on_new_record (shared with AD) XXX
333 // NOTE: cannot distinguish get and update here. Is it important ?
334 // NOTE: Would record key be sufficient for update ?
336 on_clear_records: function()
341 on_new_record: function(record)
345 on_query_done: function()
350 // PC : Pending changes
351 // NOTE: record_key could be sufficient
352 on_added_record: function(record)
354 this.set_record_state(record, RECORD_STATE_ADDED);
355 // update pending number
358 on_removed_record: function(record_key)
360 this.set_record_state(RECORD_STATE_REMOVED);
363 // PU : Pending update
364 // - on_query_in_progress (already done)
366 // PCA : Pending change with annotation
367 // NOTE: Manifold will inform the plugin about updates, and thus won't
368 // call new record, even if the same channel UUID is used...
369 // - TODO on_updated_record
370 // - Key and confirmation could be sufficient, or key and record state
371 // XXX move record state to the manifold plugin API
373 on_field_state_changed: function(result)
375 messages.debug(result)
376 /* this.set_state(result.request, result.key, result.value, result.status); */
377 this.set_state(result);
380 // XXX we will have the requests for change
381 // XXX + the requests to move into the query cycle = the buttons aforementioned
383 // XXX what happens in case of updates ? not implemented yet
384 // XXX we want resources and leases
385 // listen for SET_ADD and SET_REMOVE for slice query
387 /************************** PRIVATE METHODS ***************************/
389 _close_click: function(e)
393 //jQuery.publish('selected', 'add/'+key_value);
394 // this.parentNode is <td> this.parentNode.parentNode is <tr>
395 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
396 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
397 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
398 var remove_urn = this.id;
399 var current_resources = event.data.instance.current_resources;
400 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
401 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
402 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
405 /******************************** TODO ********************************/
407 update_resources: function(resources, change)
409 messages.debug("update_resources");
410 var my_oTable = this.table.dataTable();
411 var prev_resources = this.current_resources;
414 * The first time the query is advertised, don't do anything. The
415 * component will learn nodes in the slice through the manifest
416 * received through the other subscription
421 var initial = this.initial_resources;
422 //var r_removed = []; //
423 /*-----------------------------------------------------------------------
424 TODO: remove this dirty hack !!!
426 resources = jQuery.map(resources, function(x){
427 if(!('timeslot' in x)){x.timeslot=0;}
431 TODO: handle generic keys instead of specific stuff
435 var initial_urn = $.map(initial, function(x){return x.urn;});
436 var resources_urn = $.map(resources, function(x){return x.urn;});
437 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
438 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
439 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
440 exists = false; // ioi
441 /*-----------------------------------------------------------------------*/
443 my_oTable.fnClearTable();
445 TODO: factorization of this code !!!
447 $.each(r_added, function(i, r) {
448 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
450 // Create the resource objects
455 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
456 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
459 newline.push(type, urn, slot, SPAN); // ioi
460 var line = my_oTable.fnAddData(newline);
461 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
462 nTr.className = type;
464 $.each(r_attached, function(i, r) {
465 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
466 var type = 'attached';
467 // Create the resource objects
472 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
473 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
476 newline.push(type, node, slot, SPAN); // ioi
477 var line = my_oTable.fnAddData(newline);
478 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
479 nTr.className = type;
481 $.each(r_removed, function(i, r) {
482 // The list contains objects
485 var time = r.timeslot;
487 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
488 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
491 newline.push('remove', node, slot, SPAN); // ioi
492 var line = my_oTable.fnAddData(newline);
493 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
494 nTr.className = 'remove';
497 this.current_resources = $.merge(r_attached,r_added);
499 /* Allow the user to update the slice */
500 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
502 }, // update_resources
506 $.plugin('QueryUpdater', QueryUpdater);