Parent: [a9af82] (diff)

Child: [975afb] (diff)

Download this file

custom-fields.js    46 lines (40 with data), 1.7 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
function add_field(name, type){
var $new_field = $('<div class="custom-field">'
+ '<label>Name:</label><input class="field-name" type="text"/><br/>'
+ '<label>Type:</label>'
+ '<select>'
+ '<option value="string">string</option>'
+ '<option value="number">number</option>'
+ '<option value="date">date</option>'
+ '</select><br/>'
+ '<button onclick="delete_field(this)">Delete</button>'
+ '</div>');
name && $new_field.find('input.field-name').val(name);
type && $new_field.find('option:contains('+type+')').attr('selected', 'selected')
$('#custom-field-list').append($new_field);
}
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 ('{'
+ '"name":"' + $this.find('input.field-name').val() + '",'
+ '"type":"' + $this.find('select').val() + '"'
+ '}'
);
}).
get().
join(',') + ']';
$.post('set_custom_fields', { custom_fields: json });
}
$(function(){
$('div.custom-field-stub').each(function(){
var $this=$(this), name=$this.attr('data-name'), type=$this.attr('data-type');
add_field(name, type);
$this.remove();
});
$('#custom-field-list').sortable();
});