From 5c2d9ab59e9c31ddab1632edc5f7f6d2cce06096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 7 Aug 2015 13:43:44 +0200 Subject: [PATCH] Feature #3922: Fix resize dialog, move code to capacity-inputs The value were not filled correctly because a .change is needed after the .val call --- .../wizard-tabs/general/capacity-inputs.js | 58 ++++++++++++++++++- .../public/app/tabs/vms-tab/dialogs/resize.js | 20 +------ 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general/capacity-inputs.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general/capacity-inputs.js index 19bc2cd5c2..393c887232 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general/capacity-inputs.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general/capacity-inputs.js @@ -6,6 +6,7 @@ define(function(require) { require('foundation.slider'); var Locale = require('utils/locale'); var Tips = require('utils/tips'); + var WizardFields = require('utils/wizard-fields'); /* TEMPLATES @@ -19,7 +20,10 @@ define(function(require) { return { 'html': _html, - 'setup': _setup + 'setup': _setup, + 'fill': _fill, + 'retrieve': _retrieve, + 'retrieveResize': _retrieveResize }; /* @@ -165,4 +169,56 @@ define(function(require) { vcpu_slider.foundation('slider', 'set_value', 0); } + + /** + * Fills the capacity inputs + * @param {Object} context JQuery selector + * @param {Object} template VM or VMTemplate object + */ + function _fill(context, element) { + var fields = $('[wizard_field]', context); + + fields.each(function() { + var field_name = $(this).attr('wizard_field'); + $(this).data("original_value", element.TEMPLATE[field_name]); + }); + + WizardFields.fill(context, element.TEMPLATE); + } + + /** + * Retrieves the input values + * @param {Object} context JQuery selector + * @return {Object} If the input is not empty, returns: + * - CPU + * - MEMORY + * - VCPU + */ + function _retrieve(context) { + return WizardFields.retrieve(context); + } + + /** + * Retrieves the input values, but only if the value has changed from the + * original set in fill() + * @param {Object} context JQuery selector + * @return {Object} If the input has changed, returns: + * - CPU + * - MEMORY + * - VCPU + */ + function _retrieveResize(context) { + var templateJSON = WizardFields.retrieve(context); + + var fields = $('[wizard_field]', context); + + fields.each(function() { + var field_name = $(this).attr('wizard_field'); + if (templateJSON[field_name] == $(this).data("original_value")){ + delete templateJSON[field_name]; + } + }); + + return templateJSON; + } }); diff --git a/src/sunstone/public/app/tabs/vms-tab/dialogs/resize.js b/src/sunstone/public/app/tabs/vms-tab/dialogs/resize.js index 24553f3359..3a9e1de4fc 100644 --- a/src/sunstone/public/app/tabs/vms-tab/dialogs/resize.js +++ b/src/sunstone/public/app/tabs/vms-tab/dialogs/resize.js @@ -56,19 +56,7 @@ define(function(require) { Tips.setup(context); $('#' + DIALOG_ID + 'Form', context).submit(function() { - var templateJSON = WizardFields.retrieve(context); - - if (templateJSON["CPU"] == that.element.TEMPLATE.CPU) { - delete templateJSON["CPU"]; - }; - - if (templateJSON["MEMORY"] == that.element.TEMPLATE.MEMORY) { - delete templateJSON["MEMORY"]; - }; - - if (templateJSON["VCPU"] == that.element.TEMPLATE.VCPU) { - delete templateJSON["VCPU"]; - }; + var templateJSON = CapacityInputs.retrieveResize(context); var enforce = $("#enforce", this).is(":checked"); @@ -90,11 +78,7 @@ define(function(require) { function _onShow(context) { var that = this; $('#vm_id', context).text(that.element.ID); - $('#CPU', context).val(that.element.TEMPLATE.CPU); - $('#MEMORY_TMP', context).val(that.element.TEMPLATE.MEMORY); - if (that.element.VCPU) { - $('#VCPU', context).val(that.element.TEMPLATE.VCPU); - } + CapacityInputs.fill(context, that.element); context.foundation('slider', 'reflow');