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