| 1 |
(function( $ ) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
/** |
| 5 |
* All of the code for your admin-facing JavaScript source |
| 6 |
* should reside in this file. |
| 7 |
* |
| 8 |
* Note: It has been assumed you will write jQuery code here, so the |
| 9 |
* $ function reference has been prepared for usage within the scope |
| 10 |
* of this function. |
| 11 |
* |
| 12 |
* This enables you to define handlers, for when the DOM is ready: |
| 13 |
* |
| 14 |
* $(function() { |
| 15 |
* |
| 16 |
* }); |
| 17 |
* |
| 18 |
* When the window is loaded: |
| 19 |
* |
| 20 |
* $( window ).load(function() { |
| 21 |
* |
| 22 |
* }); |
| 23 |
* |
| 24 |
* ...and/or other possibilities. |
| 25 |
* |
| 26 |
* Ideally, it is not considered best practise to attach more than a |
| 27 |
* single DOM-ready or window-load handler for a particular page. |
| 28 |
* Although scripts in the WordPress core, Plugins and Themes may be |
| 29 |
* practising this, we should strive to set a better example in our own work. |
| 30 |
*/ |
| 31 |
jQuery(document).ready(function ($) { |
| 32 |
|
| 33 |
$(document).on( "click", ".upload-media-button", function (e) { |
| 34 |
e.preventDefault(); |
| 35 |
var $button = $(this); |
| 36 |
|
| 37 |
// Create the media frame. |
| 38 |
var file_frame = wp.media.frames.file_frame = wp.media({ |
| 39 |
title: 'Select or upload media', |
| 40 |
button: { |
| 41 |
text: 'Select' |
| 42 |
}, |
| 43 |
multiple: false // Set to true to allow multiple files to be selected |
| 44 |
}); |
| 45 |
|
| 46 |
// When an image is selected, run a callback. |
| 47 |
file_frame.on('select', function () { |
| 48 |
// We set multiple to false so only get one image from the uploader |
| 49 |
|
| 50 |
var attachment = file_frame.state().get('selection').first().toJSON(); |
| 51 |
|
| 52 |
$button.siblings('input').val(attachment.url); |
| 53 |
$button.siblings('span.image-holder').html('<img src="' + attachment.url + '">'); |
| 54 |
}); |
| 55 |
|
| 56 |
// Finally, open the modal |
| 57 |
file_frame.open(); |
| 58 |
}); |
| 59 |
|
| 60 |
$('.image-url').each(function(){ |
| 61 |
$(this).on('keyup', function(){ |
| 62 |
$(this).siblings('span.image-holder').html('<img src="' + $(this).val() + '">'); |
| 63 |
}); |
| 64 |
}); |
| 65 |
|
| 66 |
|
| 67 |
/* Show More event */ |
| 68 |
$('.more').on('click', function() { |
| 69 |
$(this).next('div.advanced-section').toggle(); |
| 70 |
}); |
| 71 |
|
| 72 |
$(".more").toggle(function() { |
| 73 |
$(this).html('Hide Options<i class="dashicons dashicons-arrow-up"></i>') |
| 74 |
.stop(); |
| 75 |
}, function() { |
| 76 |
$(this).html('More Options<i class="dashicons dashicons-arrow-down"></i>') |
| 77 |
.stop(); |
| 78 |
}); |
| 79 |
|
| 80 |
$(document).on('widget-added', function(e, widget) { |
| 81 |
/* Show More event */ |
| 82 |
$('.more').on('click', function() { |
| 83 |
$(this).next('div.advanced-section').toggle(); |
| 84 |
}); |
| 85 |
|
| 86 |
$(".more").toggle(function() { |
| 87 |
$(this).html('Hide Options<i class="dashicons dashicons-arrow-up"></i>') |
| 88 |
.stop(); |
| 89 |
}, function() { |
| 90 |
$(this).html('More Options<i class="dashicons dashicons-arrow-down"></i>') |
| 91 |
.stop(); |
| 92 |
}); |
| 93 |
}); |
| 94 |
|
| 95 |
$(document).on('widget-updated', function(e, widget) { |
| 96 |
/* Show More event */ |
| 97 |
$('.more').on('click', function() { |
| 98 |
$(this).next('div.advanced-section').toggle(); |
| 99 |
}); |
| 100 |
|
| 101 |
$(".more").toggle(function() { |
| 102 |
$(this).html('Hide Options<i class="dashicons dashicons-arrow-up"></i>') |
| 103 |
.stop(); |
| 104 |
}, function() { |
| 105 |
$(this).html('More Options<i class="dashicons dashicons-arrow-down"></i>') |
| 106 |
.stop(); |
| 107 |
}); |
| 108 |
}); |
| 109 |
|
| 110 |
}); |
| 111 |
|
| 112 |
})( jQuery ); |
| 113 |
|