summaryrefslogtreecommitdiff
path: root/modules/menu/menu.admin.js
blob: 681c4814d9d6b70e3ee31894306f103db113bf4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// $Id$

(function ($) {

  Drupal.behaviors.menuChangeParentItems = {
    attach: function (context, settings) {
      $('fieldset#edit-menu input').each(function () {
        $(this).change(function () {
          // Update list of available parent menu items.
          Drupal.menu_update_parent_list();
        });
      });
    }
  }

  /**
   * Function to set the options of the menu parent item dropdown.
   */
  Drupal.menu_update_parent_list = function () {
    var values = [];

    $('input:checked', $('fieldset#edit-menu')).each(function () {
      // Get the names of all checked menus.
      values.push(Drupal.checkPlain($.trim($(this).val())));
    });

    var url = Drupal.settings.basePath + 'admin/structure/menu/parents';
    $.ajax({
      url: location.protocol + '//' + location.host + url,
      type: 'POST',
      data: {'menus[]' : values},
      dataType: 'json',
      success: function (options) {
        // Save key of last selected element.
        var selected = $('fieldset#edit-menu #edit-menu-parent :selected').val();
        // Remove all exisiting options from dropdown.
        $('fieldset#edit-menu #edit-menu-parent').children().remove();
        // Add new options to dropdown.
        jQuery.each(options, function(index, value) {
          $('fieldset#edit-menu #edit-menu-parent').append(
            $('<option ' + (index == selected ? ' selected="selected"' : '') + '></option>').val(index).text(value)
          );
        });
      }
    });
  }

})(jQuery);