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;
175 // if no, create it, else replace it
176 // XXX it's not just about adding lines, but sometimes removing some
177 // XXX how do we handle status reset ?
178 data.value = JSON.stringify(data.value);
179 row = this.find_row(data.value);
188 // XXX second parameter refresh = false can improve performance. todo in querytable also
189 this.table.fnAddData(newline);
190 row = this.find_row(data.value);
192 // Update row text...
193 this.table.fnUpdate(newline, row.nTr);
196 // Change cell color according to status
198 $(row.nTr).removeClass('add remove')
199 var cls = action.toLowerCase();
201 $(row.nTr).addClass(cls);
205 /*************************** QUERY HANDLER ****************************/
209 /*************************** RECORD HANDLER ***************************/
211 on_new_record: function(record)
213 // if (not and update) {
215 // initial['resource'], initial['lease'] ?
216 this.initial.push(record.urn);
218 // We simply add to the table
220 // \ this.initial_resources
223 // current_resources \ YES | NO
224 // --------------------+----------+---------
225 // YES | attached | added
233 // +-----------------+--------------+
235 // +-------+ ?G +-------+ +-------+ +---+---+ |
236 // | | -----> | | !G | | | | DA |
237 // | ND | | PG | -----> | D | -----> | PC | <------+ |
238 // | | <----- | | ~G | | C | | | |
239 // +-------+ GE +-------+ +-------+ +-------+ +------+
241 // | DA UE | | ?U | PCA |
243 // +-------+ +-------+ +------+
245 // | AD | <----- | PU | --------+
247 // +-------+ +-------+
252 // Plugins (i) receive state information, (ii) perform actions
255 // ND : No data ?G : Get query
256 // PG : Pending Get !G : Get reply
257 // D : Data present ~G : Get partial reply
258 // PC : Pending changes GE : Get error
259 // PU : Pending update C : Change request
260 // PCA: Pending change with annotation R : Reset request
261 // AD : Annotated data ?U : Update query
263 // ~U : Update partial reply
265 // DA : Delete annotation request
267 // - D -> PU directly if the user chooses 'dynamic update'
268 // - Be careful for updates if partial Get success
270 // ND: No data == initialization state
273 // - on_query_in_progress
274 // NOTE: cannot distinguish get and update here. Is it important ?
276 on_query_in_progress: function()
278 messages.debug("queryupdater.on_query_in_progress");
283 // - on_clear_records (Get)
284 // - on_new_record (shared with AD) XXX
286 // NOTE: cannot distinguish get and update here. Is it important ?
287 // NOTE: Would record key be sufficient for update ?
289 on_clear_records: function()
294 on_new_record: function(record)
298 on_query_done: function()
303 // PC : Pending changes
304 // NOTE: record_key could be sufficient
305 on_added_record: function(record)
307 this.set_record_state(record, RECORD_STATE_ADDED);
310 on_removed_record: function(record_key)
312 this.set_record_state(RECORD_STATE_REMOVED);
315 // PU : Pending update
316 // - on_query_in_progress (already done)
318 // PCA : Pending change with annotation
319 // NOTE: Manifold will inform the plugin about updates, and thus won't
320 // call new record, even if the same channel UUID is used...
321 // - TODO on_updated_record
322 // - Key and confirmation could be sufficient, or key and record state
323 // XXX move record state to the manifold plugin API
325 on_field_state_changed: function(result)
327 messages.debug(result)
328 /* this.set_state(result.request, result.key, result.value, result.status); */
329 this.set_state(result);
332 // XXX we will have the requests for change
333 // XXX + the requests to move into the query cycle = the buttons aforementioned
335 // XXX what happens in case of updates ? not implemented yet
336 // XXX we want resources and leases
337 // listen for SET_ADD and SET_REMOVE for slice query
339 /************************** PRIVATE METHODS ***************************/
341 _close_click: function(e)
345 //jQuery.publish('selected', 'add/'+key_value);
346 // this.parentNode is <td> this.parentNode.parentNode is <tr>
347 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
348 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
349 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
350 var remove_urn = this.id;
351 var current_resources = event.data.instance.current_resources;
352 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
353 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
354 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
357 /******************************** TODO ********************************/
359 update_resources: function(resources, change)
361 messages.debug("update_resources");
362 var my_oTable = this.table.dataTable();
363 var prev_resources = this.current_resources;
366 * The first time the query is advertised, don't do anything. The
367 * component will learn nodes in the slice through the manifest
368 * received through the other subscription
373 var initial = this.initial_resources;
374 //var r_removed = []; //
375 /*-----------------------------------------------------------------------
376 TODO: remove this dirty hack !!!
378 resources = jQuery.map(resources, function(x){
379 if(!('timeslot' in x)){x.timeslot=0;}
383 TODO: handle generic keys instead of specific stuff
387 var initial_urn = $.map(initial, function(x){return x.urn;});
388 var resources_urn = $.map(resources, function(x){return x.urn;});
389 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
390 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
391 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
392 exists = false; // ioi
393 /*-----------------------------------------------------------------------*/
395 my_oTable.fnClearTable();
397 TODO: factorization of this code !!!
399 $.each(r_added, function(i, r) {
400 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
402 // Create the resource objects
407 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
408 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
411 newline.push(type, urn, slot, SPAN); // ioi
412 var line = my_oTable.fnAddData(newline);
413 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
414 nTr.className = type;
416 $.each(r_attached, function(i, r) {
417 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
418 var type = 'attached';
419 // Create the resource objects
424 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
425 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
428 newline.push(type, node, slot, SPAN); // ioi
429 var line = my_oTable.fnAddData(newline);
430 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
431 nTr.className = type;
433 $.each(r_removed, function(i, r) {
434 // The list contains objects
437 var time = r.timeslot;
439 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
440 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
443 newline.push('remove', node, slot, SPAN); // ioi
444 var line = my_oTable.fnAddData(newline);
445 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
446 nTr.className = 'remove';
449 this.current_resources = $.merge(r_attached,r_added);
451 /* Allow the user to update the slice */
452 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
454 }, // update_resources
458 $.plugin('QueryUpdater', QueryUpdater);