jquery.treeview.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Treeview 1.2 - jQuery plugin to hide and show branches of a tree
  3. *
  4. * Copyright (c) 2006 Jörn Zaefferer, Myles Angell
  5. *
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl.html
  9. *
  10. * Revision: $Id$
  11. *
  12. */
  13. /**
  14. * Takes an unordered list and makes all branches collapsable.
  15. *
  16. * The "treeview" class is added if not already present.
  17. *
  18. * To hide branches on first display, mark their li elements with
  19. * the class "closed". If the "collapsed" option is used, mark intially open
  20. * branches with class "open".
  21. *
  22. * @example .treeview, .treeview ul {
  23. * padding: 0;
  24. * margin: 0;
  25. * list-style: none;
  26. * }
  27. *
  28. * .treeview li {
  29. * margin: 0;
  30. * padding: 4px 0 3px 20px;
  31. * }
  32. *
  33. * .treeview li { background: url(images/tv-item.gif) 0 0 no-repeat; }
  34. * .treeview .collapsable { background-image: url(images/tv-collapsable.gif); }
  35. * .treeview .expandable { background-image: url(images/tv-expandable.gif); }
  36. * .treeview .last { background-image: url(images/tv-item-last.gif); }
  37. * .treeview .lastCollapsable { background-image: url(images/tv-collapsable-last.gif); }
  38. * .treeview .lastExpandable { background-image: url(images/tv-expandable-last.gif); }
  39. * @desc The following styles are necessary in your stylesheet. There is are alternative sets of images available.
  40. *
  41. * @example $("ul").Treeview();
  42. * @before <ul>
  43. * <li>Item 1
  44. * <ul>
  45. * <li>Item 1.1</li>
  46. * </ul>
  47. * </li>
  48. * <li class="closed">Item 2 (starts closed)
  49. * <ul>
  50. * <li>Item 2.1
  51. * <ul>
  52. * <li>Item 2.1.1</li>
  53. * <li>Item 2.1.2</li>
  54. * </ul>
  55. * </li>
  56. * <li>Item 2.2</li>
  57. * </ul>
  58. * </li>
  59. * <li>Item 3</li>
  60. * </ul>
  61. * @desc Basic usage example
  62. *
  63. * @example $("ul").Treeview({ speed: "fast", collapsed: true});
  64. * @before <ul>
  65. * <li class="open">Item 1 (starts open)
  66. * <ul>
  67. * <li>Item 1.1</li>
  68. * </ul>
  69. * </li>
  70. * <li>Item 2
  71. * <ul>
  72. * <li>Item 2.1</li>
  73. * <li>Item 2.2</li>
  74. * </ul>
  75. * </li>
  76. * </ul>
  77. * @desc Create a treeview that starts collapsed. Toggling branches is animated.
  78. *
  79. * @example $("ul").Treeview({ control: #treecontrol });
  80. * @before <div id="treecontrol">
  81. * <a href="#">Collapse All</a>
  82. * <a href="#">Expand All</a>
  83. * <a href="#">Toggle All</a>
  84. * </div>
  85. * @desc Creates a treeview that can be controlled with a few links.
  86. * Very likely to be changed/improved in future versions.
  87. *
  88. * @param Map options Optional settings to configure treeview
  89. * @option String|Number speed Speed of animation, see animate() for details. Default: none, no animation
  90. * @option Boolean collapsed Start with all branches collapsed. Default: none, all expanded
  91. * @option <Content> control Container for a treecontrol, see last example.
  92. * @option Boolean unique Set to allow only one branch on one level to be open
  93. * (closing siblings which opening). Default: none
  94. * @option Function toggle Callback when toggling a branch.
  95. * Arguments: "this" refers to the UL that was shown or hidden.
  96. * Works only with speed option set (set speed: 1 to enable callback without animations).
  97. * Default: none
  98. * @type jQuery
  99. * @name Treeview
  100. * @cat Plugins/Treeview
  101. */
  102. (function($) {
  103. // classes used by the plugin
  104. // need to be styled via external stylesheet, see first example
  105. var CLASSES = {
  106. open: "open",
  107. closed: "closed",
  108. expandable: "expandable",
  109. collapsable: "collapsable",
  110. lastCollapsable: "lastCollapsable",
  111. lastExpandable: "lastExpandable",
  112. last: "last",
  113. hitarea: "hitarea"
  114. };
  115. // styles for hitareas
  116. var hitareaCSS = {
  117. height: 15,
  118. width: 15,
  119. marginLeft: "-15px",
  120. "float": "left",
  121. cursor: "pointer"
  122. };
  123. // ie specific styles for hitareas
  124. if( $.browser.msie ) {
  125. $.extend( hitareaCSS, {
  126. background: "#fff",
  127. filter: "alpha(opacity=0)",
  128. display: "inline"
  129. });
  130. }
  131. $.extend($.fn, {
  132. swapClass: function(c1, c2) {
  133. return this.each(function() {
  134. var $this = $(this);
  135. if ( $.className.has(this, c1) )
  136. $this.removeClass(c1).addClass(c2);
  137. else if ( $.className.has(this, c2) )
  138. $this.removeClass(c2).addClass(c1);
  139. });
  140. },
  141. replaceclass: function(c1, c2) {
  142. return this.each(function() {
  143. var $this = $(this);
  144. if ( $.className.has(this, c1) )
  145. $this.removeClass(c1).addClass(c2);
  146. });
  147. },
  148. Treeview: function(settings) {
  149. // currently no defaults necessary, all implicit
  150. settings = $.extend({}, settings);
  151. // factory for treecontroller
  152. function treeController(tree, control) {
  153. // factory for click handlers
  154. function handler(filter) {
  155. return function() {
  156. // reuse toggle event handler, applying the elements to toggle
  157. // start searching for all hitareas
  158. toggler.apply( $("div." + CLASSES.hitarea, tree).filter(function() {
  159. // for plain toggle, no filter is provided, otherwise we need to check the parent element
  160. return filter ? $(this).parent("." + filter).length : true;
  161. }) );
  162. return false;
  163. }
  164. }
  165. // click on first element to collapse tree
  166. $(":eq(0)", control).click( handler(CLASSES.collapsable) );
  167. // click on second to expand tree
  168. $(":eq(1)", control).click( handler(CLASSES.expandable) );
  169. // click on third to toggle tree
  170. $(":eq(2)", control).click( handler() );
  171. }
  172. // handle toggle event
  173. function toggler() {
  174. // this refers to hitareas, we need to find the parent lis first
  175. $( this ).parent()
  176. // swap classes
  177. .swapClass( CLASSES.collapsable, CLASSES.expandable )
  178. .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
  179. // find child lists
  180. .find( ">ul" )
  181. // toggle them
  182. .toggle( settings.speed, settings.toggle );
  183. if ( settings.unique ) {
  184. $( this ).parent()
  185. .siblings()
  186. .replaceclass( CLASSES.collapsable, CLASSES.expandable )
  187. .replaceclass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
  188. .find( ">ul" )
  189. .hide( settings.speed, settings.toggle );
  190. }
  191. }
  192. // add treeview class to activate styles
  193. this.addClass("treeview");
  194. // mark last tree items
  195. $("li:last-child", this).addClass(CLASSES.last);
  196. // collapse whole tree, or only those marked as closed, anyway except those marked as open
  197. $( (settings.collapsed ? "li" : "li." + CLASSES.closed) + ":not(." + CLASSES.open + ") > ul", this).hide();
  198. // find all tree items with child lists
  199. $("li[ul]", this)
  200. // handle closed ones first
  201. .filter("[>ul:hidden]")
  202. .addClass(CLASSES.expandable)
  203. .swapClass(CLASSES.last, CLASSES.lastExpandable)
  204. .end()
  205. // handle open ones
  206. .not("[>ul:hidden]")
  207. .addClass(CLASSES.collapsable)
  208. .swapClass(CLASSES.last, CLASSES.lastCollapsable)
  209. .end()
  210. // append hitarea
  211. .prepend("<div class=\"" + CLASSES.hitarea + "\">")
  212. // find hitarea
  213. .find("div." + CLASSES.hitarea)
  214. // apply styles to hitarea
  215. .css(hitareaCSS)
  216. // apply toggle event to hitarea
  217. .toggle( toggler, toggler );
  218. // if control option is set, create the treecontroller
  219. if ( settings.control )
  220. treeController(this, settings.control);
  221. return this;
  222. }
  223. });
  224. })(jQuery);