collapsible-div.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @file
  3. * Javascript required for a simple collapsible div.
  4. *
  5. * Creating a collapsible div with this doesn't take too much. There are
  6. * three classes necessary:
  7. *
  8. * - ctools-collapsible-container: This is the overall container that will be
  9. * collapsible. This must be a div.
  10. * - ctools-collapsible-handle: This is the title area, and is what will be
  11. * visible when it is collapsed. This can be any block element, such as div
  12. * or h2.
  13. * - ctools-collapsible-content: This is the ocntent area and will only be
  14. * visible when expanded. This must be a div.
  15. *
  16. * Adding 'ctools-collapsible-remember' to the container class will cause the
  17. * state of the container to be stored in a cookie, and remembered from page
  18. * load to page load. This will only work if the container has a unique ID, so
  19. * very carefully add IDs to your containers.
  20. *
  21. * If the class 'ctools-no-container' is placed on the container, the container
  22. * will be the handle. The content will be found by appending '-content' to the
  23. * id of the handle. The ctools-collapsible-handle and
  24. * ctools-collapsible-content classes will not be required in that case, and no
  25. * restrictions on what of data the container is are placed. Like
  26. * ctools-collapsible-remember this requires an id to eist.
  27. *
  28. * The content will be 'open' unless the container class has 'ctools-collapsed'
  29. * as a class, which will cause the container to draw collapsed.
  30. */
  31. (function ($) {
  32. // All CTools tools begin with this if they need to use the CTools namespace.
  33. if (!Drupal.CTools) {
  34. Drupal.CTools = {};
  35. }
  36. /**
  37. * Object to store state.
  38. *
  39. * This object will remember the state of collapsible containers. The first
  40. * time a state is requested, it will check the cookie and set up the variable.
  41. * If a state has been changed, when the window is unloaded the state will be
  42. * saved.
  43. */
  44. Drupal.CTools.Collapsible = {
  45. state: {},
  46. stateLoaded: false,
  47. stateChanged: false,
  48. cookieString: 'ctools-collapsible-state=',
  49. /**
  50. * Get the current collapsed state of a container.
  51. *
  52. * If set to 1, the container is open. If set to -1, the container is
  53. * collapsed. If unset the state is unknown, and the default state should
  54. * be used.
  55. */
  56. getState: function (id) {
  57. if (!this.stateLoaded) {
  58. this.loadCookie();
  59. }
  60. return this.state[id];
  61. },
  62. /**
  63. * Set the collapsed state of a container for subsequent page loads.
  64. *
  65. * Set the state to 1 for open, -1 for collapsed.
  66. */
  67. setState: function (id, state) {
  68. if (!this.stateLoaded) {
  69. this.loadCookie();
  70. }
  71. this.state[id] = state;
  72. if (!this.stateChanged) {
  73. this.stateChanged = true;
  74. $(window).unload(this.unload);
  75. }
  76. },
  77. /**
  78. * Check the cookie and load the state variable.
  79. */
  80. loadCookie: function () {
  81. // If there is a previous instance of this cookie
  82. if (document.cookie.length > 0) {
  83. // Get the number of characters that have the list of values
  84. // from our string index.
  85. offset = document.cookie.indexOf(this.cookieString);
  86. // If its positive, there is a list!
  87. if (offset != -1) {
  88. offset += this.cookieString.length;
  89. var end = document.cookie.indexOf(';', offset);
  90. if (end == -1) {
  91. end = document.cookie.length;
  92. }
  93. // Get a list of all values that are saved on our string
  94. var cookie = unescape(document.cookie.substring(offset, end));
  95. if (cookie != '') {
  96. var cookieList = cookie.split(',');
  97. for (var i = 0; i < cookieList.length; i++) {
  98. var info = cookieList[i].split(':');
  99. this.state[info[0]] = info[1];
  100. }
  101. }
  102. }
  103. }
  104. this.stateLoaded = true;
  105. },
  106. /**
  107. * Turn the state variable into a string and store it in the cookie.
  108. */
  109. storeCookie: function () {
  110. var cookie = '';
  111. // Get a list of IDs, saparated by comma
  112. for (i in this.state) {
  113. if (cookie != '') {
  114. cookie += ',';
  115. }
  116. cookie += i + ':' + this.state[i];
  117. }
  118. // Save this values on the cookie
  119. document.cookie = this.cookieString + escape(cookie) + ';path=/';
  120. },
  121. /**
  122. * Respond to the unload event by storing the current state.
  123. */
  124. unload: function() {
  125. Drupal.CTools.Collapsible.storeCookie();
  126. }
  127. };
  128. // Set up an array for callbacks.
  129. Drupal.CTools.CollapsibleCallbacks = [];
  130. Drupal.CTools.CollapsibleCallbacksAfterToggle = [];
  131. /**
  132. * Bind collapsible behavior to a given container.
  133. */
  134. Drupal.CTools.bindCollapsible = function () {
  135. var $container = $(this);
  136. // Allow the specification of the 'no container' class, which means the
  137. // handle and the container can be completely independent.
  138. if ($container.hasClass('ctools-no-container') && $container.attr('id')) {
  139. // In this case, the container *is* the handle and the content is found
  140. // by adding '-content' to the id. Obviously, an id is required.
  141. var handle = $container;
  142. var content = $('#' + $container.attr('id') + '-content');
  143. }
  144. else {
  145. var handle = $container.children('.ctools-collapsible-handle');
  146. var content = $container.children('div.ctools-collapsible-content');
  147. }
  148. if (content.length) {
  149. // Create the toggle item and place it in front of the toggle.
  150. var toggle = $('<span class="ctools-toggle"></span>');
  151. handle.before(toggle);
  152. // If the remember class is set, check to see if we have a remembered
  153. // state stored.
  154. if ($container.hasClass('ctools-collapsible-remember') && $container.attr('id')) {
  155. var state = Drupal.CTools.Collapsible.getState($container.attr('id'));
  156. if (state == 1) {
  157. $container.removeClass('ctools-collapsed');
  158. }
  159. else if (state == -1) {
  160. $container.addClass('ctools-collapsed');
  161. }
  162. }
  163. // If we should start collapsed, do so:
  164. if ($container.hasClass('ctools-collapsed')) {
  165. toggle.toggleClass('ctools-toggle-collapsed');
  166. content.hide();
  167. }
  168. var afterToggle = function () {
  169. if (Drupal.CTools.CollapsibleCallbacksAfterToggle) {
  170. for (i in Drupal.CTools.CollapsibleCallbacksAfterToggle) {
  171. Drupal.CTools.CollapsibleCallbacksAfterToggle[i]($container, handle, content, toggle);
  172. }
  173. }
  174. }
  175. var clickMe = function () {
  176. if (Drupal.CTools.CollapsibleCallbacks) {
  177. for (i in Drupal.CTools.CollapsibleCallbacks) {
  178. Drupal.CTools.CollapsibleCallbacks[i]($container, handle, content, toggle);
  179. }
  180. }
  181. // If the container is a table element slideToggle does not do what
  182. // we want, so use toggle() instead.
  183. if ($container.is('table')) {
  184. content.toggle(0, afterToggle);
  185. }
  186. else {
  187. content.slideToggle(100, afterToggle);
  188. }
  189. $container.toggleClass('ctools-collapsed');
  190. toggle.toggleClass('ctools-toggle-collapsed');
  191. // If we're supposed to remember the state of this class, do so.
  192. if ($container.hasClass('ctools-collapsible-remember') && $container.attr('id')) {
  193. var state = toggle.hasClass('ctools-toggle-collapsed') ? -1 : 1;
  194. Drupal.CTools.Collapsible.setState($container.attr('id'), state);
  195. }
  196. return false;
  197. }
  198. // Let both the toggle and the handle be clickable.
  199. toggle.click(clickMe);
  200. handle.click(clickMe);
  201. }
  202. };
  203. /**
  204. * Support Drupal's 'behaviors' system for binding.
  205. */
  206. Drupal.behaviors.CToolsCollapsible = {
  207. attach: function(context) {
  208. $('.ctools-collapsible-container', context).once('ctools-collapsible', Drupal.CTools.bindCollapsible);
  209. }
  210. }
  211. })(jQuery);