| 1 |
"use strict"; |
| 2 |
|
| 3 |
jQuery.fn.wpMediaElement = 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_image_id', |
| 12 |
addImgLink: null, |
| 13 |
delImgLink: null, |
| 14 |
imgContainer: null, |
| 15 |
imgIdInput: null, |
| 16 |
frame: {}, |
| 17 |
isfileUpload: false, |
| 18 |
}; |
| 19 |
|
| 20 |
var options = jQuery.extend(defaults, options); |
| 21 |
|
| 22 |
if(typeof wpmediaelement_parameters !== 'undefined' && typeof options.frame.title == 'undefined'){ |
| 23 |
options.frame.title = wpmediaelement_parameters.text.frame_title; |
| 24 |
} else if(typeof options.frame.title == 'undefined') { |
| 25 |
options.frame.title = 'Select or Upload Media Of Your Chosen Persuasion'; |
| 26 |
} |
| 27 |
|
| 28 |
if(typeof wpmediaelement_parameters !== 'undefined' && typeof options.frame.button == 'undefined'){ |
| 29 |
options.frame.button = wpmediaelement_parameters.text.frame_button; |
| 30 |
} else if(typeof options.frame.button == 'undefined') { |
| 31 |
options.frame.button = 'Use this media'; |
| 32 |
} |
| 33 |
|
| 34 |
/* Public API */ |
| 35 |
this.getCurrent = function() |
| 36 |
{ |
| 37 |
return options.obj; |
| 38 |
} |
| 39 |
|
| 40 |
return this.each (function () |
| 41 |
{ |
| 42 |
options.obj = jQuery(this); |
| 43 |
|
| 44 |
options.addImgLink = options.obj.find(options.addImgLinkSelector); |
| 45 |
options.delImgLink = options.obj.find(options.delImgLinkSelector); |
| 46 |
options.imgContainer = options.obj.find(options.imgContainerSelector); |
| 47 |
options.imgIdInput = options.obj.find(options.imgIdInputSelector); |
| 48 |
|
| 49 |
init_start(); |
| 50 |
|
| 51 |
return this; |
| 52 |
}); |
| 53 |
|
| 54 |
function init_start() |
| 55 |
{ |
| 56 |
console.log('init_start'+options.obj.attr('id')); |
| 57 |
|
| 58 |
options.frame = wp.media({ |
| 59 |
title: options.frame.title, |
| 60 |
library: { |
| 61 |
type: 'image' |
| 62 |
}, |
| 63 |
button: { |
| 64 |
text: options.frame.button |
| 65 |
}, |
| 66 |
multiple: false |
| 67 |
}); |
| 68 |
|
| 69 |
options.frame.on( 'open', updateFrame ).state('library').on( 'select', selectImg ); |
| 70 |
|
| 71 |
options.addImgLink.on( 'click', function( e ) { |
| 72 |
e.preventDefault(); |
| 73 |
|
| 74 |
options.frame.open(); |
| 75 |
}); |
| 76 |
|
| 77 |
// DELETE IMAGE LINK |
| 78 |
options.delImgLink.on( 'click', function( event ){ |
| 79 |
|
| 80 |
event.preventDefault(); |
| 81 |
|
| 82 |
// Clear out the preview image |
| 83 |
options.imgContainer.html( '' ); |
| 84 |
|
| 85 |
// Un-hide the add image link |
| 86 |
options.addImgLink.removeClass( 'hidden' ); |
| 87 |
|
| 88 |
// Hide the delete image link |
| 89 |
options.delImgLink.addClass( 'hidden' ); |
| 90 |
|
| 91 |
// Delete the image id from the hidden input |
| 92 |
options.imgIdInput.val( '' ); |
| 93 |
|
| 94 |
//trigger update widget |
| 95 |
options.imgIdInput.change(); |
| 96 |
}); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
function updateFrame() |
| 101 |
{ |
| 102 |
// Do something when the media frame is opened. |
| 103 |
} |
| 104 |
|
| 105 |
function selectImg() |
| 106 |
{ |
| 107 |
// Get media attachment details from the frame state |
| 108 |
var attachment = options.frame.state().get('selection').first().toJSON(); |
| 109 |
|
| 110 |
// Send the attachment URL to our custom image input field. |
| 111 |
if (options.isfileUpload) { |
| 112 |
options.imgContainer.append( attachment.name ); |
| 113 |
} else { |
| 114 |
options.imgContainer.append( '<img src="'+attachment.url+'" alt="" style="max-width:100%;"/>' ); |
| 115 |
} |
| 116 |
// Send the attachment id to our hidden input |
| 117 |
options.imgIdInput.val( attachment.id ); |
| 118 |
|
| 119 |
// Hide the add image link |
| 120 |
options.addImgLink.addClass( 'hidden' ); |
| 121 |
|
| 122 |
// Unhide the remove image link |
| 123 |
options.delImgLink.removeClass( 'hidden' ); |
| 124 |
|
| 125 |
//trigger update widget |
| 126 |
options.imgIdInput.change(); |
| 127 |
} |
| 128 |
|
| 129 |
} |