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.4 KiB

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