| 1 |
/** |
| 2 |
* |
| 3 |
* wpMediaUploader v1.0 2016-11-05 |
| 4 |
* Copyright (c) 2016 Smartcat |
| 5 |
* |
| 6 |
*/ |
| 7 |
( function(jQuery) { |
| 8 |
var $ = jQuery; |
| 9 |
$.wpMediaUploader = function( options ) { |
| 10 |
var settings = $.extend({ |
| 11 |
|
| 12 |
target : '.smartcat-uploader', // The class wrapping the textbox |
| 13 |
uploaderTitle : 'Select or upload image', // The title of the media upload popup |
| 14 |
uploaderButton : 'Set image', // the text of the button in the media upload popup |
| 15 |
multiple : false, // Allow the user to select multiple images |
| 16 |
buttonText : 'Upload image', // The text of the upload button |
| 17 |
buttonClass : '.smartcat-upload', // the class of the upload button |
| 18 |
previewSize : '32px', // The preview image size |
| 19 |
modal : false, // is the upload button within a bootstrap modal ? |
| 20 |
buttonStyle : { // style the button |
| 21 |
color : '#fff', |
| 22 |
background : '#3bafda', |
| 23 |
fontSize : '16px', |
| 24 |
padding : '10px 15px', |
| 25 |
}, |
| 26 |
|
| 27 |
}, options ); |
| 28 |
|
| 29 |
if(typeof $(settings.target).data("image") != 'undefined') { |
| 30 |
var img = $(settings.target).data("image"); |
| 31 |
}else{ |
| 32 |
var img = ""; |
| 33 |
} |
| 34 |
|
| 35 |
$( settings.target ).append( '<a href="#" class="' + settings.buttonClass.replace('.','') + ' button">' + settings.buttonText + '</a>' ); |
| 36 |
$( settings.target ).append('<div><br><div class="button-preview"><img src="'+ (img == '' ? '#' : img) +'" style="' + (img == "" ? 'display: none;' : '') +' width: ' + settings.previewSize + '; vertical-align: middle;"/></div></div>') |
| 37 |
|
| 38 |
$( settings.buttonClass ).css( settings.buttonStyle ); |
| 39 |
|
| 40 |
$('body').on('click', settings.buttonClass, function(e) { |
| 41 |
|
| 42 |
e.preventDefault(); |
| 43 |
var selector = $(this).parent( settings.target ); |
| 44 |
var custom_uploader = wp.media({ |
| 45 |
title: settings.uploaderTitle, |
| 46 |
button: { |
| 47 |
text: settings.uploaderButton |
| 48 |
}, |
| 49 |
multiple: settings.multiple |
| 50 |
}) |
| 51 |
.on('select', function() { |
| 52 |
var attachment = custom_uploader.state().get('selection').first().toJSON(); |
| 53 |
selector.find( 'img' ).attr( 'src', attachment.url).show(); |
| 54 |
selector.find( 'input' ).val(attachment.url); |
| 55 |
if( settings.modal ) { |
| 56 |
$('.modal').css( 'overflowY', 'auto'); |
| 57 |
} |
| 58 |
}) |
| 59 |
.open(); |
| 60 |
}); |
| 61 |
} |
| 62 |
})(jQuery); |
| 63 |
|