Skip to content

Commit

Permalink
Merge pull request #1225 from juhijariwala/simplify-environment-varia…
Browse files Browse the repository at this point in the history
…bles

Fix for the bug #1216 (related to #194)
  • Loading branch information
mdaliejaz committed Jun 17, 2015
2 parents 0dc1833 + ee8b4d1 commit 2732069
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 139 deletions.
Expand Up @@ -79,3 +79,35 @@ var EnvironmentVariableEdit = function (options) {
resetLink.toggle();
})
};

var EnvironmentVariableAddRemove = function(parentElement, options) {
var $ = jQuery;

options = $.extend({}, options);

var addButton = $(parentElement).find('.add_item');
var template = $(parentElement).find('.template').html();
var addRowTo = $(parentElement).find('.variables tbody');

// add a blank row
addRowTo.append(template);

addButton.on('click.environmentVariableTemplateCopy', function(evt){
evt.preventDefault();
addRowTo.append(template);
if(options.onAdd){
options.onAdd(addButton);
}
});

$(parentElement).on('click.environmentVariableTemplateCopy', '.delete_parent', function(evt){
evt.preventDefault();
var deleteLink = $(evt.target);
deleteLink.parents('.environment-variable-edit-row').remove();
if(options.onRemove){
options.onRemove(deleteLink);
}

});

};
Expand Up @@ -2311,6 +2311,7 @@ label.label_with_help {

.environment_variables_section #add_variables{
float: none;
margin: 0;
}


Expand Down
@@ -1,109 +1,102 @@
<div class="form_content">
<div class="environment_variables_section popup_form">
<% template_row_id_prefix = scope[:collection_name].to_s + (scope[:secure] ? "_secure" : "") %>
<textarea id="<%= template_row_id_prefix -%>_variables_template" class="template">
<%= scope[:form].fields_for scope[:collection_name], :index => "" do |var_form| %>
<td class="name_value_cell">
<%= var_form.text_field :name, :class => "form_input environment_variable_name", :id => nil -%>
</td>
<td class="name_value_cell">
<span class="equals_sign"><%= l.string("EQUALS_SIGN") -%></span>
</td>
<td class="name_value_cell">
<div>
<% if scope[:secure] %>
<%= var_form.password_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil -%>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][secure]", true %>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][#{com.thoughtworks.go.config.EnvironmentVariableConfig::ISCHANGED}]", true, class: 'is-changed-field' %>
<% else %>
<%= var_form.text_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil -%>
<% end %>
</div>
</td>
<td class="name_value_cell">
<span class="icon_remove delete_parent"></span>
</td>
<% end %>
</textarea>
<table class="<%= scope[:collection_name] -%>">
<thead>
<tr>
<th>Name</th>
<th></th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody class="<%= scope[:collection_name] -%>" id="<%= template_row_id_prefix -%>">
<% scope[:collection].each do |variable| -%>
<%= scope[:form].fields_for scope[:collection_name], variable, :index => "" do |var_form| %>
<tr class="environment-variable-edit-row">
<td class="name_value_cell">
<%= var_form.text_field :name, :class => "form_input environment_variable_name", :id => nil -%>
<%= error_message_on(variable, com.thoughtworks.go.config.EnvironmentVariableConfig::NAME, :css_class => "name_value_error") %>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][original_name]", variable.getName() %>
</td>
<td class="name_value_cell">
<span class="equals_sign"><%= l.string("EQUALS_SIGN") -%></span>
</td>
<td class="name_value_cell">
<div>
<% if scope[:secure] %>
<%= var_form.password_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil, :readonly => 'readonly' -%>
<%= hidden_field_tag("#{scope[:form].object_name}[variables][][originalValue]", variable.getValueForDisplay(), :class => "original-secure-variable-value") -%>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][secure]", true %>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][#{com.thoughtworks.go.config.EnvironmentVariableConfig::ISCHANGED}]", false, :class => "is-changed-field" %>
<% else %>
<%= var_form.text_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil -%>
<% end %>
<div id="<%= template_row_id_prefix %>">
<table class="<%= scope[:collection_name] -%> variables">
<thead>
<tr>
<th>Name</th>
<th></th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody class="<%= scope[:collection_name] -%>" >
<% scope[:collection].each do |variable| -%>
<%= scope[:form].fields_for scope[:collection_name], variable, :index => "" do |var_form| %>
<tr class="environment-variable-edit-row">
<td class="name_value_cell">
<%= var_form.text_field :name, :class => "form_input environment_variable_name", :id => nil -%>
<%= error_message_on(variable, com.thoughtworks.go.config.EnvironmentVariableConfig::NAME, :css_class => "name_value_error") %>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][original_name]", variable.getName() %>
</td>
<td class="name_value_cell">
<span class="equals_sign"><%= l.string("EQUALS_SIGN") -%></span>
</td>
<td class="name_value_cell">
<div>
<% if scope[:secure] %>
<%= var_form.password_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil, :readonly => 'readonly' -%>
<%= hidden_field_tag("#{scope[:form].object_name}[variables][][originalValue]", variable.getValueForDisplay(), :class => "original-secure-variable-value") -%>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][secure]", true %>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][#{com.thoughtworks.go.config.EnvironmentVariableConfig::ISCHANGED}]", false, :class => "is-changed-field" %>
<% else %>
<%= var_form.text_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil -%>
<% end %>
<%= error_message_on(variable, com.thoughtworks.go.config.EnvironmentVariableConfig::VALUE, :css_class => "name_value_error") %>
</div>
</td>
<% if scope[:secure] %>
<td class="name_value_cell cell_edit_reset_actions">
<%= link_to 'Edit', '#', :class => "edit skip_dirty_stop" -%>
<%= link_to 'Reset', '#', :class => "reset hidden skip_dirty_stop" -%>
<%= error_message_on(variable, com.thoughtworks.go.config.EnvironmentVariableConfig::VALUE, :css_class => "name_value_error") %>
</div>
</td>
<% if scope[:secure] %>
<td class="name_value_cell cell_edit_reset_actions">
<%= link_to 'Edit', '#', :class => "edit skip_dirty_stop" -%>
<%= link_to 'Reset', '#', :class => "reset hidden skip_dirty_stop" -%>
</td>
<% end %>
<td class="name_value_cell icon_remove_cell">
<span class="icon_remove delete_parent"></span>
</td>
</tr>
<%- end -%>
<%- end -%>
</tbody>
</table>
<%= action_icon({:type => "add", :text => l.string("ADD"), :href => "#", :class => "skip_dirty_stop add_item", :id => 'add_variables'}) %>
<table style="display: none;">
<tbody class="template">
<tr class="environment-variable-edit-row">
<%= scope[:form].fields_for scope[:collection_name], :index => "" do |var_form| %>
<td class="name_value_cell">
<%= var_form.text_field :name, :class => "form_input environment_variable_name", :id => nil -%>
</td>
<td class="name_value_cell">
<span class="equals_sign"><%= l.string("EQUALS_SIGN") -%></span>
</td>
<td class="name_value_cell">
<div>
<% if scope[:secure] %>
<%= var_form.password_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil -%>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][secure]", true %>
<%= hidden_field_tag "#{scope[:form].object_name}[#{scope[:collection_name]}][][#{com.thoughtworks.go.config.EnvironmentVariableConfig::ISCHANGED}]", true, class: 'is-changed-field' %>
<% else %>
<%= var_form.text_field :valueForDisplay, :class => "form_input environment_variable_value", :id => nil -%>
<% end %>
</div>
</td>
<td class="name_value_cell">
<span class="icon_remove delete_parent"></span>
</td>
<% end %>
<td class="name_value_cell icon_remove_cell">
<span class="icon_remove delete_parent"></span>
</td>
</tr>
<%- end -%>
<%- end -%>
</tbody>
</table>
<%= action_icon({:type => "add", :text => l.string("ADD"), :href => "#", :id => id="add_#{template_row_id_prefix}", :class => "skip_dirty_stop"}) %>
</tbody>
</table>
</div>

</div>
</div>
<script type="text/javascript">
(function($){
$(function(){
EnvironmentVariableEdit({tableRowSelector: '.environment-variable-edit-row'});
});
})(jQuery);

Util.on_load(function () {
function setupVarForm(finish_button) {
var rowCreator = new EnvironmentVariables.RowCreator(jQuery('#<%=template_row_id_prefix-%>_variables_template'), 'tr', '.delete_parent');
var variables = new EnvironmentVariables(
jQuery('tbody#<%=template_row_id_prefix-%>'),
rowCreator,
null,
function (inputs) {
inputs.dirty_form();
},
function (row) {
row.parents("form.dirtyform").data("dirty", true);
});

variables.registerAddButton(jQuery("#add_<%=template_row_id_prefix-%>"))
variables.addDefaultRow();
variables.registerFinishButton(finish_button);
<script type="text/javascript">
(function ($) {
$(function () {
EnvironmentVariableEdit({tableRowSelector: '.environment-variable-edit-row'});
EnvironmentVariableAddRemove($('#<%=template_row_id_prefix-%>'), {
onAdd: function(addButton){
addButton.parents('form.dirtyform').data('dirty', true);
},
onRemove: function(deleteLink){
deleteLink.parents('form.dirtyform').data('dirty', true);
}

var finish_button = jQuery(".finish");
setupVarForm(finish_button);
});
});
})(jQuery);
</script>
@@ -1,47 +1,46 @@
<div class="form_content">
<div class="environment_variables_section popup_form">
<table style="display: none;">
<tbody id="plain_text_environment_variables_template" class="template">
<%= scope[:form].fields_for :variables, :index => "" do |var_form| -%>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => false}} -%>
<%- end -%>
</tbody>
</table>

<table style="display: none;">
<tbody id="secure_environment_variables_template" class="template">
<%= scope[:form].fields_for :variables, :index => "" do |var_form| -%>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => true}} -%>
<%- end -%>
</tbody>
</table>

<div class="plain-text-variables">
<h3><%= l.string("ENVIRONMENT_VARIABLES") %></h3>
<table class="variables">
<tbody>
<% scope[:form].object.getPlainTextVariables().each do |variable| -%>
<%= scope[:form].fields_for :variables, variable, :index => "" do |var_form| %>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => false}} -%>
<% end %>
<%- end -%>
<% scope[:form].object.getPlainTextVariables().each do |variable| -%>
<%= scope[:form].fields_for :variables, variable, :index => "" do |var_form| %>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => false, :is_changed => false}} -%>
<% end %>
<%- end -%>
</tbody>
</table>
<a class="add_item"><%= l.string("ADD") -%></a>
<table style="display: none;">
<tbody class="template">
<%= scope[:form].fields_for :variables, :index => "" do |var_form| -%>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => false, :is_changed => true}} -%>
<%- end -%>
</tbody>
</table>
<a class="add_item" data-add-environment-to=".plain-text-variables .variables tbody" data-environment-template="#plain_text_environment_variables_template"><%= l.string("ADD") -%></a>
</div>

<div class="secure-variables">
<h3><%= l.string("SECURE_ENVIRONMENT_VARIABLES") %></h3>
<table class="variables">
<tbody>
<% scope[:form].object.getSecureVariables().each do |variable| -%>
<%= scope[:form].fields_for :variables, variable, :index => "" do |var_form| %>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => true}} -%>
<% end %>
<%- end -%>
<% scope[:form].object.getSecureVariables().each do |variable| -%>
<%= scope[:form].fields_for :variables, variable, :index => "" do |var_form| %>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => true, :is_changed => false}} -%>
<% end %>
<%- end -%>
</tbody>
</table>
<a class="add_item"><%= l.string("ADD") -%></a>
<table style="display: none;">
<tbody class="template">
<%= scope[:form].fields_for :variables, :index => "" do |var_form| -%>
<%= render :partial => "environment_variable_entry", :locals => {:scope => {:var_form => var_form, :is_secure => true, :is_changed => true}} -%>
<%- end -%>
</tbody>
</table>
<a class="add_item" data-add-environment-to=".secure-variables .variables tbody" data-environment-template="#secure_environment_variables_template"><%= l.string("ADD") -%></a>
</div>

</div>
Expand All @@ -50,20 +49,8 @@
(function ($) {
$(function () {
EnvironmentVariableEdit({tableRowSelector: '.environment-variable-edit-row'});
EnvironmentVariableAddRemove($('.secure-variables'));
EnvironmentVariableAddRemove($('.plain-text-variables'));
});
function setupEnvVarForm(finish_button) {
$('.add_item').each(function (i, elem) {
var addButton = $(elem);
var template = $(addButton.attr('data-environment-template'));
var addRowTo = $(addButton.attr('data-add-environment-to'));
var rowCreator = new EnvironmentVariables.RowCreator(template, null, '.delete_parent', true);
var environmentVariables = new EnvironmentVariables(addRowTo, rowCreator, null, null, null);
environmentVariables.addDefaultRow();
environmentVariables.registerAddButton(addButton);
environmentVariables.registerFinishButton(finish_button);
});
}

setupEnvVarForm($('.finish'))
})(jQuery);
</script>
Expand Up @@ -15,7 +15,7 @@
<% if scope[:var_form].object %>
<%= scope[:var_form].hidden_field :originalValue, value: scope[:var_form].object.getValueForDisplay, class: "original-secure-variable-value" -%>
<% end %>
<%= scope[:var_form].hidden_field com.thoughtworks.go.config.EnvironmentVariableConfig::ISCHANGED, value: true, :class => "is-changed-field" -%>
<%= scope[:var_form].hidden_field com.thoughtworks.go.config.EnvironmentVariableConfig::ISCHANGED, value: scope[:is_changed], :class => "is-changed-field" -%>
<% else %>
<%= scope[:var_form].text_field :valueForDisplay, :class => 'form_input environment_variable_value', :id => nil -%>
<% end %>
Expand Down

0 comments on commit 2732069

Please sign in to comment.