WordPress/wp-admin/js/postbox.min.js
Andrea Fercia 22d0cd6c96 Accessibility: Allow post boxes on the Dashboard and Classic Editor pages to be reordered by using the keyboard.
So far, it has been possible to rearrange into a new order the post boxes (also known as "widgets" on the Dashboard and "meta boxes" on the Edit post page) only by using a pointing device, for example a mouse.

This change adds new controls and functionality to allow the boxes to be rearranged also with the keyboard. Additionally, audible messages are sent to the admin ARIA live region to notify screen reader users of the reorder action result.

Props joedolson, anevins, antpb, audrasjb, xkon, MarcoZ, karmatosed, afercia.
Fixes #39074.

Built from https://develop.svn.wordpress.org/trunk@48373


git-svn-id: http://core.svn.wordpress.org/trunk@48142 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-07-07 13:00:02 +00:00

2 lines
6.3 KiB
JavaScript

/*! This file is auto-generated */
!function(n){var a=n(document),d=wp.i18n.__;window.postboxes={handle_click:function(){var e,o=n(this),s=o.closest(".postbox"),t=s.attr("id");"dashboard_browser_nag"!==t&&(s.toggleClass("closed"),e=!s.hasClass("closed"),o.hasClass("handlediv")?o.attr("aria-expanded",e):o.closest(".postbox").find("button.handlediv").attr("aria-expanded",e),"press-this"!==postboxes.page&&postboxes.save_state(postboxes.page),t&&(!s.hasClass("closed")&&n.isFunction(postboxes.pbshow)?postboxes.pbshow(t):s.hasClass("closed")&&n.isFunction(postboxes.pbhide)&&postboxes.pbhide(t)),a.trigger("postbox-toggled",s))},handleOrder:function(){var e,o=n(this),s=o.closest(".postbox"),t=s.attr("id"),a=s.closest(".meta-box-sortables").find(".postbox:visible"),r=a.length,i=a.index(s);if("dashboard_browser_nag"!==t){if("true"===o.attr("aria-disabled"))return e=o.hasClass("handle-order-higher")?d("The box is on the first position"):d("The box is on the last position"),void wp.a11y.speak(e);if(o.hasClass("handle-order-higher")){if(0===i)return void postboxes.handleOrderBetweenSortables("previous",o,s);s.prevAll(".postbox:visible").eq(0).before(s),o.focus(),postboxes.updateOrderButtonsProperties(),postboxes.save_order(postboxes.page)}if(o.hasClass("handle-order-lower")){if(i+1===r)return void postboxes.handleOrderBetweenSortables("next",o,s);s.nextAll(".postbox:visible").eq(0).after(s),o.focus(),postboxes.updateOrderButtonsProperties(),postboxes.save_order(postboxes.page)}}},handleOrderBetweenSortables:function(e,o,s){var t,a,r=o.closest(".meta-box-sortables").attr("id"),i=[];n(".meta-box-sortables:visible").each(function(){i.push(n(this).attr("id"))}),1!==i.length&&(t=n.inArray(r,i),a=s.detach(),"previous"===e&&n(a).appendTo("#"+i[t-1]),"next"===e&&n(a).prependTo("#"+i[t+1]),postboxes._mark_area(),o.focus(),postboxes.updateOrderButtonsProperties(),postboxes.save_order(postboxes.page))},updateOrderButtonsProperties:function(){var e=n(".meta-box-sortables:first").attr("id"),o=n(".meta-box-sortables:last").attr("id"),s=n(".postbox:visible:first"),t=n(".postbox:visible:last"),a=s.closest(".meta-box-sortables").attr("id"),r=t.closest(".meta-box-sortables").attr("id");n(".handle-order-higher").attr("aria-disabled","false"),n(".handle-order-lower").attr("aria-disabled","false"),e===a&&n(s).find(".handle-order-higher").attr("aria-disabled","true"),o===r&&n(".postbox:visible .handle-order-lower").last().attr("aria-disabled","true")},add_postbox_toggles:function(t,e){var o=n(".postbox .hndle, .postbox .handlediv"),s=n(".postbox .handle-order-higher, .postbox .handle-order-lower");this.page=t,this.init(t,e),o.on("click.postboxes",this.handle_click),s.on("click.postboxes",this.handleOrder),n(".postbox .hndle a").click(function(e){e.stopPropagation()}),n(".postbox a.dismiss").on("click.postboxes",function(e){var o=n(this).parents(".postbox").attr("id")+"-hide";e.preventDefault(),n("#"+o).prop("checked",!1).triggerHandler("click")}),n(".hide-postbox-tog").bind("click.postboxes",function(){var e=n(this),o=e.val(),s=n("#"+o);e.prop("checked")?(s.show(),n.isFunction(postboxes.pbshow)&&postboxes.pbshow(o)):(s.hide(),n.isFunction(postboxes.pbhide)&&postboxes.pbhide(o)),postboxes.save_state(t),postboxes._mark_area(),a.trigger("postbox-toggled",s)}),n('.columns-prefs input[type="radio"]').bind("click.postboxes",function(){var e=parseInt(n(this).val(),10);e&&(postboxes._pb_edit(e),postboxes.save_order(t))})},init:function(o,e){var s=n(document.body).hasClass("mobile"),t=n(".postbox .handlediv");n.extend(this,e||{}),n(".meta-box-sortables").sortable({placeholder:"sortable-placeholder",connectWith:".meta-box-sortables",items:".postbox",handle:".hndle",cursor:"move",delay:s?200:0,distance:2,tolerance:"pointer",forcePlaceholderSize:!0,helper:function(e,o){return o.clone().find(":input").attr("name",function(e,o){return"sort_"+parseInt(1e5*Math.random(),10).toString()+"_"+o}).end()},opacity:.65,start:function(){n("body").addClass("is-dragging-metaboxes")},stop:function(){var e=n(this);n("body").removeClass("is-dragging-metaboxes"),e.find("#dashboard_browser_nag").is(":visible")&&"dashboard_browser_nag"!=this.firstChild.id?e.sortable("cancel"):(postboxes.updateOrderButtonsProperties(),postboxes.save_order(o))},receive:function(e,o){"dashboard_browser_nag"==o.item[0].id&&n(o.sender).sortable("cancel"),postboxes._mark_area(),a.trigger("postbox-moved",o.item)}}),s&&(n(document.body).bind("orientationchange.postboxes",function(){postboxes._pb_change()}),this._pb_change()),this._mark_area(),this.updateOrderButtonsProperties(),a.on("postbox-toggled",this.updateOrderButtonsProperties),t.each(function(){var e=n(this);e.attr("aria-expanded",!e.closest(".postbox").hasClass("closed"))})},save_state:function(e){var o,s;"nav-menus"!==e&&(o=n(".postbox").filter(".closed").map(function(){return this.id}).get().join(","),s=n(".postbox").filter(":hidden").map(function(){return this.id}).get().join(","),n.post(ajaxurl,{action:"closed-postboxes",closed:o,hidden:s,closedpostboxesnonce:jQuery("#closedpostboxesnonce").val(),page:e}))},save_order:function(e){var o,s=n(".columns-prefs input:checked").val()||0;o={action:"meta-box-order",_ajax_nonce:n("#meta-box-order-nonce").val(),page_columns:s,page:e},n(".meta-box-sortables").each(function(){o["order["+this.id.split("-")[0]+"]"]=n(this).sortable("toArray").join(",")}),n.post(ajaxurl,o,function(e){e.success&&wp.a11y.speak(d("The boxes order has been saved."))})},_mark_area:function(){var e=n("#dashboard-widgets .meta-box-sortables:visible, #post-body .meta-box-sortables:visible"),o=!0;e.each(function(){var e=n(this);e.children(".postbox:visible").length?(e.removeClass("empty-container"),o=!1):e.addClass("empty-container")}),postboxes.updateEmptySortablesText(e,o)},updateEmptySortablesText:function(e,o){var s=n("#dashboard-widgets").length,t=d(o?"Add boxes from the Screen Options menu":"Drag boxes here");s&&e.each(function(){n(this).hasClass("empty-container")&&n(this).attr("data-emptyString",t)})},_pb_edit:function(e){var o=n(".metabox-holder").get(0);o&&(o.className=o.className.replace(/columns-\d+/,"columns-"+e)),n(document).trigger("postboxes-columnchange")},_pb_change:function(){var e=n('label.columns-prefs-1 input[type="radio"]');switch(window.orientation){case 90:case-90:e.length&&e.is(":checked")||this._pb_edit(2);break;case 0:case 180:n("#poststuff").length?this._pb_edit(1):e.length&&e.is(":checked")||this._pb_edit(2)}},pbshow:!1,pbhide:!1}}(jQuery);