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;
123 console.log("do_update in progress");
125 manifold.raise_event(this.options.query_uuid, RUN_UPDATE);
127 // how to stop the spinning after the event?
128 // this should be triggered by some on_updatequery_done ?
132 do_close: function(e)
135 self.table.fnClearTable();
139 do_cancel: function(e)
141 throw 'queryupdater.do_clear_annotations Not implemented';
144 /**************************************************************************
146 **************************************************************************/
148 on_new_record: function(record)
151 // if (not and update) {
153 // initial['resource'], initial['lease'] ?
154 this.initial.push(record);
156 //this.set_record_state(record, RECORD_STATE_ATTACHED);
157 // We simply add to the table
159 // \ this.initial_resources
162 // current_resources \ YES | NO
163 // --------------------+----------+---------
164 // YES | attached | added
172 // +-----------------+--------------+
174 // +-------+ ?G +-------+ +-------+ +---+---+ |
175 // | | -----> | | !G | | | | DA |
176 // | ND | | PG | -----> | D | -----> | PC | <------+ |
177 // | | <----- | | ~G | | C | | | |
178 // +-------+ GE +-------+ +-------+ +-------+ +------+
180 // | DA UE | | ?U | PCA |
182 // +-------+ +-------+ +------+
184 // | AD | <----- | PU | --------+
186 // +-------+ +-------+
191 // Plugins (i) receive state information, (ii) perform actions
194 // ND : No data ?G : Get query
195 // PG : Pending Get !G : Get reply
196 // D : Data present ~G : Get partial reply
197 // PC : Pending changes GE : Get error
198 // PU : Pending update C : Change request
199 // PCA: Pending change with annotation R : Reset request
200 // AD : Annotated data ?U : Update query
202 // ~U : Update partial reply
204 // DA : Delete annotation request
206 // - D -> PU directly if the user chooses 'dynamic update'
207 // - Be careful for updates if partial Get success
209 // ND: No data == initialization state
212 // - on_query_in_progress
213 // NOTE: cannot distinguish get and update here. Is it important ?
215 on_query_in_progress: function()
219 on_query_done: function()
221 this.populate_table();
225 // - on_clear_records (Get)
226 // - on_new_record (shared with AD) XXX
228 // NOTE: cannot distinguish get and update here. Is it important ?
229 // NOTE: Would record key be sufficient for update ?
231 on_clear_records: function()
233 $('#applyloading').hide();
237 on_query_done: function()
239 $('#applyloading').hide();
242 // PC : Pending changes
243 // NOTE: record_key could be sufficient
244 on_added_record: function(record)
246 this.set_record_state(record, RECORD_STATE_ADDED);
247 // update pending number
250 on_removed_record: function(record_key)
252 this.set_record_state(RECORD_STATE_REMOVED);
255 // PU : Pending update
256 // - on_query_in_progress (already done)
258 // PCA : Pending change with annotation
259 // NOTE: Manifold will inform the plugin about updates, and thus won't
260 // call new record, even if the same channel UUID is used...
261 // - TODO on_updated_record
262 // - Key and confirmation could be sufficient, or key and record state
263 // XXX move record state to the manifold plugin API
265 on_field_state_changed: function(data)
268 if(result.request == FIELD_REQUEST_ADD){
269 this.selected_resources.push(result.value);
270 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
271 var i = this.selected_resources.indexOf(result.value);
273 this.selected_resources.splice(i,1);
276 this.set_state(result);
279 var action, msg, row, status, button = '';
284 // XXX other events missing !!
285 case STATE_VALUE_CHANGE_PENDING:
293 case STATE_SET_IN_PENDING:
296 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
299 case STATE_SET_OUT_PENDING:
302 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
307 // find line and delete it
308 row = this.find_row(data.value);
310 this.table.fnDeleteRow(row.nTr);
313 case STATE_SET_IN_SUCCESS:
317 case STATE_SET_IN_FAILURE:
322 case STATE_SET_OUT_SUCCESS:
326 case STATE_SET_OUT_FAILURE:
338 status = msg + status;
341 // if no, create it, else replace it
342 // XXX it's not just about adding lines, but sometimes removing some
343 // XXX how do we handle status reset ?
345 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
346 data.selected_resources = this.selected_resources;
347 row = this.find_row(data.value);
348 if (typeof(data.value) !== "string")
349 newline = [action, data.key, JSON.stringify(data.value), msg, button];
351 newline = [action, data.key, data.value, msg, button];
353 // XXX second parameter refresh = false can improve performance. todo in querytable also
354 this.table.fnAddData(newline);
355 row = this.find_row(data.value);
357 // Update row text...
358 this.table.fnUpdate(newline, row.nTr);
361 // Change cell color according to status
363 $(row.nTr).removeClass('add remove')
364 var cls = action.toLowerCase();
366 $(row.nTr).addClass(cls);
370 // XXX we will have the requests for change
371 // XXX + the requests to move into the query cycle = the buttons aforementioned
373 // XXX what happens in case of updates ? not implemented yet
374 // XXX we want resources and leases
375 // listen for SET_ADD and SET_REMOVE for slice query
377 /************************** PRIVATE METHODS ***************************/
379 _close_click: function(e)
383 //jQuery.publish('selected', 'add/'+key_value);
384 // this.parentNode is <td> this.parentNode.parentNode is <tr>
385 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
386 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
387 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
388 var remove_urn = this.id;
389 var current_resources = event.data.instance.current_resources;
390 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
391 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
392 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
395 /******************************** TODO ********************************/
397 update_resources: function(resources, change)
399 messages.debug("update_resources");
400 var my_oTable = this.table.dataTable();
401 var prev_resources = this.current_resources;
404 * The first time the query is advertised, don't do anything. The
405 * component will learn nodes in the slice through the manifest
406 * received through the other subscription
411 var initial = this.initial;
412 //var r_removed = []; //
413 /*-----------------------------------------------------------------------
414 TODO: remove this dirty hack !!!
416 resources = jQuery.map(resources, function(x){
417 if(!('timeslot' in x)){x.timeslot=0;}
421 TODO: handle generic keys instead of specific stuff
425 var initial_urn = $.map(initial, function(x){return x.urn;});
426 var resources_urn = $.map(resources, function(x){return x.urn;});
427 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
428 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
429 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
430 exists = false; // ioi
431 /*-----------------------------------------------------------------------*/
433 my_oTable.fnClearTable();
435 TODO: factorization of this code !!!
437 $.each(r_added, function(i, r) {
438 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
440 // Create the resource objects
445 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
446 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
449 newline.push(type, urn, slot, SPAN); // ioi
450 var line = my_oTable.fnAddData(newline);
451 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
452 nTr.className = type;
454 $.each(r_attached, function(i, r) {
455 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
456 var type = 'attached';
457 // Create the resource objects
462 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
463 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
466 newline.push(type, node, slot, SPAN); // ioi
467 var line = my_oTable.fnAddData(newline);
468 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
469 nTr.className = type;
471 $.each(r_removed, function(i, r) {
472 // The list contains objects
475 var time = r.timeslot;
477 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
478 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
481 newline.push('remove', node, slot, SPAN); // ioi
482 var line = my_oTable.fnAddData(newline);
483 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
484 nTr.className = 'remove';
487 this.current_resources = $.merge(r_attached,r_added);
489 /* Allow the user to update the slice */
490 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
492 }, // update_resources
496 $.plugin('ApplyPlugin', ApplyPlugin);