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
17 // XXX record selected (multiple selection ?)
18 // XXX record disabled ?
19 // XXX out of sync plugin ?
20 // XXX out of date record ?
23 // criticality of the absence of a handler in a plugin
24 // non-critical only can have switch case
26 // Record state through the query cycle
29 var ApplyPlugin = Plugin.extend({
31 /**************************************************************************
33 **************************************************************************/
35 init: function(options, element) {
36 this.classname="queryupdater";
37 this._super(options, element);
41 this.initial = Array();
42 this.selected_resources = Array();
44 this.table = this.elmt('table').dataTable({
45 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
49 //this.elmt('update').click(this, this.do_ok);
50 //this.elmt('refresh').click(this, this.do_cancel);
52 this.elmt('apply').on('shown.bs.modal', function() {
56 this.listen_query(options.query_uuid);
59 /*************************** PLUGIN EVENTS ****************************/
61 /***************************** GUI EVENTS *****************************/
63 /************************** GUI MANIPULATION **************************/
65 populate_table: function()
69 // Loop over records and display pending ones
70 manifold.query_store.iter_records(this.options.query_uuid, function (record_key, record) {
71 state = manifold.query_store.get_record_state(this.options.query_uuid, null, STATE_SET);
76 set_button_state: function(name, state)
78 this.elmt(name).attr('disabled', state ? false : 'disabled');
86 find_row: function(key)
88 // key in third position, column id = 2
91 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
92 return (col._aData[KEY_POS] == key);
98 throw "Too many same-key rows in ResourceSelected plugin";
103 _do_update: function(e) {
108 do_update: function() {
110 var username = this.options.username;
113 console.log("do_update in progress");
115 manifold.raise_event(this.options.query_uuid, RUN_UPDATE);
117 // how to stop the spinning after the event?
118 // this should be triggered by some on_updatequery_done ?
124 throw 'queryupdater.do_reset Not implemented';
127 do_cancel: function(e)
129 throw 'queryupdater.do_clear_annotations Not implemented';
132 /**************************************************************************
134 **************************************************************************/
136 on_new_record: function(record)
139 // if (not and update) {
141 // initial['resource'], initial['lease'] ?
142 this.initial.push(record);
144 //this.set_record_state(record, RECORD_STATE_ATTACHED);
145 // We simply add to the table
147 // \ this.initial_resources
150 // current_resources \ YES | NO
151 // --------------------+----------+---------
152 // YES | attached | added
160 // +-----------------+--------------+
162 // +-------+ ?G +-------+ +-------+ +---+---+ |
163 // | | -----> | | !G | | | | DA |
164 // | ND | | PG | -----> | D | -----> | PC | <------+ |
165 // | | <----- | | ~G | | C | | | |
166 // +-------+ GE +-------+ +-------+ +-------+ +------+
168 // | DA UE | | ?U | PCA |
170 // +-------+ +-------+ +------+
172 // | AD | <----- | PU | --------+
174 // +-------+ +-------+
179 // Plugins (i) receive state information, (ii) perform actions
182 // ND : No data ?G : Get query
183 // PG : Pending Get !G : Get reply
184 // D : Data present ~G : Get partial reply
185 // PC : Pending changes GE : Get error
186 // PU : Pending update C : Change request
187 // PCA: Pending change with annotation R : Reset request
188 // AD : Annotated data ?U : Update query
190 // ~U : Update partial reply
192 // DA : Delete annotation request
194 // - D -> PU directly if the user chooses 'dynamic update'
195 // - Be careful for updates if partial Get success
197 // ND: No data == initialization state
200 // - on_query_in_progress
201 // NOTE: cannot distinguish get and update here. Is it important ?
203 on_query_in_progress: function()
208 on_query_done: function()
210 this.populate_table();
215 // - on_clear_records (Get)
216 // - on_new_record (shared with AD) XXX
218 // NOTE: cannot distinguish get and update here. Is it important ?
219 // NOTE: Would record key be sufficient for update ?
221 on_clear_records: function()
226 on_query_done: function()
231 // PC : Pending changes
232 // NOTE: record_key could be sufficient
233 on_added_record: function(record)
235 this.set_record_state(record, RECORD_STATE_ADDED);
236 // update pending number
239 on_removed_record: function(record_key)
241 this.set_record_state(RECORD_STATE_REMOVED);
244 // PU : Pending update
245 // - on_query_in_progress (already done)
247 // PCA : Pending change with annotation
248 // NOTE: Manifold will inform the plugin about updates, and thus won't
249 // call new record, even if the same channel UUID is used...
250 // - TODO on_updated_record
251 // - Key and confirmation could be sufficient, or key and record state
252 // XXX move record state to the manifold plugin API
254 on_field_state_changed: function(data)
257 if(result.request == FIELD_REQUEST_ADD){
258 this.selected_resources.push(result.value);
259 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
260 var i = this.selected_resources.indexOf(result.value);
262 this.selected_resources.splice(i,1);
265 this.set_state(result);
268 var action, msg, row, status, button = '';
273 // XXX other events missing !!
274 case STATE_VALUE_CHANGE_PENDING:
282 case STATE_SET_IN_PENDING:
285 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
288 case STATE_SET_OUT_PENDING:
291 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
296 // find line and delete it
297 row = this.find_row(data.value);
299 this.table.fnDeleteRow(row.nTr);
302 case STATE_SET_IN_SUCCESS:
306 case STATE_SET_IN_FAILURE:
311 case STATE_SET_OUT_SUCCESS:
315 case STATE_SET_OUT_FAILURE:
327 status = msg + status;
330 // if no, create it, else replace it
331 // XXX it's not just about adding lines, but sometimes removing some
332 // XXX how do we handle status reset ?
334 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
335 if (typeof(data.value) !== "string")
336 data.value = JSON.stringify(data.value);
337 data.selected_resources = this.selected_resources;
338 row = this.find_row(data.value);
339 newline = [action, data.key, data.value, msg, button];
341 // XXX second parameter refresh = false can improve performance. todo in querytable also
342 this.table.fnAddData(newline);
343 row = this.find_row(data.value);
345 // Update row text...
346 this.table.fnUpdate(newline, row.nTr);
349 // Change cell color according to status
351 $(row.nTr).removeClass('add remove')
352 var cls = action.toLowerCase();
354 $(row.nTr).addClass(cls);
358 // XXX we will have the requests for change
359 // XXX + the requests to move into the query cycle = the buttons aforementioned
361 // XXX what happens in case of updates ? not implemented yet
362 // XXX we want resources and leases
363 // listen for SET_ADD and SET_REMOVE for slice query
365 /************************** PRIVATE METHODS ***************************/
367 _close_click: function(e)
371 //jQuery.publish('selected', 'add/'+key_value);
372 // this.parentNode is <td> this.parentNode.parentNode is <tr>
373 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
374 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
375 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
376 var remove_urn = this.id;
377 var current_resources = event.data.instance.current_resources;
378 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
379 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
380 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
383 /******************************** TODO ********************************/
385 update_resources: function(resources, change)
387 messages.debug("update_resources");
388 var my_oTable = this.table.dataTable();
389 var prev_resources = this.current_resources;
392 * The first time the query is advertised, don't do anything. The
393 * component will learn nodes in the slice through the manifest
394 * received through the other subscription
399 var initial = this.initial;
400 //var r_removed = []; //
401 /*-----------------------------------------------------------------------
402 TODO: remove this dirty hack !!!
404 resources = jQuery.map(resources, function(x){
405 if(!('timeslot' in x)){x.timeslot=0;}
409 TODO: handle generic keys instead of specific stuff
413 var initial_urn = $.map(initial, function(x){return x.urn;});
414 var resources_urn = $.map(resources, function(x){return x.urn;});
415 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
416 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
417 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
418 exists = false; // ioi
419 /*-----------------------------------------------------------------------*/
421 my_oTable.fnClearTable();
423 TODO: factorization of this code !!!
425 $.each(r_added, function(i, r) {
426 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
428 // Create the resource objects
433 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
434 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
437 newline.push(type, urn, slot, SPAN); // ioi
438 var line = my_oTable.fnAddData(newline);
439 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
440 nTr.className = type;
442 $.each(r_attached, function(i, r) {
443 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
444 var type = 'attached';
445 // Create the resource objects
450 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
451 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
454 newline.push(type, node, slot, SPAN); // ioi
455 var line = my_oTable.fnAddData(newline);
456 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
457 nTr.className = type;
459 $.each(r_removed, function(i, r) {
460 // The list contains objects
463 var time = r.timeslot;
465 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
466 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
469 newline.push('remove', node, slot, SPAN); // ioi
470 var line = my_oTable.fnAddData(newline);
471 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
472 nTr.className = 'remove';
475 this.current_resources = $.merge(r_attached,r_added);
477 /* Allow the user to update the slice */
478 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
480 }, // update_resources
484 $.plugin('ApplyPlugin', ApplyPlugin);