VTAM & OCF almost integrated
[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].urn)
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             //{"urn:publicid:IDN+vtam.univbris+node+uclalien":[{"name":"toto"}]}
95             var vtam='{"'+$('#uob_virtualization_server option:selected').val()+'":';
96             vtam+='[{"name":"'+$('#uob_vm_name').val()+'"}]}';
97                         //var vtam="testbed="+$('#uob_testbed_name option:selected').text();
98             // Use the URN instead of the hostname of the server
99                         //vtam +=",vt_server="+$('#uob_virtualization_server option:selected').val();
100                         //vtam +=",vm_name="+$("#uob_vm_name").val();
101                         var vm_name="<p id='"+vtam+"'>"+$("#uob_vm_name").val()+"</p>";
102                         var vtamtable = $("#univbris_vtam__table").dataTable();
103                         vtamtable.fnAddData([$('#uob_testbed_name option:selected').text(),$('#uob_virtualization_server option:selected').text(), vm_name,'<a class="delete" href="">Delete</a>']);
104                         jQuery("#univbris_vtam_form").hide();
105                         jQuery("#univbris_vtam").show();
106                 }
107                 else{
108                         $('#uob_vm_name').addClass('error');
109                         jQuery('#uob_vm_name_error').show();
110                 }
111         },
112
113         /* PLUGIN EVENTS */
114
115         on_show: function(e) {
116         },        
117
118         on_resize: function(e) {
119         },        
120
121         /* GUI EVENTS */
122
123         /* GUI MANIPULATION */
124
125         new_record: function(record)
126         {
127             var urn = record['urn'];
128             var pos = urn.search('vtam');
129             if (pos!=-1){
130                 ///line =new Array();
131                 var vt_server={};
132                 var com_ay=urn.split("+");
133                 vt_server['name']=com_ay[com_ay.length-1];
134                 var testbed_ay=com_ay[1].split(":");
135                 vt_server['testbed']=testbed_ay[0];
136                 vt_server['urn']=urn;
137                 this.add_new_vt_server(vt_server);
138                 this.add_new_testbed(testbed_ay[0]);
139                 //this.vt_servers_list.push(vt_server);
140             }
141         },
142
143         clear_table: function()
144         {
145         },
146
147         redraw_table: function()
148         {
149         },
150
151
152         add_new_vt_server:function(vt_server){
153                 var found=false;
154                 for(var i=0;i<vt_servers_list.length;i++){
155                         if(vt_servers_list[i].name==vt_server.name && vt_servers_list[i].testbed==vt_server.testbed){
156                                 found=true;
157                                 break;
158                         }
159                 }
160
161                 //console.log(found);
162
163                 if (found==false){
164                          vt_servers_list.push(vt_server);
165                 }
166         },
167
168         add_new_testbed:function(testbed){
169                 var found=false;
170                 for(var i=0;i<testbeds_list.length;i++){
171                         if(testbeds_list[i]==testbed){
172                                 found=true;
173                                 break;
174                         }
175                 }
176
177                 //console.log(found);
178
179                 if (found==false){
180                          testbeds_list.push(testbed);
181                 }
182         },
183
184
185
186         
187
188         /*************************** QUERY HANDLER ****************************/
189
190         on_filter_added: function(filter)
191         {
192         },
193
194         on_filter_removed: function(filter)
195         {
196         },
197         
198         on_filter_clear: function()
199         {
200             this.redraw_table();
201         },
202
203         on_field_added: function(field)
204         {
205             this.show_column(field);
206         },
207
208         on_field_removed: function(field)
209         {
210             this.hide_column(field);
211         },
212
213         on_field_clear: function()
214         {
215         },
216
217
218         /*************************** ALL QUERY HANDLER ****************************/
219
220         on_all_filter_added: function(filter)
221         {
222         },
223
224         on_all_filter_removed: function(filter)
225         {
226
227         },
228         
229         on_all_filter_clear: function()
230         {
231         },
232
233         on_all_field_added: function(field)
234         {
235         },
236
237         on_all_field_removed: function(field)
238         {
239         },
240
241         on_all_field_clear: function()
242         {
243         },
244
245
246         /*************************** RECORD HANDLER ***************************/
247
248         on_new_record: function(record)
249         {
250             //alert("record:")
251             console.log(record.hostname);
252             if (this.received_all_query) {
253                 
254             } else {
255                 this.buffered_records_to_check.push(record);
256             }
257         },
258
259         on_clear_records: function()
260         {
261         },
262
263         // Could be the default in parent
264         on_query_in_progress: function()
265         {
266             this.spin();
267         },
268
269         on_query_done: function()
270         {
271             this.received_query = true;
272             for(var i=0;i<this.buffered_records_to_check.length;i++){
273                 this.new_record(this.buffered_records_to_check[i]);
274             }
275
276         
277             /*$.each(vt_servers_list, function(key, value) {   
278                         $('#uob_testbed_name')
279                                 .append($("<option></option>")
280                                 .attr("value",key)
281                                 .text(value.testbed)); 
282             });*/
283
284              $('#uob_testbed_name')
285                         .find('option')
286                         .remove()
287                         .end()
288
289             for(var t=0;t<testbeds_list.length;t++){
290                 $('#uob_testbed_name')
291                                 .append($("<option></option>")
292                                 .attr("value",testbeds_list[t])
293                                 .text(testbeds_list[t])); 
294             }
295
296             $('#uob_virtualization_server')
297                         .find('option')
298                         .remove()
299                         .end()
300
301             for(var i=0;i<vt_servers_list.length;i++){
302                     if(vt_servers_list[i].testbed==$('#uob_testbed_name option:selected').text()){
303                                 $('#uob_virtualization_server')
304                                         .append($("<option></option>")
305                                         .attr("value",vt_servers_list[i].urn)
306                                         .text(vt_servers_list[i].name)); 
307                     }
308             }
309          
310             this.unspin();
311         },
312         
313         on_field_state_changed: function(data)
314         {
315
316         },
317
318     }); //close plugin extend
319
320     $.plugin('UnivbrisVtamForm', UnivbrisVtamForm);
321
322 })(jQuery);
323
324
325
326