2 * MySlice QueryUpdater 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
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 QueryUpdaterPlugin = 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.listen_query(options.query_uuid);
55 /*************************** PLUGIN EVENTS ****************************/
57 /***************************** GUI EVENTS *****************************/
59 /************************** GUI MANIPULATION **************************/
61 populate_table: function()
65 // Loop over records and display pending ones
66 manifold.query_store.iter_records(this.options.query_uuid, function (record_key, record) {
67 state = manifold.query_store.get_record_state(this.options.query_uuid, null, STATE_SET);
72 set_button_state: function(name, state)
74 this.elmt(name).attr('disabled', state ? false : 'disabled');
82 find_row: function(key)
84 // key in third position, column id = 2
87 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
88 return (col._aData[KEY_POS] == key);
94 throw "Too many same-key rows in ResourceSelected plugin";
99 do_update: function(e) {
102 var username = e.data.options.username;
103 var urn = data.value;
104 // XXX check that the query is not disabled
107 console.log("do_update");
108 // XXX check that the query is not disabled
109 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
111 // how to stop the spinning after the event?
112 // this should be triggered by some on_updatequery_done ?
118 throw 'queryupdater.do_reset Not implemented';
121 do_cancel: function(e)
123 throw 'queryupdater.do_clear_annotations Not implemented';
126 /**************************************************************************
128 **************************************************************************/
130 on_new_record: function(record)
133 // if (not and update) {
135 // initial['resource'], initial['lease'] ?
136 this.initial.push(record);
138 //this.set_record_state(record, RECORD_STATE_ATTACHED);
139 // We simply add to the table
141 // \ this.initial_resources
144 // current_resources \ YES | NO
145 // --------------------+----------+---------
146 // YES | attached | added
154 // +-----------------+--------------+
156 // +-------+ ?G +-------+ +-------+ +---+---+ |
157 // | | -----> | | !G | | | | DA |
158 // | ND | | PG | -----> | D | -----> | PC | <------+ |
159 // | | <----- | | ~G | | C | | | |
160 // +-------+ GE +-------+ +-------+ +-------+ +------+
162 // | DA UE | | ?U | PCA |
164 // +-------+ +-------+ +------+
166 // | AD | <----- | PU | --------+
168 // +-------+ +-------+
173 // Plugins (i) receive state information, (ii) perform actions
176 // ND : No data ?G : Get query
177 // PG : Pending Get !G : Get reply
178 // D : Data present ~G : Get partial reply
179 // PC : Pending changes GE : Get error
180 // PU : Pending update C : Change request
181 // PCA: Pending change with annotation R : Reset request
182 // AD : Annotated data ?U : Update query
184 // ~U : Update partial reply
186 // DA : Delete annotation request
188 // - D -> PU directly if the user chooses 'dynamic update'
189 // - Be careful for updates if partial Get success
191 // ND: No data == initialization state
194 // - on_query_in_progress
195 // NOTE: cannot distinguish get and update here. Is it important ?
197 on_query_in_progress: function()
202 on_query_done: function()
204 this.populate_table();
209 // - on_clear_records (Get)
210 // - on_new_record (shared with AD) XXX
212 // NOTE: cannot distinguish get and update here. Is it important ?
213 // NOTE: Would record key be sufficient for update ?
215 on_clear_records: function()
220 on_query_done: function()
225 // PC : Pending changes
226 // NOTE: record_key could be sufficient
227 on_added_record: function(record)
229 this.set_record_state(record, RECORD_STATE_ADDED);
230 // update pending number
233 on_removed_record: function(record_key)
235 this.set_record_state(RECORD_STATE_REMOVED);
238 // PU : Pending update
239 // - on_query_in_progress (already done)
241 // PCA : Pending change with annotation
242 // NOTE: Manifold will inform the plugin about updates, and thus won't
243 // call new record, even if the same channel UUID is used...
244 // - TODO on_updated_record
245 // - Key and confirmation could be sufficient, or key and record state
246 // XXX move record state to the manifold plugin API
248 on_field_state_changed: function(data)
251 if(result.request == FIELD_REQUEST_ADD){
252 this.selected_resources.push(result.value);
253 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
254 var i = this.selected_resources.indexOf(result.value);
256 this.selected_resources.splice(i,1);
259 this.set_state(result);
262 var action, msg, row, status, button = '';
267 // XXX other events missing !!
268 case STATE_VALUE_CHANGE_PENDING:
276 case STATE_SET_IN_PENDING:
279 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
282 case STATE_SET_OUT_PENDING:
285 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
290 // find line and delete it
291 row = this.find_row(data.value);
293 this.table.fnDeleteRow(row.nTr);
296 case STATE_SET_IN_SUCCESS:
297 case STATE_SET_OUT_SUCCESS:
301 case STATE_SET_IN_FAILURE:
302 case STATE_SET_OUT_FAILURE:
313 status = msg + status;
316 // if no, create it, else replace it
317 // XXX it's not just about adding lines, but sometimes removing some
318 // XXX how do we handle status reset ?
320 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
321 if (typeof(data.value) !== "string")
322 data.value = JSON.stringify(data.value);
323 data.selected_resources = this.selected_resources;
324 row = this.find_row(data.value);
325 newline = [action, data.key, data.value, msg, button];
327 // XXX second parameter refresh = false can improve performance. todo in querytable also
328 this.table.fnAddData(newline);
329 row = this.find_row(data.value);
331 // Update row text...
332 this.table.fnUpdate(newline, row.nTr);
335 // Change cell color according to status
337 $(row.nTr).removeClass('add remove')
338 var cls = action.toLowerCase();
340 $(row.nTr).addClass(cls);
344 // XXX we will have the requests for change
345 // XXX + the requests to move into the query cycle = the buttons aforementioned
347 // XXX what happens in case of updates ? not implemented yet
348 // XXX we want resources and leases
349 // listen for SET_ADD and SET_REMOVE for slice query
351 /************************** PRIVATE METHODS ***************************/
353 _close_click: function(e)
357 //jQuery.publish('selected', 'add/'+key_value);
358 // this.parentNode is <td> this.parentNode.parentNode is <tr>
359 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
360 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
361 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
362 var remove_urn = this.id;
363 var current_resources = event.data.instance.current_resources;
364 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
365 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
366 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
369 /******************************** TODO ********************************/
371 update_resources: function(resources, change)
373 messages.debug("update_resources");
374 var my_oTable = this.table.dataTable();
375 var prev_resources = this.current_resources;
378 * The first time the query is advertised, don't do anything. The
379 * component will learn nodes in the slice through the manifest
380 * received through the other subscription
385 var initial = this.initial;
386 //var r_removed = []; //
387 /*-----------------------------------------------------------------------
388 TODO: remove this dirty hack !!!
390 resources = jQuery.map(resources, function(x){
391 if(!('timeslot' in x)){x.timeslot=0;}
395 TODO: handle generic keys instead of specific stuff
399 var initial_urn = $.map(initial, function(x){return x.urn;});
400 var resources_urn = $.map(resources, function(x){return x.urn;});
401 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
402 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
403 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
404 exists = false; // ioi
405 /*-----------------------------------------------------------------------*/
407 my_oTable.fnClearTable();
409 TODO: factorization of this code !!!
411 $.each(r_added, function(i, r) {
412 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
414 // Create the resource objects
419 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
420 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
423 newline.push(type, urn, slot, SPAN); // ioi
424 var line = my_oTable.fnAddData(newline);
425 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
426 nTr.className = type;
428 $.each(r_attached, function(i, r) {
429 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
430 var type = 'attached';
431 // Create the resource objects
436 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
437 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
440 newline.push(type, node, slot, SPAN); // ioi
441 var line = my_oTable.fnAddData(newline);
442 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
443 nTr.className = type;
445 $.each(r_removed, function(i, r) {
446 // The list contains objects
449 var time = r.timeslot;
451 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
452 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
455 newline.push('remove', node, slot, SPAN); // ioi
456 var line = my_oTable.fnAddData(newline);
457 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
458 nTr.className = 'remove';
461 this.current_resources = $.merge(r_attached,r_added);
463 /* Allow the user to update the slice */
464 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
466 }, // update_resources
470 $.plugin('QueryUpdaterPlugin', QueryUpdaterPlugin);