PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/webapp/static/ub/js/materialize-plugins/collapsible.js

https://bitbucket.org/Rklplol/backend
JavaScript | 164 lines | 117 code | 28 blank | 19 comment | 18 complexity | e9dc4c683def0098c19168b2706ba8bb MD5 | raw file
  1. (function ($) {
  2. $.fn.collapsible = function (options) {
  3. var defaults = {
  4. accordion: undefined
  5. };
  6. options = $.extend(defaults, options);
  7. return this.each(function () {
  8. var $this = $(this);
  9. var $panel_headers = $(this).find('> li > .collapsible-header');
  10. var collapsible_type = $this.data("collapsible");
  11. // Turn off any existing event handlers
  12. $this.off('click.collapse', '.collapsible-header');
  13. $panel_headers.off('click.collapse');
  14. /****************
  15. Helper Functions
  16. ****************/
  17. // Accordion Open
  18. function accordionOpen(object) {
  19. $panel_headers = $this.find('> li > .collapsible-header');
  20. if (object.hasClass('active')) {
  21. object.parent().addClass('active');
  22. }
  23. else {
  24. object.parent().removeClass('active');
  25. }
  26. if (object.parent().hasClass('active')) {
  27. object.siblings('.collapsible-body').stop(true, false).slideDown({
  28. duration: 350,
  29. easing: "easeOutQuart",
  30. queue: false,
  31. complete: function () {
  32. $(this).css('height', '');
  33. }
  34. });
  35. }
  36. else {
  37. object.siblings('.collapsible-body').stop(true, false).slideUp({
  38. duration: 350,
  39. easing: "easeOutQuart",
  40. queue: false,
  41. complete: function () {
  42. $(this).css('height', '');
  43. }
  44. });
  45. }
  46. $panel_headers.not(object).removeClass('active').parent().removeClass('active');
  47. $panel_headers.not(object).parent().children('.collapsible-body').stop(true, false).slideUp(
  48. {
  49. duration: 350,
  50. easing: "easeOutQuart",
  51. queue: false,
  52. complete: function () {
  53. $(this).css('height', '');
  54. }
  55. });
  56. }
  57. // Expandable Open
  58. function expandableOpen(object) {
  59. if (object.hasClass('active')) {
  60. object.parent().addClass('active');
  61. }
  62. else {
  63. object.parent().removeClass('active');
  64. }
  65. if (object.parent().hasClass('active')) {
  66. object.siblings('.collapsible-body').stop(true, false).slideDown({
  67. duration: 350,
  68. easing: "easeOutQuart",
  69. queue: false,
  70. complete: function () {
  71. $(this).css('height', '');
  72. }
  73. });
  74. }
  75. else {
  76. object.siblings('.collapsible-body').stop(true, false).slideUp({
  77. duration: 350,
  78. easing: "easeOutQuart",
  79. queue: false,
  80. complete: function () {
  81. $(this).css('height', '');
  82. }
  83. });
  84. }
  85. }
  86. /**
  87. * Check if object is children of panel header
  88. * @param {Object} object Jquery object
  89. * @return {Boolean} true if it is children
  90. */
  91. function isChildrenOfPanelHeader(object) {
  92. var panelHeader = getPanelHeader(object);
  93. return panelHeader.length > 0;
  94. }
  95. /**
  96. * Get panel header from a children element
  97. * @param {Object} object Jquery object
  98. * @return {Object} panel header object
  99. */
  100. function getPanelHeader(object) {
  101. return object.closest('li > .collapsible-header');
  102. }
  103. /***** End Helper Functions *****/
  104. // Add click handler to only direct collapsible header children
  105. $this.on('click.collapse', '> li > .collapsible-header', function (e) {
  106. var $header = $(this),
  107. element = $(e.target);
  108. if (isChildrenOfPanelHeader(element)) {
  109. element = getPanelHeader(element);
  110. }
  111. element.toggleClass('active');
  112. if (options.accordion || collapsible_type === "accordion" || collapsible_type === undefined) { // Handle Accordion
  113. accordionOpen(element);
  114. } else { // Handle Expandables
  115. expandableOpen(element);
  116. if ($header.hasClass('active')) {
  117. expandableOpen($header);
  118. }
  119. }
  120. });
  121. // Open first active
  122. var $panel_headers = $this.find('> li > .collapsible-header');
  123. if (options.accordion || collapsible_type === "accordion" || collapsible_type === undefined) { // Handle Accordion
  124. accordionOpen($panel_headers.filter('.active').first());
  125. }
  126. else { // Handle Expandables
  127. $panel_headers.filter('.active').each(function () {
  128. expandableOpen($(this));
  129. });
  130. }
  131. });
  132. };
  133. $(document).ready(function () {
  134. $('.collapsible').collapsible();
  135. });
  136. }(jQuery));