2 * MySlice ResourcesSelected 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
14 // XXX record selected (multiple selection ?)
15 // XXX record disabled ?
16 // XXX out of sync plugin ?
17 // XXX out of date record ?
20 // criticality of the absence of a handler in a plugin
21 // non-critical only can have switch case
23 // Record state through the query cycle
26 var ResourcesSelected = Plugin.extend({
28 init: function(options, element)
30 this._super(options, element);
33 this.table = this.el('table').dataTable({
34 //sPaginationType: 'full_numbers', // Use pagination
35 sPaginationType: 'bootstrap',
38 sScrollX : '100%', // Horizontal scrolling
39 bSortClasses : false, // Disable style for the sorted column
40 aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
41 fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
42 /* Prevent to loop on click while redrawing table */
43 $('.ResourceSelectedClose').unbind('click');
44 /* Handle clicks on close span */
45 /* Reassociate close click every time the table is redrawn */
46 $('.ResourceSelectedClose').bind('click', self, self._close_click);
50 // XXX This should not be done at init...
51 this.el('update').click(this, this.do_update);
52 this.el('refresh').click(this, this.do_refresh);
53 this.el('reset').click(this, this.do_reset);
54 this.el('clear_annotations').click(this, this.do_clear_annotations);
56 this.listen_query(options.query_uuid);
59 /*************************** PLUGIN EVENTS ****************************/
61 /***************************** GUI EVENTS *****************************/
63 do_update: function(e)
66 // XXX check that the query is not disabled
67 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
70 do_refresh: function(e)
72 throw 'Not implemented';
77 throw 'Not implemented';
80 do_clear_annotations: function(e)
82 throw 'Not implemented';
85 /************************** GUI MANIPULATION **************************/
88 set_button_state: function(name, state)
90 this.el(name).attr('disabled', state ? false : 'disabled');
98 find_row: function(key)
100 // key in third position, column id = 2
103 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
104 return (col._aData[KEY_POS] == key);
107 if (cols.length == 0)
110 throw "Too many same-key rows in ResourceSelected plugin";
115 set_state: function(data)
123 if (data.request == FIELD_REQUEST_RESET) {
128 switch(data.request) {
129 case FIELD_REQUEST_CHANGE:
132 case FIELD_REQUEST_ADD:
135 case FIELD_REQUEST_REMOVE:
140 switch(data.status) {
141 case FIELD_REQUEST_PENDING:
143 button = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='" + data.key + "'/>";
145 case FIELD_REQUEST_SUCCESS:
148 case FIELD_REQUEST_FAILURE:
153 var status = msg + status;
156 // if no, create it, else replace it
157 // XXX it's not just about adding lines, but sometimes removing some
158 // XXX how do we handle status reset ?
159 row = this.find_row(data.value);
168 // XXX second parameter refresh = false can improve performance. todo in hazelnut also
169 this.table.fnAddData(newline);
170 row = this.find_row(data.value);
172 // Update row text...
173 this.table.fnUpdate(newline, row.nTr);
176 // Change cell color according to status
178 $(row.nTr).removeClass('add remove')
179 var cls = action.toLowerCase();
181 $(row.nTr).addClass(cls);
185 /*************************** QUERY HANDLER ****************************/
189 /*************************** RECORD HANDLER ***************************/
191 on_new_record: function(record)
193 // if (not and update) {
195 // initial['resource'], initial['lease'] ?
196 this.initial.push(record.urn);
198 // We simply add to the table
200 // \ this.initial_resources
203 // current_resources \ YES | NO
204 // --------------------+----------+---------
205 // YES | attached | added
213 // +-----------------+--------------+
215 // +-------+ ?G +-------+ +-------+ +---+---+ |
216 // | | -----> | | !G | | | | DA |
217 // | ND | | PG | -----> | D | -----> | PC | <------+ |
218 // | | <----- | | ~G | | C | | | |
219 // +-------+ GE +-------+ +-------+ +-------+ +------+
221 // | DA UE | | ?U | PCA |
223 // +-------+ +-------+ +------+
225 // | AD | <----- | PU | --------+
227 // +-------+ +-------+
232 // Plugins (i) receive state information, (ii) perform actions
235 // ND : No data ?G : Get query
236 // PG : Pending Get !G : Get reply
237 // D : Data present ~G : Get partial reply
238 // PC : Pending changes GE : Get error
239 // PU : Pending update C : Change request
240 // PCA: Pending change with annotation R : Reset request
241 // AD : Annotated data ?U : Update query
243 // ~U : Update partial reply
245 // DA : Delete annotation request
247 // - D -> PU directly if the user chooses 'dynamic update'
248 // - Be careful for updates if partial Get success
250 // ND: No data == initialization state
253 // - on_query_in_progress
254 // NOTE: cannot distinguish get and update here. Is it important ?
256 on_query_in_progress: function()
262 // - on_clear_records (Get)
263 // - on_new_record (shared with AD) XXX
265 // NOTE: cannot distinguish get and update here. Is it important ?
266 // NOTE: Would record key be sufficient for update ?
268 on_clear_records: function()
273 on_new_record: function(record)
277 on_query_done: function()
282 // PC : Pending changes
283 // NOTE: record_key could be sufficient
284 on_added_record: function(record)
286 this.set_record_state(record, RECORD_STATE_ADDED);
289 on_removed_record: function(record_key)
291 this.set_record_state(RECORD_STATE_REMOVED);
294 // PU : Pending update
295 // - on_query_in_progress (already done)
297 // PCA : Pending change with annotation
298 // NOTE: Manifold will inform the plugin about updates, and thus won't
299 // call new record, even if the same channel UUID is used...
300 // - TODO on_updated_record
301 // - Key and confirmation could be sufficient, or key and record state
302 // XXX move record state to the manifold plugin API
304 on_field_state_changed: function(request, key, value, status)
306 this.set_state(request, key, value, status);
309 // XXX we will have the requests for change
310 // XXX + the requests to move into the query cycle = the buttons aforementioned
312 // XXX what happens in case of updates ? not implemented yet
313 // XXX we want resources and leases
314 // listen for SET_ADD and SET_REMOVE for slice query
316 /************************** PRIVATE METHODS ***************************/
318 _close_click: function(e)
322 //jQuery.publish('selected', 'add/'+key_value);
323 // this.parentNode is <td> this.parentNode.parentNode is <tr>
324 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
325 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
326 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
327 var remove_urn = this.id;
328 var current_resources = event.data.instance.current_resources;
329 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
330 //jQuery.publish('selected', 'cancel/'+this.id+'/'+get_value(firstCellVal));
331 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
334 /******************************** TODO ********************************/
336 update_resources: function(resources, change)
338 console.log("update_resources");
339 var my_oTable = this.table.dataTable();
340 var prev_resources = this.current_resources;
343 * The first time the query is advertised, don't do anything. The
344 * component will learn nodes in the slice through the manifest
345 * received through the other subscription
350 var initial = this.initial_resources;
351 //var r_removed = []; //
352 /*-----------------------------------------------------------------------
353 TODO: remove this dirty hack !!!
355 resources = jQuery.map(resources, function(x){
356 if(!('timeslot' in x)){x.timeslot=0;}
360 TODO: handle generic keys instead of specific stuff
364 var initial_urn = $.map(initial, function(x){return x.urn;});
365 var resources_urn = $.map(resources, function(x){return x.urn;});
366 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
367 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
368 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
369 exists = false; // ioi
370 /*-----------------------------------------------------------------------*/
372 my_oTable.fnClearTable();
374 TODO: factorization of this code !!!
376 $.each(r_added, function(i, r) {
377 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
379 // Create the resource objects
384 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+urn+"'/>";
385 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
388 newline.push(type, urn, slot, SPAN); // ioi
389 var line = my_oTable.fnAddData(newline);
390 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
391 nTr.className = type;
393 $.each(r_attached, function(i, r) {
394 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
395 var type = 'attached';
396 // Create the resource objects
401 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
402 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
405 newline.push(type, node, slot, SPAN); // ioi
406 var line = my_oTable.fnAddData(newline);
407 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
408 nTr.className = type;
410 $.each(r_removed, function(i, r) {
411 // The list contains objects
414 var time = r.timeslot;
416 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
417 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
420 newline.push('remove', node, slot, SPAN); // ioi
421 var line = my_oTable.fnAddData(newline);
422 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
423 nTr.className = 'remove';
426 this.current_resources = $.merge(r_attached,r_added);
428 /* Allow the user to update the slice */
429 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
431 }, // update_resources
435 $.plugin('ResourcesSelected', ResourcesSelected);