Skip to content

Commit 8ef6de8

Browse files
committed
Clean up edit javascript
1 parent 1b2dc4e commit 8ef6de8

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

app/views/custom_message_settings/_messages.html.erb

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,37 @@
66

77
<%= javascript_tag do %>
88
$(document).ready(function() {
9-
$("#key-selector").select2({
10-
width: '100%',
11-
placeholder: '<%= l(:text_placeholder_choose_key) %>',
12-
templateResult: function(option) {
13-
return $('<span class="key">' + option.id + ':</span> <span>' + option.text.replace(/.*: /, '') + '</span>');
14-
}
15-
});
9+
setSelect2();
1610
});
1711

1812
$('#key-selector').on('select2:select', function (e) {
19-
var data = e.params.data;
20-
if($('input[name="settings[custom_messages[' + data.id + ']]"]').length === 0){
13+
var key = e.params.data.id;
14+
var val = e.params.data.text;
15+
if($('input[name="settings[custom_messages[' + key + ']]"]').length === 0){
2116
$('<p></p>').prependTo($('#edit-custom-messages .tabular'));
22-
$('<label>' + data.id + '</label><br>').appendTo($('#edit-custom-messages .tabular p:first'));
17+
$('<label>' + key + '</label><br>').appendTo($('#edit-custom-messages .tabular p:first'));
2318
$('<input>').attr({
2419
type: 'text',
25-
value: data.text.replace(/.*: /, ''),
26-
name: 'settings[custom_messages[' + data.id + ']]'
20+
value: val.replace(/.*: /, ''),
21+
name: 'settings[custom_messages[' + key + ']]'
2722
}).appendTo($('#edit-custom-messages .tabular p:first'));
2823
$('<a>').attr({
2924
class: 'icon-only icon-del clear-key-link',
3025
href: '#',
3126
onclick: '$(this).closest("p").remove();; return false;'
3227
}).appendTo($('#edit-custom-messages .tabular p:first'));
33-
$('#key-selector option[value="' + data.id + '"]').prop("disabled", true).change();
34-
$("#key-selector").select2({
35-
width: '100%',
36-
placeholder: '<%= l(:text_placeholder_choose_key) %>',
37-
templateResult: function(option) {
38-
return $('<span class="key">' + option.id + ':</span> <span>' + option.text.replace(/.*: /, '') + '</span>');
39-
}
40-
});
28+
$('#key-selector option[value="' + key + '"]').prop("disabled", true).change();
29+
setSelect2();
4130
}
4231
});
32+
33+
function setSelect2(){
34+
$("#key-selector").select2({
35+
width: '100%',
36+
placeholder: '<%= l(:text_placeholder_choose_key) %>',
37+
templateResult: function(option) {
38+
return $('<span class="key">' + option.id + ':</span> <span>' + option.text.replace(/.*: /, '') + '</span>');
39+
}
40+
});
41+
}
4342
<% end %>

0 commit comments

Comments
 (0)