Parent: [d5cccd] (diff)

Child: [0fa8a4] (diff)

Download this file

custom-fields.js    98 lines (88 with data), 4.1 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function add_field(label, type, options, show_in_search){
var $new_field = $('<div class="custom-field">'
+ '<div class="span-3 clear"><label>Label: </label></div><div class="span-13 last"><input class="field-label title wide" type="text"/></div>'
+ '<div class="span-3 clear"><label>Type: </label></div>'
+ '<div class="span-13 last"><select class="title wide">'
+ '<option value="string">text</option>'
+ '<option value="sum">sum</option>'
+ '<option value="number">number</option>'
+ '<option value="boolean">boolean</option>'
+ '<option value="select">select</option>'
+ '</select></div>'
+ '<span class="options-wrapper"><div class="span-3 clear"><label>Options: </label></div>'
+ '<div class="span-13 last"><input class="field-options title wide" type="text"/></div></span>'
+ '<div class="prepend-3 span-13 last">'
+ ' <input type="checkbox" class="field-show-in-search" />'
+ ' <label>Show in search results</label>'
+ '</div>'
+ '<div class="push-3 span-13 last"><input type="button" onclick="delete_field(this)" value="Delete" class="ui-state-default ui-button ui-button-text"/></div>'
+ '<div class="clear clearfix"/>'
+ '</div>');
label && $new_field.find('input.field-label').val(label);
type && $new_field.find('option[value="'+type+'"]').attr('selected', 'selected');
options && $new_field.find('input.field-options').val(options);
show_in_search && $new_field.find('input.field-show-in-search').attr('checked', show_in_search);
$('#custom-field-list').append($new_field);
$new_field.find('select').change(show_hide_options).change();
manage_messages();
}
function delete_field(el){
$(el).closest('div.custom-field').remove();
manage_messages();
}
function save_fields(){
var foundBlank = false;
$('#custom-field-list>div.custom-field input.field-label').each(function(ele){
if(this.value==''){
foundBlank = true;
}
});
if(foundBlank){
alert('Every custom field must have a label.')
}
else{
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() + '",'
+ '"show_in_search":' + $this.find('input.field-show-in-search').is(':checked') + ','
+ '"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 manage_messages(){
if($('div.custom-field').length){
$('#no_fields_message').hide();
$('#has_fields_message').show();
}
else{
$('#no_fields_message').show();
$('#has_fields_message').hide();
}
}
$(function(){
$('div.custom-field-stub').each(function(){
var $this = $(this);
var label = $this.attr('data-label');
var type = $this.attr('data-type');
var options = $this.attr('data-options');
var show_in_search = $this.attr('data-show-in-search') == 'true';
add_field(label, type, options, show_in_search);
$this.remove();
});
$('#custom-field-list').sortable();
manage_messages();
});