[Kimchi-devel] [PATCH] [Kimchi 3/9] template_edit_main.js: initProcessor now a global function

dhbarboza82 at gmail.com dhbarboza82 at gmail.com
Thu Nov 24 16:10:08 UTC 2016


From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>

This patch makes the initProcessor function declared inside
template_edit_main.js a global function called
kimchi.init_processor_tab. The idea is to avoid code repetition
when the same tab is used by other modals (Guest Edit, for
example).

To make the function generic, references of 'template' inside
ids of init_processor_tab were removed. Changes in
template-edit.html.tmpl were required to make the tab work with
this new function.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 ui/js/src/kimchi.template_edit_main.js | 159 +++++++++++++++++----------------
 ui/pages/template-edit.html.tmpl       |   2 +-
 2 files changed, 85 insertions(+), 76 deletions(-)

diff --git a/ui/js/src/kimchi.template_edit_main.js b/ui/js/src/kimchi.template_edit_main.js
index be53701..6fd257b 100644
--- a/ui/js/src/kimchi.template_edit_main.js
+++ b/ui/js/src/kimchi.template_edit_main.js
@@ -15,6 +15,88 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+kimchi.init_processor_tab = function(cpu_info) {
+    var setCPUValue = function(){
+        if (!$('#sockets').hasClass("invalid-field") &&
+            !$('#cores').hasClass("invalid-field") &&
+            $('#sockets').val()!="" && $('#cores').val()!="") {
+
+            var sockets = parseInt($("#sockets").val());
+            var cores = parseInt($("#cores").val());
+            var threads = parseInt($("#threads").val());
+
+            var computedCpu = sockets * cores * threads;
+            $("#vcpus").val(computedCpu);
+            if ($("#cpus-check").prop("checked")) {
+                //If topology is checked, set maxcpu to be the same as # of cpu otherwise, backend gives error
+                $("#guest-edit-max-processor-textbox").val(computedCpu);
+            }
+        } else {
+            $("#vcpus").val('');
+        }
+    };
+
+    $("input:text", "#form-edit-processor").on('keyup', function() {
+        $(this).toggleClass("invalid-field", !$(this).val().match('^[0-9]*$'));
+        if ($(this).prop('id') == 'sockets' ||
+        $(this).prop('id') == 'cores') {
+            setCPUValue();
+        }
+    });
+
+    $("input:checkbox", "#form-edit-processor").click(function() {
+        $('#threads').selectpicker();
+        $(".topology", "#form-edit-processor").slideToggle();
+        $("#vcpus").attr("disabled", $(this).prop("checked"));
+        $("#guest-edit-max-processor-textbox").attr("disabled", $(this).prop("checked"));
+        setCPUValue();
+    });
+
+    $('#threads').change(function() {
+        setCPUValue();
+    });
+
+    kimchi.getCPUInfo(function(data) {
+        var options = "";
+        var topo = cpu_info.topology;
+
+        for (var i = 0; Math.pow(2, i) <= data.threads_per_core; i++) {
+            var lastOne = Math.pow(2, i + 1) > data.threads_per_core ? " selected" : "";
+            options += "<option" + lastOne + ">" + Math.pow(2, i) + "</option>";
+         }
+
+        $('#threads').append(options);
+
+        if (cpu_info.vcpus) {
+            $("#vcpus").val(cpu_info.vcpus);
+        }
+        if (cpu_info.maxvcpus) {
+            $("#guest-edit-max-processor-textbox").val(cpu_info.maxvcpus);
+        }
+        if (topo && topo.sockets) {
+            $("#sockets").val(topo.sockets);
+        }
+        if (topo && topo.cores) {
+            $("#cores").val(topo.cores);
+        }
+        if (topo && topo.threads) {
+            $('#threads').val(topo.threads);
+            $('#threads').selectpicker();
+            $("input:checkbox", "#form-edit-processor").trigger('click');
+        }
+    });
+
+    $('#guest-show-max-processor').on('click', function(e) {
+        e.preventDefault;
+        $('#guest-max-processor-panel').slideToggle();
+        var text = $('#guest-show-max-processor span.text').text();
+        $('#guest-show-max-processor span.text').text(text == i18n['KCHVMED6008M'] ? i18n['KCHVMED6009M'] : i18n['KCHVMED6008M']);
+        $('#guest-show-max-processor i.fa').toggleClass('fa-plus-circle fa-minus-circle');
+    });
+};
+
+
 kimchi.template_edit_main = function() {
     var templateEditMain = $('#edit-template-tabs');
     var origDisks;
@@ -268,79 +350,6 @@ kimchi.template_edit_main = function() {
             });
         };
 
-        var initProcessor = function() {
-            var setCPUValue = function() {
-                if(!$('#sockets').hasClass("invalid-field") &&
-                   !$('#cores').hasClass("invalid-field") &&
-                   $('#sockets').val()!="" && $('#cores').val()!=""){
-
-                    var sockets = parseInt($("#sockets").val());
-                    var cores = parseInt($("#cores").val());
-                    var threads = parseInt($("#threads").val());
-
-                    var computedCpu = sockets * cores * threads;
-
-                    $("#vcpus").val(computedCpu);
-                    if ($("#cpus-check").prop("checked")) {
-                        //If topology is checked, set maxcpu to be the same as # of cpu otherwise, backend gives error
-                        $("#guest-edit-max-processor-textbox").val(computedCpu);
-                    }
-                } else {
-                    $("#vcpus").val('');
-                }
-            };
-            $("input:text", "#form-template-processor").on('keyup', function() {
-                $(this).toggleClass("invalid-field", !$(this).val().match('^[0-9]*$'));
-                if ($(this).prop('id') == 'sockets' ||
-                    $(this).prop('id') == 'cores') {
-                        setCPUValue();
-                }
-            });
-            $("input:checkbox", "#form-template-processor").click(function() {
-                $('#threads').selectpicker();
-                $(".topology", "#form-template-processor").slideToggle();
-                $("#vcpus").attr("disabled", $(this).prop("checked"));
-                $("#guest-edit-max-processor-textbox").attr("disabled", $(this).prop("checked"));
-                setCPUValue();
-            });
-            $('#threads').change(function() {
-                setCPUValue();
-            });
-            kimchi.getCPUInfo(function(data) {
-                var options = "";
-                var topo = template.cpu_info.topology;
-                for (var i = 0; Math.pow(2, i) <= data.threads_per_core; i++) {
-                    var lastOne = Math.pow(2, i + 1) > data.threads_per_core ? " selected" : "";
-                    options += "<option" + lastOne + ">" + Math.pow(2, i) + "</option>";
-                }
-                $('#threads').append(options);
-                if (template.cpu_info.vcpus) {
-                    $("#vcpus").val(template.cpu_info.vcpus);
-                }
-                if (template.cpu_info.maxvcpus) {
-                    $("#guest-edit-max-processor-textbox").val(template.cpu_info.maxvcpus);
-                }
-                if (topo && topo.sockets) {
-                    $("#sockets").val(topo.sockets);
-                }
-                if (topo && topo.cores) {
-                    $("#cores").val(topo.cores);
-                }
-                if (topo && topo.threads) {
-                    $('#threads').val(topo.threads);
-                    $('#threads').selectpicker();
-                    $("input:checkbox", "#form-template-processor").trigger('click');
-                }
-            });
-            $('#guest-show-max-processor').on('click', function(e) {
-                e.preventDefault;
-                $('#guest-max-processor-panel').slideToggle();
-                var text = $('#guest-show-max-processor span.text').text();
-                $('#guest-show-max-processor span.text').text(text == i18n['KCHVMED6008M'] ? i18n['KCHVMED6009M'] : i18n['KCHVMED6008M']);
-                $('#guest-show-max-processor i.fa').toggleClass('fa-plus-circle fa-minus-circle');
-            });
-        };
-
         var checkInvalids = function() {
             $.each(template.invalid, function(key, value) {
                 if (key === 'cdrom' || key === 'vm-image') {
@@ -356,7 +365,7 @@ kimchi.template_edit_main = function() {
 
         kimchi.listNetworks(initInterface);
         kimchi.listStoragePools(initStorage);
-        initProcessor();
+        kimchi.init_processor_tab(template.cpu_info);
         checkInvalids();
     };
     kimchi.retrieveTemplate(kimchi.selectedTemplate, initTemplate);
@@ -424,7 +433,7 @@ kimchi.template_edit_main = function() {
             data['cdrom'] = $('.tab-content input[name="cdrom"]').val();
         }
 
-        if ($("input:checkbox", "#form-template-processor").prop("checked")) {
+        if ($("input:checkbox", "#form-edit-processor").prop("checked")) {
             //Check if maxCpu field has a value
             data['cpu_info'] = {
                 vcpus: cpu,
diff --git a/ui/pages/template-edit.html.tmpl b/ui/pages/template-edit.html.tmpl
index f1a0eee..5d712d3 100644
--- a/ui/pages/template-edit.html.tmpl
+++ b/ui/pages/template-edit.html.tmpl
@@ -131,7 +131,7 @@
                     </form>
                 </div>
                 <div role="tabpanel" class="tab-pane" id="processor">
-                    <form id="form-template-processor">
+                    <form id="form-edit-processor">
                         <div class="form-group">
                             <div id="guest-processor">
                                 <label for="vcpus">$_("Current CPU Number")</label>
-- 
2.7.4




More information about the Kimchi-devel mailing list