rubik.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Implementation of Drupal behavior.
  3. */
  4. (function($) {
  5. Drupal.behaviors.rubik = {};
  6. Drupal.behaviors.rubik.attach = function(context, settings) {
  7. // If there are both main column and side column buttons, only show the main
  8. // column buttons if the user scrolls past the ones to the side.
  9. $('div.form:has(div.column-main div.form-actions):not(.rubik-processed)', context).each(function() {
  10. var form = $(this);
  11. var offset = $('div.column-side div.form-actions', form).height() + $('div.column-side div.form-actions', form).offset().top;
  12. $(window).scroll(function() {
  13. if ($(this).scrollTop() > offset) {
  14. $('div.column-main .column-wrapper > div.form-actions#edit-actions', form).show();
  15. }
  16. else {
  17. $('div.column-main .column-wrapper > div.form-actions#edit-actions', form).hide();
  18. }
  19. });
  20. form.addClass('rubik-processed');
  21. });
  22. $('a.toggler:not(.rubik-processed)', context).each(function() {
  23. var id = $(this).attr('href').split('#')[1];
  24. // Target exists, add click handler.
  25. if ($('#' + id).size() > 0) {
  26. $(this).click(function() {
  27. var toggleable = $('#' + id);
  28. toggleable.toggle();
  29. $(this).toggleClass('toggler-active');
  30. return false;
  31. });
  32. }
  33. // Target does not exist, remove click handler.
  34. else {
  35. $(this).addClass('toggler-disabled');
  36. $(this).click(function() { return false; });
  37. }
  38. // Mark as processed.
  39. $(this).addClass('rubik-processed');
  40. });
  41. // If there's no active secondary tab, make the first one show.
  42. var activeli = $('.primary-tabs li.active .secondary-tabs li.active');
  43. if (activeli.length === 0) {
  44. $('.primary-tabs li.active .secondary-tabs li:first-child a').css('display', 'block');
  45. }
  46. $('.secondary-tabs li a, .secondary-tabs', context).bind('focus blur', function(){
  47. $(this).parents('.secondary-tabs').toggleClass('focused');
  48. });
  49. // Sticky sidebar functionality.
  50. var disableSticky = (settings.rubik !== undefined) ? settings.rubik.disable_sticky : false;
  51. if ($('#content .column-side .column-wrapper').length !== 0 ) {
  52. // Move fields to sidebar if it exists.
  53. $('.rubik_sidebar_field', context).once('rubik', function() {
  54. $('.column-side .column-wrapper').append($(this));
  55. });
  56. // Check if the sidebar should be made sticky.
  57. if (!disableSticky) {
  58. var rubikColumn = $('#content .column-side .column-wrapper', context);
  59. if (rubikColumn && rubikColumn.offset()) {
  60. var rubikStickySidebar = rubikColumn.offset().top;
  61. $(window).scroll(function() {
  62. if ($(window).scrollTop() > rubikStickySidebar) {
  63. rubikColumn.each(function() {
  64. $(this).addClass("fixed");
  65. $(this).width($(this).parent().width());
  66. });
  67. }
  68. else {
  69. rubikColumn.each(function() {
  70. $(this).removeClass("fixed");
  71. $(this).width($(this).parent().width());
  72. });
  73. }
  74. });
  75. }
  76. }
  77. }
  78. // Cache the primary tabs.
  79. var $primaryTabsWrap = $('.primary-tabs');
  80. if ($primaryTabsWrap.length) {
  81. var $primaryTabs = $primaryTabsWrap.find('> li');
  82. // Trigger adjusting function upon first page load.
  83. adjustPrimaryTabs();
  84. // Trigger adjusting function upon any screen resizing.
  85. $(window).resize(function() {
  86. adjustPrimaryTabs();
  87. });
  88. }
  89. function adjustPrimaryTabs() {
  90. // Get the position of whole element.
  91. var parentPosition = $primaryTabs.offset().top;
  92. // Complicated count.
  93. var count = [];
  94. var rowNumber = 1;
  95. // Remove remainings of other classes we attached.
  96. $primaryTabs.removeClass('last-row-link');
  97. $primaryTabs.removeClass('first-row-link');
  98. // Loop through and compare the position of each tab.
  99. $primaryTabs.each(function(index) {
  100. var $this = $(this);
  101. // New row.
  102. if (count[rowNumber] != $this.offset().top) {
  103. // Increase the count for this row.
  104. rowNumber++;
  105. count[rowNumber] = $this.offset().top;
  106. // Add "first" class to this element.
  107. $this.addClass('first-row-link');
  108. // Add "last" class to the previous element, if there is one.
  109. if ($this.prev('li').length) {
  110. $this.prev('li').addClass('last-row-link');
  111. }
  112. }
  113. // Add "last" class if this is the last element.
  114. if (index === ($primaryTabs.length - 1)) {
  115. $this.addClass('last-row-link');
  116. }
  117. });
  118. }
  119. };
  120. })(jQuery);