<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38643] trunk/src/wp-admin/js/postbox.js: Docs: Add documentation for `wp-admin/js/postbox.js`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38643">38643</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38643","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-22 18:46:34 +0000 (Thu, 22 Sep 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Docs: Add documentation for `wp-admin/js/postbox.js`.
Props atimmer, andizer.
Fixes <a href="https://core.trac.wordpress.org/ticket/37365">#37365</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjspostboxjs">trunk/src/wp-admin/js/postbox.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjspostboxjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/postbox.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/postbox.js 2016-09-22 14:58:41 UTC (rev 38642)
+++ trunk/src/wp-admin/js/postbox.js 2016-09-22 18:46:34 UTC (rev 38643)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,11 +1,45 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Contains the postboxes logic, opening and closing postboxes, reordering and saving
+ * the state and ordering to the database.
+ *
+ * @summary Contains postboxes logic
+ *
+ * @since 2.5.0
+ * @requires jQuery
+ */
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /* global ajaxurl, postBoxL10n */
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * This object contains all function to handle the behaviour of the post boxes. The post boxes are the boxes you see
+ * around the content on the edit page.
+ *
+ * @since 2.7.0
+ *
+ * @namespace postboxes
+ *
+ * @type {Object}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> var postboxes;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> (function($) {
</span><span class="cx" style="display: block; padding: 0 10px"> var $document = $( document );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> postboxes = {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @summary Handles a click on either the postbox heading or the postbox open/close icon.
+ *
+ * Opens or closes the postbox. Expects `this` to equal the clicked element.
+ * Calls postboxes.pbshow if the postbox has been opened, calls postboxes.pbhide
+ * if the postbox has been closed.
+ *
+ * @since 4.4.0
+ * @memberof postboxes
+ * @fires postboxes#postbox-toggled
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> handle_click : function () {
</span><span class="cx" style="display: block; padding: 0 10px"> var $el = $( this ),
</span><span class="cx" style="display: block; padding: 0 10px"> p = $el.parent( '.postbox' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -41,9 +75,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Fires when a postbox has been opened or closed.
+ *
+ * Contains a jQuery object with the relevant postbox element.
+ *
+ * @since 4.0.0
+ * @event postboxes#postbox-toggled
+ * @type {Object}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $document.trigger( 'postbox-toggled', p );
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Adds event handlers to all postboxes and screen option on the current page.
+ *
+ * @since 2.7.0
+ * @memberof postboxes
+ *
+ * @param {string} page The page we are currently on.
+ * @param {Object} [args]
+ * @param {Function} args.pbshow A callback that is called when a postbox opens.
+ * @param {Function} args.pbhide A callback that is called when a postbox closes.
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> add_postbox_toggles : function (page, args) {
</span><span class="cx" style="display: block; padding: 0 10px"> var $handles = $( '.postbox .hndle, .postbox .handlediv' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,16 +107,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $handles.on( 'click.postboxes', this.handle_click );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @since 2.7.0
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $('.postbox .hndle a').click( function(e) {
</span><span class="cx" style="display: block; padding: 0 10px"> e.stopPropagation();
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Hides a postbox.
+ *
+ * Event handler for the postbox dismiss button. After clicking the button
+ * the postbox will be hidden.
+ *
+ * @since 3.2.0
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $( '.postbox a.dismiss' ).on( 'click.postboxes', function( e ) {
</span><span class="cx" style="display: block; padding: 0 10px"> var hide_id = $(this).parents('.postbox').attr('id') + '-hide';
</span><span class="cx" style="display: block; padding: 0 10px"> e.preventDefault();
</span><span class="cx" style="display: block; padding: 0 10px"> $( '#' + hide_id ).prop('checked', false).triggerHandler('click');
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Hides the postbox element
+ *
+ * Event handler for the screen options checkboxes. When a checkbox is
+ * clicked this function will hide or show the relevant postboxes.
+ *
+ * @since 2.7.0
+ * @fires postboxes#postbox-toggled
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $('.hide-postbox-tog').bind('click.postboxes', function() {
</span><span class="cx" style="display: block; padding: 0 10px"> var $el = $(this),
</span><span class="cx" style="display: block; padding: 0 10px"> boxId = $el.val(),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,11 +157,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> postboxes.pbhide( boxId );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> postboxes.save_state( page );
</span><span class="cx" style="display: block; padding: 0 10px"> postboxes._mark_area();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @since 4.0.0
+ * @see postboxes.handle_click
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $document.trigger( 'postbox-toggled', $postbox );
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Changes the amount of columns based on the layout preferences.
+ *
+ * @since 2.8.0
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $('.columns-prefs input[type="radio"]').bind('click.postboxes', function(){
</span><span class="cx" style="display: block; padding: 0 10px"> var n = parseInt($(this).val(), 10);
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -93,6 +185,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Initializes all the postboxes, mainly their sortable behaviour.
+ *
+ * @since 2.7.0
+ * @memberof postboxes
+ *
+ * @param {string} page The page we are currently on.
+ * @param {Object} [args={}] The arguments for the postbox initializer.
+ * @param {Function} args.pbshow A callback that is called when a postbox opens.
+ * @param {Function} args.pbhide A callback that is called when a postbox
+ * closes.
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> init : function(page, args) {
</span><span class="cx" style="display: block; padding: 0 10px"> var isMobile = $( document.body ).hasClass( 'mobile' ),
</span><span class="cx" style="display: block; padding: 0 10px"> $handleButtons = $( '.postbox .handlediv' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,12 +216,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> tolerance: 'pointer',
</span><span class="cx" style="display: block; padding: 0 10px"> forcePlaceholderSize: true,
</span><span class="cx" style="display: block; padding: 0 10px"> helper: function( event, element ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // `helper: 'clone'` is equivalent to `return element.clone();`
- // Cloning a checked radio and then inserting that clone next to the original
- // radio unchecks the original radio (since only one of the two can be checked).
- // We get around this by renaming the helper's inputs' name attributes so that,
- // when the helper is inserted into the DOM for the sortable, no radios are
- // duplicated, and no original radio gets unchecked.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /* `helper: 'clone'` is equivalent to `return element.clone();`
+ * Cloning a checked radio and then inserting that clone next to the original
+ * radio unchecks the original radio (since only one of the two can be checked).
+ * We get around this by renaming the helper's inputs' name attributes so that,
+ * when the helper is inserted into the DOM for the sortable, no radios are
+ * duplicated, and no original radio gets unchecked.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> return element.clone()
</span><span class="cx" style="display: block; padding: 0 10px"> .find( ':input' )
</span><span class="cx" style="display: block; padding: 0 10px"> .attr( 'name', function( i, currentName ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -157,6 +264,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Saves the state of the postboxes to the server.
+ *
+ * Saves the state of the postboxes to the server. It sends two lists, one with
+ * all the closed postboxes, one with all the hidden postboxes.
+ *
+ * @since 2.7.0
+ * @memberof postboxes
+ *
+ * @param {string} page The page we are currently on.
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> save_state : function(page) {
</span><span class="cx" style="display: block; padding: 0 10px"> var closed, hidden;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -177,6 +296,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Saves the order of the postboxes to the server.
+ *
+ * Saves the order of the postboxes to the server. Sends a list of all postboxes
+ * inside a sortable area to the server.
+ *
+ * @since 2.8.0
+ * @memberof postboxes
+ *
+ * @param {string} page The page we are currently on.
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> save_order : function(page) {
</span><span class="cx" style="display: block; padding: 0 10px"> var postVars, page_columns = $('.columns-prefs input:checked').val() || 0;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -186,12 +317,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> page_columns: page_columns,
</span><span class="cx" style="display: block; padding: 0 10px"> page: page
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $('.meta-box-sortables').each( function() {
</span><span class="cx" style="display: block; padding: 0 10px"> postVars[ 'order[' + this.id.split( '-' )[0] + ']' ] = $( this ).sortable( 'toArray' ).join( ',' );
</span><span class="cx" style="display: block; padding: 0 10px"> } );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $.post( ajaxurl, postVars );
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Marks empty postbox areas.
+ *
+ * Adds a message to empty sortable areas on the dashboard page. Also adds a
+ * border around the side area on the post edit screen if there are no postboxes
+ * present.
+ *
+ * @since 3.3.0
+ * @memberof postboxes
+ * @access private
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> _mark_area : function() {
</span><span class="cx" style="display: block; padding: 0 10px"> var visible = $('div.postbox:visible').length, side = $('#post-body #side-sortables');
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -215,6 +361,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Changes the amount of columns on the post edit page.
+ *
+ * @since 3.3.0
+ * @memberof postboxes
+ * @fires postboxes#postboxes-columnchange
+ * @access private
+ *
+ * @param {number} n The amount of columns to divide the post edit page in.
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> _pb_edit : function(n) {
</span><span class="cx" style="display: block; padding: 0 10px"> var el = $('.metabox-holder').get(0);
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -222,9 +379,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> el.className = el.className.replace(/columns-\d+/, 'columns-' + n);
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Fires when the amount of columns on the post edit page has been changed.
+ *
+ * @since 4.0.0
+ * @event postboxes#postboxes-columnchange
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $( document ).trigger( 'postboxes-columnchange' );
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @summary Changes the amount of columns the postboxes are in based on the
+ * current orientation of the browser.
+ *
+ * @since 3.3.0
+ * @memberof postboxes
+ * @access private
+ *
+ * @returns {void}
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> _pb_change : function() {
</span><span class="cx" style="display: block; padding: 0 10px"> var check = $( 'label.columns-prefs-1 input[type="radio"]' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -247,8 +420,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /* Callbacks */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @since 2.7.0
+ * @memberof postboxes
+ * @access public
+ * @property {Function|boolean} pbshow A callback that is called when a postbox
+ * is opened.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> pbshow : false,
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @since 2.7.0
+ * @memberof postboxes
+ * @access public
+ * @property {Function|boolean} pbhide A callback that is called when a postbox
+ * is closed.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> pbhide : false
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>