forked from backflip/Wordpress-Simple-Fields-Prettify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_script.js
79 lines (63 loc) · 2.68 KB
/
_script.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function($){
var selectors = {
wrapper : '.simple-fields-meta-box-field-group-wrapper',
add : '.simple-fields-metabox-field-add',
repeatable : '.simple-fields-metabox-field-group-fields-repeatable',
group : '.simple-fields-metabox-field-group',
field : '.simple-fields-metabox-field',
handle : '.simple-fields-metabox-field-group-handle',
del : '.simple-fields-metabox-field-group-delete',
editor : '.simple_fields_editor_switch'
}
/*
Most elements receive custom CSS classes.
Otherwise both _script.js and _styles.css would have to be updated whenever a selector changed in the original plugin.
*/
function initFieldGroups(){
$(selectors.wrapper).each(function(){
var wrapper = $(this).addClass('sfp-wrapper'),
repeatable = (wrapper.find(selectors.repeatable).length) ? true : false;
if (repeatable) {
wrapper.addClass('sfp-wrapper-repeatable');
}
// Add button to toggle field groups (e.g. for easier sorting)
if (repeatable && !wrapper.find('.sfp-toggle').length && wrapper.find(selectors.group).length) {
var toggle = $('<div class="sfp-toggle">Toggle</div>');
toggle.prependTo(wrapper).click(function(){
$(this).parent().find('.sfp-inner').toggle();
return false;
});
}
// Wrap each field group in additional container and add standard WP classes to apply "default" styles
wrapper.find(selectors.group).each(function(){
var group = $(this).addClass('sfp-group'),
title = 'Field group ' + (group.index()+1),
handle = $('<div class="handlediv"> </div>');
if (group.find('.sfp-inner').length > 0) {
group.find('.sfp-handle h3').text(title);
return true;
}
// Custom styling for checkboxes / radiobuttons
group.find('.simple-fields-for-checkbox').parent().addClass('sfp-field-checkbox');
group.find('.simple-fields-metabox-field-radiobutton').parent().addClass('sfp-field-radiobutton');
if (repeatable) {
group.wrapInner('<div class="sfp-inner" />').addClass('postbox');
group.find(selectors.handle).addClass('sfp-handle').prependTo(group).text(title).wrapInner('<h3 />');
handle.prependTo(group).click(function(){
group.find('.sfp-inner').toggle();
});
group.find(selectors.del).addClass('sfp-delete');
}
group.find(selectors.field).addClass('sfp-field');
group.find(selectors.editor).addClass('sfp-editor');
});
});
}
initFieldGroups();
// Init again after inserting new field group (timeout needed due to Simple Field's inserting effect)
$(selectors.add).addClass('sfp-add').click(function(){
setTimeout(function(){
initFieldGroups();
}, 1000);
});
})(jQuery);