From b10dc84a2b4caa17150f021b2e2e58aef910606a Mon Sep 17 00:00:00 2001 From: juanmont <jjmontiel@opennebula.org> Date: Fri, 7 Apr 2017 11:08:15 +0200 Subject: [PATCH] Bug 5042 (#250) * Added new functions to TemplateUtils to convert string to json and merge 2 templates * B #5042 merged of 2 templates, advanced and wizard --- .../form-panels/create-common.js | 20 +-- .../public/app/utils/template-utils.js | 117 ++++++++++++++++++ 2 files changed, 130 insertions(+), 7 deletions(-) diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create-common.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create-common.js index cc320856b0..4e9653669b 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create-common.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create-common.js @@ -1,4 +1,4 @@ -/* -------------------------------------------------------------------------- */ + /* -------------------------------------------------------------------------- */ /* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); you may */ @@ -18,7 +18,7 @@ define(function(require) { /* DEPENDENCIES */ - + var Notifier = require('utils/notifier'); var BaseFormPanel = require('utils/form-panels/form-panel'); var Sunstone = require('sunstone'); var Locale = require('utils/locale'); @@ -194,24 +194,30 @@ define(function(require) { function _submitWizard(context) { var templateJSON = this.retrieve(context); + var templateStr = $('textarea#template', $("form#createVMTemplateFormAdvanced")).val(); + var template_final = TemplateUtils.mergeTemplates(templateJSON, templateStr); if (this.action == "create") { - Sunstone.runAction(this.resource+".create", {'vmtemplate': templateJSON}); + Sunstone.runAction(this.resource+".create", {'vmtemplate': template_final}); return false; } else if (this.action == "update") { - Sunstone.runAction(this.resource+".update", this.resourceId, TemplateUtils.templateToString(templateJSON)); + Sunstone.runAction(this.resource+".update", this.resourceId, TemplateUtils.templateToString(template_final)); return false; } } function _submitAdvanced(context) { - var template = $('textarea#template', context).val(); + var templateStr = $('textarea#template', context).val(); + var templateJSON = this.retrieve($("form#createVMTemplateFormWizard")); + var template_final = TemplateUtils.mergeTemplates(templateStr, templateJSON, true); + template_final = TemplateUtils.templateToString(template_final); + if (this.action == "create") { - Sunstone.runAction(this.resource+".create", {"vmtemplate": {"template_raw": template}}); + Sunstone.runAction(this.resource+".create", {"vmtemplate": {"template_raw": template_final}}); return false; } else if (this.action == "update") { - Sunstone.runAction(this.resource+".update", this.resourceId, template); + Sunstone.runAction(this.resource+".update", this.resourceId, template_final); return false; } } diff --git a/src/sunstone/public/app/utils/template-utils.js b/src/sunstone/public/app/utils/template-utils.js index 7c5d4c1b6c..734bc70956 100644 --- a/src/sunstone/public/app/utils/template-utils.js +++ b/src/sunstone/public/app/utils/template-utils.js @@ -18,6 +18,7 @@ define(function(require) { var Locale = require('utils/locale'); var Sunstone = require('sunstone'); + var Notifier = require('utils/notifier'); //Escape doublequote in a string and return it function _escapeDoubleQuotes(string) { @@ -92,7 +93,123 @@ define(function(require) { return template_str; } + function _merge_templates(template_master, template_slave, advanced){ + if(!advanced) + template_slave = _convert_string_to_template(template_slave); + else + template_master = _convert_string_to_template(template_master); + if((advanced && template_master) || (!advanced && template_slave)){ + var template_final = {}; + $.extend(true, template_final, template_slave, template_master); + return template_final; + }else{ + Notifier.notifyError(Locale.tr("Advanced template malformed")); + } + return template_master; + } + // Transforms an object to an opennebula template string + function _convert_string_to_template(string_json, unshown_values) { + string_json = string_json.split("\n").join(" "); + string_json = string_json.split(" ").join(" "); + string_json = string_json.split(" ").join(" "); + var match_symbols = "=[,]" + var template_json = {}; + var array_string = string_json.split(" "); + var i = 0; + var array = false; + while(i < array_string.length-1){ + if(!array_string[i].match('"') && !array_string[i].match(match_symbols)){ //is key + var key = array_string[i]; + if(template_json[key]){ //exists key, generate Array + if(!Array.isArray(template_json[key])){ + var obj = template_json[key]; + template_json[key] = []; + template_json[key].push(obj); + } + array = true; + } + else{ + array = false; + } + template_json[key]; + i+=1; + if(array_string[i] == "="){ + i+=1; + if(array_string[i] != "["){ + var value = ""; + if(key == "DESCRIPTION" && array_string[i][0] == '"' && array_string[i][array_string[i].length-1] != '"'){ + while (array_string[i][array_string[i].length-1] != '"' && i < array_string.length-1){ + value += array_string[i] + " "; + i+=1; + } + if(!value.match("=")) + value = value.split('"').join(""); + else{ + value = value .slice(0,-1); + value = value .slice(1); + } + if(array){ + template_json[key].push(value); + }else{ + template_json[key] = value; + } + i+=1; + } + else if(array_string[i].match('"')){ + value = array_string[i]; + if(!value.match("=")) + value = value.split('"').join(""); + else{ + value = value .slice(0,-1); + value = value .slice(1); + } + if(array){ + template_json[key].push(value); + }else{ + template_json[key] = value; + } + i+=1; + } + else return false; + }else{ + var obj = {} + i+=1; + while(array_string[i] != ']' && i < array_string.length-1){ + var sub_key; + if(!array_string[i].match('"') && !array_string[i].match(match_symbols)){ + sub_key = array_string[i]; + i+=1; + if(array_string[i] == "="){ + i+=1; + if(array_string[i].match('"')){ + if(array_string[i][array_string[i].length-1] == ","){ + array_string[i] = array_string[i].slice(0,-1); + } + var value = array_string[i]; + obj[sub_key] = value; + obj[sub_key] = obj[sub_key].split('"').join(""); + i+=1; + }else return false; + }else return false; + }else return false; + } + if(array){ + template_json[key].push(obj); + }else{ + template_json[key] = {}; + template_json[key] = obj; + } + i+=1; + } + }else return false; + }else return false; + } + return template_json; + } + return { + 'mergeTemplates' : _merge_templates, + 'stringToTemplate': _convert_string_to_template, 'templateToString': _convert_template_to_string, 'htmlDecode': _htmlDecode, 'htmlEncode': _htmlEncode,