You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

breadcrumbs.js 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2002-2004 Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * This script, when included in a html file, builds a neat breadcrumb trail
  19. * based on its url. That is, if it doesn't contains bugs (I'm relatively
  20. * sure it does).
  21. *
  22. * Typical usage:
  23. * <script type="text/javascript" language="JavaScript" src="breadcrumbs.js"></script>
  24. *
  25. *@author <a href="mailto:leosimons@apache.org">Leo Simons</a> (main author)
  26. *@author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a> (integration in skin)
  27. *@created July 12, 2002
  28. *@version 1.0
  29. */
  30. /**
  31. * IE 5 on Mac doesn't know Array.push.
  32. *
  33. * Implement it - courtesy to fritz.
  34. */
  35. var abc = new Array();
  36. if (!abc.push) {
  37. Array.prototype.push = function(what){this[this.length]=what}
  38. }
  39. /* ========================================================================
  40. CONSTANTS
  41. ======================================================================== */
  42. /**
  43. * Two-dimensional array containing extra crumbs to place at the front of
  44. * the trail. Specify first the name of the crumb, then the URI that belongs
  45. * to it. You'll need to modify this for every domain or subdomain where
  46. * you use this script (you can leave it as an empty array if you wish)
  47. */
  48. var PREPREND_CRUMBS = new Array();
  49. if(!("apache"=="")){
  50. PREPREND_CRUMBS.push( new Array( "apache", "http://www.apache.org/" ) );
  51. }
  52. if(!("xml.apache"=="")){
  53. PREPREND_CRUMBS.push( new Array( "ant.apache", "http://ant.apache.org/" ) );
  54. }
  55. if(!(""=="")){
  56. PREPREND_CRUMBS.push( new Array( "", "" ) );
  57. }
  58. /**
  59. * String to include between crumbs:
  60. */
  61. var DISPLAY_SEPARATOR = " &gt; ";
  62. /**
  63. * String to include at the beginning of the trail
  64. */
  65. var DISPLAY_PREPREND = "";
  66. /**
  67. * String to include at the end of the trail
  68. */
  69. var DISPLAY_POSTPREND = "";
  70. /**
  71. * CSS Class to use for a single crumb:
  72. */
  73. var CSS_CLASS_CRUMB = "breadcrumb";
  74. /**
  75. * CSS Class to use for the complete trail:
  76. */
  77. var CSS_CLASS_TRAIL = "breadcrumbTrail";
  78. /**
  79. * CSS Class to use for crumb separator:
  80. */
  81. var CSS_CLASS_SEPARATOR = "crumbSeparator";
  82. /**
  83. * Array of strings containing common file extensions. We use this to
  84. * determine what part of the url to ignore (if it contains one of the
  85. * string specified here, we ignore it).
  86. */
  87. var FILE_EXTENSIONS = new Array( ".html", ".htm", ".jsp", ".php", ".php3", ".php4" );
  88. /**
  89. * String that separates parts of the breadcrumb trail from each other.
  90. * When this is no longer a slash, I'm sure I'll be old and grey.
  91. */
  92. var PATH_SEPARATOR = "/";
  93. /* ========================================================================
  94. UTILITY FUNCTIONS
  95. ======================================================================== */
  96. /**
  97. * Capitalize first letter of the provided string and return the modified
  98. * string.
  99. */
  100. function sentenceCase( string )
  101. {
  102. var lower = string.toLowerCase();
  103. return lower.substr(0,1).toUpperCase() + lower.substr(1);
  104. }
  105. /**
  106. * Returns an array containing the names of all the directories in the
  107. * current document URL
  108. */
  109. function getDirectoriesInURL()
  110. {
  111. var trail = document.location.pathname.split( PATH_SEPARATOR );
  112. // check whether last section is a file or a directory
  113. var lastcrumb = trail[trail.length-1];
  114. for( var i = 0; i < FILE_EXTENSIONS.length; i++ )
  115. {
  116. if( lastcrumb.indexOf( FILE_EXTENSIONS[i] ) )
  117. {
  118. // it is, remove it and send results
  119. return trail.slice( 1, trail.length-1 );
  120. }
  121. }
  122. // it's not; send the trail unmodified
  123. return trail.slice( 1, trail.length );
  124. }
  125. /* ========================================================================
  126. BREADCRUMB FUNCTIONALITY
  127. ======================================================================== */
  128. /**
  129. * Return a two-dimensional array describing the breadcrumbs based on the
  130. * array of directories passed in.
  131. */
  132. function getBreadcrumbs( dirs )
  133. {
  134. var prefix = "/";
  135. var postfix = "/";
  136. // the array we will return
  137. var crumbs = new Array();
  138. if( dirs != null )
  139. {
  140. for( var i = 0; i < dirs.length; i++ )
  141. {
  142. prefix += dirs[i] + postfix;
  143. crumbs.push( new Array( dirs[i], prefix ) );
  144. }
  145. }
  146. // preprend the PREPREND_CRUMBS
  147. if(PREPREND_CRUMBS.length > 0 )
  148. {
  149. return PREPREND_CRUMBS.concat( crumbs );
  150. }
  151. return crumbs;
  152. }
  153. /**
  154. * Return a string containing a simple text breadcrumb trail based on the
  155. * two-dimensional array passed in.
  156. */
  157. function getCrumbTrail( crumbs )
  158. {
  159. var xhtml = DISPLAY_PREPREND;
  160. for( var i = 0; i < crumbs.length; i++ )
  161. {
  162. xhtml += '<a href="' + crumbs[i][1] + '" >';
  163. xhtml += sentenceCase( crumbs[i][0] ) + '</a>';
  164. if( i != (crumbs.length-1) )
  165. {
  166. xhtml += DISPLAY_SEPARATOR;
  167. }
  168. }
  169. xhtml += DISPLAY_POSTPREND;
  170. return xhtml;
  171. }
  172. /**
  173. * Return a string containing an XHTML breadcrumb trail based on the
  174. * two-dimensional array passed in.
  175. */
  176. function getCrumbTrailXHTML( crumbs )
  177. {
  178. var xhtml = '<span class="' + CSS_CLASS_TRAIL + '">';
  179. xhtml += DISPLAY_PREPREND;
  180. for( var i = 0; i < crumbs.length; i++ )
  181. {
  182. xhtml += '<a href="' + crumbs[i][1] + '" class="' + CSS_CLASS_CRUMB + '">';
  183. xhtml += sentenceCase( crumbs[i][0] ) + '</a>';
  184. if( i != (crumbs.length-1) )
  185. {
  186. xhtml += '<span class="' + CSS_CLASS_SEPARATOR + '">' + DISPLAY_SEPARATOR + '</span>';
  187. }
  188. }
  189. xhtml += DISPLAY_POSTPREND;
  190. xhtml += '</span>';
  191. return xhtml;
  192. }
  193. /* ========================================================================
  194. PRINT BREADCRUMB TRAIL
  195. ======================================================================== */
  196. // check if we're local; if so, only print the PREPREND_CRUMBS
  197. if( document.location.href.toLowerCase().indexOf( "http://" ) == -1 )
  198. {
  199. document.write( getCrumbTrail( getBreadcrumbs() ) );
  200. }
  201. else
  202. {
  203. document.write( getCrumbTrail( getBreadcrumbs( getDirectoriesInURL() ) ) );
  204. }