88d3ce74fc73019f47162f074efb8573455a11ac
[myslice.git] / plugins / univbrisvtamform / static / js / univbrisvtamform.js
1 /**
2  * Description: display a query result in a datatables-powered <table>
3  * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */ 
6
7 (function($){
8
9     vt_servers_list=[];
10     testbeds_list=[];
11
12     var UnivbrisVtamForm = Plugin.extend({
13
14         init: function(options, element) {
15             //console.log("hello....................");
16             this._super(options, element);
17             this.filters = Array(); 
18
19             // an internal buffer for records that are 'in' and thus need to be checked 
20             this.buffered_records_to_check = [];
21             // an internal buffer for keeping lines and display them in one call to fnAddData
22             this.buffered_lines = [];
23
24             
25
26             
27             //var keys = manifold.metadata.get_key(this.object);
28             //this.canonical_key = (keys && keys.length == 1) ? keys[0] : undefined;
29             // 
30             //this.init_key = this.options.init_key;
31             // have init_key default to canonical_key
32             //this.init_key = this.init_key || this.canonical_key;
33             // sanity check
34             //if ( ! this.init_key ) messages.warning ("UnivbrisVtamForm : cannot find init_key");
35             //if ( ! this.canonical_key ) messages.warning ("UnivbrisVtamForm : cannot find canonical_key");
36             //if (debug) messages.debug("UnivbrisVtamForm: canonical_key="+this.canonical_key+" init_key="+this.init_key);
37
38             /* Setup query and record handlers */
39             this.listen_query(options.query_uuid);
40            // this.listen_query(options.query_all_uuid, 'all');
41
42             /* GUI setup and event binding */
43
44
45             $('<button id="cancelvtamform" type="button" style="height: 25px; width: 200px" onclick="fnCancelVtamForm()">Cancel</button>').appendTo('#vtam_form_buttons');
46             $("#cancelvtamform").unbind('click').click(this, this.fnCancelVtamForm);
47
48             $('<button id="addvtamform" type="button" style="height: 25px; width: 200px" onclick="fnAddVtamForm()">Add VM</button>').appendTo('#vtam_form_buttons');
49             $("#addvtamform").unbind('click').click(this, this.fnAddVtamForm);
50
51            jQuery("#univbris_vtam_form").hide();
52
53            $('#uob_testbed_name').on("change", this.testbedChanged);
54
55            //this.on_new_record("urn:publicid:IDN+i2cat:vtam+node+Rodoreda");
56            //this.on_new_record("urn:publicid:IDN+bristol:vtam+node+March");
57            //this.on_new_record("urn:publicid:IDN+brazil:vtam+node+Verdaguer");
58            //this.on_query_done();
59            
60            // console.log("after");
61         },
62
63         testbedChanged: function(e){
64                 console.log($('#uob_testbed_name option:selected').text());
65
66                 $('#uob_virtualization_server')
67                         .find('option')
68                         .remove()
69                         .end();
70
71                 for(var i=0;i<vt_servers_list.length;i++){
72                     if(vt_servers_list[i].testbed==$('#uob_testbed_name option:selected').text()){
73                                 $('#uob_virtualization_server')
74                                         .append($("<option></option>")
75                                         .attr("value",vt_servers_list[i].name)
76                                         .text(vt_servers_list[i].name)); 
77                     }
78                 }
79         },
80
81
82         fnCancelVtamForm:function(e){
83                 jQuery("#univbris_vtam_form").hide();
84                 jQuery("#univbris_vtam").show();
85                 console.log("cancel vtam form");
86         },
87
88         fnAddVtamForm:function(e){
89                 console.log("add vtam form");
90                 var vmNameVal = /^[a-zA-Z0-9]+$/;
91                 //console.log($("#uob_vm_name").val());
92                 if (vmNameVal.test($("#uob_vm_name").val())){
93                         //build form string
94                         var vtam="testbed:"+$('#uob_testbed_name option:selected').text();
95                         vtam +=",vt_server:"+$('#uob_virtualization_server option:selected').text();
96                         vtam +=",vm_name:"+$("#uob_vm_name").val();
97                         var vm_name="<p id='"+vtam+"'>"+$("#uob_vm_name").val()+"</p>";
98                         var vtamtable = $("#univbris_vtam__table").dataTable();
99                         vtamtable.fnAddData([$('#uob_testbed_name option:selected').text(),$('#uob_virtualization_server option:selected').text(), vm_name,'<a class="delete" href="">Delete</a>']);
100                         jQuery("#univbris_vtam_form").hide();
101                         jQuery("#univbris_vtam").show();
102                 }
103                 else{
104                         $('#uob_vm_name').addClass('error');
105                         jQuery('#uob_vm_name_error').show();
106                 }
107         },
108
109         /* PLUGIN EVENTS */
110
111         on_show: function(e) {
112         },        
113
114         on_resize: function(e) {
115         },        
116
117         /* GUI EVENTS */
118
119         /* GUI MANIPULATION */
120
121         new_record: function(record)
122         {
123             var urn = record['urn'];
124             var pos = urn.search('vtam');
125             if (pos!=-1){
126                 ///line =new Array();
127                 var vt_server={};
128                 var com_ay=urn.split("+");
129                 vt_server['name']=com_ay[com_ay.length-1];
130                 var testbed_ay=com_ay[1].split(":");
131                 vt_server['testbed']=testbed_ay[0];
132                 this.add_new_vt_server(vt_server);
133                 this.add_new_testbed(testbed_ay[0]);
134                 //this.vt_servers_list.push(vt_server);
135             }
136         },
137
138         clear_table: function()
139         {
140         },
141
142         redraw_table: function()
143         {
144         },
145
146
147         add_new_vt_server:function(vt_server){
148                 var found=false;
149                 for(var i=0;i<vt_servers_list.length;i++){
150                         if(vt_servers_list[i].name==vt_server.name && vt_servers_list[i].testbed==vt_server.testbed){
151                                 found=true;
152                                 break;
153                         }
154                 }
155
156                 //console.log(found);
157
158                 if (found==false){
159                          vt_servers_list.push(vt_server);
160                 }
161         },
162
163         add_new_testbed:function(testbed){
164                 var found=false;
165                 for(var i=0;i<testbeds_list.length;i++){
166                         if(testbeds_list[i]==testbed){
167                                 found=true;
168                                 break;
169                         }
170                 }
171
172                 //console.log(found);
173
174                 if (found==false){
175                          testbeds_list.push(testbed);
176                 }
177         },
178
179
180
181         
182
183         /*************************** QUERY HANDLER ****************************/
184
185         on_filter_added: function(filter)
186         {
187         },
188
189         on_filter_removed: function(filter)
190         {
191         },
192         
193         on_filter_clear: function()
194         {
195             this.redraw_table();
196         },
197
198         on_field_added: function(field)
199         {
200             this.show_column(field);
201         },
202
203         on_field_removed: function(field)
204         {
205             this.hide_column(field);
206         },
207
208         on_field_clear: function()
209         {
210         },
211
212
213         /*************************** ALL QUERY HANDLER ****************************/
214
215         on_all_filter_added: function(filter)
216         {
217         },
218
219         on_all_filter_removed: function(filter)
220         {
221
222         },
223         
224         on_all_filter_clear: function()
225         {
226         },
227
228         on_all_field_added: function(field)
229         {
230         },
231
232         on_all_field_removed: function(field)
233         {
234         },
235
236         on_all_field_clear: function()
237         {
238         },
239
240
241         /*************************** RECORD HANDLER ***************************/
242
243         on_new_record: function(record)
244         {
245             //alert("record:")
246             console.log(record.hostname);
247             if (this.received_all_query) {
248                 
249             } else {
250                 this.buffered_records_to_check.push(record);
251             }
252         },
253
254         on_clear_records: function()
255         {
256         },
257
258         // Could be the default in parent
259         on_query_in_progress: function()
260         {
261             this.spin();
262         },
263
264         on_query_done: function()
265         {
266             this.received_query = true;
267             for(var i=0;i<this.buffered_records_to_check.length;i++){
268                 this.new_record(this.buffered_records_to_check[i]);
269             }
270
271         
272             /*$.each(vt_servers_list, function(key, value) {   
273                         $('#uob_testbed_name')
274                                 .append($("<option></option>")
275                                 .attr("value",key)
276                                 .text(value.testbed)); 
277             });*/
278
279              $('#uob_testbed_name')
280                         .find('option')
281                         .remove()
282                         .end()
283
284             for(var t=0;t<testbeds_list.length;t++){
285                 $('#uob_testbed_name')
286                                 .append($("<option></option>")
287                                 .attr("value",testbeds_list[t])
288                                 .text(testbeds_list[t])); 
289             }
290
291             $('#uob_virtualization_server')
292                         .find('option')
293                         .remove()
294                         .end()
295
296             for(var i=0;i<vt_servers_list.length;i++){
297                     if(vt_servers_list[i].testbed==$('#uob_testbed_name option:selected').text()){
298                                 $('#uob_virtualization_server')
299                                         .append($("<option></option>")
300                                         .attr("value",vt_servers_list[i].name)
301                                         .text(vt_servers_list[i].name)); 
302                     }
303             }
304          
305             this.unspin();
306         },
307         
308         on_field_state_changed: function(data)
309         {
310
311         },
312
313     }); //close plugin extend
314
315     $.plugin('UnivbrisVtamForm', UnivbrisVtamForm);
316
317 })(jQuery);
318
319
320
321