| 1 |
"use strict"; |
| 2 |
|
| 3 |
jQuery.fn.wpMediaElementFile = function (options) |
| 4 |
{ |
| 5 |
var defaults = { |
| 6 |
obj: null, |
| 7 |
mediaControl: null, |
| 8 |
addImgLinkSelector: '.upload-custom-img', |
| 9 |
delImgLinkSelector: '.delete-custom-img', |
| 10 |
imgContainerSelector: '.custom-img-container', |
| 11 |
imgIdInputSelector: '.logo_file_id', |
| 12 |
addImgLink: null, |
| 13 |
delImgLink: null, |
| 14 |
imgContainer: null, |
| 15 |
imgIdInput: null, |
| 16 |
frame: null |
| 17 |
}; |
| 18 |
var options = jQuery.extend(defaults, options); |
| 19 |
|
| 20 |
/* Public API */ |
| 21 |
this.getCurrent = function() |
| 22 |
{ |
| 23 |
return options.obj; |
| 24 |
} |
| 25 |
|
| 26 |
return this.each (function () |
| 27 |
{ |
| 28 |
options.obj = jQuery(this); |
| 29 |
|
| 30 |
options.addImgLink = options.obj.find(options.addImgLinkSelector); |
| 31 |
options.delImgLink = options.obj.find(options.delImgLinkSelector); |
| 32 |
options.imgContainer = options.obj.find(options.imgContainerSelector); |
| 33 |
options.imgIdInput = options.obj.find(options.imgIdInputSelector); |
| 34 |
|
| 35 |
init_start(); |
| 36 |
|
| 37 |
return this; |
| 38 |
}); |
| 39 |
|
| 40 |
function init_start() |
| 41 |
{ |
| 42 |
options.frame = wp.media({ |
| 43 |
title: 'Select or Upload File Of Your Chosen Persuasion', |
| 44 |
library: { |
| 45 |
type: 'image' |
| 46 |
}, |
| 47 |
button: { |
| 48 |
text: 'Use this media' |
| 49 |
}, |
| 50 |
multiple: false |
| 51 |
}); |
| 52 |
|
| 53 |
options.frame.on( 'open', updateFrame ).state('library').on( 'select', selectImg ); |
| 54 |
|
| 55 |
options.addImgLink.on( 'click', function( e ) { |
| 56 |
e.preventDefault(); |
| 57 |
|
| 58 |
options.frame.open(); |
| 59 |
}); |
| 60 |
|
| 61 |
// DELETE IMAGE LINK |
| 62 |
options.delImgLink.on( 'click', function( event ){ |
| 63 |
|
| 64 |
event.preventDefault(); |
| 65 |
|
| 66 |
// Clear out the preview image |
| 67 |
options.imgContainer.html( '' ); |
| 68 |
|
| 69 |
// Un-hide the add image link |
| 70 |
options.addImgLink.removeClass( 'hidden' ); |
| 71 |
|
| 72 |
// Hide the delete image link |
| 73 |
options.delImgLink.addClass( 'hidden' ); |
| 74 |
|
| 75 |
// Delete the image id from the hidden input |
| 76 |
options.imgIdInput.val( '' ); |
| 77 |
|
| 78 |
//trigger update widget |
| 79 |
options.imgIdInput.change(); |
| 80 |
}); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
function updateFrame() |
| 85 |
{ |
| 86 |
// Do something when the media frame is opened. |
| 87 |
} |
| 88 |
|
| 89 |
function selectImg() |
| 90 |
{ |
| 91 |
// Get media attachment details from the frame state |
| 92 |
var attachment = options.frame.state().get('selection').first().toJSON(); |
| 93 |
// Send the attachment URL to our custom image input field. |
| 94 |
options.imgContainer.append( attachment.name ); |
| 95 |
|
| 96 |
// Send the attachment id to our hidden input |
| 97 |
options.imgIdInput.val( attachment.id ); |
| 98 |
|
| 99 |
// Hide the add image link |
| 100 |
options.addImgLink.addClass( 'hidden' ); |
| 101 |
|
| 102 |
// Unhide the remove image link |
| 103 |
options.delImgLink.removeClass( 'hidden' ); |
| 104 |
|
| 105 |
//trigger update widget |
| 106 |
options.imgIdInput.change(); |
| 107 |
} |
| 108 |
|
| 109 |
} |