| 1 |
;(function($) { |
| 2 |
|
| 3 |
|
| 4 |
$(document).ready( function($) { |
| 5 |
$('#BS_guest_author_image_media_manager_edit_button').click(handleEditButtonClick); |
| 6 |
$('#BS_guest_author_image_media_manager_remove_button').click(handleRemoveButtonClick); |
| 7 |
}); |
| 8 |
|
| 9 |
function handleEditButtonClick (e) { |
| 10 |
e.preventDefault(); |
| 11 |
var media_manager = $("#BS_guest_author_image_media_manager"); |
| 12 |
|
| 13 |
console.log(media_manager.hasClass('disabled')); |
| 14 |
|
| 15 |
if (media_manager.hasClass('disabled')) |
| 16 |
return false; |
| 17 |
|
| 18 |
var image_frame; |
| 19 |
if(image_frame) { |
| 20 |
image_frame.open(); |
| 21 |
} |
| 22 |
// Define image_frame as wp.media object |
| 23 |
image_frame = wp.media({ |
| 24 |
title: 'Select Media', |
| 25 |
multiple : false, |
| 26 |
library : { |
| 27 |
type : 'image' |
| 28 |
} |
| 29 |
}); |
| 30 |
image_frame.on('close',function() { |
| 31 |
// On close, get selections and save to the hidden input |
| 32 |
// plus other AJAX stuff to refresh the image preview |
| 33 |
var selection = image_frame.state().get('selection'); |
| 34 |
var gallery_ids = []; |
| 35 |
var my_index = 0; |
| 36 |
selection.each(function(attachment) { |
| 37 |
gallery_ids[my_index] = attachment['id']; |
| 38 |
my_index++; |
| 39 |
}); |
| 40 |
var ids = gallery_ids.join(","); |
| 41 |
$('input#BS_guest_author_image_id').val(ids); |
| 42 |
Refresh_Image(ids); |
| 43 |
}); |
| 44 |
|
| 45 |
image_frame.on('open',function() { |
| 46 |
// On open, get the id from the hidden input |
| 47 |
// and select the appropriate images in the media manager |
| 48 |
var selection = image_frame.state().get('selection'); |
| 49 |
ids = $('input#BS_guest_author_image_id').val().split(','); |
| 50 |
ids.forEach(function(id) { |
| 51 |
var attachment = wp.media.attachment(id); |
| 52 |
attachment.fetch(); |
| 53 |
selection.add( attachment ? [ attachment ] : [] ); |
| 54 |
}); |
| 55 |
|
| 56 |
}); |
| 57 |
|
| 58 |
image_frame.open(); |
| 59 |
} |
| 60 |
|
| 61 |
function handleRemoveButtonClick (e) { |
| 62 |
var media_manager = $("#BS_guest_author_image_media_manager"); |
| 63 |
|
| 64 |
if (media_manager.hasClass('disabled')) |
| 65 |
return false; |
| 66 |
|
| 67 |
var imageContainer = $('#BS_guest_author_image_media_manager_image'); |
| 68 |
var defaultImageUrl = imageContainer.attr('data-default-image'); |
| 69 |
var defaultImageElement = $("<img src='"+ defaultImageUrl +"' id='BS-guest-author-preview-image' />"); |
| 70 |
imageContainer.html( defaultImageElement ); |
| 71 |
|
| 72 |
// $('#BS_guest_author_image_media_manager').addClass('BS_default-image'); |
| 73 |
$('input#BS_guest_author_image_id').val(''); |
| 74 |
$("#BS_guest_author_image_media_manager_remove_button").remove(); |
| 75 |
} |
| 76 |
|
| 77 |
// Ajax request to refresh the image preview |
| 78 |
function Refresh_Image(the_id){ |
| 79 |
var data = { |
| 80 |
action: 'BS_get_image', |
| 81 |
id: the_id |
| 82 |
}; |
| 83 |
|
| 84 |
$.get(ajaxurl, data, function(response) { |
| 85 |
if(response.success === true) { |
| 86 |
if (response.data.image.startsWith('<img')){ |
| 87 |
var removeButton = $("<p id=\"BS_guest_author_image_media_manager_remove_button\"><span class=\"dashicons dashicons-no-alt\"></span><span class=\"screen-reader-text\">Remove</span></p>"); |
| 88 |
removeButton.click(handleRemoveButtonClick); |
| 89 |
$("#BS_guest_author_image_media_manager_edit_panel").append(removeButton); |
| 90 |
$('#BS_guest_author_image_media_manager_image').html(response.data.image) |
| 91 |
|
| 92 |
} |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
})(jQuery); |