diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 472faa7..d4087f7 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,17 +1,40 @@ // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require bootstrap.min //= require_tree . //= require highcharts + +$(document).ready(function() { + $(".include_files-plus-sign").click(function() { + addIncludedFileTextArea(); + }); + $(".exclude_directions-plus-sign").click(function() { + addExcludeDirectionsTextArea(); + }); +}); + +function addIncludedFileTextArea() { + var textArrea = $('.include_files:last').clone(true).val(""); + $('.include_files-plus-sign:first').parent().remove(); + textArrea.insertAfter('.include_files:last'); + $('.include_files:last input').val(""); +} + +function addExcludeDirectionsTextArea() { + var textArrea = $('.exclude_directions:last').clone(true).val(""); + $('.exclude_directions-plus-sign:first').parent().remove(); + textArrea.insertAfter('.exclude_directions:last'); + $('.exclude_directions:last input').val(""); +} diff --git a/app/helpers/filesets_helper.rb b/app/helpers/filesets_helper.rb new file mode 100644 index 0000000..518e9f6 --- /dev/null +++ b/app/helpers/filesets_helper.rb @@ -0,0 +1,43 @@ +module FilesetsHelper + # Creates a bootstrap form-group div with an additional 'Add' button next to the text field + # + # @param object[ActiveRecord::Object] the form's subject + # @param resource[Symbol] the objects class + # @param attr[Symbol] the select box's attribute + # @param attr_name[String] the attribute's display name + # @param placeholder[String] the text box's placeholder + def text_with_errors_and_plus(object, resource, attr, attr_name, placeholder, value=nil) + has_errors = object.errors[attr].present? + content_tag(:div, class: "form-group #{attr} #{' has_error' if has_errors }") do + attr_label = label(resource, attr, attr_name, class: 'control-label col-xs-3') + text_div = content_tag(:div, class: 'col-xs-6') do + text_part = text_field_tag([resource, attr].join('_').to_sym, + value || object.send(attr.to_sym), + placeholder: placeholder, + name: "#{resource}[#{attr}][]", + multiple: true, + class: 'form-control' + ) {} + + if has_errors + text_part.concat(content_tag(:span, class: 'help-block') { object.errors[attr].first }) + end + text_part + end + + actions_part = content_tag(:div, class: 'col-xs-1') do + add_link = content_tag(:a, href: '#', class: "#{attr}-plus-sign") do + content_tag(:span, class: 'glyphicon glyphicon-plus') {} + end + + remove_link = content_tag(:a, href: '#', + class: "#{attr}-remove-sign", style: "display:none") do + content_tag(:span, class: 'glyphicon glyphicon-remove text-danger') {} + end + add_link.concat(remove_link) + end + + attr_label.concat(text_div).concat(actions_part) + end + end +end diff --git a/app/views/filesets/_form.html.erb b/app/views/filesets/_form.html.erb index 436a60a..3bc01df 100644 --- a/app/views/filesets/_form.html.erb +++ b/app/views/filesets/_form.html.erb @@ -1,18 +1,16 @@ <%= bootstrap_form_for(@fileset, url: host_filesets_path(@host, @fileset), layout: :horizontal, label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %>
<%= f.text_field :name %> - <%= f.text_field :include_files, label: 'Files', placeholder: '/', multiple: true %> - <%= f.text_field :include_files, label: 'Files', placeholder: '/media', multiple: true %> - <%= f.text_field :exclude_directions, label: 'Exclude', placeholder: '/proc', multiple: true %> - <%= f.text_field :exclude_directions, label: 'Exclude', placeholder: '/bacula', multiple: true %> + <%= text_with_errors_and_plus(@fileset, :fileset, :include_files, 'Files', '/') %> + <%= text_with_errors_and_plus(@fileset, :fileset, :exclude_directions, 'Exclude', '/bacula') %> <%= (hidden_field_tag :job_id, @job_id) if @job_id%>
<%= f.submit class: 'btn btn-success' %>
<% end %>