Skip to content

Commit eb97d06

Browse files
committed
Add a custom and different JS for the admin render of the field
1 parent b263dd1 commit eb97d06

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

assets/js/input-5.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(function ($) {
2+
function initialize_field($field) {
3+
var input = $field.find('.acf-input input');
4+
var allowClear = $(input).attr('data-allow-clear') || 0;
5+
var opts = {
6+
dropdownCssClass: "bigdrop widefat",
7+
dropdownAutoWidth: true,
8+
formatResult: svg_icon_format,
9+
formatSelection: svg_icon_format_small,
10+
data: {results: svg_icon_format_data},
11+
allowClear: 1 == allowClear
12+
};
13+
14+
input.select2(opts);
15+
16+
/**
17+
* Format the content in select 2 for the selected item
18+
*
19+
* @param css
20+
* @returns {string}
21+
*/
22+
function svg_icon_format(css) {
23+
return '<svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use> </svg>' + css.text;
24+
}
25+
26+
/**
27+
* Format the content in select 2 for the dropdown list
28+
*
29+
* @param css
30+
* @returns {string}
31+
*/
32+
function svg_icon_format_small(css) {
33+
return '<svg class="acf_svg__icon small icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use> </svg>' + css.text;
34+
}
35+
36+
}
37+
38+
/*
39+
* ready append (ACF5)
40+
*
41+
* These are 2 events which are fired during the page load
42+
* ready = on page load similar to jQuery(document).ready()
43+
* append = on new DOM elements appended via repeater field
44+
*
45+
* @type event
46+
* @date 20/07/13
47+
*
48+
* @param jQueryel (jQuery selection) the jQuery element which contains the ACF fields
49+
* @return n/a
50+
*/
51+
acf.add_action('ready append', function (jQueryel) {
52+
// search jQueryel for fields of type 'FIELD_NAME'
53+
acf.get_fields({type: 'svg_icon'}, jQueryel).each(function () {
54+
initialize_field($(this));
55+
});
56+
});
57+
})(jQuery);
File renamed without changes.

assets/js/input-56.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(function ($) {
2+
function initialize_field($field) {
3+
var input = $field.find('.acf-input input');
4+
var allowClear = $(input).attr('data-allow-clear') || 0;
5+
var opts = {
6+
dropdownCssClass: "bigdrop widefat",
7+
dropdownAutoWidth: true,
8+
templateResult: bea_svg_format,
9+
templateSelection: bea_svg_format_small,
10+
data: svg_icon_format_data,
11+
allowClear: 1 == allowClear,
12+
};
13+
14+
input.select2(opts);
15+
16+
/**
17+
* Format the content in select 2 for the selected item
18+
*
19+
* @param css
20+
* @returns {string}
21+
*/
22+
function bea_svg_format(css) {
23+
if (!css.id) { return css.text; }
24+
return $('<span class="acf_svg__span"><svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text + '</span>');
25+
};
26+
27+
/**
28+
* Format the content in select 2 for the dropdown list
29+
*
30+
* @param css
31+
* @returns {string}
32+
*/
33+
function bea_svg_format_small(css) {
34+
if (!css.id) { return css.text; }
35+
return $('<span class="acf_svg__span"><svg class="acf_svg__icon small icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text + '</span>');
36+
};
37+
}
38+
39+
/*
40+
* ready append (ACF5)
41+
*
42+
* These are 2 events which are fired during the page load
43+
* ready = on page load similar to jQuery(document).ready()
44+
* append = on new DOM elements appended via repeater field
45+
*
46+
* @type event
47+
* @date 20/07/13
48+
*
49+
* @param jQueryel (jQuery selection) the jQuery element which contains the ACF fields
50+
* @return n/a
51+
*/
52+
acf.add_action('ready append', function (jQueryel) {
53+
// search jQueryel for fields of type 'FIELD_NAME'
54+
acf.get_fields({type: 'svg_icon'}, jQueryel).each(function () {
55+
initialize_field($(this));
56+
});
57+
});
58+
})(jQuery);

assets/js/input-56.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)