| 1 |
"use strict"; |
| 2 |
|
| 3 |
jQuery.fn.wpMediaMultiple = 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: null |
| 17 |
}; |
| 18 |
|
| 19 |
var options = jQuery.extend(defaults, options); |
| 20 |
|
| 21 |
/* Public API */ |
| 22 |
this.getCurrent = function() |
| 23 |
{ |
| 24 |
return options.obj; |
| 25 |
} |
| 26 |
|
| 27 |
return this.each (function () |
| 28 |
{ |
| 29 |
options.obj = jQuery(this); |
| 30 |
|
| 31 |
options.addImgLink = options.obj.find(options.addImgLinkSelector); |
| 32 |
options.delImgLink = options.obj.find(options.delImgLinkSelector); |
| 33 |
options.imgContainer = options.obj.find(options.imgContainerSelector); |
| 34 |
options.imgIdInput = options.obj.find(options.imgIdInputSelector); |
| 35 |
|
| 36 |
init_start(); |
| 37 |
|
| 38 |
return this; |
| 39 |
}); |
| 40 |
|
| 41 |
function init_start() |
| 42 |
{ |
| 43 |
console.log('init_start'+options.obj.attr('id')); |
| 44 |
|
| 45 |
options.frame = wp.media({ |
| 46 |
title: 'Select or Upload Media Of Your Chosen Persuasion', |
| 47 |
library: { |
| 48 |
type: 'image' |
| 49 |
}, |
| 50 |
button: { |
| 51 |
text: 'Use this media' |
| 52 |
}, |
| 53 |
multiple: true |
| 54 |
}); |
| 55 |
|
| 56 |
options.frame.on( 'open', updateFrame ).state('library').on( 'select', selectImg ); |
| 57 |
|
| 58 |
options.addImgLink.on( 'click', function( e ) { |
| 59 |
e.preventDefault(); |
| 60 |
|
| 61 |
options.frame.open(); |
| 62 |
}); |
| 63 |
|
| 64 |
// DELETE IMAGE LINK |
| 65 |
options.delImgLink.on( 'click', function( event ){ |
| 66 |
|
| 67 |
event.preventDefault(); |
| 68 |
|
| 69 |
// Clear out the preview image |
| 70 |
options.imgContainer.html( '' ); |
| 71 |
|
| 72 |
// Un-hide the add image link |
| 73 |
options.addImgLink.removeClass( 'hidden' ); |
| 74 |
|
| 75 |
// Hide the delete image link |
| 76 |
options.delImgLink.addClass( 'hidden' ); |
| 77 |
|
| 78 |
// Delete the image id from the hidden input |
| 79 |
options.imgIdInput.val( '' ); |
| 80 |
|
| 81 |
//trigger update widget |
| 82 |
options.imgIdInput.change(); |
| 83 |
}); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
function updateFrame() |
| 88 |
{ |
| 89 |
// Do something when the media frame is opened. |
| 90 |
} |
| 91 |
|
| 92 |
function selectImg() |
| 93 |
{ |
| 94 |
// Get media attachment details from the frame state |
| 95 |
//var attachment = options.frame.state().get('selection').first().toJSON(); |
| 96 |
|
| 97 |
var attachments = options.frame.state().get('selection').toJSON(); |
| 98 |
|
| 99 |
//console.log(attachments); |
| 100 |
|
| 101 |
// Send the attachment URL to our custom image input field. |
| 102 |
var input_values = options.imgIdInput.val(); |
| 103 |
|
| 104 |
for (var item in attachments) { |
| 105 |
//console.log(attachments[item]); |
| 106 |
options.imgContainer.append( '<div class="winter_mvc-media-card" data-media-id="'+attachments[item].id+'"><img src="'+attachments[item].url+'" alt="" class="thumbnail"/><a href="#" class="remove"></a></div>' ); |
| 107 |
if (input_values.slice(-1) != ',') |
| 108 |
input_values += ','; |
| 109 |
|
| 110 |
input_values += attachments[item].id + ','; |
| 111 |
} |
| 112 |
|
| 113 |
// Send the attachment id to our hidden input |
| 114 |
options.imgIdInput.val( input_values ); |
| 115 |
|
| 116 |
// Hide the add image link |
| 117 |
options.addImgLink.addClass( 'uploaded-hidden' ); |
| 118 |
|
| 119 |
// Unhide the remove image link |
| 120 |
options.delImgLink.removeClass( 'hidden' ); |
| 121 |
|
| 122 |
//trigger update widget |
| 123 |
options.imgIdInput.change(); |
| 124 |
|
| 125 |
// start js manager feature |
| 126 |
/* order */ |
| 127 |
var re_order = function(media_element){ |
| 128 |
var list_media = ''; |
| 129 |
media_element.find('.winter_mvc-media-card').each(function(){ |
| 130 |
if(list_media !='') |
| 131 |
list_media +=','; |
| 132 |
|
| 133 |
list_media += jQuery(this).attr('data-media-id'); |
| 134 |
}) |
| 135 |
media_element.closest('.postbox-upload-multiple').find('.logo_image_id').val(list_media); |
| 136 |
} |
| 137 |
/* Sort table */ |
| 138 |
options.imgIdInput.closest( '.postbox-upload-multiple').find( '.winter_mvc-media' ).sortable({ |
| 139 |
update: function(event, ui) { |
| 140 |
re_order(jQuery(this)); |
| 141 |
} |
| 142 |
}); |
| 143 |
/* remove media */ |
| 144 |
options.imgIdInput.closest( '.postbox-upload-multiple').find('.winter_mvc-media .winter_mvc-media-card .remove').off().on('click', function(e){ |
| 145 |
e.preventDefault(); |
| 146 |
var media = jQuery(this).closest('.winter_mvc-media') |
| 147 |
jQuery(this).closest('.winter_mvc-media-card').remove(); |
| 148 |
re_order(media) |
| 149 |
}) |
| 150 |
// end js manager feature |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/* fix for mobile draggable */ |
| 156 |
/*! |
| 157 |
* Depends: |
| 158 |
* jquery.ui.widget.js |
| 159 |
* jquery.ui.mouse.js |
| 160 |
*/ |
| 161 |
|
| 162 |
!function(a){function f(a,b){if(!(a.originalEvent.touches.length>1)){a.preventDefault();var c=a.originalEvent.changedTouches[0],d=document.createEvent("MouseEvents");d.initMouseEvent(b,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null),a.target.dispatchEvent(d)}}if(a.support.touch="ontouchend"in document,a.support.touch){var e,b=a.ui.mouse.prototype,c=b._mouseInit,d=b._mouseDestroy;b._touchStart=function(a){var b=this;!e&&b._mouseCapture(a.originalEvent.changedTouches[0])&&(e=!0,b._touchMoved=!1,f(a,"mouseover"),f(a,"mousemove"),f(a,"mousedown"))},b._touchMove=function(a){e&&(this._touchMoved=!0,f(a,"mousemove"))},b._touchEnd=function(a){e&&(f(a,"mouseup"),f(a,"mouseout"),this._touchMoved||f(a,"click"),e=!1)},b._mouseInit=function(){var b=this;b.element.bind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),c.call(b)},b._mouseDestroy=function(){var b=this;b.element.unbind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),d.call(b)}}}(jQuery); |