jQuery(document).ready(function($){
"use strict";
/**
* Adding functionality for Upload input boxes in Custom Fields and Options
* New Code (new media manager)
*
* IMPORTANT: Needs wp_enqueue_media(); anywhere in the theme to work in other
* places than regular post editor. We are calling it as soon as we initiate the
* QI Framework.
*
* Credit to: https://github.com/thomasgriffin/New-Media-Image-Uploader/blob/master/js/media.js
*/
// Prepare the variable that holds our custom media manager
var qiMedia, qiMediaGallery;
// For simple uploads
jQuery(document).on('click', '.upload_file_button', function() {
var thisButton = jQuery(this),
uploadID = thisButton.prev('input');
qiMedia = wp.media.frames.qiMedia = wp.media({
frame: 'select',
title: adminscripts_localized.mediaTitle,
button: {
text: adminscripts_localized.mediaButton
},
});
qiMedia.on('select', function(){
// Grab our attachment selection and construct a JSON representation of the model.
var media_attachment = qiMedia.state().get('selection').first().toJSON();
// Send the attachment URL to our custom input field via jQuery.
uploadID.val( media_attachment.url );
});
qiMedia.open();
return false;
});
// For Gallery Uploads
jQuery(document).on('click', '.gallery_pick_button', function() {
var thisButton = jQuery(this),
uploadID = thisButton.prev('input');
qiMediaGallery = wp.media.frames.qiMediaGallery = wp.media({
frame: 'post',
title: adminscripts_localized.mediaTitle,
button: {
text: adminscripts_localized.mediaButton
},
});
qiMediaGallery.on( 'update', function(selection){
// Send the gallery shortcode to our custom input field via jQuery.
uploadID.val( wp.media.gallery.shortcode( selection ).string() );
});
qiMediaGallery.open();
return false;
});
/**
* Adding functionality for repeatable input boxes
*/
jQuery('.repeatable-add').click(function() {
var thisRepeat = jQuery(this),
field = thisRepeat.closest('td').find('.custom_repeatable li:last').clone(true),
fieldLocation = thisRepeat.closest('td').find('.custom_repeatable li:last');
// Reset new item, and increase index number for inputs
jQuery('input, textarea, select', field).val('').attr('name', function(index, name) {
if (name) {
return name.replace(/(\d+)/, function(fullMatch, n) {
return Number(n) + 1;
});
}
});
// Increase new item title index
var index = parseInt( jQuery('.repeat-index', field).html() );
jQuery('.repeat-index', field).html(++index);
// Insert newly added item in DOM
field.insertAfter(fieldLocation, jQuery(this).closest('td'));
return false;
});
jQuery('.repeatable-remove').click(function(){
if ( jQuery('.custom_repeatable .fields-set').length != 1 ) {
jQuery(this).parents('li').remove();
} else {
// If there is only one set, just clean it up
jQuery(this).parent().find('input').val('');
}
return false;
});
// Setting a variable
var sortablePrevChecked;
jQuery('.custom_repeatable').sortable({
opacity: 0.6,
revert: true,
cursor: 'move',
handle: '.sort',
start: function(event, ui) {
sortablePrevChecked = ui.item.find('input:checked').val();
},
update: function(event, ui) {
// Renumber Array indexes when re ordered
jQuery('.custom_repeatable li .fields-set').each(function(rowIndex){
jQuery(this).find('input[name], textarea[name]').each(function(){
var selectName;
selectName = jQuery(this).attr('name');
selectName = selectName.replace(/\[[0-9]+\]/g, '['+rowIndex+']');
jQuery(this).attr('name',selectName);
// sortablePrevChecked.prop('checked', true);
});
});
// alert(sortablePrevChecked);
ui.item.find(':radio[value='+sortablePrevChecked+']').prop("checked", true);
},
});
// Enable Fieldset Toggle
jQuery('.custom_repeatable .fields-toggle').click(function(){
var thisToggle = jQuery(this);
thisToggle.next('.fields-wrapper').slideToggle();
thisToggle.find('i').toggleClass('fa-flip-vertical');
});
// End repeatable section
// Enables Color Picker for Admin Options
jQuery('.quadropickcolor').click( function(e) {
var colorPicker = jQuery(this).next('div'),
input = jQuery(this).prev('input');
jQuery(colorPicker).farbtastic(input);
colorPicker.children().show();
e.preventDefault();
jQuery(document).mousedown( function() {
jQuery(colorPicker).children().hide();
jQuery('#bg-color').trigger('change');
});
});
/**
* Post selection and pickers functionality
*/
var generalEditLink = jQuery('.qi-general-edit-link').val(),
generalPostLink = jQuery('.qi-general-post-link').val(),
selTemplate = jQuery('#page_template').val(),
maybeIntPreview = selTemplate == 'template-modular.php' ? '&qi=internal-preview' : '';
// Enables Posts Select functionality in custom fields
jQuery('.posts-adder').click(function(){
// Grab all selected
var $selPosts = jQuery(this).prev('.posts-picker').find('option:selected'),
$listContainer = jQuery(this).next();
// Map them and construct icon prefixed li's with Posts' ID as data-id
$selPosts.map(function(){
// Get current posts list and check if current post is already there
var $currentPosts = $listContainer.next().val().split(', ');
if ( jQuery.inArray( jQuery(this).val(), $currentPosts ) == -1 ) {
var thisId = jQuery(this).val();
// If it isn't, add it
$listContainer.append('
").hide().insertBefore(c),ae.one(he,function(){r.replaceWith(c)}),h(c)}else _.get("iframe")?h(" "):_.get("html")?h(_.get("html")):s(_,e)?(e=l(_,e),U=_.get("createImg"),t(U).addClass(Z+"Photo").bind("error",function(){h(n(se,"Error").html(_.get("imgError")))}).one("load",function(){d===le&&setTimeout(function(){var t;_.get("retinaImage")&&i.devicePixelRatio>1&&(U.height=U.height/i.devicePixelRatio,U.width=U.width/i.devicePixelRatio),_.get("scalePhotos")&&(o=function(){U.height-=U.height*t,U.width-=U.width*t},_.mw&&U.width>_.mw&&(t=(U.width-_.mw)/U.width,o()),_.mh&&U.height>_.mh&&(t=(U.height-_.mh)/U.height,o())),_.h&&(U.style.marginTop=Math.max(_.mh-U.height,0)/2+"px"),W[1]&&(_.get("loop")||W[A+1])&&(U.style.cursor="pointer",U.onclick=function(){J.next()}),U.style.width=U.width+"px",U.style.height=U.height+"px",h(U)},1)}),U.src=e):e&&L.load(e,_.get("data"),function(e,i){d===le&&h("error"===i?n(se,"Error").html(_.get("xhrError")):t(this).contents())})}var v,y,x,b,T,C,H,k,W,E,I,L,M,S,F,R,K,P,B,O,_,j,D,N,z,A,U,$,q,G,Q,J,V,X={html:!1,photo:!1,iframe:!1,inline:!1,transition:"elastic",speed:300,fadeOut:300,width:!1,initialWidth:"600",innerWidth:!1,maxWidth:!1,height:!1,initialHeight:"450",innerHeight:!1,maxHeight:!1,scalePhotos:!0,scrolling:!0,opacity:.9,preloading:!0,className:!1,overlayClose:!0,escKey:!0,arrowKey:!0,top:!1,bottom:!1,left:!1,right:!1,fixed:!1,data:void 0,closeButton:!0,fastIframe:!0,open:!1,reposition:!0,loop:!0,slideshow:!1,slideshowAuto:!0,slideshowSpeed:2500,slideshowStart:"start slideshow",slideshowStop:"stop slideshow",photoRegex:/\.(gif|png|jp(e|g|eg)|bmp|ico|webp|jxr|svg)((#|\?).*)?$/i,retinaImage:!1,retinaUrl:!1,retinaSuffix:"@2x.$1",current:"image {current} of {total}",previous:"previous",next:"next",close:"close",xhrError:"This content failed to load.",imgError:"This image failed to load.",returnFocus:!0,trapFocus:!0,onOpen:!1,onLoad:!1,onComplete:!1,onCleanup:!1,onClosed:!1,rel:function(){return this.rel},href:function(){return t(this).attr("href")},title:function(){return this.title},createImg:function(){var e=new Image,i=t(this).data("cbox-img-attrs");return"object"==typeof i&&t.each(i,function(t,i){e[t]=i}),e},createIframe:function(){var i=e.createElement("iframe"),n=t(this).data("cbox-iframe-attrs");return"object"==typeof n&&t.each(n,function(t,e){i[t]=e}),"frameBorder"in i&&(i.frameBorder=0),"allowTransparency"in i&&(i.allowTransparency="true"),i.name=(new Date).getTime(),i.allowFullScreen=!0,i}},Y="colorbox",Z="cbox",te=Z+"Element",ee=Z+"_open",ie=Z+"_load",ne=Z+"_complete",oe=Z+"_cleanup",re=Z+"_closed",he=Z+"_purge",ae=t("
"),se="div",le=0,de={},ce=function(){function t(){clearTimeout(h)}function e(){(_.get("loop")||W[A+1])&&(t(),h=setTimeout(J.next,_.get("slideshowSpeed")))}function i(){R.html(_.get("slideshowStop")).unbind(s).one(s,n),ae.bind(ne,e).bind(ie,t),y.removeClass(a+"off").addClass(a+"on")}function n(){t(),ae.unbind(ne,e).unbind(ie,t),R.html(_.get("slideshowStart")).unbind(s).one(s,function(){J.next(),i()}),y.removeClass(a+"on").addClass(a+"off")}function o(){r=!1,R.hide(),t(),ae.unbind(ne,e).unbind(ie,t),y.removeClass(a+"off "+a+"on")}var r,h,a=Z+"Slideshow_",s="click."+Z;return function(){r?_.get("slideshow")||(ae.unbind(oe,o),o()):_.get("slideshow")&&W[1]&&(r=!0,ae.one(oe,o),_.get("slideshowAuto")?i():n(),R.show())}}();t[Y]||(t(p),J=t.fn[Y]=t[Y]=function(e,i){var n,o=this;return e=e||{},t.isFunction(o)&&(o=t("
"),e.open=!0),o[0]?(p(),m()&&(i&&(e.onComplete=i),o.each(function(){var i=t.data(this,Y)||{};t.data(this,Y,t.extend(i,e))}).addClass(te),n=new r(o[0],e),n.get("open")&&f(o[0])),o):o},J.position=function(e,i){function n(){T[0].style.width=k[0].style.width=b[0].style.width=parseInt(y[0].style.width,10)-D+"px",b[0].style.height=C[0].style.height=H[0].style.height=parseInt(y[0].style.height,10)-j+"px"}var r,h,s,l=0,d=0,c=y.offset();if(E.unbind("resize."+Z),y.css({top:-9e4,left:-9e4}),h=E.scrollTop(),s=E.scrollLeft(),_.get("fixed")?(c.top-=h,c.left-=s,y.css({position:"fixed"})):(l=h,d=s,y.css({position:"absolute"})),d+=_.get("right")!==!1?Math.max(E.width()-_.w-z-D-a(_.get("right"),"x"),0):_.get("left")!==!1?a(_.get("left"),"x"):Math.round(Math.max(E.width()-_.w-z-D,0)/2),l+=_.get("bottom")!==!1?Math.max(o()-_.h-N-j-a(_.get("bottom"),"y"),0):_.get("top")!==!1?a(_.get("top"),"y"):Math.round(Math.max(o()-_.h-N-j,0)/2),y.css({top:c.top,left:c.left,visibility:"visible"}),x[0].style.width=x[0].style.height="9999px",r={width:_.w+z+D,height:_.h+N+j,top:l,left:d},e){var g=0;t.each(r,function(t){return r[t]!==de[t]?(g=e,void 0):void 0}),e=g}de=r,e||y.css(r),y.dequeue().animate(r,{duration:e||0,complete:function(){n(),q=!1,x[0].style.width=_.w+z+D+"px",x[0].style.height=_.h+N+j+"px",_.get("reposition")&&setTimeout(function(){E.bind("resize."+Z,J.position)},1),t.isFunction(i)&&i()},step:n})},J.resize=function(t){var e;$&&(t=t||{},t.width&&(_.w=a(t.width,"x")-z-D),t.innerWidth&&(_.w=a(t.innerWidth,"x")),I.css({width:_.w}),t.height&&(_.h=a(t.height,"y")-N-j),t.innerHeight&&(_.h=a(t.innerHeight,"y")),t.innerHeight||t.height||(e=I.scrollTop(),I.css({height:"auto"}),_.h=I.height()),I.css({height:_.h}),e&&I.scrollTop(e),J.position("none"===_.get("transition")?0:_.get("speed")))},J.prep=function(i){function o(){return _.w=_.w||I.width(),_.w=_.mw&&_.mw<_.w?_.mw:_.w,_.w}function a(){return _.h=_.h||I.height(),_.h=_.mh&&_.mh<_.h?_.mh:_.h,_.h}if($){var d,g="none"===_.get("transition")?0:_.get("speed");I.remove(),I=n(se,"LoadedContent").append(i),I.hide().appendTo(L.show()).css({width:o(),overflow:_.get("scrolling")?"auto":"hidden"}).css({height:a()}).prependTo(b),L.hide(),t(U).css({"float":"none"}),c(_.get("className")),d=function(){function i(){t.support.opacity===!1&&y[0].style.removeAttribute("filter")}var n,o,a=W.length;$&&(o=function(){clearTimeout(Q),M.hide(),u(ne),_.get("onComplete")},S.html(_.get("title")).show(),I.show(),a>1?("string"==typeof _.get("current")&&F.html(_.get("current").replace("{current}",A+1).replace("{total}",a)).show(),K[_.get("loop")||a-1>A?"show":"hide"]().html(_.get("next")),P[_.get("loop")||A?"show":"hide"]().html(_.get("previous")),ce(),_.get("preloading")&&t.each([h(-1),h(1)],function(){var i,n=W[this],o=new r(n,t.data(n,Y)),h=o.get("href");h&&s(o,h)&&(h=l(o,h),i=e.createElement("img"),i.src=h)})):O.hide(),_.get("iframe")?(n=_.get("createIframe"),_.get("scrolling")||(n.scrolling="no"),t(n).attr({src:_.get("href"),"class":Z+"Iframe"}).one("load",o).appendTo(I),ae.one(he,function(){n.src="//about:blank"}),_.get("fastIframe")&&t(n).trigger("load")):o(),"fade"===_.get("transition")?y.fadeTo(g,1,i):i())},"fade"===_.get("transition")?y.fadeTo(g,0,function(){J.position(0,d)}):J.position(g,d)}},J.next=function(){!q&&W[1]&&(_.get("loop")||W[A+1])&&(A=h(1),f(W[A]))},J.prev=function(){!q&&W[1]&&(_.get("loop")||A)&&(A=h(-1),f(W[A]))},J.close=function(){$&&!G&&(G=!0,$=!1,u(oe),_.get("onCleanup"),E.unbind("."+Z),v.fadeTo(_.get("fadeOut")||0,0),y.stop().fadeTo(_.get("fadeOut")||0,0,function(){y.hide(),v.hide(),u(he),I.remove(),setTimeout(function(){G=!1,u(re),_.get("onClosed")},1)}))},J.remove=function(){y&&(y.stop(),t[Y].close(),y.stop(!1,!0).remove(),v.remove(),G=!1,y=null,t("."+te).removeData(Y).removeClass(te),t(e).unbind("click."+Z).unbind("keydown."+Z))},J.element=function(){return t(_.el)},J.settings=X)})(jQuery,document,window);