-
Notifications
You must be signed in to change notification settings - Fork 13
/
admin.js
78 lines (63 loc) · 2.15 KB
/
admin.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
/**
* Better Font Awesome Library admin JS.
*
* @since 1.0.3
*
* @package Better Font Awesome Library
*/
( function( $ ) {
var icons = Object.values( bfa_vars.fa_icons );
function get_icon_by_title( title ) {
return icons.find( function( icon ) {
return icon.title == title;
});
}
function icon_shortcode( icon ) {
var icon_style_string = icon.style ? ' style="' + icon.style + '"' : '';
return '[icon name="' + icon.slug + '"' + icon_style_string + ' class="" unprefixed_class=""]';
}
$( function() {
// We initialize on click instead of document.ready to ensure
// that BFA triggers still work when dynamically loaded later
// (e.g. repeatable fields/editors).
//
// We use the 'mousedown' handler so that we can effectively
// initialize the trigger, then manually trigger a 'click'
// event to trigger the iconpicker's click handler, all
// without causing an infinite loop.
$( 'body' ).on( 'mousedown', '.bfa-iconpicker', function(e) {
var $iconPicker = $( this );
// Initialize if not already initialized.
$iconPicker.not( '.initialized' )
.addClass( 'initialized' )
.iconpicker({
placement: 'bottomLeft',
hideOnSelect: true,
icons: icons,
fullClassFormatter: function( icon_title ) {
var classes = [];
var icon = get_icon_by_title( icon_title );
return icon.base_class;
},
})
// Place cursor focus on the search input.
.on( 'iconpickerShown', function( e ) {
$iconPicker.find( '.iconpicker-search' ).trigger( 'focus');
})
// Handle inserting selected icon into editor.
.on( 'iconpickerSelected', function( e ) {
var icon_title = e.iconpickerItem.title.replace( '.', '' );
var icon = get_icon_by_title( icon_title );
wp.media.editor.insert( icon_shortcode( icon ) );
})
// Fake a click to ensure trigger the iconpicker's native click handler.
.trigger( 'click' )
// Clean up human-readable icon names.
.find( '.iconpicker-item' ).each( function() {
var $item = $( this );
var title = $item.attr( 'title' ).replace( '.', '' );
$item.attr( 'title', title );
});
});
});
} )( jQuery );