Index: modules/menu/menu.js
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.js,v
retrieving revision 1.3
diff -u -p -r1.3 menu.js
--- modules/menu/menu.js	3 Oct 2009 17:43:52 -0000	1.3
+++ modules/menu/menu.js	2 Nov 2009 22:13:25 -0000
@@ -10,21 +10,42 @@ Drupal.behaviors.menuFieldsetSummaries =
   }
 };
 
-Drupal.behaviors.menuDisplayForm = {
-  attach: function () {
-    $('fieldset#edit-menu .form-item:first').before('<div class="form-item form-type-checkbox form-item-menu-create"><label for="edit-menu-create" class="option"><input type="checkbox" class="form-checkbox" id="edit-menu-create" name="menu[create]"/> ' + Drupal.t('Create a menu item.') + '</label></div>');
-    $('fieldset#edit-menu .form-item:gt(0)').hide();
-    $('#edit-menu-create').change(function () {
-    	if($(this).is(':checked')){
-    	  $('fieldset#edit-menu .form-item:gt(0)').show();
-    	  $('#edit-menu-link-title').val(Drupal.checkPlain($('#edit-title').val())).change();
-    	}else{
-    	  $('fieldset#edit-menu .form-item:gt(0)').hide();
-    	  $('#edit-menu-link-title').val('').change();
-    	}
+/**
+ * Automatically fill in a menu link title, if possible.
+ */
+Drupal.behaviors.menuLinkAutomaticTitle = {
+  attach: function (context) {
+    // 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.
+    var $checkbox = $('fieldset#edit-menu #edit-menu-create', context);
+    var $link_title = $('#menu-wrapper #edit-menu-link-title', context);
+    var $title = $('#menu-wrapper', context).closest('form').find('#title-wrapper input.form-text');
+    // Bail out if we do not have all required fields or when there is a link
+    // title already.
+    if (!($checkbox.length && $link_title.length && $title.length) || $checkbox.val() && $link_title.val().length) {
+      return;
+    }
+    // Whenever the value is changed manually, disable this behavior.
+    $link_title.keyup(function () {
+      $link_title.data('menuLinkAutomaticTitleOveridden', true);
     });
-    $('#edit-menu-link-title').keyup(function () {
-    	$('#edit-menu-create').attr('checked', $(this).val());
+    // Global trigger on checkbox (do not fill-in a value when disabled).
+    $checkbox.change(function () {
+      if ($checkbox.attr('checked')) {
+        if (!$link_title.data('menuLinkAutomaticTitleOveridden')) {
+          $link_title.val($title.val());
+        }
+      }
+      else {
+        $link_title.val('');
+        $link_title.removeData('menuLinkAutomaticTitleOveridden');
+      }
+    });
+    // Take over any title change.
+    $title.keyup(function () {
+      if (!$link_title.data('menuLinkAutomaticTitleOveridden') && $checkbox.attr('checked')) {
+        $link_title.val($title.val());
+      }
     });
   }
 };
Index: modules/menu/menu.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.module,v
retrieving revision 1.214
diff -u -p -r1.214 menu.module
--- modules/menu/menu.module	1 Nov 2009 12:11:10 -0000	1.214
+++ modules/menu/menu.module	2 Nov 2009 21:05:29 -0000
@@ -525,7 +525,19 @@ function menu_node_prepare(stdClass $nod
       }
     }
     // Set default values.
-    $node->menu = $item + array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0, 'customized' => 0);
+    $node->menu = $item + array(
+      'link_title' => '',
+      'mlid' => 0,
+      'plid' => 0,
+      'menu_name' => $menu_name,
+      'weight' => 0,
+      'options' => array(),
+      'module' => 'menu',
+      'expanded' => 0,
+      'hidden' => 0,
+      'has_children' => 0,
+      'customized' => 0,
+    );
   }
   // Find the depth limit for the parent select.
   if (!isset($node->menu['parent_depth_limit'])) {
@@ -553,54 +565,60 @@ function menu_form_alter(&$form, $form_s
       // menu options.
       return;
     }
-
-    // Note - doing this to make sure the delete checkbox stays in the form.
-    $form['#cache'] = TRUE;
+    $link = $form['#node']->menu;
+    $form['#submit'][] = 'menu_node_form_submit';
 
     $form['menu'] = array(
       '#type' => 'fieldset',
       '#title' => t('Menu settings'),
       '#access' => user_access('administer menu'),
       '#collapsible' => TRUE,
-      '#collapsed' => FALSE,
+      '#collapsed' => !$link['link_title'],
       '#group' => 'additional_settings',
+      '#tree' => TRUE,
       '#attached' => array(
         'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
       ),
-      '#tree' => TRUE,
       '#weight' => -2,
       '#attributes' => array('class' => array('menu-item-form')),
     );
-    $item = $form['#node']->menu;
-
-    if ($item['mlid']) {
-      // There is an existing link.
-      $form['menu']['delete'] = array(
-        '#type' => 'checkbox',
-        '#title' => t('Delete this menu item.'),
-      );
-    }
-    if (!$item['link_title']) {
-      $form['menu']['#collapsed'] = TRUE;
-    }
+    $form['menu']['create'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Create menu link'),
+      '#default_value' => (int) (bool) $link['mlid'],
+    );
+    $form['menu']['delete'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Delete this menu link'),
+      '#access' => $link['mlid'],
+    );
+    $form['menu']['link'] = array(
+      '#type' => 'container',
+      '#parents' => array('menu'),
+      '#link' => $link,
+      '#states' => array(
+        'invisible' => array(
+          'input[name="menu[create]"]' => array('checked' => FALSE),
+        ),
+      ),
+    );
 
     foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
-      $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
+      $form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]);
     }
-    $form['menu']['#item'] = $item;
 
-    $form['menu']['link_title'] = array('#type' => 'textfield',
+    $form['menu']['link']['link_title'] = array('#type' => 'textfield',
       '#title' => t('Menu link title'),
-      '#default_value' => $item['link_title'],
+      '#default_value' => $link['link_title'],
       '#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'),
       '#required' => FALSE,
     );
 
-    $default = ($item['mlid'] ? $item['menu_name'] . ':' . $item['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
+    $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
     if (!isset($options[$default])) {
       $default = 'navigation:0';
     }
-    $form['menu']['parent'] = array(
+    $form['menu']['link']['parent'] = array(
       '#type' => 'select',
       '#title' => t('Parent item'),
       '#default_value' => $default,
@@ -608,13 +626,11 @@ function menu_form_alter(&$form, $form_s
       '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
       '#attributes' => array('class' => array('menu-title-select')),
     );
-    $form['#submit'][] = 'menu_node_form_submit';
-
-    $form['menu']['weight'] = array(
+    $form['menu']['link']['weight'] = array(
       '#type' => 'weight',
       '#title' => t('Weight'),
       '#delta' => 50,
-      '#default_value' => $item['weight'],
+      '#default_value' => $link['weight'],
       '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
     );
   }
