jqtreetable.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright: Paul Hanlon
  3. Released under the MIT/BSD licence which means you can do anything you want
  4. with it, as long as you keep this copyright notice on the page
  5. */
  6. (function(jq){
  7. jq.fn.jqTreeTable=function(map, options){
  8. var opts = jq.extend({openImg:"",shutImg:"",leafImg:"",lastOpenImg:"",lastShutImg:"",lastLeafImg:"",vertLineImg:"",blankImg:"",collapse:false,column:0,striped:false,highlight:false,state:true},options),
  9. mapa=[],mapb=[],tid=this.attr("id"),collarr=[],
  10. stripe=function(){
  11. if(opts.striped){
  12. $("#"+tid+" tr:visible").filter(":even").addClass("even").end().filter(":odd").removeClass("even");
  13. }
  14. },
  15. buildText = function(parno, preStr){//Recursively build up the text for the images that make it work
  16. var mp=mapa[parno], ro=0, pre="", pref, img;
  17. for (var y=0,yl=mp.length;y<yl;y++){
  18. ro = mp[y];
  19. if (mapa[ro]){//It's a parent as well. Build it's string and move on to it's children
  20. pre=(y==yl-1)? opts.blankImg: opts.vertLineImg;
  21. img=(y==yl-1)? opts.lastOpenImg: opts.openImg;
  22. mapb[ro-1] = preStr + '<img src="'+img+'" class="parimg" id="'+tid+ro+'">';
  23. pref = preStr + '<img src="'+pre+'" class="preimg">';
  24. arguments.callee(ro, pref);
  25. }else{//it's a child
  26. img = (y==yl-1)? opts.lastLeafImg: opts.leafImg;//It's the last child, It's child will have a blank field behind it
  27. mapb[ro-1] = preStr + '<img src="'+img+'" class="ttimage" id="'+tid+ro+'">';
  28. }
  29. }
  30. },
  31. expandKids = function(num, last){//Expands immediate children, and their uncollapsed children
  32. jq("#"+tid+num).attr("src", (last)? opts.lastOpenImg: opts.openImg);//
  33. for (var x=0, xl=mapa[num].length;x<xl;x++){
  34. var mnx = mapa[num][x];
  35. jq("#"+tid+mnx).parents("tr").removeClass("collapsed");
  36. if (mapa[mnx] && opts.state && jq.inArray(mnx, collarr)<0){////If it is a parent and its number is not in the collapsed array
  37. arguments.callee(mnx,(x==xl-1));//Expand it. More intuitive way of displaying the tree
  38. }
  39. }
  40. },
  41. collapseKids = function(num, last){//Recursively collapses all children and their children and change icon
  42. jq("#"+tid+num).attr("src", (last)? opts.lastShutImg: opts.shutImg);
  43. for (var x=0, xl=mapa[num].length;x<xl;x++){
  44. var mnx = mapa[num][x];
  45. jq("#"+tid+mnx).parents("tr").addClass("collapsed");
  46. if (mapa[mnx]){//If it is a parent
  47. arguments.callee(mnx,(x==xl-1));
  48. }
  49. }
  50. },
  51. creset = function(num, exp){//Resets the collapse array
  52. var o = (exp)? collarr.splice(jq.inArray(num, collarr), 1): collarr.push(num);
  53. cset(tid,collarr);
  54. },
  55. cget = function(n){
  56. var v='',c=' '+document.cookie+';',s=c.indexOf(' '+n+'=');
  57. if (s>=0) {
  58. s+=n.length+2;
  59. v=(c.substring(s,c.indexOf(';',s))).split("|");
  60. }
  61. return v||0;
  62. },
  63. cset = function (n,v) {
  64. jq.unique(v);
  65. document.cookie = n+"="+v.join("|")+";";
  66. };
  67. for (var x=0,xl=map.length; x<xl;x++){//From map of parents, get map of kids
  68. num = map[x];
  69. if (!mapa[num]){
  70. mapa[num]=[];
  71. }
  72. mapa[num].push(x+1);
  73. }
  74. buildText(0,"");
  75. jq("tr", this).each(function(i){//Inject the images into the column to make it work
  76. jq(this).children("td").eq(opts.column).prepend(mapb[i]);
  77. //jq(this).children("td").eq(4).prepend("["+((mapa[i+1])? mapa[i+1]: "Child")+"]");//REMOVE THIS for production
  78. });
  79. collarr = cget(tid)||opts.collapse||collarr;
  80. if (collarr.length){
  81. cset(tid,collarr);
  82. for (var y=0,yl=collarr.length;y<yl;y++){
  83. collapseKids(collarr[y],($("#"+collarr[y]+ " .parimg").attr("src")==opts.lastOpenImg));
  84. }
  85. }
  86. stripe();
  87. jq(".parimg", this).each(function(i){
  88. var jqt = jq(this),last;
  89. jqt.click(function(){
  90. var num = parseInt(jqt.attr("id").substr(tid.length));//Number of the row
  91. if (jqt.parents("tr").next().is(".collapsed")){//If the table row directly below is collapsed
  92. expandKids(num, (jqt.attr("src")==opts.lastShutImg));//Then expand all children not in collarr
  93. if(opts.state){creset(num,true);}//If state is set, store in cookie
  94. }else{//Collapse all and set image to opts.shutImg or opts.lastShutImg on parents
  95. collapseKids(num, (jqt.attr("src")==opts.lastOpenImg));
  96. if(opts.state){creset(num,false);}//If state is set, store in cookie
  97. }
  98. stripe();//Restripe the rows
  99. });
  100. });
  101. if (opts.highlight){//This is where it highlights the rows
  102. jq("tr", this).hover(
  103. function(){jq(this).addClass("over");},
  104. function(){jq(this).removeClass("over");}
  105. );
  106. };
  107. };
  108. return this;
  109. })(jQuery);