diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-08-15 11:17:16 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-08-15 11:17:16 +0400 |
commit | 433dac7799a1a5fd4a0dfeaa0157148eb9058202 (patch) | |
tree | 363bd1609d2bef7fb502971bc7bb8d51209e5343 /app | |
parent | 7120af725148aae5092c0870c1da34ca5652f60d (diff) | |
parent | cbc90565b55d89704d64bc48db323b82b739a873 (diff) |
Merge pull request #7465 from Razer6/better_label_color_validation
Better label color validation, fixes #7454
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/application.js.coffee | 33 | ||||
-rw-r--r-- | app/assets/javascripts/dispatcher.js.coffee | 2 | ||||
-rw-r--r-- | app/assets/javascripts/labels.js.coffee | 35 | ||||
-rw-r--r-- | app/models/concerns/issuable.rb | 3 | ||||
-rw-r--r-- | app/models/label.rb | 13 | ||||
-rw-r--r-- | app/views/projects/labels/_form.html.haml | 18 |
6 files changed, 79 insertions, 25 deletions
diff --git a/app/assets/javascripts/application.js.coffee b/app/assets/javascripts/application.js.coffee index 82a810b6551..1960479321c 100644 --- a/app/assets/javascripts/application.js.coffee +++ b/app/assets/javascripts/application.js.coffee @@ -53,15 +53,40 @@ window.split = (val) -> window.extractLast = (term) -> return split( term ).pop() +window.rstrip = (val) -> + return val.replace(/\s+$/, '') + # Disable button if text field is empty window.disableButtonIfEmptyField = (field_selector, button_selector) -> field = $(field_selector) - closest_submit = field.closest("form").find(button_selector) + closest_submit = field.closest('form').find(button_selector) + + closest_submit.disable() if rstrip(field.val()) is "" + + field.on 'input', -> + if rstrip($(@).val()) is "" + closest_submit.disable() + else + closest_submit.enable() + +# Disable button if any input field with given selector is empty +window.disableButtonIfAnyEmptyField = (form, form_selector, button_selector) -> + closest_submit = form.find(button_selector) + empty = false + form.find('input').filter(form_selector).each -> + empty = true if rstrip($(this).val()) is "" + + if empty + closest_submit.disable() + else + closest_submit.enable() - closest_submit.disable() if field.val().replace(/\s+$/, "") is "" + form.keyup -> + empty = false + form.find('input').filter(form_selector).each -> + empty = true if rstrip($(this).val()) is "" - field.on "input", -> - if $(@).val().replace(/\s+$/, "") is "" + if empty closest_submit.disable() else closest_submit.enable() diff --git a/app/assets/javascripts/dispatcher.js.coffee b/app/assets/javascripts/dispatcher.js.coffee index 89bb475a8a6..a463a2eb194 100644 --- a/app/assets/javascripts/dispatcher.js.coffee +++ b/app/assets/javascripts/dispatcher.js.coffee @@ -50,6 +50,8 @@ class Dispatcher new TreeView() when 'projects:blob:show' new BlobView() + when 'projects:labels:new' + new Labels() switch path.first() when 'admin' then new Admin() diff --git a/app/assets/javascripts/labels.js.coffee b/app/assets/javascripts/labels.js.coffee new file mode 100644 index 00000000000..8e53d6929df --- /dev/null +++ b/app/assets/javascripts/labels.js.coffee @@ -0,0 +1,35 @@ +class Labels + constructor: -> + form = $('.label-form') + @setupLabelForm(form) + @cleanBinding() + @addBinding() + @updateColorPreview + + addBinding: -> + $(document).on 'click', '.suggest-colors a', @setSuggestedColor + $(document).on 'input', 'input#label_color', @updateColorPreview + + cleanBinding: -> + $(document).off 'click', '.suggest-colors a' + $(document).off 'input', 'input#label_color' + + # Initializes the form to disable the save button if no color or title is entered + setupLabelForm: (form) -> + disableButtonIfAnyEmptyField form, '.form-control', form.find('.js-save-button') + + # Updates the the preview color with the hex-color input + updateColorPreview: => + previewColor = $('input#label_color').val() + $('div.label-color-preview').css('background-color', previewColor) + + # Updates the preview color with a click on a suggested color + setSuggestedColor: (e) => + color = $(e.currentTarget).data('color') + $('input#label_color').val(color) + @updateColorPreview() + # Notify the form, that color has changed + $('.label-form').trigger('keyup') + e.preventDefault() + +@Labels = Labels diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 517e4548624..0a5fe24b5af 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -140,7 +140,8 @@ module Issuable def add_labels_by_names(label_names) label_names.each do |label_name| - label = project.labels.find_or_create_by(title: label_name.strip) + label = project.labels.create_with( + color: Label::DEFAULT_COLOR).find_or_create_by(title: label_name.strip) self.labels << label end end diff --git a/app/models/label.rb b/app/models/label.rb index ce982579675..a511b7940ed 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -1,13 +1,20 @@ class Label < ActiveRecord::Base + DEFAULT_COLOR = '#82C5FF' + belongs_to :project has_many :label_links, dependent: :destroy has_many :issues, through: :label_links, source: :target, source_type: 'Issue' - validates :color, format: { with: /\A\#[0-9A-Fa-f]{6}+\Z/ }, allow_blank: true + validates :color, + format: { with: /\A\#[0-9A-Fa-f]{6}+\Z/ }, + allow_blank: false validates :project, presence: true - # Dont allow '?', '&', and ',' for label titles - validates :title, presence: true, format: { with: /\A[^&\?,&]*\z/ } + # Don't allow '?', '&', and ',' for label titles + validates :title, + presence: true, + format: { with: /\A[^&\?,&]*\z/ }, + uniqueness: { scope: :project_id } scope :order_by_name, -> { reorder("labels.title ASC") } diff --git a/app/views/projects/labels/_form.html.haml b/app/views/projects/labels/_form.html.haml index 2a5c907febe..72a01e1c271 100644 --- a/app/views/projects/labels/_form.html.haml +++ b/app/views/projects/labels/_form.html.haml @@ -28,22 +28,6 @@ .form-actions - = f.submit 'Save', class: 'btn btn-save' + = f.submit 'Save', class: 'btn btn-save js-save-button' = link_to "Cancel", project_labels_path(@project), class: 'btn btn-cancel' - -:coffeescript - updateColorPreview = -> - previewColor = $('input#label_color').val() - $('div.label-color-preview').css('background-color', previewColor) - - $('.suggest-colors a').on 'click', (e) -> - color = $(this).data("color") - $('input#label_color').val(color) - updateColorPreview() - e.preventDefault() - - $('input#label_color').on 'input', -> - updateColorPreview() - - updateColorPreview() |