Skip to content

Commit c5bb5b1

Browse files
committed
Chenge selectbox style
1 parent 9a1da01 commit c5bb5b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+59
-36
lines changed

app/controllers/custom_message_settings_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ def edit
1010

1111
def update
1212
original_custom_messages = @setting.custom_messages
13-
languages = (original_custom_messages ? original_custom_messages.keys.map(&:to_s) : [])
13+
languages = (original_custom_messages.try(:keys) ? original_custom_messages.keys.map(&:to_s) : [])
1414

1515
if custom_message_setting_params[:custom_messages]
16-
messages = original_custom_messages.merge({@lang => CustomMessageSetting.to_nested_hash(custom_message_setting_params[:custom_messages].to_unsafe_h.to_hash) })
16+
messages = original_custom_messages.merge({@lang => CustomMessageSetting.nested_hash(custom_message_setting_params[:custom_messages].to_unsafe_h.to_hash) })
1717
elsif custom_message_setting_params[:custom_messages_yaml]
1818
messages = custom_message_setting_params[:custom_messages_yaml]
1919
else
20-
message = {@lang => {}}
20+
messages = {@lang => {}}
2121
end
22+
2223
if @setting.update_custom_messages(messages)
2324
flash[:notice] = l(:notice_successful_update)
2425
new_custom_messages = @setting.custom_messages
2526
if new_custom_messages.present?
2627
languages += new_custom_messages.keys.map(&:to_s)
2728
CustomMessageSetting.reload_translations!(languages)
2829
end
29-
redirect_to edit_custom_message_settings_path
30+
redirect_to edit_custom_message_settings_path(tab: params[:tab])
3031
else
3132
@lang ||= User.current.language
3233
render :edit
3334
end
3435
end
3536

3637
private
37-
3838
def set_custom_message_setting
3939
@setting = CustomMessageSetting.find_or_default
4040
end

app/helpers/custom_message_settings_helper.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
module CustomMessageSettingsHelper
2-
def available_messages(lang)
3-
messages = I18n.backend.translations[lang.to_sym]
4-
if messages.nil?
5-
CustomMessageSetting.reload_translations!([lang])
6-
messages = I18n.backend.translations[lang.to_sym] || {}
7-
end
8-
9-
CustomMessageSetting.flatten_hash(messages)
10-
end
2+
def available_message_options(setting, lang)
3+
options = [['', '']] +
4+
CustomMessageSetting.available_messages(lang).map{|k, v| ["#{k}: #{v}", k]}
115

12-
def available_message_options(lang)
13-
[['', '']] +
14-
available_messages(lang).map do |k, v|
15-
["#{k}: #{v}", k]
16-
end
6+
options_for_select(options, disabled: setting.custom_messages_to_flatten_hash(lang).keys)
177
end
188

199
def normal_mode_input_fields(setting, lang)
2010
return '' if setting.value[:custom_messages].is_a?(String) || setting.value[:custom_messages].blank?
2111
content = ''
22-
custom_messages_hash = CustomMessageSetting.flatten_hash(setting.custom_messages[lang.to_s] || {})
12+
custom_messages_hash = setting.custom_messages_to_flatten_hash(lang.to_s)
2313
custom_messages_hash.each do |k, v|
2414
content += content_tag(:p) do
2515
content_tag(:label, k) +

app/models/custom_message_setting.rb

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class CustomMessageSetting < Setting
2-
32
before_validation :change_format_to_hash
43
validate :custom_message_keys_are_available
54

@@ -15,6 +14,10 @@ def custom_messages(lang=nil)
1514
end
1615
end
1716

17+
def custom_messages_to_flatten_hash(lang=nil)
18+
self.class.flatten_hash(custom_messages(lang))
19+
end
20+
1821
def custom_messages_to_yaml
1922
if self.custom_messages.blank?
2023
''
@@ -31,8 +34,12 @@ def update_custom_messages(messages)
3134
end
3235

3336
def self.available_messages(lang='en')
34-
list = I18n.backend.translations[self.find_language(lang).to_sym] || {}
35-
self.flatten_hash(list)
37+
messages = I18n.backend.translations[self.find_language(lang).to_sym] || {}
38+
if messages.nil?
39+
CustomMessageSetting.reload_translations!([lang])
40+
messages = I18n.backend.translations[lang.to_sym] || {}
41+
end
42+
self.flatten_hash(messages)
3643
end
3744

3845
def self.flatten_hash(hash=nil)
@@ -45,7 +52,7 @@ def self.flatten_hash(hash=nil)
4552
end
4653
end
4754

48-
def self.to_nested_hash(hash=nil)
55+
def self.nested_hash(hash=nil)
4956
hash = self.to_hash unless hash
5057
new_hash = {}
5158
hash.each do |key, value|
@@ -78,6 +85,7 @@ def self.find_language(language=nil)
7885

7986
def custom_message_keys_are_available
8087
return false if self.value[:custom_messages].is_a?(Hash) == false || self.errors.present?
88+
8189
custom_messages_hash = {}
8290
custom_messages.values.each do |hash|
8391
custom_messages_hash = self.class.flatten_hash(custom_messages_hash.merge(hash))
@@ -91,15 +99,15 @@ def custom_message_keys_are_available
9199
end
92100

93101
def change_format_to_hash
94-
begin
95-
if self.value[:custom_messages].is_a?(Hash)
96-
YAML.dump(self.value[:custom_messages])
97-
else
98-
self.value = {custom_messages: YAML.load(self.value[:custom_messages])}
99-
end
100-
rescue => e
101-
self.errors.add(:base, e.message)
102-
return false
102+
if self.value[:custom_messages].is_a?(Hash)
103+
YAML.dump(self.value[:custom_messages])
104+
else
105+
custom_messages = YAML.load(self.value[:custom_messages])
106+
raise l(:error_invalid_yaml_format) unless custom_messages.is_a?(Hash)
107+
self.value = {custom_messages: custom_messages}
103108
end
109+
rescue => e
110+
self.errors.add(:base, e.message)
111+
false
104112
end
105113
end

app/views/custom_message_settings/_messages.html.erb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% lang ||= 'en' %>
2-
<div><%= l(:text_placeholder_choose_key) %><%= select_tag 'select-key', options_for_select(available_message_options(lang)), id: 'key-selector' %></div>
2+
<div><%= select_tag 'select-key', available_message_options(@setting, lang), id: 'key-selector' %></div>
33
<br>
44
<div class='tabular'>
55
<%= normal_mode_input_fields(@setting, lang) %>
@@ -8,7 +8,11 @@
88
<%= javascript_tag do %>
99
$(document).ready(function() {
1010
$("#key-selector").select2({
11-
width: '100%'
11+
width: '100%',
12+
placeholder: '<%= l(:text_placeholder_choose_key) %>',
13+
templateResult: function(option) {
14+
return $('<span class="key">' + option.id + ':</span> <span>' + option.text.replace(/.*: /, '') + '</span>');
15+
}
1216
});
1317
});
1418

@@ -27,7 +31,14 @@ $('#key-selector').on('select2:select', function (e) {
2731
href: '#',
2832
onclick: '$(this).closest("p").remove();; return false;'
2933
}).appendTo($('#edit-custom-messages .tabular p:first'));
30-
$('#key-selector').val('');
34+
$('#key-selector option[value="' + data.id + '"]').prop("disabled", true).change();
35+
$("#key-selector").select2({
36+
width: '100%',
37+
placeholder: '<%= l(:text_placeholder_choose_key) %>',
38+
templateResult: function(option) {
39+
return $('<span class="key">' + option.id + ':</span> <span>' + option.text.replace(/.*: /, '') + '</span>');
40+
}
41+
});
3142
}
3243
});
3344
<% end %>

app/views/custom_message_settings/_normal_tab.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<%= form_tag custom_message_settings_path, method: :patch do %>
2+
<%= hidden_field_tag :tab, 'normal' %>
23
<label><%= l(:field_language) %>: </label><%= select_tag 'lang', options_for_select(languages_options, @lang), include_blank: true, data: {remote: true, url: url_for(action: :edit)} %>
34
<hr>
45

app/views/custom_message_settings/_yaml_tab.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<%= form_tag custom_message_settings_path, method: :patch do %>
2+
<%= hidden_field_tag :tab, 'yaml' %>
23
<div class='help-message wiki'>
34
<p><%= l(:text_help_for_input_format) %></p>
45
<p><%= l(:text_placeholder_template_case) %></p>

assets/stylesheets/custom_messages.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@
2424
pointer-events: none;
2525
cursor: default;
2626
text-decoration: none;
27+
}
28+
29+
.select2-container--default .select2-results__option span.key {
30+
color: gray;
31+
}
32+
.select2-container--default .select2-results__option[aria-disabled=true] {
33+
background: #ccc;
34+
}
35+
.select2-container--default .select2-results__option[aria-disabled=true] span {
36+
color: white;
2737
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)