1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-12 09:17:41 +03:00

B #4263: Fix form behavior in oneflow templates (#4264)

This commit is contained in:
Sergio Betanzos 2020-02-27 11:25:54 +01:00 committed by GitHub
parent 9b271c3b14
commit 58f90345e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 132 additions and 80 deletions

View File

@ -413,71 +413,69 @@ define(function(require) {
//----------------------------------------------------------------------------
function _redo_service_networks_selector(dialog, that){
function _redo_service_networks_selector(dialog, template){
$('#roles_tabs_content .role_content', dialog).each(function(){
var role_section = this;
_redo_service_networks_selector_role(dialog, role_section);
$(Object.values(that.roleTabObjects)).each(function(_, section) {
section && section.refresh(role_section);
});
_redo_service_networks_selector_role(dialog, role_section, template);
});
}
function _redo_service_networks_selector_role(dialog, role_section){
$('#roles_tabs_content .role_content', dialog).each(function(){
var role_section = this;
var selected_networks = [];
$(".service_network_checkbox:checked", role_section).each(function(){
selected_networks.push($(this).val());
});
$(".networks_role", role_section).hide();
$(".networks_role_rdp", role_section).hide();
var service_networks = false;
var role_tab_id = $(role_section).attr('id');
var str = "";
$(".service_networks .service_network_name", dialog).each(function(index, input){
var pattern = $("input.service_network_name").not(input).map(function(_, v) {
return $(v).val();
}).get().join("|");
$(this).attr("pattern", "^(?!"+pattern+")(\\w+)$");
var valueNetwork = $(this).val();
var regexp = new RegExp("^(?!"+pattern+")(\\w+)$", "gi");
if (valueNetwork && valueNetwork.match(regexp)) {
service_networks = true;
var idNetwork = role_tab_id + "_" + index;
var idName = idNetwork + "_name";
str += "<tr id='"+idNetwork+"'>\
<td style='width:10%'>\
<input class='service_network_checkbox check_item'\
type='checkbox' value='"+valueNetwork+"' id='"+idName+"' data-index='"+index+"'/>\
</td>\
<td>\
<label for='"+idName+"'>"+valueNetwork+"</label>\
</td>\
</tr>";
}
});
$(".networks_role_body", role_section).html(str);
if (service_networks) {
$(".networks_role", role_section).show();
$(".networks_role_rdp", role_section).show();
}
$.each(selected_networks, function(){
$(".service_network_checkbox[value='"+this+"']", role_section).attr('checked', true).change();
});
function _redo_service_networks_selector_role(dialog, role_section, template){
var selected_networks = [];
$(".service_network_checkbox:checked", role_section).each(function(){
selected_networks.push($(this).val());
});
$(".networks_role", role_section).hide();
$(".networks_role_rdp", role_section).hide();
var service_networks = false;
var role_tab_id = $(role_section).attr('id');
var str = "";
$(".service_networks .service_network_name", dialog).each(function(index, input){
var othersNames = $("input.service_network_name").not(input).map(function(_, v) {
return $(v).val();
}).get().join("|");
$(this).attr("pattern", "^(?!"+othersNames+")(\\w+)$");
var name = $(this).val();
var regexp = new RegExp("^(?!"+othersNames+")(\\w+)$", "gi");
if (name && othersNames === "" || name.match(regexp)) {
service_networks = true;
var idNetwork = role_tab_id + "_" + index;
var idName = idNetwork + "_name";
str += "<tr id='"+idNetwork+"'>\
<td style='width:10%'>\
<input class='service_network_checkbox check_item'\
type='checkbox' value='"+name+"' id='"+idName+"' data-index='"+index+"'/>\
</td>\
<td>\
<label for='"+idName+"'>"+name+"</label>\
</td>\
</tr>";
}
});
$(".networks_role_body", role_section).html(str);
if (service_networks) {
$(".networks_role", role_section).show();
$(".networks_role_rdp", role_section).show();
}
$.each(selected_networks, function(){
$(".service_network_checkbox[value='"+this+"']", role_section).attr('checked', true);
});
if (template && template.roleTabObjects) {
$(Object.values(template.roleTabObjects)).each(function(_, section) {
section && section.refresh(role_section);
});
}
}
function _add_role_tab(role_id, dialog) {
@ -492,7 +490,7 @@ define(function(require) {
role_tab.html() +
'</div>').appendTo($("#roles_tabs_content", dialog));
_redo_service_networks_selector_role(dialog, role_section);
_redo_service_networks_selector_role(dialog, role_section, that);
Tips.setup(role_section);

View File

@ -104,7 +104,7 @@ define(function(require) {
// Network values
var index = $(this).data("index");
var name = $(this).val();
updateOptionsRDP(that.global_template, role_section);
(this.checked)
@ -114,6 +114,15 @@ define(function(require) {
updateTextareaTemplate(role_section, that.global_template);
});
role_section.on("change", ".vm_template_contents", role_section, function(){
var value = $(this).val();
var diff = diffWithTemplate(value, that.global_template);
updateExtraTemplate(role_section, that.global_template, diff)
updateTextareaTemplate(role_section, that.global_template);
});
role_section.on("change", ".networks_role_rdp select#rdp", role_section, function(){
var valueSelected = this.value;
@ -127,17 +136,39 @@ define(function(require) {
});
if (valueSelected !== "" && that.global_template[valueSelected]) {
that.global_template[valueSelected]["RDP"] = "yes";
that.global_template[valueSelected]["RDP"] = "yes";
}
updateTextareaTemplate(role_section, that.global_template);
});
}
function updateExtraTemplate(roleSection, currentTemplate, extraValue) {
var checkboxes = $(".service_network_checkbox", roleSection);
var lastIndex = checkboxes.length > 100 ? checkboxes.length : 100;
currentTemplate[lastIndex] = String(extraValue);
}
function diffWithTemplate(newValue, currentTemplate) {
// map template with index checkbox
var sortedTemplate = TemplateUtils.templateToString({
NIC: Object.values(currentTemplate).filter(function(nic) {
return typeof nic === "object"
})
})//.concat(Object.values(currentTemplate).filter(rest => typeof rest !== "object"));
var sortedValue = TemplateUtils.templateToString(TemplateUtils.stringToTemplate(newValue))
return sortedValue.split("").filter(function(c, i) {
return sortedTemplate.split("")[i] !== c
}).join("");
}
function updateTextareaTemplate(roleContext, currentTemplate) {
// create a correct template with new changes
var newTemplate = TemplateUtils.templateToString({
NIC: Object.values(currentTemplate)
});
NIC: Object.values(currentTemplate).filter(nic => typeof nic === "object")
}).concat(Object.values(currentTemplate).filter(rest => typeof rest !== "object"));
var textareaTemplate = $(".vm_template_contents", roleContext);
textareaTemplate.val(newTemplate);
@ -259,17 +290,24 @@ define(function(require) {
}
});
// map template
var template = TemplateUtils.stringToTemplate(value.vm_template_contents)["NIC"];
$(network_names).each(function(index, name) {
var nicTemplate = $.grep(template, function(nic) {
return (nic["NETWORK_ID"] === "$"+name) && nic;
})
if (nicTemplate && nicTemplate[0]) {
that.global_template[String(index)] = nicTemplate[0];
}
});
// map template with index checkbox
var nics = TemplateUtils.stringToTemplate(value.vm_template_contents)["NIC"];
if (nics && Array.isArray(nics)) {
$(network_names).each(function(index, name) {
var nicTemplate = $.grep(nics, function(nic) {
return (nic["NETWORK_ID"] === "$"+name) && nic;
})
if (nicTemplate && nicTemplate[0]) {
that.global_template[String(index)] = nicTemplate[0];
}
});
}
// add extra of strings at the end of template
var extraTemplate = diffWithTemplate(value.vm_template_contents, that.global_template);
updateExtraTemplate(context, that.global_template, extraTemplate);
// update textarea template content
updateTextareaTemplate(context, that.global_template);
@ -328,12 +366,28 @@ define(function(require) {
}
function _refreshVMTemplate(role_section) {
var that = this;
$.grep($(".service_network_checkbox:not(:checked)", role_section), function (nic) {
delete that.global_template[$(nic).data("index")];
var selected_networks = [];
$(".service_network_checkbox:checked", role_section).each(function(){
selected_networks.push($(this).val());
});
updateTextareaTemplate(role_section, that.global_template);
Object.values(this.global_template)
.filter(function(nic) { return typeof nic === "object" })
.reduce(function(acc, nic, indexKey) {
if (!selected_networks.find(
function(net) {
return nic && nic.NETWORK_ID && nic.NETWORK_ID === "$"+net
})
) {
acc.push(indexKey);
}
return acc;
}, [])
.forEach(function(index) {
delete this.global_template[index];
}, this);
updateTextareaTemplate(role_section, this.global_template);
}
//----------------------------------------------------------------------------