/src/treeList/jquery.ex.treeList.js

https://github.com/gilpederiva/JqueryExtended · JavaScript · 199 lines · 101 code · 71 blank · 27 comment · 23 complexity · 4089548ec87337f54452a104d956d70b MD5 · raw file

  1. /**
  2. * Tree List Jquery Plugin
  3. *
  4. * @Author Gilberto Pederiva
  5. * @version 0.1
  6. *
  7. *
  8. * @Description: Transforma uma Lista HTML em uma estrutura de árvore
  9. *
  10. **/
  11. (function( $ ) {
  12. var methods = {
  13. init : function( options ) {
  14. var settings = {
  15. 'speedEffect' : 200,
  16. 'script': '',
  17. 'persistent': false,
  18. 'cookiePrefix':'tree_list_'
  19. }
  20. var listAncoras = null;
  21. var listItems = null;
  22. return this.each(function(){
  23. if ( options ) {
  24. $.extend( settings, options );
  25. }
  26. $(this).find("li").addClass("collapsed");
  27. listItems = $(this).find("li");
  28. listAncoras = listItems.find("a");
  29. listAncoras.live('click', function(){
  30. var itemList = $(this).closest("li");
  31. if ( itemList.hasClass("expanded") ){
  32. itemList.find('ul:eq(0)')
  33. .slideUp( settings.speedEffect ).end()
  34. .removeClass("expanded")
  35. .addClass("collapsed");
  36. if (settings.persistent == true)
  37. $.cookie( settings.cookiePrefix + itemList.attr("id"), null);
  38. }else{
  39. var itemId = itemList.attr("id");
  40. if ( itemList.find('ul:eq(0)').length == 0 && settings.script != '' ){
  41. itemList.addClass("wait");
  42. methods.getRemoteData(settings, itemId);
  43. if (settings.persistent == true)
  44. $.cookie( settings.cookiePrefix + itemId, 'true');
  45. }else{
  46. itemList.find( "ul:eq(0)" )
  47. .slideDown( settings.speedEffect ).end()
  48. .removeClass( "collapsed" )
  49. .addClass( "expanded" );
  50. if (settings.persistent == true)
  51. $.cookie( settings.cookiePrefix + itemId, 'true');
  52. }
  53. }
  54. });//end live(click)
  55. /**
  56. * Inicializa a lista conforme os dados armazenados no cookie
  57. */
  58. if (settings.persistent == true){
  59. listItems.each(function(){
  60. if ( $.cookie( settings.cookiePrefix + $(this).attr("id") ) == 'true' ){
  61. $(this).find("a:eq(0)").click();
  62. }
  63. });
  64. }
  65. });//end each
  66. },//end init
  67. /**
  68. * Obtem dados remotamente
  69. */
  70. getRemoteData: function(settings, itemId){
  71. $.get( settings.script , {
  72. parameter : itemId
  73. },
  74. function(data){
  75. $(".wait").append(data)
  76. .children('ul')
  77. .slideDown( settings.speedEffect ).end()
  78. .removeClass("collapsed wait")
  79. .addClass("expanded");
  80. }, "html");
  81. },
  82. /**
  83. * Expande todos os itens da lista
  84. */
  85. expandeAllNodes: function(){
  86. return this.each(function(){
  87. $(this).find("li.collapsed > a").click();
  88. });
  89. },
  90. /*
  91. * Recolhe todos os itens da lista
  92. */
  93. collapseAllNodes: function(){
  94. return this.each(function(){
  95. $(this).find("li.expanded > a").click();
  96. });
  97. },
  98. /**
  99. * Expande um item específico da lista
  100. */
  101. expandeNode: function(nodeId){
  102. if ( $(nodeId).length <= 0){
  103. alert("Não existe esse Item!");
  104. return false;
  105. }
  106. var node = $(nodeId);
  107. var parent = node.closest("ul");
  108. while(!parent.hasClass("treeList")){
  109. node = node.closest("ul").closest("li");
  110. if ( !node.hasClass("expanded") ){
  111. node.find("a:eq(0)").click();
  112. }
  113. parent = node.closest("ul");
  114. }
  115. }
  116. };
  117. //fim do method
  118. //define a inicialização do plugin
  119. $.fn.treeList = function( method ) {
  120. if ( methods[method] ) {
  121. return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
  122. } else if ( typeof method === 'object' || ! method ) {
  123. return methods.init.apply( this, arguments );
  124. } else {
  125. $.error( 'Method ' + method + ' does not exist on jQuery.TreeList' );
  126. }
  127. };
  128. })( jQuery );