Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Fix selecting input for newly-inserted post's first control #206

Merged
merged 6 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions js/customize-post-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@
section.postFieldControls.post_title = control;
api.control.add( control.id, control );

// Select the input's contents when the value is a placeholder.
control.deferred.embedded.done( function() {
control.container.find( ':input' ).on( 'focus', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter What is the use of selecting all input/textarea/select/button ( :input ) when post title field will only be input? I mean why not just use input ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. It could just be input.

if ( api.Posts.data.l10n.noTitle === control.setting().post_title ) {
$( this ).select();
}
} );
} );

if ( control.notifications ) {
control.notifications.add = section.addPostFieldControlNotification;
control.notifications.setting_property = control.params.setting_property;
Expand Down
18 changes: 16 additions & 2 deletions js/customize-posts-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
*/
function focusControlOnceFocusable() {
var firstControl = data.section.controls()[0];
if ( ! firstControl ) {
return;
}
function onChangeActive( isActive ) {
if ( isActive ) {
data.section.active.unbind( onChangeActive );
Expand All @@ -195,13 +198,24 @@
} );
}
}
if ( firstControl ) {
if ( data.section.active.get() ) {
onChangeActive( true );
} else {
data.section.active.bind( onChangeActive );
}
}

data.section.focus( {
completeCallback: focusControlOnceFocusable
completeCallback: function() {
var delay = 500;

/*
* Note the delay is because the controls get embedded
* once the section is expanded and also because it seems
* that focus fails when the input is not visible yet.
*/
_.defer( focusControlOnceFocusable, delay );
}
} );
} );
promise.always( function() {
Expand Down