/
fieldsuppressor.sections.js
63 lines (50 loc) · 2.03 KB
/
fieldsuppressor.sections.js
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
/*-----------------------------------------------------------------------------
Language strings
-----------------------------------------------------------------------------*/
Symphony.Language.add({
' Hide this field from the backend edit page': false
});
/*-----------------------------------------------------------------------------
Section Editor
-----------------------------------------------------------------------------*/
jQuery(document).ready(function() {
// Add a input field for every field instance
var $duplicator = jQuery('#fields-duplicator');
$fields = $duplicator.find('.instance');
// Get JSON data for the fields
data = Symphony.Context.get('fieldsuppressor');
// Template to clone for each field instance
field_template = jQuery('<label />')
.text(Symphony.Language.get(' Hide this field from the backend edit page'))
.prepend(
jQuery('<input />')
.attr({ type: 'checkbox', value: 'yes'})
);
// Inject the template into current $field
addField = function($field, template) {
$field.find('div.content > :last-child').after(template);
};
// Initially run over the all the existing fields
$fields.each(function(i) {
var $field = jQuery(this);
var field_id = $field.find(':hidden[name*=id]').val();
template = field_template.clone();
if(data != undefined) {
if(data[field_id]['suppress'] == 'yes') {
template.find('input').attr('checked', 'checked');
}
}
template.find('input').attr({name: 'fields[' + i + '][fieldsuppressor]'})
addField($field, template);
});
// Listen for when the duplicator changes
$duplicator.bind('click.duplicator', function() {
var $field = $duplicator.find('.instance:last');
// If the field doesn't have a suppress field already, add one
if($field.filter(':has(input[name*=fieldsuppressor])').length == 0) {
var template = field_template.clone();
template.find('input').attr({name: 'fields[' + ($field.index() - 1) + '][fieldsuppressor]', value: 'yes'})
addField($field, template);
}
});
});