PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/js/decisionTree.js

https://code.google.com/
JavaScript | 183 lines | 153 code | 17 blank | 13 comment | 18 complexity | 0467f33cfab5e175de1a2883b5f83b70 MD5 | raw file
  1. var treeData;
  2. $(document).ready( function(){
  3. windowWidth = $('#tree-window').outerWidth( false );
  4. sliderWidth = 0;
  5. slideTime = 300;
  6. branches = new Array();
  7. var thisURL = new String(document.location);
  8. var urlParts = thisURL.split('?');
  9. loadData( urlParts[1] );
  10. });
  11. function debug( str ){
  12. $('#debug').append( str + '<br />' );
  13. }
  14. function loadData( id ){
  15. $.ajax({
  16. type: "GET",
  17. url: "xml/tree" + id + ".xml",
  18. dataType: "xml",
  19. success: function( xml ){
  20. buildNodes( xml );
  21. }
  22. });
  23. }
  24. function TreeBranch(){
  25. this.id = '';
  26. this.content = '';
  27. this.forkIDs = new Array();
  28. this.forkLabels = new Array();
  29. }
  30. function buildNodes( xmlData ){
  31. var maxDepth = 0;
  32. treeData = xmlData;
  33. $(xmlData).find('branch').each(
  34. function(){
  35. var branch = new TreeBranch();
  36. branch.id = $(this).attr('id');
  37. branch.content = $(this).find('content').text();
  38. $(this).find('fork').each(
  39. function(){
  40. branch.forkIDs.push( $(this).attr('target') );
  41. branch.forkLabels.push( $(this).text() );
  42. }
  43. );
  44. branches.push( branch );
  45. var branchDepthParts = branch.id.split('.');
  46. if( branchDepthParts.length > maxDepth ){
  47. maxDepth = branchDepthParts.length;
  48. }
  49. });
  50. sliderWidth = windowWidth * maxDepth;
  51. $('#tree-slider').width( sliderWidth );
  52. showBranch( 1 );
  53. }
  54. function resetActionLinks(){
  55. $('.decision-links a').unbind( 'click' );
  56. $('a.back-link').unbind( 'click' );
  57. $('.decision-links a').click( function(e){
  58. if( !$(this).attr('href') ){
  59. showBranch( $(this).attr('id') );
  60. }
  61. });
  62. $('a.back-link').click( function(){
  63. $('#tree-window').scrollTo( '-=' + windowWidth + 'px', { axis:'x', duration:slideTime, easing:'easeInOutExpo' } );
  64. $(this).parent().fadeOut( slideTime, function(){
  65. $(this).remove();
  66. });
  67. });
  68. }
  69. function showBranch( id ){
  70. for(i = 0; i < branches.length; i++ ){
  71. if( branches[i].id == id ){
  72. var currentBranch = branches[i];
  73. break;
  74. }
  75. }
  76. var decisionLinksHTML = '<div class="decision-links">';
  77. for( d = 0; d < currentBranch.forkIDs.length; d++ ){
  78. var link = '';
  79. var forkContent = $(treeData).find('branch[id="' + currentBranch.forkIDs[d] + '"]').find('content').text();
  80. if( forkContent.indexOf('http://') == 0 || forkContent.indexOf('https://') == 0 ){
  81. link = 'href="' + forkContent + '"'
  82. }
  83. decisionLinksHTML += '<a ' + link + ' id="' + currentBranch.forkIDs[d] + '">' + currentBranch.forkLabels[d] + '</a>';
  84. }
  85. decisionLinksHTML += '</div>';
  86. var branchHTML = '<div id="branch-' + currentBranch.id + '" class="tree-content-box"><div class="content">' + currentBranch.content + '</div>' + decisionLinksHTML;
  87. if( currentBranch.id != 1 ){
  88. branchHTML += '<a class="back-link">&laquo; Back</a>';
  89. }
  90. branchHTML += '</div>';
  91. $('#tree-slider').append( branchHTML );
  92. resetActionLinks();
  93. if( currentBranch.id != 1 ){
  94. $('#tree-window').scrollTo( '+=' + windowWidth + 'px', { axis:'x', duration:slideTime, easing:'easeInOutExpo' } );
  95. }
  96. // add last-child class for IE
  97. $('.decision-links a:last').addClass( 'last-child' );
  98. }
  99. /*
  100. Useful timer functions used for menu mouseout delays
  101. Source: http://www.codingforums.com/showthread.php?t=10531
  102. */
  103. function Timer(){
  104. this.obj = (arguments.length)?arguments[0]:window;
  105. return this;
  106. }
  107. // The set functions should be called with:
  108. // - The name of the object method (as a string) (required)
  109. // - The millisecond delay (required)
  110. // - Any number of extra arguments, which will all be
  111. // passed to the method when it is evaluated.
  112. Timer.prototype.setInterval = function(func, msec){
  113. var i = Timer.getNew();
  114. var t = Timer.buildCall(this.obj, i, arguments);
  115. Timer.set[i].timer = window.setInterval(t,msec);
  116. return i;
  117. }
  118. Timer.prototype.setTimeout = function(func, msec){
  119. var i = Timer.getNew();
  120. Timer.buildCall(this.obj, i, arguments);
  121. Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
  122. return i;
  123. }
  124. // The clear functions should be called with
  125. // the return value from the equivalent set function.
  126. Timer.prototype.clearInterval = function(i){
  127. if(!Timer.set[i]) return;
  128. window.clearInterval(Timer.set[i].timer);
  129. Timer.set[i] = null;
  130. }
  131. Timer.prototype.clearTimeout = function(i){
  132. if(!Timer.set[i]) return;
  133. window.clearTimeout(Timer.set[i].timer);
  134. Timer.set[i] = null;
  135. }
  136. // Private data
  137. Timer.set = new Array();
  138. Timer.buildCall = function(obj, i, args){
  139. var t = "";
  140. Timer.set[i] = new Array();
  141. if(obj != window){
  142. Timer.set[i].obj = obj;
  143. t = "Timer.set["+i+"].obj.";
  144. }
  145. t += args[0]+"(";
  146. if(args.length > 2){
  147. Timer.set[i][0] = args[2];
  148. t += "Timer.set["+i+"][0]";
  149. for(var j=1; (j+2)<args.length; j++){
  150. Timer.set[i][j] = args[j+2];
  151. t += ", Timer.set["+i+"]["+j+"]";
  152. }}
  153. t += ");";
  154. Timer.set[i].call = t;
  155. return t;
  156. }
  157. Timer.callOnce = function(i){
  158. if(!Timer.set[i]) return;
  159. eval(Timer.set[i].call);
  160. Timer.set[i] = null;
  161. }
  162. Timer.getNew = function(){
  163. var i = 0;
  164. while(Timer.set[i]) i++;
  165. return i;
  166. }