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('close').click(this, this.do_close);
50 //this.elmt('apply_cancel').click(this, this.do_cancel);
52 this.elmt('apply__window').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');
85 find_row: function(value)
87 var KEY_POS, VALUE_POS, data, object_type, object_key;
90 // key in third position, column id = 2
93 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
94 cur_value = col._aData[VALUE_POS];
95 object_type = col._aData[TYPE_POS];
96 object_key = manifold.metadata.get_key(object_type);
98 if (cur_value[0] == '{') {
99 cur_value = JSON.parse(cur_value);
100 return manifold._record_equals(cur_value, value, object_key);
102 return (cur_value == value);
106 if (cols.length == 0)
109 throw "Too many same-key rows in ResourceSelected plugin";
114 _do_update: function(e) {
119 do_update: function() {
121 var username = this.options.username;
124 console.log("do_update in progress");
126 manifold.raise_event(this.options.query_uuid, RUN_UPDATE);
128 // how to stop the spinning after the event?
129 // this should be triggered by some on_updatequery_done ?
133 do_close: function(e)
136 self.table.fnClearTable();
140 do_cancel: function(e)
142 throw 'queryupdater.do_clear_annotations Not implemented';
145 /**************************************************************************
147 **************************************************************************/
149 on_new_record: function(record)
152 // if (not and update) {
154 // initial['resource'], initial['lease'] ?
155 this.initial.push(record);
157 //this.set_record_state(record, RECORD_STATE_ATTACHED);
158 // We simply add to the table
160 // \ this.initial_resources
163 // current_resources \ YES | NO
164 // --------------------+----------+---------
165 // YES | attached | added
173 // +-----------------+--------------+
175 // +-------+ ?G +-------+ +-------+ +---+---+ |
176 // | | -----> | | !G | | | | DA |
177 // | ND | | PG | -----> | D | -----> | PC | <------+ |
178 // | | <----- | | ~G | | C | | | |
179 // +-------+ GE +-------+ +-------+ +-------+ +------+
181 // | DA UE | | ?U | PCA |
183 // +-------+ +-------+ +------+
185 // | AD | <----- | PU | --------+
187 // +-------+ +-------+
192 // Plugins (i) receive state information, (ii) perform actions
195 // ND : No data ?G : Get query
196 // PG : Pending Get !G : Get reply
197 // D : Data present ~G : Get partial reply
198 // PC : Pending changes GE : Get error
199 // PU : Pending update C : Change request
200 // PCA: Pending change with annotation R : Reset request
201 // AD : Annotated data ?U : Update query
203 // ~U : Update partial reply
205 // DA : Delete annotation request
207 // - D -> PU directly if the user chooses 'dynamic update'
208 // - Be careful for updates if partial Get success
210 // ND: No data == initialization state
213 // - on_query_in_progress
214 // NOTE: cannot distinguish get and update here. Is it important ?
216 on_query_in_progress: function()
221 on_query_done: function()
223 this.populate_table();
228 // - on_clear_records (Get)
229 // - on_new_record (shared with AD) XXX
231 // NOTE: cannot distinguish get and update here. Is it important ?
232 // NOTE: Would record key be sufficient for update ?
234 on_clear_records: function()
239 on_query_done: function()
244 // PC : Pending changes
245 // NOTE: record_key could be sufficient
246 on_added_record: function(record)
248 this.set_record_state(record, RECORD_STATE_ADDED);
249 // update pending number
252 on_removed_record: function(record_key)
254 this.set_record_state(RECORD_STATE_REMOVED);
257 // PU : Pending update
258 // - on_query_in_progress (already done)
260 // PCA : Pending change with annotation
261 // NOTE: Manifold will inform the plugin about updates, and thus won't
262 // call new record, even if the same channel UUID is used...
263 // - TODO on_updated_record
264 // - Key and confirmation could be sufficient, or key and record state
265 // XXX move record state to the manifold plugin API
267 on_field_state_changed: function(data)
270 if(result.request == FIELD_REQUEST_ADD){
271 this.selected_resources.push(result.value);
272 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
273 var i = this.selected_resources.indexOf(result.value);
275 this.selected_resources.splice(i,1);
278 this.set_state(result);
281 var action, msg, row, status, button = '';
286 // XXX other events missing !!
287 case STATE_VALUE_CHANGE_PENDING:
295 case STATE_SET_IN_PENDING:
298 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
301 case STATE_SET_OUT_PENDING:
304 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
309 // find line and delete it
310 row = this.find_row(data.value);
312 this.table.fnDeleteRow(row.nTr);
315 case STATE_SET_IN_SUCCESS:
319 case STATE_SET_IN_FAILURE:
324 case STATE_SET_OUT_SUCCESS:
328 case STATE_SET_OUT_FAILURE:
340 status = msg + status;
343 // if no, create it, else replace it
344 // XXX it's not just about adding lines, but sometimes removing some
345 // XXX how do we handle status reset ?
347 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
348 data.selected_resources = this.selected_resources;
349 row = this.find_row(data.value);
350 if (typeof(data.value) !== "string")
351 newline = [action, data.key, JSON.stringify(data.value), msg, button];
353 newline = [action, data.key, data.value, msg, button];
355 // XXX second parameter refresh = false can improve performance. todo in querytable also
356 this.table.fnAddData(newline);
357 row = this.find_row(data.value);
359 // Update row text...
360 this.table.fnUpdate(newline, row.nTr);
363 // Change cell color according to status
365 $(row.nTr).removeClass('add remove')
366 var cls = action.toLowerCase();
368 $(row.nTr).addClass(cls);
372 // XXX we will have the requests for change
373 // XXX + the requests to move into the query cycle = the buttons aforementioned
375 // XXX what happens in case of updates ? not implemented yet
376 // XXX we want resources and leases
377 // listen for SET_ADD and SET_REMOVE for slice query
379 /************************** PRIVATE METHODS ***************************/
381 _close_click: function(e)
385 //jQuery.publish('selected', 'add/'+key_value);
386 // this.parentNode is <td> this.parentNode.parentNode is <tr>
387 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
388 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
389 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
390 var remove_urn = this.id;
391 var current_resources = event.data.instance.current_resources;
392 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
393 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
394 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
397 /******************************** TODO ********************************/
399 update_resources: function(resources, change)
401 messages.debug("update_resources");
402 var my_oTable = this.table.dataTable();
403 var prev_resources = this.current_resources;
406 * The first time the query is advertised, don't do anything. The
407 * component will learn nodes in the slice through the manifest
408 * received through the other subscription
413 var initial = this.initial;
414 //var r_removed = []; //
415 /*-----------------------------------------------------------------------
416 TODO: remove this dirty hack !!!
418 resources = jQuery.map(resources, function(x){
419 if(!('timeslot' in x)){x.timeslot=0;}
423 TODO: handle generic keys instead of specific stuff
427 var initial_urn = $.map(initial, function(x){return x.urn;});
428 var resources_urn = $.map(resources, function(x){return x.urn;});
429 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
430 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
431 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
432 exists = false; // ioi
433 /*-----------------------------------------------------------------------*/
435 my_oTable.fnClearTable();
437 TODO: factorization of this code !!!
439 $.each(r_added, function(i, r) {
440 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
442 // Create the resource objects
447 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
448 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
451 newline.push(type, urn, slot, SPAN); // ioi
452 var line = my_oTable.fnAddData(newline);
453 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
454 nTr.className = type;
456 $.each(r_attached, function(i, r) {
457 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
458 var type = 'attached';
459 // Create the resource objects
464 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
465 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
468 newline.push(type, node, slot, SPAN); // ioi
469 var line = my_oTable.fnAddData(newline);
470 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
471 nTr.className = type;
473 $.each(r_removed, function(i, r) {
474 // The list contains objects
477 var time = r.timeslot;
479 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
480 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
483 newline.push('remove', node, slot, SPAN); // ioi
484 var line = my_oTable.fnAddData(newline);
485 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
486 nTr.className = 'remove';
489 this.current_resources = $.merge(r_attached,r_added);
491 /* Allow the user to update the slice */
492 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
494 }, // update_resources
498 $.plugin('ApplyPlugin', ApplyPlugin);