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 // make sure the change is visible : toggle on the whole plugin
125 // this might hae to be made an 'auto-toggle' option of this plugin..
126 // also it might be needed to be a little finer-grained here
129 switch(data.request) {
130 case FIELD_REQUEST_ADD_RESET:
131 case FIELD_REQUEST_REMOVE_RESET:
132 // find line and delete it
133 row = this.find_row(data.value);
135 this.table.fnDeleteRow(row.nTr);
137 case FIELD_REQUEST_CHANGE:
140 case FIELD_REQUEST_ADD:
143 case FIELD_REQUEST_REMOVE:
148 switch(data.status) {
149 case FIELD_REQUEST_PENDING:
151 button = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='" + data.key + "'/>";
153 case FIELD_REQUEST_SUCCESS:
156 case FIELD_REQUEST_FAILURE:
161 var status = msg + status;
164 // if no, create it, else replace it
165 // XXX it's not just about adding lines, but sometimes removing some
166 // XXX how do we handle status reset ?
167 row = this.find_row(data.value);
176 // XXX second parameter refresh = false can improve performance. todo in hazelnut also
177 this.table.fnAddData(newline);
178 row = this.find_row(data.value);
180 // Update row text...
181 this.table.fnUpdate(newline, row.nTr);
184 // Change cell color according to status
186 $(row.nTr).removeClass('add remove')
187 var cls = action.toLowerCase();
189 $(row.nTr).addClass(cls);
193 /*************************** QUERY HANDLER ****************************/
197 /*************************** RECORD HANDLER ***************************/
199 on_new_record: function(record)
201 // if (not and update) {
203 // initial['resource'], initial['lease'] ?
204 this.initial.push(record.urn);
206 // We simply add to the table
208 // \ this.initial_resources
211 // current_resources \ YES | NO
212 // --------------------+----------+---------
213 // YES | attached | added
221 // +-----------------+--------------+
223 // +-------+ ?G +-------+ +-------+ +---+---+ |
224 // | | -----> | | !G | | | | DA |
225 // | ND | | PG | -----> | D | -----> | PC | <------+ |
226 // | | <----- | | ~G | | C | | | |
227 // +-------+ GE +-------+ +-------+ +-------+ +------+
229 // | DA UE | | ?U | PCA |
231 // +-------+ +-------+ +------+
233 // | AD | <----- | PU | --------+
235 // +-------+ +-------+
240 // Plugins (i) receive state information, (ii) perform actions
243 // ND : No data ?G : Get query
244 // PG : Pending Get !G : Get reply
245 // D : Data present ~G : Get partial reply
246 // PC : Pending changes GE : Get error
247 // PU : Pending update C : Change request
248 // PCA: Pending change with annotation R : Reset request
249 // AD : Annotated data ?U : Update query
251 // ~U : Update partial reply
253 // DA : Delete annotation request
255 // - D -> PU directly if the user chooses 'dynamic update'
256 // - Be careful for updates if partial Get success
258 // ND: No data == initialization state
261 // - on_query_in_progress
262 // NOTE: cannot distinguish get and update here. Is it important ?
264 on_query_in_progress: function()
270 // - on_clear_records (Get)
271 // - on_new_record (shared with AD) XXX
273 // NOTE: cannot distinguish get and update here. Is it important ?
274 // NOTE: Would record key be sufficient for update ?
276 on_clear_records: function()
281 on_new_record: function(record)
285 on_query_done: function()
290 // PC : Pending changes
291 // NOTE: record_key could be sufficient
292 on_added_record: function(record)
294 this.set_record_state(record, RECORD_STATE_ADDED);
297 on_removed_record: function(record_key)
299 this.set_record_state(RECORD_STATE_REMOVED);
302 // PU : Pending update
303 // - on_query_in_progress (already done)
305 // PCA : Pending change with annotation
306 // NOTE: Manifold will inform the plugin about updates, and thus won't
307 // call new record, even if the same channel UUID is used...
308 // - TODO on_updated_record
309 // - Key and confirmation could be sufficient, or key and record state
310 // XXX move record state to the manifold plugin API
312 on_field_state_changed: function(request, key, value, status)
314 this.set_state(request, key, value, status);
317 // XXX we will have the requests for change
318 // XXX + the requests to move into the query cycle = the buttons aforementioned
320 // XXX what happens in case of updates ? not implemented yet
321 // XXX we want resources and leases
322 // listen for SET_ADD and SET_REMOVE for slice query
324 /************************** PRIVATE METHODS ***************************/
326 _close_click: function(e)
330 //jQuery.publish('selected', 'add/'+key_value);
331 // this.parentNode is <td> this.parentNode.parentNode is <tr>
332 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
333 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
334 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
335 var remove_urn = this.id;
336 var current_resources = event.data.instance.current_resources;
337 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
338 //jQuery.publish('selected', 'cancel/'+this.id+'/'+get_value(firstCellVal));
339 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
342 /******************************** TODO ********************************/
344 update_resources: function(resources, change)
346 console.log("update_resources");
347 var my_oTable = this.table.dataTable();
348 var prev_resources = this.current_resources;
351 * The first time the query is advertised, don't do anything. The
352 * component will learn nodes in the slice through the manifest
353 * received through the other subscription
358 var initial = this.initial_resources;
359 //var r_removed = []; //
360 /*-----------------------------------------------------------------------
361 TODO: remove this dirty hack !!!
363 resources = jQuery.map(resources, function(x){
364 if(!('timeslot' in x)){x.timeslot=0;}
368 TODO: handle generic keys instead of specific stuff
372 var initial_urn = $.map(initial, function(x){return x.urn;});
373 var resources_urn = $.map(resources, function(x){return x.urn;});
374 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
375 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
376 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
377 exists = false; // ioi
378 /*-----------------------------------------------------------------------*/
380 my_oTable.fnClearTable();
382 TODO: factorization of this code !!!
384 $.each(r_added, function(i, r) {
385 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
387 // Create the resource objects
392 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+urn+"'/>";
393 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
396 newline.push(type, urn, slot, SPAN); // ioi
397 var line = my_oTable.fnAddData(newline);
398 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
399 nTr.className = type;
401 $.each(r_attached, function(i, r) {
402 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
403 var type = 'attached';
404 // Create the resource objects
409 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
410 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
413 newline.push(type, node, slot, SPAN); // ioi
414 var line = my_oTable.fnAddData(newline);
415 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
416 nTr.className = type;
418 $.each(r_removed, function(i, r) {
419 // The list contains objects
422 var time = r.timeslot;
424 var SPAN = "<span class='ui-icon ui-icon-close ResourceSelectedClose' id='"+node+"'/>";
425 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
428 newline.push('remove', node, slot, SPAN); // ioi
429 var line = my_oTable.fnAddData(newline);
430 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
431 nTr.className = 'remove';
434 this.current_resources = $.merge(r_attached,r_added);
436 /* Allow the user to update the slice */
437 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
439 }, // update_resources
443 $.plugin('ResourcesSelected', ResourcesSelected);