2013-11-14 00:40:11 -05:00
|
|
|
/* global ajaxurl */
|
2012-08-22 20:04:18 -04:00
|
|
|
(function($) {
|
|
|
|
$(document).ready(function() {
|
2013-11-14 00:40:11 -05:00
|
|
|
var frame,
|
|
|
|
bgImage = $( '#custom-background-image' );
|
2012-09-26 21:57:38 -04:00
|
|
|
|
|
|
|
$('#background-color').wpColorPicker({
|
|
|
|
change: function( event, ui ) {
|
|
|
|
bgImage.css('background-color', ui.color.toString());
|
|
|
|
},
|
|
|
|
clear: function() {
|
|
|
|
bgImage.css('background-color', '');
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
$('input[name="background-position-x"]').change(function() {
|
2012-09-26 21:57:38 -04:00
|
|
|
bgImage.css('background-position', $(this).val() + ' top');
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
$('input[name="background-repeat"]').change(function() {
|
2012-09-26 21:57:38 -04:00
|
|
|
bgImage.css('background-repeat', $(this).val());
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2012-11-09 06:37:24 -05:00
|
|
|
$('#choose-from-library-link').click( function( event ) {
|
|
|
|
var $el = $(this);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
2012-12-04 11:21:57 -05:00
|
|
|
// If the media frame already exists, reopen it.
|
2012-11-09 06:37:24 -05:00
|
|
|
if ( frame ) {
|
|
|
|
frame.open();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:21:57 -05:00
|
|
|
// Create the media frame.
|
2012-12-06 00:06:49 -05:00
|
|
|
frame = wp.media.frames.customBackground = wp.media({
|
2012-12-04 11:21:57 -05:00
|
|
|
// Set the title of the modal.
|
|
|
|
title: $el.data('choose'),
|
|
|
|
|
|
|
|
// Tell the modal to show only images.
|
|
|
|
library: {
|
2012-11-09 06:37:24 -05:00
|
|
|
type: 'image'
|
2012-12-04 11:21:57 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Customize the submit button.
|
|
|
|
button: {
|
|
|
|
// Set the text of the button.
|
|
|
|
text: $el.data('update'),
|
|
|
|
// Tell the button not to close the modal, since we're
|
|
|
|
// going to refresh the page when the image is selected.
|
|
|
|
close: false
|
2012-11-09 06:37:24 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-12-04 11:21:57 -05:00
|
|
|
// When an image is selected, run a callback.
|
|
|
|
frame.on( 'select', function() {
|
|
|
|
// Grab the selected attachment.
|
|
|
|
var attachment = frame.state().get('selection').first();
|
2020-10-29 15:00:24 -04:00
|
|
|
var nonceValue = $( '#_wpnonce' ).val() || '';
|
2012-12-04 11:21:57 -05:00
|
|
|
|
|
|
|
// Run an AJAX request to set the background image.
|
|
|
|
$.post( ajaxurl, {
|
|
|
|
action: 'set-background-image',
|
|
|
|
attachment_id: attachment.id,
|
2020-10-29 15:00:24 -04:00
|
|
|
_ajax_nonce: nonceValue,
|
2012-12-04 11:21:57 -05:00
|
|
|
size: 'full'
|
|
|
|
}).done( function() {
|
|
|
|
// When the request completes, reload the window.
|
|
|
|
window.location.reload();
|
2012-11-09 06:37:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-12-04 11:21:57 -05:00
|
|
|
// Finally, open the modal.
|
|
|
|
frame.open();
|
2012-11-09 06:37:24 -05:00
|
|
|
});
|
|
|
|
});
|
2013-11-14 00:40:11 -05:00
|
|
|
})(jQuery);
|