menu.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (function ($) {
  2. Drupal.behaviors.menuFieldsetSummaries = {
  3. attach: function (context) {
  4. $('fieldset.menu-link-form', context).drupalSetSummary(function (context) {
  5. if ($('.form-item-menu-enabled input', context).is(':checked')) {
  6. return Drupal.checkPlain($('.form-item-menu-link-title input', context).val());
  7. }
  8. else {
  9. return Drupal.t('Not in menu');
  10. }
  11. });
  12. }
  13. };
  14. /**
  15. * Automatically fill in a menu link title, if possible.
  16. */
  17. Drupal.behaviors.menuLinkAutomaticTitle = {
  18. attach: function (context) {
  19. $('fieldset.menu-link-form', context).each(function () {
  20. // Try to find menu settings widget elements as well as a 'title' field in
  21. // the form, but play nicely with user permissions and form alterations.
  22. var $checkbox = $('.form-item-menu-enabled input', this);
  23. var $link_title = $('.form-item-menu-link-title input', context);
  24. var $title = $(this).closest('form').find('.form-item-title input');
  25. // Bail out if we do not have all required fields.
  26. if (!($checkbox.length && $link_title.length && $title.length)) {
  27. return;
  28. }
  29. // If there is a link title already, mark it as overridden. The user expects
  30. // that toggling the checkbox twice will take over the node's title.
  31. if ($checkbox.is(':checked') && $link_title.val().length) {
  32. $link_title.data('menuLinkAutomaticTitleOveridden', true);
  33. }
  34. // Whenever the value is changed manually, disable this behavior.
  35. $link_title.keyup(function () {
  36. $link_title.data('menuLinkAutomaticTitleOveridden', true);
  37. });
  38. // Global trigger on checkbox (do not fill-in a value when disabled).
  39. $checkbox.change(function () {
  40. if ($checkbox.is(':checked')) {
  41. if (!$link_title.data('menuLinkAutomaticTitleOveridden')) {
  42. $link_title.val($title.val());
  43. }
  44. }
  45. else {
  46. $link_title.val('');
  47. $link_title.removeData('menuLinkAutomaticTitleOveridden');
  48. }
  49. $checkbox.closest('fieldset.vertical-tabs-pane').trigger('summaryUpdated');
  50. $checkbox.trigger('formUpdated');
  51. });
  52. // Take over any title change.
  53. $title.keyup(function () {
  54. if (!$link_title.data('menuLinkAutomaticTitleOveridden') && $checkbox.is(':checked')) {
  55. $link_title.val($title.val());
  56. $link_title.val($title.val()).trigger('formUpdated');
  57. }
  58. });
  59. });
  60. }
  61. };
  62. })(jQuery);