| 1 |
/*eslint-env jquery*/ |
| 2 |
/*eslint no-unused-vars: 0*/ |
| 3 |
/*global wp, console*/ |
| 4 |
(function( $ ) { |
| 5 |
|
| 6 |
// Add Color Picker to all inputs that have 'color-field' class |
| 7 |
$(function() { |
| 8 |
$('.color-field').wpColorPicker(); |
| 9 |
}); |
| 10 |
|
| 11 |
var $cryptX_option = $('#opt_linktext4'), |
| 12 |
$cryptX_notice = $('#opt_linktext4_notice'), |
| 13 |
$cryptX_image_id = $('#image_attachment_id'), |
| 14 |
$cryptX_preview = $('#image-preview'), |
| 15 |
$cryptX_addButton = $('#upload_image_button'), |
| 16 |
$cryptX_delButton = $('#remove_image_button'), |
| 17 |
$cryptX_altOption = $('#opt_linktext'), |
| 18 |
file_frame, |
| 19 |
wp_media_post_id, |
| 20 |
set_to_post_id; |
| 21 |
|
| 22 |
function cryptx_check_image_state() { |
| 23 |
if ($cryptX_image_id.val() == 0) { |
| 24 |
if( $cryptX_option.prop('checked') == true ) { |
| 25 |
$cryptX_option.prop('checked', false); |
| 26 |
$cryptX_altOption.prop('checked', true); |
| 27 |
} |
| 28 |
$cryptX_option.prop("disabled", true); |
| 29 |
$cryptX_notice.removeClass( 'hidden' ); |
| 30 |
$cryptX_preview.addClass( 'hidden' ); |
| 31 |
$cryptX_addButton.removeClass( 'hidden' ); |
| 32 |
$cryptX_delButton.addClass( 'hidden' ); |
| 33 |
} else { |
| 34 |
$cryptX_option.prop("disabled", false); |
| 35 |
$cryptX_notice.addClass( 'hidden' ); |
| 36 |
$cryptX_preview.removeClass( 'hidden' ); |
| 37 |
$cryptX_addButton.addClass( 'hidden' ); |
| 38 |
$cryptX_delButton.removeClass( 'hidden' ); |
| 39 |
} |
| 40 |
} |
| 41 |
cryptx_check_image_state() |
| 42 |
|
| 43 |
$cryptX_image_id.on("change", function() { |
| 44 |
cryptx_check_image_state(); |
| 45 |
}); |
| 46 |
|
| 47 |
/* |
| 48 |
$cryptX_preview.on("load", function() { |
| 49 |
cryptx_check_image_state(); |
| 50 |
}); |
| 51 |
*/ |
| 52 |
|
| 53 |
// Uploading files |
| 54 |
$cryptX_addButton.on('click', function( event ){ |
| 55 |
var attachment; |
| 56 |
event.preventDefault(); |
| 57 |
set_to_post_id = $cryptX_image_id.val(); |
| 58 |
|
| 59 |
// If the media frame already exists, reopen it. |
| 60 |
if ( file_frame ) { |
| 61 |
// Set the post ID to what we want |
| 62 |
wp_media_post_id = wp.media.model.settings.post.id; // Store the old id |
| 63 |
file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); |
| 64 |
// Open frame |
| 65 |
file_frame.open(); |
| 66 |
return; |
| 67 |
} else { |
| 68 |
// Set the wp.media post id so the uploader grabs the ID we want when initialised |
| 69 |
wp.media.model.settings.post.id = set_to_post_id; |
| 70 |
} |
| 71 |
|
| 72 |
// Create the media frame. |
| 73 |
file_frame = wp.media.frames.file_frame = wp.media({ |
| 74 |
title: 'Select a image to upload', |
| 75 |
button: { |
| 76 |
text: 'Use this image' |
| 77 |
}, |
| 78 |
multiple: false // Set to true to allow multiple files to be selected |
| 79 |
}); |
| 80 |
|
| 81 |
// When an image is selected, run a callback. |
| 82 |
file_frame.on( 'select', function() { |
| 83 |
// We set multiple to false so only get one image from the uploader |
| 84 |
attachment = file_frame.state().get('selection').first().toJSON(); |
| 85 |
// Do something with attachment.id and/or attachment.url here |
| 86 |
$cryptX_preview.attr( 'src', attachment.url ).css( 'width', 'auto' ); |
| 87 |
$cryptX_image_id.val( attachment.id ); |
| 88 |
$cryptX_image_id.trigger('change'); |
| 89 |
// Un-hide the add image link |
| 90 |
// $cryptX_addButton.addClass( 'hidden' ); |
| 91 |
// Hide the delete image link |
| 92 |
// $cryptX_delButton.removeClass( 'hidden' ); |
| 93 |
// Restore the main post ID |
| 94 |
wp.media.model.settings.post.id = wp_media_post_id; |
| 95 |
}); |
| 96 |
|
| 97 |
// Finally, open the modal |
| 98 |
file_frame.open(); |
| 99 |
}); |
| 100 |
|
| 101 |
// Restore the main ID when the add media button is pressed |
| 102 |
$( 'a.add_media' ).on( 'click', function() { |
| 103 |
wp.media.model.settings.post.id = wp_media_post_id; |
| 104 |
}); |
| 105 |
|
| 106 |
// DELETE IMAGE LINK |
| 107 |
$cryptX_delButton.on( 'click', function( event ){ |
| 108 |
event.preventDefault(); |
| 109 |
// Clear out the preview image |
| 110 |
$cryptX_preview.attr( 'src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==' ); |
| 111 |
// Un-hide the add image link |
| 112 |
$cryptX_addButton.removeClass( 'hidden' ); |
| 113 |
// Hide the delete image link |
| 114 |
$cryptX_delButton.addClass( 'hidden' ); |
| 115 |
// Delete the image id from the hidden input |
| 116 |
$cryptX_image_id.val( '0' ).trigger('change'); |
| 117 |
}); |
| 118 |
|
| 119 |
|
| 120 |
})( jQuery ); |