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-md-5'l><'col-md-1'r><'col-md-6'f>>t<'row'<'col-md-5'i><'col-md-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 console.log("elmt(update) is " + this.elmt('update'));
54 this.elmt('update').click(this, this.do_update);
55 this.elmt('refresh').click(this, this.do_refresh);
56 this.elmt('reset').click(this, this.do_reset);
57 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
59 this.listen_query(options.query_uuid);
62 /*************************** PLUGIN EVENTS ****************************/
64 /***************************** GUI EVENTS *****************************/
66 do_update: function(e)
68 console.log ("in do_update");
70 // XXX check that the query is not disabled
71 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
74 do_refresh: function(e)
76 throw 'Not implemented';
81 throw 'Not implemented';
84 do_clear_annotations: function(e)
86 throw 'Not implemented';
89 /************************** GUI MANIPULATION **************************/
92 set_button_state: function(name, state)
94 this.elmt(name).attr('disabled', state ? false : 'disabled');
102 find_row: function(key)
104 // key in third position, column id = 2
107 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
108 return (col._aData[KEY_POS] == key);
111 if (cols.length == 0)
114 throw "Too many same-key rows in ResourceSelected plugin";
119 set_state: function(data)
127 switch(data.request) {
128 case FIELD_REQUEST_ADD_RESET:
129 case FIELD_REQUEST_REMOVE_RESET:
130 // find line and delete it
131 row = this.find_row(data.value);
133 this.table.fnDeleteRow(row.nTr);
135 case FIELD_REQUEST_CHANGE:
138 case FIELD_REQUEST_ADD:
141 case FIELD_REQUEST_REMOVE:
146 switch(data.status) {
147 case FIELD_REQUEST_PENDING:
149 button = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='" + data.key + "'/>";
151 case FIELD_REQUEST_SUCCESS:
154 case FIELD_REQUEST_FAILURE:
159 var status = msg + status;
162 // if no, create it, else replace it
163 // XXX it's not just about adding lines, but sometimes removing some
164 // XXX how do we handle status reset ?
165 row = this.find_row(data.value);
174 // XXX second parameter refresh = false can improve performance. todo in hazelnut also
175 this.table.fnAddData(newline);
176 row = this.find_row(data.value);
178 // Update row text...
179 this.table.fnUpdate(newline, row.nTr);
182 // Change cell color according to status
184 $(row.nTr).removeClass('add remove')
185 var cls = action.toLowerCase();
187 $(row.nTr).addClass(cls);
191 /*************************** QUERY HANDLER ****************************/
195 /*************************** RECORD HANDLER ***************************/
197 on_new_record: function(record)
199 // if (not and update) {
201 // initial['resource'], initial['lease'] ?
202 this.initial.push(record.urn);
204 // We simply add to the table
206 // \ this.initial_resources
209 // current_resources \ YES | NO
210 // --------------------+----------+---------
211 // YES | attached | added
219 // +-----------------+--------------+
221 // +-------+ ?G +-------+ +-------+ +---+---+ |
222 // | | -----> | | !G | | | | DA |
223 // | ND | | PG | -----> | D | -----> | PC | <------+ |
224 // | | <----- | | ~G | | C | | | |
225 // +-------+ GE +-------+ +-------+ +-------+ +------+
227 // | DA UE | | ?U | PCA |
229 // +-------+ +-------+ +------+
231 // | AD | <----- | PU | --------+
233 // +-------+ +-------+
238 // Plugins (i) receive state information, (ii) perform actions
241 // ND : No data ?G : Get query
242 // PG : Pending Get !G : Get reply
243 // D : Data present ~G : Get partial reply
244 // PC : Pending changes GE : Get error
245 // PU : Pending update C : Change request
246 // PCA: Pending change with annotation R : Reset request
247 // AD : Annotated data ?U : Update query
249 // ~U : Update partial reply
251 // DA : Delete annotation request
253 // - D -> PU directly if the user chooses 'dynamic update'
254 // - Be careful for updates if partial Get success
256 // ND: No data == initialization state
259 // - on_query_in_progress
260 // NOTE: cannot distinguish get and update here. Is it important ?
262 on_query_in_progress: function()
268 // - on_clear_records (Get)
269 // - on_new_record (shared with AD) XXX
271 // NOTE: cannot distinguish get and update here. Is it important ?
272 // NOTE: Would record key be sufficient for update ?
274 on_clear_records: function()
279 on_new_record: function(record)
283 on_query_done: function()
288 // PC : Pending changes
289 // NOTE: record_key could be sufficient
290 on_added_record: function(record)
292 this.set_record_state(record, RECORD_STATE_ADDED);
295 on_removed_record: function(record_key)
297 this.set_record_state(RECORD_STATE_REMOVED);
300 // PU : Pending update
301 // - on_query_in_progress (already done)
303 // PCA : Pending change with annotation
304 // NOTE: Manifold will inform the plugin about updates, and thus won't
305 // call new record, even if the same channel UUID is used...
306 // - TODO on_updated_record
307 // - Key and confirmation could be sufficient, or key and record state
308 // XXX move record state to the manifold plugin API
310 on_field_state_changed: function(request, key, value, status)
312 this.set_state(request, key, value, status);
315 // XXX we will have the requests for change
316 // XXX + the requests to move into the query cycle = the buttons aforementioned
318 // XXX what happens in case of updates ? not implemented yet
319 // XXX we want resources and leases
320 // listen for SET_ADD and SET_REMOVE for slice query
322 /************************** PRIVATE METHODS ***************************/
324 _close_click: function(e)
328 //jQuery.publish('selected', 'add/'+key_value);
329 // this.parentNode is <td> this.parentNode.parentNode is <tr>
330 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
331 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
332 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
333 var remove_urn = this.id;
334 var current_resources = event.data.instance.current_resources;
335 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
336 //jQuery.publish('selected', 'cancel/'+this.id+'/'+get_value(firstCellVal));
337 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
340 /******************************** TODO ********************************/
342 update_resources: function(resources, change)
344 console.log("update_resources");
345 var my_oTable = this.table.dataTable();
346 var prev_resources = this.current_resources;
349 * The first time the query is advertised, don't do anything. The
350 * component will learn nodes in the slice through the manifest
351 * received through the other subscription
356 var initial = this.initial_resources;
357 //var r_removed = []; //
358 /*-----------------------------------------------------------------------
359 TODO: remove this dirty hack !!!
361 resources = jQuery.map(resources, function(x){
362 if(!('timeslot' in x)){x.timeslot=0;}
366 TODO: handle generic keys instead of specific stuff
370 var initial_urn = $.map(initial, function(x){return x.urn;});
371 var resources_urn = $.map(resources, function(x){return x.urn;});
372 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
373 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
374 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
375 exists = false; // ioi
376 /*-----------------------------------------------------------------------*/
378 my_oTable.fnClearTable();
380 TODO: factorization of this code !!!
382 $.each(r_added, function(i, r) {
383 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
385 // Create the resource objects
390 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+urn+"'/>";
391 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
394 newline.push(type, urn, slot, SPAN); // ioi
395 var line = my_oTable.fnAddData(newline);
396 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
397 nTr.className = type;
399 $.each(r_attached, function(i, r) {
400 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
401 var type = 'attached';
402 // Create the resource objects
407 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
408 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
411 newline.push(type, node, 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_removed, function(i, r) {
417 // The list contains objects
420 var time = r.timeslot;
422 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
423 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
426 newline.push('remove', node, slot, SPAN); // ioi
427 var line = my_oTable.fnAddData(newline);
428 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
429 nTr.className = 'remove';
432 this.current_resources = $.merge(r_attached,r_added);
434 /* Allow the user to update the slice */
435 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
437 }, // update_resources
441 $.plugin('ResourcesSelected', ResourcesSelected);