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.elmt('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.elmt('update').click(this, this.do_update);
52 this.elmt('refresh').click(this, this.do_refresh);
53 this.elmt('reset').click(this, this.do_reset);
54 this.elmt('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.elmt(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 switch(data.request) {
124 case FIELD_REQUEST_ADD_RESET:
125 case FIELD_REQUEST_REMOVE_RESET:
126 // find line and delete it
127 row = this.find_row(data.value);
129 this.table.fnDeleteRow(row.nTr);
131 case FIELD_REQUEST_CHANGE:
134 case FIELD_REQUEST_ADD:
137 case FIELD_REQUEST_REMOVE:
142 switch(data.status) {
143 case FIELD_REQUEST_PENDING:
145 button = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='" + data.key + "'/>";
147 case FIELD_REQUEST_SUCCESS:
150 case FIELD_REQUEST_FAILURE:
155 var status = msg + status;
158 // if no, create it, else replace it
159 // XXX it's not just about adding lines, but sometimes removing some
160 // XXX how do we handle status reset ?
161 row = this.find_row(data.value);
170 // XXX second parameter refresh = false can improve performance. todo in hazelnut also
171 this.table.fnAddData(newline);
172 row = this.find_row(data.value);
174 // Update row text...
175 this.table.fnUpdate(newline, row.nTr);
178 // Change cell color according to status
180 $(row.nTr).removeClass('add remove')
181 var cls = action.toLowerCase();
183 $(row.nTr).addClass(cls);
187 /*************************** QUERY HANDLER ****************************/
191 /*************************** RECORD HANDLER ***************************/
193 on_new_record: function(record)
195 // if (not and update) {
197 // initial['resource'], initial['lease'] ?
198 this.initial.push(record.urn);
200 // We simply add to the table
202 // \ this.initial_resources
205 // current_resources \ YES | NO
206 // --------------------+----------+---------
207 // YES | attached | added
215 // +-----------------+--------------+
217 // +-------+ ?G +-------+ +-------+ +---+---+ |
218 // | | -----> | | !G | | | | DA |
219 // | ND | | PG | -----> | D | -----> | PC | <------+ |
220 // | | <----- | | ~G | | C | | | |
221 // +-------+ GE +-------+ +-------+ +-------+ +------+
223 // | DA UE | | ?U | PCA |
225 // +-------+ +-------+ +------+
227 // | AD | <----- | PU | --------+
229 // +-------+ +-------+
234 // Plugins (i) receive state information, (ii) perform actions
237 // ND : No data ?G : Get query
238 // PG : Pending Get !G : Get reply
239 // D : Data present ~G : Get partial reply
240 // PC : Pending changes GE : Get error
241 // PU : Pending update C : Change request
242 // PCA: Pending change with annotation R : Reset request
243 // AD : Annotated data ?U : Update query
245 // ~U : Update partial reply
247 // DA : Delete annotation request
249 // - D -> PU directly if the user chooses 'dynamic update'
250 // - Be careful for updates if partial Get success
252 // ND: No data == initialization state
255 // - on_query_in_progress
256 // NOTE: cannot distinguish get and update here. Is it important ?
258 on_query_in_progress: function()
264 // - on_clear_records (Get)
265 // - on_new_record (shared with AD) XXX
267 // NOTE: cannot distinguish get and update here. Is it important ?
268 // NOTE: Would record key be sufficient for update ?
270 on_clear_records: function()
275 on_new_record: function(record)
279 on_query_done: function()
284 // PC : Pending changes
285 // NOTE: record_key could be sufficient
286 on_added_record: function(record)
288 this.set_record_state(record, RECORD_STATE_ADDED);
291 on_removed_record: function(record_key)
293 this.set_record_state(RECORD_STATE_REMOVED);
296 // PU : Pending update
297 // - on_query_in_progress (already done)
299 // PCA : Pending change with annotation
300 // NOTE: Manifold will inform the plugin about updates, and thus won't
301 // call new record, even if the same channel UUID is used...
302 // - TODO on_updated_record
303 // - Key and confirmation could be sufficient, or key and record state
304 // XXX move record state to the manifold plugin API
306 on_field_state_changed: function(request, key, value, status)
308 this.set_state(request, key, value, status);
311 // XXX we will have the requests for change
312 // XXX + the requests to move into the query cycle = the buttons aforementioned
314 // XXX what happens in case of updates ? not implemented yet
315 // XXX we want resources and leases
316 // listen for SET_ADD and SET_REMOVE for slice query
318 /************************** PRIVATE METHODS ***************************/
320 _close_click: function(e)
324 //jQuery.publish('selected', 'add/'+key_value);
325 // this.parentNode is <td> this.parentNode.parentNode is <tr>
326 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
327 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
328 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
329 var remove_urn = this.id;
330 var current_resources = event.data.instance.current_resources;
331 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
332 //jQuery.publish('selected', 'cancel/'+this.id+'/'+get_value(firstCellVal));
333 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
336 /******************************** TODO ********************************/
338 update_resources: function(resources, change)
340 console.log("update_resources");
341 var my_oTable = this.table.dataTable();
342 var prev_resources = this.current_resources;
345 * The first time the query is advertised, don't do anything. The
346 * component will learn nodes in the slice through the manifest
347 * received through the other subscription
352 var initial = this.initial_resources;
353 //var r_removed = []; //
354 /*-----------------------------------------------------------------------
355 TODO: remove this dirty hack !!!
357 resources = jQuery.map(resources, function(x){
358 if(!('timeslot' in x)){x.timeslot=0;}
362 TODO: handle generic keys instead of specific stuff
366 var initial_urn = $.map(initial, function(x){return x.urn;});
367 var resources_urn = $.map(resources, function(x){return x.urn;});
368 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
369 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
370 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
371 exists = false; // ioi
372 /*-----------------------------------------------------------------------*/
374 my_oTable.fnClearTable();
376 TODO: factorization of this code !!!
378 $.each(r_added, function(i, r) {
379 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
381 // Create the resource objects
386 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+urn+"'/>";
387 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
390 newline.push(type, urn, slot, SPAN); // ioi
391 var line = my_oTable.fnAddData(newline);
392 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
393 nTr.className = type;
395 $.each(r_attached, function(i, r) {
396 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
397 var type = 'attached';
398 // Create the resource objects
403 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
404 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
407 newline.push(type, node, slot, SPAN); // ioi
408 var line = my_oTable.fnAddData(newline);
409 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
410 nTr.className = type;
412 $.each(r_removed, function(i, r) {
413 // The list contains objects
416 var time = r.timeslot;
418 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
419 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
422 newline.push('remove', node, slot, SPAN); // ioi
423 var line = my_oTable.fnAddData(newline);
424 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
425 nTr.className = 'remove';
428 this.current_resources = $.merge(r_attached,r_added);
430 /* Allow the user to update the slice */
431 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
433 }, // update_resources
437 $.plugin('ResourcesSelected', ResourcesSelected);