jQuery( document ).ready(function( $ ) {
// Media Uploader
$( document ).on( 'click', '.aigpl-img-uploader', function() {
var imgfield, showfield;
imgfield = jQuery(this).prev('input').attr('id');
showfield = jQuery(this).parents('td').find('.aigpl-imgs-preview');
var multiple_img = jQuery(this).attr('data-multiple');
multiple_img = (typeof(multiple_img) != 'undefined' && multiple_img == 'true') ? true : false;
if( typeof wp == "undefined" || AigplAdmin.new_ui != '1' ) { // check for media uploader
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html) {
if(imgfield) {
var mediaurl = $('img',html).attr('src');
$('#'+imgfield).val(mediaurl);
showfield.html('');
tb_remove();
imgfield = '';
} else {
window.original_send_to_editor(html);
}
};
return false;
} else {
var file_frame;
//window.formfield = '';
//new media uploader
var button = jQuery(this);
// If the media frame already exists, reopen it.
if ( file_frame ) {
file_frame.open();
return;
}
if( multiple_img == true ) {
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: button.data( 'title' ),
button: {
text: button.data( 'button-text' ),
},
multiple: true // Set to true to allow multiple files to be selected
});
} else {
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
frame: 'post',
state: 'insert',
title: button.data( 'title' ),
button: {
text: button.data( 'button-text' ),
},
multiple: false // Set to true to allow multiple files to be selected
});
}
file_frame.on( 'menu:render:default', function(view) {
// Store our views in an object.
var views = {};
// Unset default menu items
view.unset('library-separator');
view.unset('gallery');
view.unset('featured-image');
view.unset('embed');
// Initialize the views in our view object.
view.set(views);
});
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
// Get selected size from media uploader
var selected_size = $('.attachment-display-settings .size').val();
var selection = file_frame.state().get('selection');
selection.each( function( attachment, index ) {
attachment = attachment.toJSON();
// Selected attachment url from media uploader
var attachment_id = attachment.id ? attachment.id : '';
if( attachment_id && attachment.sizes && multiple_img == true ) {
var attachment_url = attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
var attachment_edit_link = attachment.editLink ? attachment.editLink : '';
showfield.append('\