Child: [2347d9] (diff)

Download this file

custom-fields.js    59 lines (51 with data), 2.5 kB

 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
function add_field(label, type, options){
var $new_field = $('<div class="custom-field">'
+ '<div class="span-3"><label>Label: </label></div><div class="span-15 last"><input class="field-label" type="text"/></div>'
+ '<div class="span-3"><label>Type: </label></div>'
+ '<div class="span-15 last"><select>'
+ '<option value="string">string</option>'
+ '<option value="sum">sum</option>'
+ '<option value="select">select</option>'
+ '</select></div>'
+ '<span class="options-wrapper"><div class="span-3"><label>Options: </label></div>'
+ '<div class="span-15 last"><input class="field-options" type="text"/></div></span>'
+ '<button onclick="delete_field(this)">Delete</button>'
+ '</div>');
label && $new_field.find('input.field-label').val(label);
type && $new_field.find('option:contains('+type+')').attr('selected', 'selected');
options && $new_field.find('input.field-options').val(options);
$('#custom-field-list').append($new_field);
$new_field.find('select').change(show_hide_options).change();
}
function delete_field(el){
$(el).closest('div.custom-field').remove();
}
function save_fields(){
var json = '[' + $('#custom-field-list>div.custom-field').
map(function(){
var $this=$(this);
return ('{'
+ '"label":"' + $this.find('input.field-label').val() + '",'
+ '"type":"' + $this.find('select').val() + '",'
+ '"options":"' + $this.find('input.field-options').val() + '"'
+ '}'
);
}).
get().
join(',') + ']';
$.post('set_custom_fields', { custom_fields: json }, function(){
location.reload();
});
}
function show_hide_options(){
var $this=$(this), show=$this.val()==='select';
$this.closest('div.custom-field').find('span.options-wrapper').toggle(show)
}
$(function(){
$('div.custom-field-stub').each(function(){
var $this=$(this), label=$this.attr('data-label'), type=$this.attr('data-type'), options=$this.attr('data-options');
add_field(label, type, options);
$this.remove();
});
$('#custom-field-list').sortable();
});