WordPress/wp-admin/js/widgets.min.js

1 line
9.7 KiB
JavaScript

var wpWidgets;!function(c){var g=c(document);wpWidgets={hoveredSidebar:null,init:function(){var r,o,s=this,i=c(".widgets-chooser"),d=i.find(".widgets-chooser-sidebars"),e=c("div.widgets-sortables"),l=!("undefined"==typeof isRtl||!isRtl);c("#widgets-right .sidebar-name").click(function(){var e=c(this),i=e.closest(".widgets-holder-wrap");i.hasClass("closed")?(i.removeClass("closed"),e.parent().sortable("refresh")):i.addClass("closed"),g.triggerHandler("wp-pin-menu")}),c("#widgets-left .sidebar-name").click(function(){c(this).closest(".widgets-holder-wrap").toggleClass("closed"),g.triggerHandler("wp-pin-menu")}),c(document.body).bind("click.widgets-toggle",function(e){var i,t,s,d,n=c(e.target),a={"z-index":100};n.parents(".widget-top").length&&!n.parents("#available-widgets").length?(t=(i=n.closest("div.widget")).children(".widget-inside"),s=parseInt(i.find("input.widget-width").val(),10),d=i.parent().width(),t.is(":hidden")?(250<s&&d<s+30&&i.closest("div.widgets-sortables").length&&(a[i.closest("div.widget-liquid-right").length?l?"margin-right":"margin-left":l?"margin-left":"margin-right"]=d-(s+30)+"px",i.css(a)),i.addClass("open"),t.slideDown("fast")):t.slideUp("fast",function(){i.attr("style",""),i.removeClass("open")}),e.preventDefault()):n.hasClass("widget-control-save")?(wpWidgets.save(n.closest("div.widget"),0,1,0),e.preventDefault()):n.hasClass("widget-control-remove")?(wpWidgets.save(n.closest("div.widget"),1,1,0),e.preventDefault()):n.hasClass("widget-control-close")?((i=n.closest("div.widget")).removeClass("open"),wpWidgets.close(i),e.preventDefault()):"inactive-widgets-control-remove"===n.attr("id")&&(wpWidgets.removeInactiveWidgets(),e.preventDefault())}),e.children(".widget").each(function(){var e=c(this);wpWidgets.appendTitle(this),e.find("p.widget-error").length&&e.find("a.widget-action").trigger("click")}),c("#widget-list").children(".widget").draggable({connectToSortable:"div.widgets-sortables",handle:"> .widget-top > .widget-title",distance:2,helper:"clone",zIndex:100,containment:"#wpwrap",refreshPositions:!0,start:function(e,i){var t=c(this).find(".widgets-chooser");i.helper.find("div.widget-description").hide(),o=this.id,t.length&&(c("#wpbody-content").append(t.hide()),i.helper.find(".widgets-chooser").remove(),s.clearWidgetSelection())},stop:function(){r&&c(r).hide(),r=""}}),e.droppable({tolerance:"intersect",over:function(e){var i=c(e.target).parent();wpWidgets.hoveredSidebar&&!i.is(wpWidgets.hoveredSidebar)&&wpWidgets.closeSidebar(e),i.hasClass("closed")&&(wpWidgets.hoveredSidebar=i).removeClass("closed"),c(this).sortable("refresh")},out:function(e){wpWidgets.hoveredSidebar&&wpWidgets.closeSidebar(e)}}),e.sortable({placeholder:"widget-placeholder",items:"> .widget",handle:"> .widget-top > .widget-title",cursor:"move",distance:2,containment:"#wpwrap",tolerance:"pointer",refreshPositions:!0,start:function(e,i){var t=c(this),s=t.parent(),d=i.item.children(".widget-inside");"block"===d.css("display")&&(i.item.removeClass("open"),d.hide(),c(this).sortable("refreshPositions")),s.hasClass("closed")||(i=i.item.hasClass("ui-draggable")?t.height():1+t.height(),t.css("min-height",i+"px"))},stop:function(e,i){var t,s,d,n=i.item,a=o;if(wpWidgets.hoveredSidebar=null,n.hasClass("deleting"))return wpWidgets.save(n,1,0,1),void n.remove();t=n.find("input.add_new").val(),s=n.find("input.multi_number").val(),n.attr("style","").removeClass("ui-draggable"),o="",t&&("multi"===t?(n.html(n.html().replace(/<[^<>]+>/g,function(e){return e.replace(/__i__|%i%/g,s)})),n.attr("id",a.replace("__i__",s)),s++,c("div#"+a).find("input.multi_number").val(s)):"single"===t&&(n.attr("id","new-"+a),r="div#"+a),wpWidgets.save(n,0,0,1),n.find("input.add_new").val(""),g.trigger("widget-added",[n])),(d=n.parent()).parent().hasClass("closed")&&(d.parent().removeClass("closed"),1<(i=d.children(".widget")).length&&(a=i.get(0),i=n.get(0),a.id&&i.id&&a.id!==i.id&&c(a).before(n))),t?n.find("a.widget-action").trigger("click"):wpWidgets.saveOrder(d.attr("id"))},activate:function(){c(this).parent().addClass("widget-hover")},deactivate:function(){c(this).css("min-height","").parent().removeClass("widget-hover")},receive:function(e,i){i=c(i.sender);-1<this.id.indexOf("orphaned_widgets")?i.sortable("cancel"):-1<i.attr("id").indexOf("orphaned_widgets")&&!i.children(".widget").length&&i.parents(".orphan-sidebar").slideUp(400,function(){c(this).remove()})}}).sortable("option","connectWith","div.widgets-sortables"),c("#available-widgets").droppable({tolerance:"pointer",accept:function(e){return"widget-list"!==c(e).parent().attr("id")},drop:function(e,i){i.draggable.addClass("deleting"),c("#removing-widget").hide().children("span").empty()},over:function(e,i){i.draggable.addClass("deleting"),c("div.widget-placeholder").hide(),i.draggable.hasClass("ui-sortable-helper")&&c("#removing-widget").show().children("span").html(i.draggable.find("div.widget-title").children("h3").html())},out:function(e,i){i.draggable.removeClass("deleting"),c("div.widget-placeholder").show(),c("#removing-widget").hide().children("span").empty()}}),c("#widgets-right .widgets-holder-wrap").each(function(e,i){var t=c(i),i=t.find(".sidebar-name h2").text(),t=t.find(".widgets-sortables").attr("id"),i=c('<li tabindex="0">').text(c.trim(i));0===e&&i.addClass("widgets-chooser-selected"),d.append(i),i.data("sidebarId",t)}),c("#available-widgets .widget .widget-title").on("click.widgets-chooser",function(){var e=c(this).closest(".widget");e.hasClass("widget-in-question")||c("#widgets-left").hasClass("chooser")?s.closeChooser():(s.clearWidgetSelection(),c("#widgets-left").addClass("chooser"),e.addClass("widget-in-question").children(".widget-description").after(i),i.slideDown(300,function(){d.find(".widgets-chooser-selected").focus()}),d.find("li").on("focusin.widgets-chooser",function(){d.find(".widgets-chooser-selected").removeClass("widgets-chooser-selected"),c(this).addClass("widgets-chooser-selected")}))}),i.on("click.widgets-chooser",function(e){e=c(e.target);e.hasClass("button-primary")?(s.addWidget(i),s.closeChooser()):e.hasClass("button-secondary")&&s.closeChooser()}).on("keyup.widgets-chooser",function(e){e.which===c.ui.keyCode.ENTER?(c(e.target).hasClass("button-secondary")||s.addWidget(i),s.closeChooser()):e.which===c.ui.keyCode.ESCAPE&&s.closeChooser()})},saveOrder:function(e){var i={action:"widgets-order",savewidgets:c("#_wpnonce_widgets").val(),sidebars:[]};e&&c("#"+e).find(".spinner:first").addClass("is-active"),c("div.widgets-sortables").each(function(){c(this).sortable&&(i["sidebars["+c(this).attr("id")+"]"]=c(this).sortable("toArray").join(","))}),c.post(ajaxurl,i,function(){c("#inactive-widgets-control-remove").prop("disabled",!c("#wp_inactive_widgets .widget").length),c(".spinner").removeClass("is-active")})},save:function(t,s,d,n){var e,a=t.closest("div.widgets-sortables").attr("id"),i=t.find("form").serialize();t=c(t),c(".spinner",t).addClass("is-active"),e={action:"save-widget",savewidgets:c("#_wpnonce_widgets").val(),sidebar:a},s&&(e.delete_widget=1),i+="&"+c.param(e),c.post(ajaxurl,i,function(e){var i;s?(c("input.widget_number",t).val()||(i=c("input.widget-id",t).val(),c("#available-widgets").find("input.widget-id").each(function(){c(this).val()===i&&c(this).closest("div.widget").show()})),d?(n=0,t.slideUp("fast",function(){c(this).remove(),wpWidgets.saveOrder()})):(t.remove(),"wp_inactive_widgets"===a&&c("#inactive-widgets-control-remove").prop("disabled",!c("#wp_inactive_widgets .widget").length))):(c(".spinner").removeClass("is-active"),e&&2<e.length&&(c("div.widget-content",t).html(e),wpWidgets.appendTitle(t),g.trigger("widget-updated",[t]),"wp_inactive_widgets"===a&&c("#inactive-widgets-control-remove").prop("disabled",!c("#wp_inactive_widgets .widget").length))),n&&wpWidgets.saveOrder()})},removeInactiveWidgets:function(){var e,i=c(".remove-inactive-widgets");c(".spinner",i).addClass("is-active"),e={action:"delete-inactive-widgets",removeinactivewidgets:c("#_wpnonce_remove_inactive_widgets").val()},e=c.param(e),c.post(ajaxurl,e,function(){c("#wp_inactive_widgets .widget").remove(),c("#inactive-widgets-control-remove").prop("disabled",!0),c(".spinner",i).removeClass("is-active")})},appendTitle:function(e){var i=(i=c('input[id*="-title"]',e).val()||"")&&": "+i.replace(/<[^<>]+>/g,"").replace(/</g,"&lt;").replace(/>/g,"&gt;");c(e).children(".widget-top").children(".widget-title").children().children(".in-widget-title").html(i)},close:function(e){e.children(".widget-inside").slideUp("fast",function(){e.attr("style","")})},addWidget:function(e){var i=e.find(".widgets-chooser-selected").data("sidebarId"),t=c("#"+i),s=c("#available-widgets").find(".widget-in-question").clone(),d=s.attr("id"),e=s.find("input.add_new").val(),n=s.find("input.multi_number").val();s.find(".widgets-chooser").remove(),"multi"===e?(s.html(s.html().replace(/<[^<>]+>/g,function(e){return e.replace(/__i__|%i%/g,n)})),s.attr("id",d.replace("__i__",n)),n++,c("#"+d).find("input.multi_number").val(n)):"single"===e&&(s.attr("id","new-"+d),c("#"+d).hide()),t.closest(".widgets-holder-wrap").removeClass("closed"),t.append(s),t.sortable("refresh"),wpWidgets.save(s,0,0,1),s.find("input.add_new").val(""),g.trigger("widget-added",[s]),e=(i=c(window).scrollTop())+c(window).height(),(d=t.offset()).bottom=d.top+t.outerHeight(),(i>d.bottom||e<d.top)&&c("html, body").animate({scrollTop:d.top-130},200),window.setTimeout(function(){s.find(".widget-title").trigger("click")},250)},closeChooser:function(){var e=this;c(".widgets-chooser").slideUp(200,function(){c("#wpbody-content").append(this),e.clearWidgetSelection()})},clearWidgetSelection:function(){c("#widgets-left").removeClass("chooser"),c(".widget-in-question").removeClass("widget-in-question")},closeSidebar:function(e){this.hoveredSidebar.addClass("closed"),c(e.target).css("min-height",""),this.hoveredSidebar=null}},g.ready(function(){wpWidgets.init()})}(jQuery);