/
_messages.html.erb
65 lines (60 loc) · 2.14 KB
/
_messages.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<div>
<%= select_tag 'select-key', available_message_options(@setting, lang), id: 'key-selector' %>
<span class='icon icon-help'><%= l(:text_description_of_search_box) %></span>
</div>
<br>
<div class='tabular'>
<%= normal_mode_input_fields(@setting, lang) %>
</div>
<%= javascript_tag do %>
$(document).ready(function() {
setSelect2();
});
// Fix a problem with focus not working in Redmine 5.0 or later.
// issue: https://github.com/select2/select2/issues/5993
$(document).on('select2:open', function() {
document.querySelector('.select2-search__field').focus();
});
var currentSearchKeyword;
$('#key-selector').on('select2:select', function (e) {
var key = e.params.data.id;
var val = e.params.data.text;
AddMessageInputField(key, val);
}).on('select2:closing', function (e) {
currentSearchKeyword = $('.select2-search__field')[0].value;
}).on('select2:open', function (e) {
setTimeout(function() {
if (currentSearchKeyword){
$('.select2-search__field').val(currentSearchKeyword).trigger('input');
}
}, 0);
});
function AddMessageInputField(key, val){
if($('input[name="settings[custom_messages[' + key + ']]"]').length === 0){
$('<p></p>').prependTo($('#edit-custom-messages .tabular'));
$('<label>' + key + '</label><br>').appendTo($('#edit-custom-messages .tabular p:first'));
$('<input>').attr({
type: 'text',
value: val.replace(/.*: /, ''),
name: 'settings[custom_messages][' + key + ']'
}).appendTo($('#edit-custom-messages .tabular p:first'));
$('<a>').attr({
class: 'icon icon-del clear-key-link',
href: '#',
onclick: '$(this).closest("p").remove();; return false;'
}).appendTo($('#edit-custom-messages .tabular p:first'));
$('#key-selector').val('').change();
$('#key-selector option[value="' + key + '"]').prop("disabled", true).change();
setSelect2();
}
}
function setSelect2(){
$("#key-selector").select2({
width: '100%',
placeholder: '<%= l(:text_placeholder_choose_key) %>',
templateResult: function(option) {
return $('<span class="key">' + option.id + ':</span> <span>' + option.text.replace(/.*: /, '') + '</span>');
}
});
}
<% end %>