Ticket #47569: 47569-postbox.js.diff
| File 47569-postbox.js.diff, 12.2 KB (added by , 6 years ago) |
|---|
-
postbox.js
9 9 10 10 /* global ajaxurl, postBoxL10n, postboxes */ 11 11 12 (function ($) {13 var $document = $( document);12 (function ($) { 13 var $document = $(document); 14 14 15 15 /** 16 16 * This object contains all function to handle the behaviour of the post boxes. The post boxes are the boxes you see … … 37 37 * 38 38 * @returns {void} 39 39 */ 40 handle_click : function () {41 var $el = $( this),42 p = $el.parent( '.postbox'),43 id = p.attr( 'id'),40 handle_click: function () { 41 var $el = $(this), 42 p = $el.parent('.postbox'), 43 id = p.attr('id'), 44 44 ariaExpandedValue; 45 45 46 if ( 'dashboard_browser_nag' === id) {46 if ('dashboard_browser_nag' === id) { 47 47 return; 48 48 } 49 49 50 p.toggleClass( 'closed');50 p.toggleClass('closed'); 51 51 52 ariaExpandedValue = ! p.hasClass( 'closed');52 ariaExpandedValue = !p.hasClass('closed'); 53 53 54 if ( $el.hasClass( 'handlediv' )) {54 if ($el.hasClass('handlediv')) { 55 55 // The handle button was clicked. 56 $el.attr( 'aria-expanded', ariaExpandedValue);56 $el.attr('aria-expanded', ariaExpandedValue); 57 57 } else { 58 58 // The handle heading was clicked. 59 $el.closest( '.postbox' ).find( 'button.handlediv')60 .attr( 'aria-expanded', ariaExpandedValue);59 $el.closest('.postbox').find('button.handlediv') 60 .attr('aria-expanded', ariaExpandedValue); 61 61 } 62 62 63 if ( postboxes.page !== 'press-this') {64 postboxes.save_state( postboxes.page);63 if (postboxes.page !== 'press-this') { 64 postboxes.save_state(postboxes.page); 65 65 } 66 66 67 if ( id) {68 if ( !p.hasClass('closed') && $.isFunction( postboxes.pbshow )) {69 postboxes.pbshow( id);70 } else if ( p.hasClass('closed') && $.isFunction( postboxes.pbhide )) {71 postboxes.pbhide( id);67 if (id) { 68 if (!p.hasClass('closed') && $.isFunction(postboxes.pbshow)) { 69 postboxes.pbshow(id); 70 } else if (p.hasClass('closed') && $.isFunction(postboxes.pbhide)) { 71 postboxes.pbhide(id); 72 72 } 73 73 } 74 74 … … 83 83 * @event postboxes#postbox-toggled 84 84 * @type {Object} 85 85 */ 86 $document.trigger( 'postbox-toggled', p);86 $document.trigger('postbox-toggled', p); 87 87 }, 88 88 89 89 /** … … 98 98 * @param {Function} args.pbhide A callback that is called when a postbox closes. 99 99 * @returns {void} 100 100 */ 101 add_postbox_toggles : function (page, args) {102 var $handles = $( '.postbox .hndle, .postbox .handlediv');101 add_postbox_toggles: function (page, args) { 102 var $handles = $('.postbox .hndle, .postbox .handlediv'); 103 103 104 104 this.page = page; 105 this.init( page, args);105 this.init(page, args); 106 106 107 $handles.on( 'click.postboxes', this.handle_click);107 $handles.on('click.postboxes', this.handle_click); 108 108 109 109 /** 110 110 * @since 2.7.0 111 111 */ 112 $('.postbox .hndle a').click( function(e) {112 $('.postbox .hndle a').click(function (e) { 113 113 e.stopPropagation(); 114 114 }); 115 115 … … 123 123 * 124 124 * @returns {void} 125 125 */ 126 $( '.postbox a.dismiss' ).on( 'click.postboxes', function( e) {126 $('.postbox a.dismiss').on('click.postboxes', function (e) { 127 127 var hide_id = $(this).parents('.postbox').attr('id') + '-hide'; 128 128 e.preventDefault(); 129 $( '#' + hide_id).prop('checked', false).triggerHandler('click');129 $('#' + hide_id).prop('checked', false).triggerHandler('click'); 130 130 }); 131 131 132 132 /** … … 142 142 * 143 143 * @returns {void} 144 144 */ 145 $('.hide-postbox-tog').bind('click.postboxes', function () {145 $('.hide-postbox-tog').bind('click.postboxes', function () { 146 146 var $el = $(this), 147 147 boxId = $el.val(), 148 $postbox = $( '#' + boxId);148 $postbox = $('#' + boxId); 149 149 150 if ( $el.prop( 'checked' )) {150 if ($el.prop('checked')) { 151 151 $postbox.show(); 152 if ( $.isFunction( postboxes.pbshow )) {153 postboxes.pbshow( boxId);152 if ($.isFunction(postboxes.pbshow)) { 153 postboxes.pbshow(boxId); 154 154 } 155 155 } else { 156 156 $postbox.hide(); 157 if ( $.isFunction( postboxes.pbhide )) {158 postboxes.pbhide( boxId);157 if ($.isFunction(postboxes.pbhide)) { 158 postboxes.pbhide(boxId); 159 159 } 160 160 } 161 161 162 postboxes.save_state( page);162 postboxes.save_state(page); 163 163 postboxes._mark_area(); 164 164 165 165 /** … … 166 166 * @since 4.0.0 167 167 * @see postboxes.handle_click 168 168 */ 169 $document.trigger( 'postbox-toggled', $postbox);169 $document.trigger('postbox-toggled', $postbox); 170 170 }); 171 171 172 172 /** … … 176 176 * 177 177 * @returns {void} 178 178 */ 179 $('.columns-prefs input[type="radio"]').bind('click.postboxes', function (){179 $('.columns-prefs input[type="radio"]').bind('click.postboxes', function () { 180 180 var n = parseInt($(this).val(), 10); 181 181 182 if ( n) {182 if (n) { 183 183 postboxes._pb_edit(n); 184 postboxes.save_order( page);184 postboxes.save_order(page); 185 185 } 186 186 }); 187 187 }, … … 200 200 * 201 201 * @returns {void} 202 202 */ 203 init : function(page, args) {204 var isMobile = $( document.body ).hasClass( 'mobile'),205 $handleButtons = $( '.postbox .handlediv');203 init: function (page, args) { 204 var isMobile = $(document.body).hasClass('mobile'), 205 $handleButtons = $('.postbox .handlediv'); 206 206 207 $.extend( this, args || {} ); 208 $('#wpbody-content').css('overflow','hidden'); 207 $.extend(this, args || {}); 209 208 $('.meta-box-sortables').sortable({ 210 209 placeholder: 'sortable-placeholder', 211 210 connectWith: '.meta-box-sortables', … … 212 211 items: '.postbox', 213 212 handle: '.hndle', 214 213 cursor: 'move', 215 delay: ( isMobile ? 200 : 0),214 delay: (isMobile ? 200 : 0), 216 215 distance: 2, 217 216 tolerance: 'pointer', 218 217 forcePlaceholderSize: true, 219 helper: function ( event, element) {218 helper: function (event, element) { 220 219 /* `helper: 'clone'` is equivalent to `return element.clone();` 221 220 * Cloning a checked radio and then inserting that clone next to the original 222 221 * radio unchecks the original radio (since only one of the two can be checked). … … 225 224 * duplicated, and no original radio gets unchecked. 226 225 */ 227 226 return element.clone() 228 .find( ':input')229 .attr( 'name', function( i, currentName) {230 return 'sort_' + parseInt( Math.random() * 100000, 10).toString() + '_' + currentName;231 })227 .find(':input') 228 .attr('name', function (i, currentName) { 229 return 'sort_' + parseInt(Math.random() * 100000, 10).toString() + '_' + currentName; 230 }) 232 231 .end(); 233 232 }, 234 233 opacity: 0.65, 235 stop: function () {236 var $el = $( this);234 stop: function () { 235 var $el = $(this); 237 236 238 if ( $el.find( '#dashboard_browser_nag' ).is( ':visible' ) && 'dashboard_browser_nag' != this.firstChild.id) {237 if ($el.find('#dashboard_browser_nag').is(':visible') && 'dashboard_browser_nag' != this.firstChild.id) { 239 238 $el.sortable('cancel'); 240 239 return; 241 240 } … … 242 241 243 242 postboxes.save_order(page); 244 243 }, 245 receive: function (e,ui) {246 if ( 'dashboard_browser_nag' == ui.item[0].id)244 receive: function (e, ui) { 245 if ('dashboard_browser_nag' == ui.item[0].id) 247 246 $(ui.sender).sortable('cancel'); 248 247 249 248 postboxes._mark_area(); 250 $document.trigger( 'postbox-moved', ui.item);249 $document.trigger('postbox-moved', ui.item); 251 250 } 252 251 }); 253 252 254 if ( isMobile) {255 $(document.body).bind('orientationchange.postboxes', function (){ postboxes._pb_change(); });253 if (isMobile) { 254 $(document.body).bind('orientationchange.postboxes', function () { postboxes._pb_change(); }); 256 255 this._pb_change(); 257 256 } 258 257 … … 259 258 this._mark_area(); 260 259 261 260 // Set the handle buttons `aria-expanded` attribute initial value on page load. 262 $handleButtons.each( function () {263 var $el = $( this);264 $el.attr( 'aria-expanded', ! $el.parent( '.postbox' ).hasClass( 'closed' ));261 $handleButtons.each(function () { 262 var $el = $(this); 263 $el.attr('aria-expanded', !$el.parent('.postbox').hasClass('closed')); 265 264 }); 266 265 }, 267 266 … … 277 276 * @param {string} page The page we are currently on. 278 277 * @returns {void} 279 278 */ 280 save_state : function(page) {279 save_state: function (page) { 281 280 var closed, hidden; 282 281 283 282 // Return on the nav-menus.php screen, see #35112. 284 if ( 'nav-menus' === page) {283 if ('nav-menus' === page) { 285 284 return; 286 285 } 287 286 288 closed = $( '.postbox' ).filter( '.closed' ).map( function() { return this.id; } ).get().join( ',');289 hidden = $( '.postbox' ).filter( ':hidden' ).map( function() { return this.id; } ).get().join( ',');287 closed = $('.postbox').filter('.closed').map(function () { return this.id; }).get().join(','); 288 hidden = $('.postbox').filter(':hidden').map(function () { return this.id; }).get().join(','); 290 289 291 290 $.post(ajaxurl, { 292 291 action: 'closed-postboxes', … … 308 307 * @param {string} page The page we are currently on. 309 308 * @returns {void} 310 309 */ 311 save_order : function(page) {310 save_order: function (page) { 312 311 var postVars, page_columns = $('.columns-prefs input:checked').val() || 0; 313 312 314 313 postVars = { … … 318 317 page: page 319 318 }; 320 319 321 $('.meta-box-sortables').each( function() {322 postVars[ 'order[' + this.id.split( '-' )[0] + ']' ] = $( this ).sortable( 'toArray' ).join( ',');323 } );320 $('.meta-box-sortables').each(function () { 321 postVars['order[' + this.id.split('-')[0] + ']'] = $(this).sortable('toArray').join(','); 322 }); 324 323 325 $.post( ajaxurl, postVars);324 $.post(ajaxurl, postVars); 326 325 }, 327 326 328 327 /** … … 338 337 * 339 338 * @returns {void} 340 339 */ 341 _mark_area : function() {340 _mark_area: function () { 342 341 var visible = $('div.postbox:visible').length, side = $('#post-body #side-sortables'); 343 342 344 $( '#dashboard-widgets .meta-box-sortables:visible' ).each( function() {343 $('#dashboard-widgets .meta-box-sortables:visible').each(function () { 345 344 var t = $(this); 346 345 347 if ( visible == 1 || t.children('.postbox:visible').length) {346 if (visible == 1 || t.children('.postbox:visible').length) { 348 347 t.removeClass('empty-container'); 349 348 } 350 349 else { … … 353 352 } 354 353 }); 355 354 356 if ( side.length) {357 if ( side.children('.postbox:visible').length)355 if (side.length) { 356 if (side.children('.postbox:visible').length) 358 357 side.removeClass('empty-container'); 359 else if ( $('#postbox-container-1').css('width') == '280px')358 else if ($('#postbox-container-1').css('width') == '280px') 360 359 side.addClass('empty-container'); 361 360 } 362 361 }, … … 372 371 * @param {number} n The amount of columns to divide the post edit page in. 373 372 * @returns {void} 374 373 */ 375 _pb_edit : function(n) {374 _pb_edit: function (n) { 376 375 var el = $('.metabox-holder').get(0); 377 376 378 if ( el) {377 if (el) { 379 378 el.className = el.className.replace(/columns-\d+/, 'columns-' + n); 380 379 } 381 380 … … 387 386 * 388 387 * @event postboxes#postboxes-columnchange 389 388 */ 390 $( document ).trigger( 'postboxes-columnchange');389 $(document).trigger('postboxes-columnchange'); 391 390 }, 392 391 393 392 /** … … 400 399 * 401 400 * @returns {void} 402 401 */ 403 _pb_change : function() {404 var check = $( 'label.columns-prefs-1 input[type="radio"]');402 _pb_change: function () { 403 var check = $('label.columns-prefs-1 input[type="radio"]'); 405 404 406 switch ( window.orientation) {405 switch (window.orientation) { 407 406 case 90: 408 407 case -90: 409 if ( !check.length || !check.is(':checked'))408 if (!check.length || !check.is(':checked')) 410 409 this._pb_edit(2); 411 410 break; 412 411 case 0: 413 412 case 180: 414 if ( $('#poststuff').length) {413 if ($('#poststuff').length) { 415 414 this._pb_edit(1); 416 415 } else { 417 if ( !check.length || !check.is(':checked'))416 if (!check.length || !check.is(':checked')) 418 417 this._pb_edit(2); 419 418 } 420 419 break; … … 430 429 * @property {Function|boolean} pbshow A callback that is called when a postbox 431 430 * is opened. 432 431 */ 433 pbshow : false,432 pbshow: false, 434 433 435 434 /** 436 435 * @since 2.7.0 … … 439 438 * @property {Function|boolean} pbhide A callback that is called when a postbox 440 439 * is closed. 441 440 */ 442 pbhide : false441 pbhide: false 443 442 }; 444 443 445 444 }(jQuery));