|
412 | 412 | api.state( 'saved' ).set( wasSaved );
|
413 | 413 | };
|
414 | 414 |
|
| 415 | + /** |
| 416 | + * Focus on the control requested from the preview. |
| 417 | + * |
| 418 | + * If the control doesn't exist yet, try to determine the section it would |
| 419 | + * be part of by parsing its ID, and then if that section exists, expand it. |
| 420 | + * Once expanded, try finding the control again, since controls for post |
| 421 | + * sections may get embedded only once section.contentsEmbedded is resolved. |
| 422 | + * |
| 423 | + * @param {string} controlId Control ID. |
| 424 | + * @return {void} |
| 425 | + */ |
| 426 | + component.focusControl = function focusControl( controlId ) { |
| 427 | + var control, section, postSectionId, matches; |
| 428 | + |
| 429 | + /** |
| 430 | + * Attempt focus on the control. |
| 431 | + * |
| 432 | + * @returns {boolean} Whether the control exists. |
| 433 | + */ |
| 434 | + function tryFocus() { |
| 435 | + control = api.control( controlId ); |
| 436 | + if ( control ) { |
| 437 | + control.focus(); |
| 438 | + return true; |
| 439 | + } |
| 440 | + return false; |
| 441 | + } |
| 442 | + if ( tryFocus() ) { |
| 443 | + return; |
| 444 | + } |
| 445 | + |
| 446 | + matches = controlId.match( /^post(?:meta)?\[(.+?)]\[(\d+)]/ ); |
| 447 | + if ( ! matches ) { |
| 448 | + return; |
| 449 | + } |
| 450 | + postSectionId = 'post[' + matches[1] + '][' + matches[2] + ']'; |
| 451 | + section = api.section( postSectionId ); |
| 452 | + if ( ! section || ! section.extended( component.PostSection ) ) { |
| 453 | + return; |
| 454 | + } |
| 455 | + section.expand(); |
| 456 | + section.contentsEmbedded.done( function() { |
| 457 | + var ms = 500; |
| 458 | + |
| 459 | + // @todo It is not clear why a delay is needed for focus to work. It could be due to focus failing during animation. |
| 460 | + _.delay( tryFocus, ms ); |
| 461 | + } ); |
| 462 | + }; |
| 463 | + |
| 464 | + /** |
| 465 | + * Ensure that the post associated with an autofocused section or control is loaded. |
| 466 | + * |
| 467 | + * @returns {int[]} Post IDs autofocused. |
| 468 | + */ |
| 469 | + component.ensureAutofocusConstructPosts = function ensureAutofocusConstructPosts() { |
| 470 | + var autofocusPostIds = []; |
| 471 | + _.each( [ 'section', 'control' ], function( construct ) { |
| 472 | + var parsedAutofocusConstruct; |
| 473 | + if ( api.settings.autofocus[ construct ] ) { |
| 474 | + parsedAutofocusConstruct = component.parseSettingId( api.settings.autofocus[ construct ] ); |
| 475 | + if ( parsedAutofocusConstruct ) { |
| 476 | + autofocusPostIds.push( parsedAutofocusConstruct.postId ); |
| 477 | + } |
| 478 | + } |
| 479 | + } ); |
| 480 | + if ( autofocusPostIds.length > 0 ) { |
| 481 | + component.ensurePosts( autofocusPostIds ); |
| 482 | + } |
| 483 | + return autofocusPostIds; |
| 484 | + }; |
| 485 | + |
415 | 486 | api.bind( 'ready', function() {
|
416 | 487 |
|
417 | 488 | // Add a post_ID input for editor integrations (like Shortcake) to be able to know the post being edited.
|
|
457 | 528 | } );
|
458 | 529 | } );
|
459 | 530 |
|
460 |
| - /** |
461 |
| - * Focus on the control requested from the preview. |
462 |
| - * |
463 |
| - * If the control doesn't exist yet, try to determine the section it would |
464 |
| - * be part of by parsing its ID, and then if that section exists, expand it. |
465 |
| - * Once expanded, try finding the control again, since controls for post |
466 |
| - * sections may get embedded only once section.contentsEmbedded is resolved. |
467 |
| - * |
468 |
| - * @param {string} controlId Control ID. |
469 |
| - * @return {void} |
470 |
| - */ |
471 |
| - function focusControl( controlId ) { |
472 |
| - var control, section, postSectionId, matches; |
473 |
| - |
474 |
| - /** |
475 |
| - * Attempt focus on the control. |
476 |
| - * |
477 |
| - * @returns {boolean} Whether the control exists. |
478 |
| - */ |
479 |
| - function tryFocus() { |
480 |
| - control = api.control( controlId ); |
481 |
| - if ( control ) { |
482 |
| - control.focus(); |
483 |
| - return true; |
484 |
| - } |
485 |
| - return false; |
486 |
| - } |
487 |
| - if ( tryFocus() ) { |
488 |
| - return; |
489 |
| - } |
490 |
| - |
491 |
| - matches = controlId.match( /^post(?:meta)?\[(.+?)]\[(\d+)]/ ); |
492 |
| - if ( ! matches ) { |
493 |
| - return; |
494 |
| - } |
495 |
| - postSectionId = 'post[' + matches[1] + '][' + matches[2] + ']'; |
496 |
| - section = api.section( postSectionId ); |
497 |
| - if ( ! section || ! section.extended( component.PostSection ) ) { |
498 |
| - return; |
499 |
| - } |
500 |
| - section.expand(); |
501 |
| - section.contentsEmbedded.done( function() { |
502 |
| - var ms = 500; |
503 |
| - |
504 |
| - // @todo It is not clear why a delay is needed for focus to work. It could be due to focus failing during animation. |
505 |
| - _.delay( tryFocus, ms ); |
506 |
| - } ); |
507 |
| - } |
508 |
| - |
509 |
| - component.focusControl = focusControl; |
510 | 531 | api.previewer.bind( 'focus-control', component.focusControl );
|
| 532 | + |
| 533 | + component.ensureAutofocusConstructPosts(); |
511 | 534 | } );
|
512 | 535 |
|
513 | 536 | })( wp.customize, jQuery );
|
0 commit comments