PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/profiles/openpublic/modules/contrib/field_group/field_group.js

https://github.com/dphan/OpenPublic
JavaScript | 169 lines | 120 code | 14 blank | 35 comment | 33 complexity | 237f9e2b6b493b8669c24ed4c2ae9def MD5 | raw file
  1. (function($) {
  2. /**
  3. * Drupal FieldGroup object.
  4. */
  5. Drupal.FieldGroup = Drupal.FieldGroup || {};
  6. Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
  7. Drupal.FieldGroup.groupWithfocus = null;
  8. Drupal.FieldGroup.setGroupWithfocus = function(element) {
  9. element.css({display: 'block'});
  10. Drupal.FieldGroup.groupWithfocus = element;
  11. }
  12. /**
  13. * Implements Drupal.FieldGroup.processHook().
  14. */
  15. Drupal.FieldGroup.Effects.processFieldset = {
  16. execute: function (context, settings, type) {
  17. if (type == 'form') {
  18. // Add required fields mark to any fieldsets containing required fields
  19. $('fieldset.fieldset').each(function(i){
  20. if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
  21. $('legend span.fieldset-legend', $(this)).eq(0).append(' ').append($('.form-required').eq(0).clone());
  22. }
  23. if ($('.error', $(this)).length) {
  24. $('legend span.fieldset-legend', $(this)).eq(0).addClass('error');
  25. Drupal.FieldGroup.setGroupWithfocus($(this));
  26. }
  27. });
  28. }
  29. }
  30. }
  31. /**
  32. * Implements Drupal.FieldGroup.processHook().
  33. */
  34. Drupal.FieldGroup.Effects.processAccordion = {
  35. execute: function (context, settings, type) {
  36. var accordions = $('div.field-group-accordion-wrapper', context).accordion({
  37. autoHeight: false,
  38. active: 0,
  39. collapsible: true
  40. });
  41. if (type == 'form') {
  42. // Add required fields mark to any element containing required fields
  43. $('div.accordion-item').each(function(i){
  44. if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
  45. $('h3.ui-accordion-header').eq(i).append(' ').append($('.form-required').eq(0).clone());
  46. }
  47. if ($('.error', $(this)).length) {
  48. $('h3.ui-accordion-header').eq(i).addClass('error');
  49. var activeOne = $(this).parent().accordion("activate" , i);
  50. $('.ui-accordion-content-active', activeOne).css({height: 'auto', width: 'auto', display: 'block'});
  51. }
  52. });
  53. }
  54. }
  55. }
  56. /**
  57. * Implements Drupal.FieldGroup.processHook().
  58. */
  59. Drupal.FieldGroup.Effects.processHtabs = {
  60. execute: function (context, settings, type) {
  61. if (type == 'form') {
  62. // Add required fields mark to any element containing required fields
  63. $('fieldset.horizontal-tabs-pane').each(function(i){
  64. if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
  65. $(this).data('horizontalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after(' ');
  66. }
  67. if ($('.error', $(this)).length) {
  68. $(this).data('horizontalTab').link.parent().addClass('error');
  69. Drupal.FieldGroup.setGroupWithfocus($(this));
  70. $(this).data('horizontalTab').focus();
  71. }
  72. });
  73. }
  74. }
  75. }
  76. /**
  77. * Implements Drupal.FieldGroup.processHook().
  78. */
  79. Drupal.FieldGroup.Effects.processTabs = {
  80. execute: function (context, settings, type) {
  81. if (type == 'form') {
  82. // Add required fields mark to any fieldsets containing required fields
  83. $('fieldset.vertical-tabs-pane').each(function(i){
  84. if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
  85. $(this).data('verticalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after(' ');
  86. }
  87. if ($('.error', $(this)).length) {
  88. $(this).data('verticalTab').link.parent().addClass('error');
  89. Drupal.FieldGroup.setGroupWithfocus($(this));
  90. $(this).data('verticalTab').focus();
  91. }
  92. });
  93. }
  94. }
  95. }
  96. /**
  97. * Implements Drupal.FieldGroup.processHook().
  98. *
  99. * TODO clean this up meaning check if this is really
  100. * necessary.
  101. */
  102. Drupal.FieldGroup.Effects.processDiv = {
  103. execute: function (context, settings, type) {
  104. $('div.collapsible', context).each(function() {
  105. var $wrapper = $(this);
  106. // Turn the legend into a clickable link, but retain span.field-group-format-toggler
  107. // for CSS positioning.
  108. var $toggler = $('span.field-group-format-toggler:first', $wrapper);
  109. var $link = $('<a class="field-group-format-title" href="#"></a>');
  110. $link.prepend($toggler.contents()).appendTo($toggler);
  111. // .wrapInner() does not retain bound events.
  112. $link.click(function () {
  113. var wrapper = $wrapper.get(0);
  114. // Don't animate multiple times.
  115. if (!wrapper.animating) {
  116. wrapper.animating = true;
  117. var speed = $wrapper.hasClass('speed-fast') ? 300 : 1000;
  118. if ($wrapper.hasClass('effect-none') && $wrapper.hasClass('speed-none')) {
  119. $('> .field-group-format-wrapper', wrapper).toggle();
  120. }
  121. else if ($wrapper.hasClass('effect-blind')) {
  122. $('> .field-group-format-wrapper', wrapper).toggle('blind', {}, speed);
  123. }
  124. else {
  125. $('> .field-group-format-wrapper', wrapper).toggle(speed);
  126. }
  127. wrapper.animating = false;
  128. }
  129. return false;
  130. });
  131. });
  132. }
  133. };
  134. /**
  135. * Behaviors.
  136. */
  137. Drupal.behaviors.fieldGroup = {
  138. attach: function (context, settings) {
  139. if (settings.field_group == undefined) {
  140. return;
  141. }
  142. $('body', context).once('fieldgroup-effects', function () {
  143. // Execute all of them.
  144. $.each(Drupal.FieldGroup.Effects, function (func) {
  145. // We check for a wrapper function in Drupal.field_group as
  146. // alternative for dynamic string function calls.
  147. var type = func.toLowerCase().replace("process", "");
  148. if (settings.field_group[type] != undefined && $.isFunction(this.execute)) {
  149. this.execute(context, settings, settings.field_group[type]);
  150. }
  151. });
  152. });
  153. }
  154. };
  155. })(jQuery);