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 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
35 sPaginationType: 'full_numbers',
39 // sScrollX : '100%', // Horizontal scrolling
40 // bSortClasses : false, // Disable style for the sorted column
41 // aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
42 // fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
43 // /* Prevent to loop on click while redrawing table */
44 // $('.ResourceSelectedClose').unbind('click');
45 // /* Handle clicks on close span */
46 // /* Reassociate close click every time the table is redrawn */
47 // $('.ResourceSelectedClose').bind('click', self, self._close_click);
51 // XXX This should not be done at init...
52 this.elmt('update').click(this, this.do_update);
53 this.elmt('refresh').click(this, this.do_refresh);
54 this.elmt('reset').click(this, this.do_reset);
55 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
57 this.listen_query(options.query_uuid);
60 /*************************** PLUGIN EVENTS ****************************/
62 /***************************** GUI EVENTS *****************************/
64 do_update: function(e)
67 // XXX check that the query is not disabled
68 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
71 do_refresh: function(e)
73 throw 'resource_selected.do_refresh Not implemented';
78 throw 'resources_selected.do_reset Not implemented';
81 do_clear_annotations: function(e)
83 throw 'resources_selected.do_clear_annotations Not implemented';
86 /************************** GUI MANIPULATION **************************/
89 set_button_state: function(name, state)
91 this.elmt(name).attr('disabled', state ? false : 'disabled');
99 find_row: function(key)
101 // key in third position, column id = 2
104 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
105 return (col._aData[KEY_POS] == key);
108 if (cols.length == 0)
111 throw "Too many same-key rows in ResourceSelected plugin";
116 set_state: function(data)
124 switch(data.request) {
125 case FIELD_REQUEST_ADD_RESET:
126 case FIELD_REQUEST_REMOVE_RESET:
127 // find line and delete it
128 row = this.find_row(data.value);
130 this.table.fnDeleteRow(row.nTr);
132 case FIELD_REQUEST_CHANGE:
135 case FIELD_REQUEST_ADD:
138 case FIELD_REQUEST_REMOVE:
143 switch(data.status) {
144 case FIELD_REQUEST_PENDING:
146 button = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='" + data.key + "'/>";
148 case FIELD_REQUEST_SUCCESS:
151 case FIELD_REQUEST_FAILURE:
156 var status = msg + status;
159 // if no, create it, else replace it
160 // XXX it's not just about adding lines, but sometimes removing some
161 // XXX how do we handle status reset ?
162 row = this.find_row(data.value);
171 // XXX second parameter refresh = false can improve performance. todo in hazelnut also
172 this.table.fnAddData(newline);
173 row = this.find_row(data.value);
175 // Update row text...
176 this.table.fnUpdate(newline, row.nTr);
179 // Change cell color according to status
181 $(row.nTr).removeClass('add remove')
182 var cls = action.toLowerCase();
184 $(row.nTr).addClass(cls);
188 /*************************** QUERY HANDLER ****************************/
192 /*************************** RECORD HANDLER ***************************/
194 on_new_record: function(record)
196 // if (not and update) {
198 // initial['resource'], initial['lease'] ?
199 this.initial.push(record.urn);
201 // We simply add to the table
203 // \ this.initial_resources
206 // current_resources \ YES | NO
207 // --------------------+----------+---------
208 // YES | attached | added
216 // +-----------------+--------------+
218 // +-------+ ?G +-------+ +-------+ +---+---+ |
219 // | | -----> | | !G | | | | DA |
220 // | ND | | PG | -----> | D | -----> | PC | <------+ |
221 // | | <----- | | ~G | | C | | | |
222 // +-------+ GE +-------+ +-------+ +-------+ +------+
224 // | DA UE | | ?U | PCA |
226 // +-------+ +-------+ +------+
228 // | AD | <----- | PU | --------+
230 // +-------+ +-------+
235 // Plugins (i) receive state information, (ii) perform actions
238 // ND : No data ?G : Get query
239 // PG : Pending Get !G : Get reply
240 // D : Data present ~G : Get partial reply
241 // PC : Pending changes GE : Get error
242 // PU : Pending update C : Change request
243 // PCA: Pending change with annotation R : Reset request
244 // AD : Annotated data ?U : Update query
246 // ~U : Update partial reply
248 // DA : Delete annotation request
250 // - D -> PU directly if the user chooses 'dynamic update'
251 // - Be careful for updates if partial Get success
253 // ND: No data == initialization state
256 // - on_query_in_progress
257 // NOTE: cannot distinguish get and update here. Is it important ?
259 on_query_in_progress: function()
265 // - on_clear_records (Get)
266 // - on_new_record (shared with AD) XXX
268 // NOTE: cannot distinguish get and update here. Is it important ?
269 // NOTE: Would record key be sufficient for update ?
271 on_clear_records: function()
276 on_new_record: function(record)
280 on_query_done: function()
285 // PC : Pending changes
286 // NOTE: record_key could be sufficient
287 on_added_record: function(record)
289 this.set_record_state(record, RECORD_STATE_ADDED);
292 on_removed_record: function(record_key)
294 this.set_record_state(RECORD_STATE_REMOVED);
297 // PU : Pending update
298 // - on_query_in_progress (already done)
300 // PCA : Pending change with annotation
301 // NOTE: Manifold will inform the plugin about updates, and thus won't
302 // call new record, even if the same channel UUID is used...
303 // - TODO on_updated_record
304 // - Key and confirmation could be sufficient, or key and record state
305 // XXX move record state to the manifold plugin API
307 on_field_state_changed: function(request, key, value, status)
309 this.set_state(request, key, value, status);
312 // XXX we will have the requests for change
313 // XXX + the requests to move into the query cycle = the buttons aforementioned
315 // XXX what happens in case of updates ? not implemented yet
316 // XXX we want resources and leases
317 // listen for SET_ADD and SET_REMOVE for slice query
319 /************************** PRIVATE METHODS ***************************/
321 _close_click: function(e)
325 //jQuery.publish('selected', 'add/'+key_value);
326 // this.parentNode is <td> this.parentNode.parentNode is <tr>
327 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
328 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
329 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
330 var remove_urn = this.id;
331 var current_resources = event.data.instance.current_resources;
332 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
333 //jQuery.publish('selected', 'cancel/'+this.id+'/'+get_value(firstCellVal));
334 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
337 /******************************** TODO ********************************/
339 update_resources: function(resources, change)
341 console.log("update_resources");
342 var my_oTable = this.table.dataTable();
343 var prev_resources = this.current_resources;
346 * The first time the query is advertised, don't do anything. The
347 * component will learn nodes in the slice through the manifest
348 * received through the other subscription
353 var initial = this.initial_resources;
354 //var r_removed = []; //
355 /*-----------------------------------------------------------------------
356 TODO: remove this dirty hack !!!
358 resources = jQuery.map(resources, function(x){
359 if(!('timeslot' in x)){x.timeslot=0;}
363 TODO: handle generic keys instead of specific stuff
367 var initial_urn = $.map(initial, function(x){return x.urn;});
368 var resources_urn = $.map(resources, function(x){return x.urn;});
369 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
370 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
371 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
372 exists = false; // ioi
373 /*-----------------------------------------------------------------------*/
375 my_oTable.fnClearTable();
377 TODO: factorization of this code !!!
379 $.each(r_added, function(i, r) {
380 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
382 // Create the resource objects
387 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+urn+"'/>";
388 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
391 newline.push(type, urn, slot, SPAN); // ioi
392 var line = my_oTable.fnAddData(newline);
393 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
394 nTr.className = type;
396 $.each(r_attached, function(i, r) {
397 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
398 var type = 'attached';
399 // Create the resource objects
404 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
405 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
408 newline.push(type, node, 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_removed, function(i, r) {
414 // The list contains objects
417 var time = r.timeslot;
419 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
420 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
423 newline.push('remove', node, slot, SPAN); // ioi
424 var line = my_oTable.fnAddData(newline);
425 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
426 nTr.className = 'remove';
429 this.current_resources = $.merge(r_attached,r_added);
431 /* Allow the user to update the slice */
432 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
434 }, // update_resources
438 $.plugin('ResourcesSelected', ResourcesSelected);