WordPress/wp-admin/js/customize-controls.min.js

1 line
16 KiB
JavaScript

!function(a,b){var c=wp.customize;c.Setting=c.Value.extend({initialize:function(a,b,d){c.Value.prototype.initialize.call(this,b,d),this.id=a,this.transport=this.transport||"refresh",this.bind(this.preview)},preview:function(){switch(this.transport){case"refresh":return this.previewer.refresh();case"postMessage":return this.previewer.send("setting",[this.id,this()])}}}),c.Control=c.Class.extend({initialize:function(a,d){var e,f,g,h=this;this.params={},b.extend(this,d||{}),this.id=a,this.selector="#customize-control-"+a.replace(/\]/g,"").replace(/\[/g,"-"),this.container=b(this.selector),g=b.map(this.params.settings,function(a){return a}),c.apply(c,g.concat(function(){var a;h.settings={};for(a in h.params.settings)h.settings[a]=c(h.params.settings[a]);h.setting=h.settings["default"]||null,h.ready()})),h.elements=[],e=this.container.find("[data-customize-setting-link]"),f={},e.each(function(){var a,d=b(this);if(d.is(":radio")){if(a=d.prop("name"),f[a])return;f[a]=!0,d=e.filter('[name="'+a+'"]')}c(d.data("customizeSettingLink"),function(a){var b=new c.Element(d);h.elements.push(b),b.sync(a),b.set(a())})})},ready:function(){},dropdownInit:function(){var a=this,b=this.container.find(".dropdown-status"),c=this.params,d=!1,e=function(a){"string"==typeof a&&c.statuses&&c.statuses[a]?b.html(c.statuses[a]).show():b.hide()};this.container.on("click keydown",".dropdown",function(b){("keydown"!==b.type||13===b.which)&&(b.preventDefault(),d||a.container.toggleClass("open"),a.container.hasClass("open")&&a.container.parent().parent().find("li.library-selected").focus(),d=!0,setTimeout(function(){d=!1},400))}),this.setting.bind(e),e(this.setting())}}),c.ColorControl=c.Control.extend({ready:function(){var a=this,b=this.container.find(".color-picker-hex");b.val(a.setting()).wpColorPicker({change:function(){a.setting.set(b.wpColorPicker("color"))},clear:function(){a.setting.set(!1)}})}}),c.UploadControl=c.Control.extend({ready:function(){var a=this;this.params.removed=this.params.removed||"",this.success=b.proxy(this.success,this),this.uploader=b.extend({container:this.container,browser:this.container.find(".upload"),dropzone:this.container.find(".upload-dropzone"),success:this.success,plupload:{},params:{}},this.uploader||{}),a.params.extensions&&(a.uploader.plupload.filters=[{title:c.l10n.allowedFiles,extensions:a.params.extensions}]),a.params.context&&(a.uploader.params["post_data[context]"]=this.params.context),c.settings.theme.stylesheet&&(a.uploader.params["post_data[theme]"]=c.settings.theme.stylesheet),this.uploader=new wp.Uploader(this.uploader),this.remover=this.container.find(".remove"),this.remover.on("click keydown",function(b){("keydown"!==b.type||13===b.which)&&(a.setting.set(a.params.removed),b.preventDefault())}),this.removerVisibility=b.proxy(this.removerVisibility,this),this.setting.bind(this.removerVisibility),this.removerVisibility(this.setting.get())},success:function(a){this.setting.set(a.get("url"))},removerVisibility:function(a){this.remover.toggle(a!=this.params.removed)}}),c.ImageControl=c.UploadControl.extend({ready:function(){var a,d=this;this.uploader={init:function(){var a,b;this.supports.dragdrop||(a=d.container.find(".upload-fallback"),b=a.children().detach(),this.browser.detach().empty().append(b),a.append(this.browser).show())}},c.UploadControl.prototype.ready.call(this),this.thumbnail=this.container.find(".preview-thumbnail img"),this.thumbnailSrc=b.proxy(this.thumbnailSrc,this),this.setting.bind(this.thumbnailSrc),this.library=this.container.find(".library"),this.tabs={},a=this.library.find(".library-content"),this.library.children("ul").children("li").each(function(){var c=b(this),e=c.data("customizeTab"),f=a.filter('[data-customize-tab="'+e+'"]');d.tabs[e]={both:c.add(f),link:c,panel:f}}),this.library.children("ul").on("click keydown","li",function(a){if("keydown"!==a.type||13===a.which){var c=b(this).data("customizeTab"),e=d.tabs[c];a.preventDefault(),e.link.hasClass("library-selected")||(d.selected.both.removeClass("library-selected"),d.selected=e,d.selected.both.addClass("library-selected"))}}),this.library.on("click keydown","a",function(a){if("keydown"!==a.type||13===a.which){var c=b(this).data("customizeImageValue");c&&(d.setting.set(c),a.preventDefault())}}),this.tabs.uploaded&&(this.tabs.uploaded.target=this.library.find(".uploaded-target"),this.tabs.uploaded.panel.find(".thumbnail").length||this.tabs.uploaded.both.addClass("hidden")),a.each(function(){var a=d.tabs[b(this).data("customizeTab")];return a.link.hasClass("hidden")?void 0:(d.selected=a,a.both.addClass("library-selected"),!1)}),this.dropdownInit()},success:function(a){c.UploadControl.prototype.success.call(this,a),this.tabs.uploaded&&this.tabs.uploaded.target.length&&(this.tabs.uploaded.both.removeClass("hidden"),a.element=b('<a href="#" class="thumbnail"></a>').data("customizeImageValue",a.get("url")).append('<img src="'+a.get("url")+'" />').appendTo(this.tabs.uploaded.target))},thumbnailSrc:function(a){/^(https?:)?\/\//.test(a)?this.thumbnail.prop("src",a).show():this.thumbnail.hide()}}),c.HeaderControl=c.Control.extend({ready:function(){this.btnRemove=b("#customize-control-header_image .actions .remove"),this.btnNew=b("#customize-control-header_image .actions .new"),_.bindAll(this,"openMedia","removeImage"),this.btnNew.on("click",this.openMedia),this.btnRemove.on("click",this.removeImage),c.HeaderTool.currentHeader=new c.HeaderTool.ImageModel,new c.HeaderTool.CurrentView({model:c.HeaderTool.currentHeader,el:".current .container"}),new c.HeaderTool.ChoiceListView({collection:c.HeaderTool.UploadsList=new c.HeaderTool.ChoiceList,el:".choices .uploaded .list"}),new c.HeaderTool.ChoiceListView({collection:c.HeaderTool.DefaultsList=new c.HeaderTool.DefaultsList,el:".choices .default .list"}),c.HeaderTool.combinedList=c.HeaderTool.CombinedList=new c.HeaderTool.CombinedList([c.HeaderTool.UploadsList,c.HeaderTool.DefaultsList])},calculateImageSelectOptions:function(a,b){var d,e,f,g,h,i,j=parseInt(_wpCustomizeHeader.data.width,10),k=parseInt(_wpCustomizeHeader.data.height,10),l=!!parseInt(_wpCustomizeHeader.data["flex-width"],10),m=!!parseInt(_wpCustomizeHeader.data["flex-height"],10);return h=a.get("width"),g=a.get("height"),this.headerImage=new c.HeaderTool.ImageModel,this.headerImage.set({themeWidth:j,themeHeight:k,themeFlexWidth:l,themeFlexHeight:m,imageWidth:h,imageHeight:g}),b.set("canSkipCrop",!this.headerImage.shouldBeCropped()),d=j/k,e=h,f=g,e/f>d?(k=f,j=k*d):(j=e,k=j/d),i={handles:!0,keys:!0,instance:!0,persistent:!0,imageWidth:h,imageHeight:g,x1:0,y1:0,x2:j,y2:k},m===!1&&l===!1&&(i.aspectRatio=j+":"+k),m===!1&&(i.maxHeight=k),l===!1&&(i.maxWidth=j),i},openMedia:function(a){var b=_wpMediaViewsL10n;a.preventDefault(),this.frame=wp.media({button:{text:b.selectAndCrop,close:!1},states:[new wp.media.controller.Library({title:b.chooseImage,library:wp.media.query({type:"image"}),multiple:!1,priority:20,suggestedWidth:_wpCustomizeHeader.data.width,suggestedHeight:_wpCustomizeHeader.data.height}),new wp.media.controller.Cropper({imgSelectOptions:this.calculateImageSelectOptions})]}),this.frame.on("select",this.onSelect,this),this.frame.on("cropped",this.onCropped,this),this.frame.on("skippedcrop",this.onSkippedCrop,this),this.frame.open()},onSelect:function(){this.frame.setState("cropper")},onCropped:function(a){var b=a.post_content,c=a.attachment_id,d=a.width,e=a.height;this.setImageFromURL(b,c,d,e)},onSkippedCrop:function(a){var b=a.get("url"),c=a.get("width"),d=a.get("height");this.setImageFromURL(b,a.id,c,d)},setImageFromURL:function(a,b,d,e){var f,g={};g.url=a,g.thumbnail_url=a,g.timestamp=_.now(),b&&(g.attachment_id=b),d&&(g.width=d),e&&(g.height=e),f=new c.HeaderTool.ImageModel({header:g,choice:a.split("/").pop()}),c.HeaderTool.UploadsList.add(f),c.HeaderTool.currentHeader.set(f.toJSON()),f.save(),f.importImage()},removeImage:function(){c.HeaderTool.currentHeader.trigger("hide"),c.HeaderTool.CombinedList.trigger("control:removeImage")}}),c.defaultConstructor=c.Setting,c.control=new c.Values({defaultConstructor:c.Control}),c.PreviewFrame=c.Messenger.extend({sensitivity:2e3,initialize:function(a,d){var e=b.Deferred();e.promise(this),this.container=a.container,this.signature=a.signature,b.extend(a,{channel:c.PreviewFrame.uuid()}),c.Messenger.prototype.initialize.call(this,a,d),this.add("previewUrl",a.previewUrl),this.query=b.extend(a.query||{},{customize_messenger_channel:this.channel()}),this.run(e)},run:function(a){var c=this,d=!1,e=!1;this._ready&&this.unbind("ready",this._ready),this._ready=function(){e=!0,d&&a.resolveWith(c)},this.bind("ready",this._ready),this.request=b.ajax(this.previewUrl(),{type:"POST",data:this.query,xhrFields:{withCredentials:!0}}),this.request.fail(function(){a.rejectWith(c,["request failure"])}),this.request.done(function(f){var g,h=c.request.getResponseHeader("Location"),i=c.signature;return h&&h!=c.previewUrl()?void a.rejectWith(c,["redirect",h]):"0"===f?void c.login(a):"-1"===f?void a.rejectWith(c,["cheatin"]):(g=f.lastIndexOf(i),-1===g||g<f.lastIndexOf("</html>")?void a.rejectWith(c,["unsigned"]):(f=f.slice(0,g)+f.slice(g+i.length),c.iframe=b("<iframe />").appendTo(c.container),c.iframe.one("load",function(){d=!0,e?a.resolveWith(c):setTimeout(function(){a.rejectWith(c,["ready timeout"])},c.sensitivity)}),c.targetWindow(c.iframe[0].contentWindow),c.targetWindow().document.open(),c.targetWindow().document.write(f),void c.targetWindow().document.close()))})},login:function(a){var d,e=this;return d=function(){a.rejectWith(e,["logged out"])},this.triedLogin?d():void b.get(c.settings.url.ajax,{action:"logged-in"}).fail(d).done(function(c){var f;"1"!==c&&d(),f=b('<iframe src="'+e.previewUrl()+'" />').hide(),f.appendTo(e.container),f.load(function(){e.triedLogin=!0,f.remove(),e.run(a)})})},destroy:function(){c.Messenger.prototype.destroy.call(this),this.request.abort(),this.iframe&&this.iframe.remove(),delete this.request,delete this.iframe,delete this.targetWindow}}),function(){var a=0;c.PreviewFrame.uuid=function(){return"preview-"+a++}}(),c.Previewer=c.Messenger.extend({refreshBuffer:250,initialize:function(a,d){var e=this,f=/^https?/;b.extend(this,d||{}),this.refresh=function(a){var b,c=a.refresh,d=function(){b=null,c.call(a)};return function(){if("number"!=typeof b){if(!a.loading)return d();a.abort()}clearTimeout(b),b=setTimeout(d,a.refreshBuffer)}}(this),this.container=c.ensure(a.container),this.allowedUrls=a.allowedUrls,this.signature=a.signature,a.url=window.location.href,c.Messenger.prototype.initialize.call(this,a),this.add("scheme",this.origin()).link(this.origin).setter(function(a){var b=a.match(f);return b?b[0]:""}),this.add("previewUrl",a.previewUrl).setter(function(a){var c;return/\/wp-admin(\/|$)/.test(a.replace(/[#?].*$/,""))?null:(b.each([a.replace(f,e.scheme()),a],function(a,d){return b.each(e.allowedUrls,function(a,b){var e;return b=b.replace(/\/+$/,""),e=d.replace(b,""),0===d.indexOf(b)&&/^([/#?]|$)/.test(e)?(c=d,!1):void 0}),c?!1:void 0}),c?c:null)}),this.previewUrl.bind(this.refresh),this.scroll=0,this.bind("scroll",function(a){this.scroll=a}),this.bind("url",this.previewUrl)},query:function(){},abort:function(){this.loading&&(this.loading.destroy(),delete this.loading)},refresh:function(){var a=this;this.abort(),this.loading=new c.PreviewFrame({url:this.url(),previewUrl:this.previewUrl(),query:this.query()||{},container:this.container,signature:this.signature}),this.loading.done(function(){this.bind("synced",function(){a.preview&&a.preview.destroy(),a.preview=this,delete a.loading,a.targetWindow(this.targetWindow()),a.channel(this.channel()),a.send("active")}),this.send("sync",{scroll:a.scroll,settings:c.get()})}),this.loading.fail(function(b,c){"redirect"===b&&c&&a.previewUrl(c),"logged out"===b&&(a.preview&&(a.preview.destroy(),delete a.preview),a.login().done(a.refresh)),"cheatin"===b&&a.cheatin()})},login:function(){var a,d,e,f=this;return this._login?this._login:(a=b.Deferred(),this._login=a.promise(),d=new c.Messenger({channel:"login",url:c.settings.url.login}),e=b('<iframe src="'+c.settings.url.login+'" />').appendTo(this.container),d.targetWindow(e[0].contentWindow),d.bind("login",function(){e.remove(),d.destroy(),delete f._login,a.resolve()}),this._login)},cheatin:function(){b(document.body).empty().addClass("cheatin").append("<p>"+c.l10n.cheatin+"</p>")}}),c.controlConstructor={color:c.ColorControl,upload:c.UploadControl,image:c.ImageControl,header:c.HeaderControl},b(function(){if(c.settings=window._wpCustomizeSettings,c.l10n=window._wpCustomizeControlsL10n,c.settings){if(!b.support.postMessage||!b.support.cors&&c.settings.isCrossDomain)return window.location=c.settings.url.fallback;var a,d,e,f=b(document.body),g=f.children(".wp-full-overlay");b("#customize-controls").on("keydown",function(a){var c=13===a.which,d=b(a.target);c&&(d.is("input:not([type=button])")||d.is("select"))&&a.preventDefault()}),a=new c.Previewer({container:"#customize-preview",form:"#customize-controls",previewUrl:c.settings.url.preview,allowedUrls:c.settings.url.allowed,signature:"WP_CUSTOMIZER_SIGNATURE"},{nonce:c.settings.nonce,query:function(){return{wp_customize:"on",theme:c.settings.theme.stylesheet,customized:JSON.stringify(c.get()),nonce:this.nonce.preview}},save:function(){var a,d,e=this,g=b.extend(this.query(),{action:"customize_save",nonce:this.nonce.save}),h=c.state("processing");f.addClass("saving"),d=function(){var a=b.post(c.settings.url.ajax,g);c.trigger("save",a),a.always(function(){f.removeClass("saving")}),a.done(function(a){return"0"===a?(e.preview.iframe.hide(),void e.login().done(function(){e.save(),e.preview.iframe.show()})):"-1"===a?void e.cheatin():void c.trigger("saved")})},0===h()?d():(a=function(){0===h()&&(c.state.unbind("change",a),d())},c.state.bind("change",a))}}),a.bind("nonce",function(a){b.extend(this.nonce,a)}),b.each(c.settings.settings,function(b,d){c.create(b,b,d.value,{transport:d.transport,previewer:a})}),b.each(c.settings.controls,function(b,d){var e,f=c.controlConstructor[d.type]||c.Control;e=c.control.add(b,new f(b,{params:d,previewer:a}))}),a.previewUrl()?a.refresh():a.previewUrl(c.settings.url.home),function(){var a=new c.Values,d=a.create("saved"),e=a.create("activated"),f=a.create("processing");a.bind("change",function(){var a=b("#save"),f=b(".back");e()?d()?(a.val(c.l10n.saved).prop("disabled",!0),f.text(c.l10n.close)):(a.val(c.l10n.save).prop("disabled",!1),f.text(c.l10n.cancel)):(a.val(c.l10n.activate).prop("disabled",!1),f.text(c.l10n.cancel))}),d(!0),e(c.settings.theme.active),f(0),c.bind("change",function(){a("saved").set(!1)}),c.bind("saved",function(){a("saved").set(!0),a("activated").set(!0)}),e.bind(function(a){a&&c.trigger("activated")}),c.state=a}(),b("#save").click(function(b){a.save(),b.preventDefault()}).keydown(function(b){9!==b.which&&(13===b.which&&a.save(),b.preventDefault())}),b(".back").keydown(function(a){9!==a.which&&(13===a.which&&this.click(),a.preventDefault())}),b(".upload-dropzone a.upload").keydown(function(a){13===a.which&&this.click()}),b(".collapse-sidebar").on("click keydown",function(a){("keydown"!==a.type||13===a.which)&&(g.toggleClass("collapsed").toggleClass("expanded"),a.preventDefault())}),d=new c.Messenger({url:c.settings.url.parent,channel:"loader"}),d.bind("back",function(){b(".back").on("click.back",function(a){a.preventDefault(),d.send("close")})}),c.bind("saved",function(){d.send("saved")}),c.bind("activated",function(){d.targetWindow()?d.send("activated",c.settings.url.activated):c.settings.url.activated&&(window.location=c.settings.url.activated)}),d.send("ready"),b.each({background_image:{controls:["background_repeat","background_position_x","background_attachment"],callback:function(a){return!!a}},show_on_front:{controls:["page_on_front","page_for_posts"],callback:function(a){return"page"===a}},header_textcolor:{controls:["header_textcolor"],callback:function(a){return"blank"!==a}}},function(a,d){c(a,function(a){b.each(d.controls,function(b,e){c.control(e,function(b){var c=function(a){b.container.toggle(d.callback(a))};c(a.get()),a.bind(c)})})})}),c.control("display_header_text",function(a){var b="";a.elements[0].unsync(c("header_textcolor")),a.element=new c.Element(a.container.find("input")),a.element.set("blank"!==a.setting()),a.element.bind(function(d){d||(b=c("header_textcolor").get()),a.setting.set(d?b:"blank")}),a.setting.bind(function(b){a.element.set("blank"!==b)})}),c.trigger("ready"),e=b(".back"),e.focus(),setTimeout(function(){e.focus()},200)}})}(wp,jQuery);