Fix error when sorting Job list by object.
[ganeti_webmgr.git] / ganeti_web / static / js / modifyVM_formUpdater.js
blobcd25c3d95a5c3726a3739025491a20bb8e11db96
1 function modifyFormUpdater(nic_count_original) {
2     /* Functions for the modify form */
4     // Security Model and Domain
5     var security_domain = $("#id_security_domain").parent();
6     var security_model = $("#id_security_model");
8     // VNC TLS, x509 Path, and x509 Verify
9     var vnc_tls = $("#id_vnc_tls");
10     var vnc_x509_path = $("#id_vnc_x509_path");
11     var vnc_x509_path_field = vnc_x509_path.parent();
12     var vnc_x509_verify = $("#id_vnc_x509_verify");
14     // Tmp variables for path and verify
15     var tmp_vnc_x509_path = "";
16     var tmp_vnc_x509_verify = false;
18     // NICs
19     var nic_count = $("#id_nic_count");
20     var nics = $("#nics");
21     var nic_add =    $("#nics .add");
22     var nic_delete = $("#nics .delete");
24     this.init = function() {
25         // Hide vnc_x509_path and verify if checkbox enabled
26         if( !vnc_tls.is(':checked')) {
27             vnc_x509_path_field.hide();
28             vnc_x509_verify.parent().hide();
29         }
30         _initChangeHooks();
31     };
33     /**
34      * If security model is anything other than user
35      *  security domain is hidden.
36      */
37     security_model.change(function() {
38         if ($(this).children("option:selected").val() == 'user') {
39             security_domain.show();
40         } else {
41             security_domain.hide();
42         }
43     }).trigger('change');
45     /**
46      * Deselecting VNC TLS will save the state of
47      *  vnc_x509_verify, and value of vnc_x509_path,
48      *  before clearing both fields.
49      * Selecting VNC TLS will set these values back
50      *  to their original state and value before VNC
51      *  TLS was deselected.
52      */
53      vnc_tls.change(function() {
54         if ($(this).is(':checked')) {
55             // Set vnc_x509_path to origin path
56             vnc_x509_path.attr('value', tmp_vnc_x509_path);
57             // Change state of checkbox
58             if (tmp_vnc_x509_verify) {
59                 vnc_x509_verify.attr('checked', true);
60             }
61             // Show vnc_x509_path and vnc_x509_verify
62             vnc_x509_path_field.show();
63             vnc_x509_verify.parent().show();
64             // Show Error Messages
65             prev = vnc_x509_path_field.prev();
66             if (prev[0].tagName == 'UL') {
67                prev.show();
68             }
69         } else {
70             // Save path and checkbox state
71             tmp_vnc_x509_path = vnc_x509_path.val();
72             tmp_vnc_x509_verify = vnc_x509_verify.is(':checked');
73             // Set path to '' and disable checkbox state
74             vnc_x509_path.attr('value', '');
75             vnc_x509_verify.removeAttr('checked');
76             // Hide vnc_x509_path and vnc_x509_verify
77             vnc_x509_path_field.hide();
78             vnc_x509_verify.parent().hide();
79             // Hide Error messages
80             prev = vnc_x509_path_field.prev();
81             if (prev[0].tagName == 'UL') {
82                prev.hide();
83             }
84         }
85     });
87     function _initChangeHooks(){
88         /* setup change hooks for the form elements */
90         nic_add.click(_add_nic);
91         nic_delete.live("click",_remove_nic);
93         //XXX reset nic count, sometimes the browser remembers old values.
94         nic_count.val(nics.children('p').length);
95         //XXX hide delete buttons for everything but the last element
96         nics.children('.delete').not(':last').hide();
97     }
99     function _add_nic() {
100         var count = nic_count.val();
101         if (count < nic_count_original+1) {
102             nic_count.val(parseInt(count)+1);
103             var p = $('<p></p>');
104             var label = $("<label>NIC/" + count +"</label>");
105             var mac = $('<input type="text"/>');
106             mac.attr("name", "nic_mac_" + count);
107             mac.attr("id", "id_nic_mac_" + count);
108             var link = $("#nics input[name^=nic_link]").first().clone();
109             link.attr("name", "nic_link_" + count);
110             link.attr("id", "id_nic_link_" + count);
111             p.append(label);
112             p.append(mac);
113             p.append(link);
114             nics.append(p);
115             nics.append('<div class="icon delete"></div>');
116             
117             if (count == nic_count_original){
118                 nic_add.addClass('disabled');
119             }
120         }
121     }
123     function _remove_nic() {
124         /**
125          * Delete a nic.  If this is a nic currently on the virtualmachine it is
126          * just disabled.  Even if a new nic is added
127          */
128         var count = nic_count.val();
129         nic_count.val(parseInt(count)-1);
130         var button = $(this);
131         button.prev("p").remove();
132         button.prev("ul").remove();
133         button.remove();
135         if (count < nic_count_original+2){
136             nic_add.removeClass('disabled');
137         }
139         // renumber remaining disks
140         var i = 0;
141         $('#nics p').each(function(){
142             $(this).children('label').html("NIC/" + i);
143             $(this).children('input[name^=nic_link]').each(function(){
144                 $(this)
145                     .attr("name", "nic_link_" + i)
146                     .attr("id", "id_nic_link_" + i);
147             });
148             $(this).children('input[name^=nic_mac]').each(function(){
149                 $(this)
150                     .attr("name", "nic_mac_" + i)
151                     .attr("id", "id_nic_mac_" + i);
152             });
153             i++;
154         });
155     }