menu_link.field.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (function ($) {
  2. /**
  3. * Automatically fill in a menu link title, if possible.
  4. */
  5. Drupal.behaviors.menuLinkFieldAutoTitle = {
  6. attach: function (context) {
  7. $('.field-type-menu-link .menu-link-item.menu-link-auto-title', context).each(function () {
  8. // Try to find menu link item elements as well as a entity 'title' field
  9. // in the form, but play nicely with user permissions and form alterations.
  10. var $enabled = $('.menu-link-item-enabled', this),
  11. $link_title = $('.menu-link-item-title', this),
  12. $title = $(this).closest('form').find('.form-item-title input'),
  13. required = $link_title.hasClass('required'),
  14. link_title = $link_title.val();
  15. // Bail out if we do not have all required fields.
  16. if (!$link_title.length || !$title.length || !($enabled.length || required)) {
  17. return;
  18. }
  19. // If there is a link title already, mark it as overridden. The user expects
  20. // that toggling the checkbox twice will take over the node's title.
  21. if (link_title.length ) {
  22. if ($enabled.is(':checked')) {
  23. $link_title.data('menuLinkFieldAutomaticTitleOverridden', true);
  24. }
  25. else if (!$enabled.length && required && link_title != $title.val()) {
  26. $link_title.data('menuLinkFieldAutomaticTitleOverridden', true);
  27. }
  28. }
  29. // Take over any title change.
  30. $title.keyup(function () {
  31. if (!$link_title.data('menuLinkFieldAutomaticTitleOverridden') && ($enabled.length ? $enabled.is(':checked') : required)) {
  32. $link_title.val($title.val());
  33. $link_title.val($title.val()).trigger('formUpdated');
  34. }
  35. });
  36. // Whenever the value is changed manually, disable this behavior.
  37. $link_title.keyup(function () {
  38. $link_title.data('menuLinkFieldAutomaticTitleOverridden', true);
  39. });
  40. // Global trigger on checkbox (do not fill-in a value when disabled).
  41. $enabled.change(function () {
  42. if ($enabled.is(':checked')) {
  43. if (!$link_title.data('menuLinkFieldAutomaticTitleOverridden')) {
  44. $link_title.val($title.val());
  45. }
  46. }
  47. else {
  48. $link_title.val('');
  49. $link_title.removeData('menuLinkFieldAutomaticTitleOverridden');
  50. }
  51. $enabled.closest('fieldset.vertical-tabs-pane').trigger('summaryUpdated');
  52. $enabled.trigger('formUpdated');
  53. });
  54. });
  55. }
  56. };
  57. /**
  58. * Automatically update summary of fieldsets containging menu link fields.
  59. *
  60. * To enable this behavior enable the Field group module and put your menu link
  61. * field into a field group.
  62. *
  63. * @link http://drupal.org/project/field_group
  64. */
  65. Drupal.behaviors.menuLinkFieldAutoFieldsetSummary = {
  66. attach: function (context) {
  67. $('fieldset.vertical-tabs-pane .field-type-menu-link', context).each(function () {
  68. $(this).closest('fieldset.vertical-tabs-pane').drupalSetSummary(function (context) {
  69. var $item = $('.menu-link-item.menu-link-auto-fieldset-summary:first', context);
  70. var $enabled = $item.find('.menu-link-item-enabled');
  71. var $title = $item.find('.menu-link-item-title');
  72. if (($enabled.length && $enabled.is(':checked')) || $title.val()) {
  73. return Drupal.checkPlain($title.val());
  74. }
  75. else {
  76. return Drupal.t('Not in menu');
  77. }
  78. });
  79. });
  80. }
  81. };
  82. })(jQuery);