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
136 switch(data.request) {
137 case FIELD_REQUEST_ADD_RESET:
138 case FIELD_REQUEST_REMOVE_RESET:
139 // find line and delete it
140 row = this.find_row(data.value);
142 this.table.fnDeleteRow(row.nTr);
144 case FIELD_REQUEST_CHANGE:
147 case FIELD_REQUEST_ADD:
150 case FIELD_REQUEST_REMOVE:
155 switch(data.status) {
156 case FIELD_REQUEST_PENDING:
158 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
160 case FIELD_REQUEST_SUCCESS:
163 case FIELD_REQUEST_FAILURE:
168 var status = msg + status;
171 // if no, create it, else replace it
172 // XXX it's not just about adding lines, but sometimes removing some
173 // XXX how do we handle status reset ?
174 row = this.find_row(data.value);
183 // XXX second parameter refresh = false can improve performance. todo in querytable also
184 this.table.fnAddData(newline);
185 row = this.find_row(data.value);
187 // Update row text...
188 this.table.fnUpdate(newline, row.nTr);
191 // Change cell color according to status
193 $(row.nTr).removeClass('add remove')
194 var cls = action.toLowerCase();
196 $(row.nTr).addClass(cls);
200 /*************************** QUERY HANDLER ****************************/
204 /*************************** RECORD HANDLER ***************************/
206 on_new_record: function(record)
208 // if (not and update) {
210 // initial['resource'], initial['lease'] ?
211 this.initial.push(record.urn);
213 // We simply add to the table
215 // \ this.initial_resources
218 // current_resources \ YES | NO
219 // --------------------+----------+---------
220 // YES | attached | added
228 // +-----------------+--------------+
230 // +-------+ ?G +-------+ +-------+ +---+---+ |
231 // | | -----> | | !G | | | | DA |
232 // | ND | | PG | -----> | D | -----> | PC | <------+ |
233 // | | <----- | | ~G | | C | | | |
234 // +-------+ GE +-------+ +-------+ +-------+ +------+
236 // | DA UE | | ?U | PCA |
238 // +-------+ +-------+ +------+
240 // | AD | <----- | PU | --------+
242 // +-------+ +-------+
247 // Plugins (i) receive state information, (ii) perform actions
250 // ND : No data ?G : Get query
251 // PG : Pending Get !G : Get reply
252 // D : Data present ~G : Get partial reply
253 // PC : Pending changes GE : Get error
254 // PU : Pending update C : Change request
255 // PCA: Pending change with annotation R : Reset request
256 // AD : Annotated data ?U : Update query
258 // ~U : Update partial reply
260 // DA : Delete annotation request
262 // - D -> PU directly if the user chooses 'dynamic update'
263 // - Be careful for updates if partial Get success
265 // ND: No data == initialization state
268 // - on_query_in_progress
269 // NOTE: cannot distinguish get and update here. Is it important ?
271 on_query_in_progress: function()
273 messages.debug("queryupdater.on_query_in_progress");
278 // - on_clear_records (Get)
279 // - on_new_record (shared with AD) XXX
281 // NOTE: cannot distinguish get and update here. Is it important ?
282 // NOTE: Would record key be sufficient for update ?
284 on_clear_records: function()
289 on_new_record: function(record)
293 on_query_done: function()
298 // PC : Pending changes
299 // NOTE: record_key could be sufficient
300 on_added_record: function(record)
302 this.set_record_state(record, RECORD_STATE_ADDED);
305 on_removed_record: function(record_key)
307 this.set_record_state(RECORD_STATE_REMOVED);
310 // PU : Pending update
311 // - on_query_in_progress (already done)
313 // PCA : Pending change with annotation
314 // NOTE: Manifold will inform the plugin about updates, and thus won't
315 // call new record, even if the same channel UUID is used...
316 // - TODO on_updated_record
317 // - Key and confirmation could be sufficient, or key and record state
318 // XXX move record state to the manifold plugin API
320 on_field_state_changed: function(result)
322 messages.debug(result)
323 /* this.set_state(result.request, result.key, result.value, result.status); */
324 this.set_state(result);
327 // XXX we will have the requests for change
328 // XXX + the requests to move into the query cycle = the buttons aforementioned
330 // XXX what happens in case of updates ? not implemented yet
331 // XXX we want resources and leases
332 // listen for SET_ADD and SET_REMOVE for slice query
334 /************************** PRIVATE METHODS ***************************/
336 _close_click: function(e)
340 //jQuery.publish('selected', 'add/'+key_value);
341 // this.parentNode is <td> this.parentNode.parentNode is <tr>
342 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
343 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
344 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
345 var remove_urn = this.id;
346 var current_resources = event.data.instance.current_resources;
347 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
348 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
349 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
352 /******************************** TODO ********************************/
354 update_resources: function(resources, change)
356 messages.debug("update_resources");
357 var my_oTable = this.table.dataTable();
358 var prev_resources = this.current_resources;
361 * The first time the query is advertised, don't do anything. The
362 * component will learn nodes in the slice through the manifest
363 * received through the other subscription
368 var initial = this.initial_resources;
369 //var r_removed = []; //
370 /*-----------------------------------------------------------------------
371 TODO: remove this dirty hack !!!
373 resources = jQuery.map(resources, function(x){
374 if(!('timeslot' in x)){x.timeslot=0;}
378 TODO: handle generic keys instead of specific stuff
382 var initial_urn = $.map(initial, function(x){return x.urn;});
383 var resources_urn = $.map(resources, function(x){return x.urn;});
384 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
385 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
386 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
387 exists = false; // ioi
388 /*-----------------------------------------------------------------------*/
390 my_oTable.fnClearTable();
392 TODO: factorization of this code !!!
394 $.each(r_added, function(i, r) {
395 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
397 // Create the resource objects
402 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
403 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
406 newline.push(type, urn, slot, SPAN); // ioi
407 var line = my_oTable.fnAddData(newline);
408 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
409 nTr.className = type;
411 $.each(r_attached, function(i, r) {
412 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
413 var type = 'attached';
414 // Create the resource objects
419 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
420 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
423 newline.push(type, node, slot, SPAN); // ioi
424 var line = my_oTable.fnAddData(newline);
425 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
426 nTr.className = type;
428 $.each(r_removed, function(i, r) {
429 // The list contains objects
432 var time = r.timeslot;
434 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
435 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
438 newline.push('remove', node, slot, SPAN); // ioi
439 var line = my_oTable.fnAddData(newline);
440 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
441 nTr.className = 'remove';
444 this.current_resources = $.merge(r_attached,r_added);
446 /* Allow the user to update the slice */
447 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
449 }, // update_resources
453 $.plugin('QueryUpdater', QueryUpdater);