2015-08-17 17:00:26 -07:00
/ * *
* @ file
* Menu UI behaviors .
* /
( function ( $ ) {
2015-10-21 21:44:50 -07:00
'use strict' ;
2015-08-17 17:00:26 -07:00
/ * *
2015-09-04 13:20:09 -07:00
* Set a summary on the menu link form .
2015-08-17 17:00:26 -07:00
*
* @ type { Drupal ~ behavior }
2015-09-04 13:20:09 -07:00
*
* @ prop { Drupal ~ behaviorAttach } attach
* Find the form and call ` drupalSetSummary ` on it .
2015-08-17 17:00:26 -07:00
* /
Drupal . behaviors . menuUiDetailsSummaries = {
attach : function ( context ) {
$ ( context ) . find ( '.menu-link-form' ) . drupalSetSummary ( function ( context ) {
var $context = $ ( context ) ;
2015-10-08 11:40:12 -07:00
if ( $context . find ( '.js-form-item-menu-enabled input' ) . is ( ':checked' ) ) {
return Drupal . checkPlain ( $context . find ( '.js-form-item-menu-title input' ) . val ( ) ) ;
2015-08-17 17:00:26 -07:00
}
else {
return Drupal . t ( 'Not in menu' ) ;
}
} ) ;
}
} ;
/ * *
* Automatically fill in a menu link title , if possible .
*
* @ type { Drupal ~ behavior }
2015-09-04 13:20:09 -07:00
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches change and keyup behavior for automatically filling out menu
* link titles .
2015-08-17 17:00:26 -07:00
* /
Drupal . behaviors . menuUiLinkAutomaticTitle = {
attach : function ( context ) {
var $context = $ ( context ) ;
$context . find ( '.menu-link-form' ) . each ( function ( ) {
var $this = $ ( this ) ;
2015-09-04 13:20:09 -07:00
// Try to find menu settings widget elements as well as a 'title' field
// in the form, but play nicely with user permissions and form
// alterations.
2015-10-08 11:40:12 -07:00
var $checkbox = $this . find ( '.js-form-item-menu-enabled input' ) ;
var $link _title = $context . find ( '.js-form-item-menu-title input' ) ;
var $title = $this . closest ( 'form' ) . find ( '.js-form-item-title-0-value input' ) ;
2015-08-17 17:00:26 -07:00
// Bail out if we do not have all required fields.
if ( ! ( $checkbox . length && $link _title . length && $title . length ) ) {
return ;
}
2015-09-04 13:20:09 -07:00
// If there is a link title already, mark it as overridden. The user
// expects that toggling the checkbox twice will take over the node's
// title.
2015-08-17 17:00:26 -07:00
if ( $checkbox . is ( ':checked' ) && $link _title . val ( ) . length ) {
$link _title . data ( 'menuLinkAutomaticTitleOverridden' , true ) ;
}
// Whenever the value is changed manually, disable this behavior.
$link _title . on ( 'keyup' , function ( ) {
$link _title . data ( 'menuLinkAutomaticTitleOverridden' , true ) ;
} ) ;
// Global trigger on checkbox (do not fill-in a value when disabled).
$checkbox . on ( 'change' , function ( ) {
if ( $checkbox . is ( ':checked' ) ) {
if ( ! $link _title . data ( 'menuLinkAutomaticTitleOverridden' ) ) {
$link _title . val ( $title . val ( ) ) ;
}
}
else {
$link _title . val ( '' ) ;
$link _title . removeData ( 'menuLinkAutomaticTitleOverridden' ) ;
}
$checkbox . closest ( '.vertical-tabs-pane' ) . trigger ( 'summaryUpdated' ) ;
$checkbox . trigger ( 'formUpdated' ) ;
} ) ;
// Take over any title change.
$title . on ( 'keyup' , function ( ) {
if ( ! $link _title . data ( 'menuLinkAutomaticTitleOverridden' ) && $checkbox . is ( ':checked' ) ) {
$link _title . val ( $title . val ( ) ) ;
$link _title . val ( $title . val ( ) ) . trigger ( 'formUpdated' ) ;
}
} ) ;
} ) ;
}
} ;
} ) ( jQuery ) ;