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 // XXX check that the query is not disabled
74 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
77 // related buttons are also disabled in the html template
78 do_refresh: function(e)
80 throw 'resource_selected.do_refresh Not implemented';
85 throw 'queryupdater.do_reset Not implemented';
88 do_clear_annotations: function(e)
90 throw 'queryupdater.do_clear_annotations Not implemented';
93 /************************** GUI MANIPULATION **************************/
96 set_button_state: function(name, state)
98 this.elmt(name).attr('disabled', state ? false : 'disabled');
106 find_row: function(key)
108 // key in third position, column id = 2
111 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
112 return (col._aData[KEY_POS] == key);
115 if (cols.length == 0)
118 throw "Too many same-key rows in ResourceSelected plugin";
123 set_state: function(data)
131 // make sure the change is visible : toggle on the whole plugin
132 // this might have to be made an 'auto-toggle' option of this plugin..
133 // also it might be needed to be a little finer-grained here
135 // XXX we don't want to show automaticaly the pending when a checkbox is checked
138 switch(data.request) {
139 case FIELD_REQUEST_ADD_RESET:
140 case FIELD_REQUEST_REMOVE_RESET:
141 // find line and delete it
142 row = this.find_row(data.value);
144 this.table.fnDeleteRow(row.nTr);
146 case FIELD_REQUEST_CHANGE:
149 case FIELD_REQUEST_ADD:
152 case FIELD_REQUEST_REMOVE:
157 switch(data.status) {
158 case FIELD_REQUEST_PENDING:
160 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
162 case FIELD_REQUEST_SUCCESS:
165 case FIELD_REQUEST_FAILURE:
170 var status = msg + status;
173 // if no, create it, else replace it
174 // XXX it's not just about adding lines, but sometimes removing some
175 // XXX how do we handle status reset ?
176 row = this.find_row(data.value);
185 // XXX second parameter refresh = false can improve performance. todo in querytable also
186 this.table.fnAddData(newline);
187 row = this.find_row(data.value);
189 // Update row text...
190 this.table.fnUpdate(newline, row.nTr);
193 // Change cell color according to status
195 $(row.nTr).removeClass('add remove')
196 var cls = action.toLowerCase();
198 $(row.nTr).addClass(cls);
202 /*************************** QUERY HANDLER ****************************/
206 /*************************** RECORD HANDLER ***************************/
208 on_new_record: function(record)
210 // if (not and update) {
212 // initial['resource'], initial['lease'] ?
213 this.initial.push(record.urn);
215 // We simply add to the table
217 // \ this.initial_resources
220 // current_resources \ YES | NO
221 // --------------------+----------+---------
222 // YES | attached | added
230 // +-----------------+--------------+
232 // +-------+ ?G +-------+ +-------+ +---+---+ |
233 // | | -----> | | !G | | | | DA |
234 // | ND | | PG | -----> | D | -----> | PC | <------+ |
235 // | | <----- | | ~G | | C | | | |
236 // +-------+ GE +-------+ +-------+ +-------+ +------+
238 // | DA UE | | ?U | PCA |
240 // +-------+ +-------+ +------+
242 // | AD | <----- | PU | --------+
244 // +-------+ +-------+
249 // Plugins (i) receive state information, (ii) perform actions
252 // ND : No data ?G : Get query
253 // PG : Pending Get !G : Get reply
254 // D : Data present ~G : Get partial reply
255 // PC : Pending changes GE : Get error
256 // PU : Pending update C : Change request
257 // PCA: Pending change with annotation R : Reset request
258 // AD : Annotated data ?U : Update query
260 // ~U : Update partial reply
262 // DA : Delete annotation request
264 // - D -> PU directly if the user chooses 'dynamic update'
265 // - Be careful for updates if partial Get success
267 // ND: No data == initialization state
270 // - on_query_in_progress
271 // NOTE: cannot distinguish get and update here. Is it important ?
273 on_query_in_progress: function()
275 messages.debug("queryupdater.on_query_in_progress");
280 // - on_clear_records (Get)
281 // - on_new_record (shared with AD) XXX
283 // NOTE: cannot distinguish get and update here. Is it important ?
284 // NOTE: Would record key be sufficient for update ?
286 on_clear_records: function()
291 on_new_record: function(record)
295 on_query_done: function()
300 // PC : Pending changes
301 // NOTE: record_key could be sufficient
302 on_added_record: function(record)
304 this.set_record_state(record, RECORD_STATE_ADDED);
307 on_removed_record: function(record_key)
309 this.set_record_state(RECORD_STATE_REMOVED);
312 // PU : Pending update
313 // - on_query_in_progress (already done)
315 // PCA : Pending change with annotation
316 // NOTE: Manifold will inform the plugin about updates, and thus won't
317 // call new record, even if the same channel UUID is used...
318 // - TODO on_updated_record
319 // - Key and confirmation could be sufficient, or key and record state
320 // XXX move record state to the manifold plugin API
322 on_field_state_changed: function(result)
324 messages.debug(result)
325 /* this.set_state(result.request, result.key, result.value, result.status); */
326 this.set_state(result);
329 // XXX we will have the requests for change
330 // XXX + the requests to move into the query cycle = the buttons aforementioned
332 // XXX what happens in case of updates ? not implemented yet
333 // XXX we want resources and leases
334 // listen for SET_ADD and SET_REMOVE for slice query
336 /************************** PRIVATE METHODS ***************************/
338 _close_click: function(e)
342 //jQuery.publish('selected', 'add/'+key_value);
343 // this.parentNode is <td> this.parentNode.parentNode is <tr>
344 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
345 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
346 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
347 var remove_urn = this.id;
348 var current_resources = event.data.instance.current_resources;
349 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
350 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
351 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
354 /******************************** TODO ********************************/
356 update_resources: function(resources, change)
358 messages.debug("update_resources");
359 var my_oTable = this.table.dataTable();
360 var prev_resources = this.current_resources;
363 * The first time the query is advertised, don't do anything. The
364 * component will learn nodes in the slice through the manifest
365 * received through the other subscription
370 var initial = this.initial_resources;
371 //var r_removed = []; //
372 /*-----------------------------------------------------------------------
373 TODO: remove this dirty hack !!!
375 resources = jQuery.map(resources, function(x){
376 if(!('timeslot' in x)){x.timeslot=0;}
380 TODO: handle generic keys instead of specific stuff
384 var initial_urn = $.map(initial, function(x){return x.urn;});
385 var resources_urn = $.map(resources, function(x){return x.urn;});
386 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
387 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
388 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
389 exists = false; // ioi
390 /*-----------------------------------------------------------------------*/
392 my_oTable.fnClearTable();
394 TODO: factorization of this code !!!
396 $.each(r_added, function(i, r) {
397 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
399 // Create the resource objects
404 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
405 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
408 newline.push(type, urn, slot, SPAN); // ioi
409 var line = my_oTable.fnAddData(newline);
410 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
411 nTr.className = type;
413 $.each(r_attached, function(i, r) {
414 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
415 var type = 'attached';
416 // Create the resource objects
421 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
422 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
425 newline.push(type, node, slot, SPAN); // ioi
426 var line = my_oTable.fnAddData(newline);
427 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
428 nTr.className = type;
430 $.each(r_removed, function(i, r) {
431 // The list contains objects
434 var time = r.timeslot;
436 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
437 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
440 newline.push('remove', node, slot, SPAN); // ioi
441 var line = my_oTable.fnAddData(newline);
442 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
443 nTr.className = 'remove';
446 this.current_resources = $.merge(r_attached,r_added);
448 /* Allow the user to update the slice */
449 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
451 }, // update_resources
455 $.plugin('QueryUpdater', QueryUpdater);