6
6
7
7
<%= javascript_tag do %>
8
8
$(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();
16
10
});
17
11
18
12
$('#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){
21
16
$('< 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'));
23
18
$('< input > ').attr({
24
19
type: 'text',
25
- value: data.text .replace(/.*: /, ''),
26
- name: 'settings[custom_messages[' + data.id + ']]'
20
+ value: val .replace(/.*: /, ''),
21
+ name: 'settings[custom_messages[' + key + ']]'
27
22
}).appendTo($('#edit-custom-messages .tabular p:first'));
28
23
$('< a > ').attr({
29
24
class: 'icon-only icon-del clear-key-link',
30
25
href: '#',
31
26
onclick: '$(this).closest("p").remove();; return false;'
32
27
}).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();
41
30
}
42
31
});
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
+ }
43
42
<% end %>
0 commit comments