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 8.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 2002-2003 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Apache" and "Apache Software Foundation"
  19. must not be used to endorse or promote products derived from this software
  20. without prior written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation. For more information on the
  37. Apache Software Foundation, please see <http://www.apache.org/>.
  38. */
  39. /**
  40. * This script, when included in a html file, builds a neat breadcrumb trail
  41. * based on its url. That is, if it doesn't contains bugs (I'm relatively
  42. * sure it does).
  43. *
  44. * Typical usage:
  45. * <script type="text/javascript" language="JavaScript" src="breadcrumbs.js"></script>
  46. *
  47. *@author <a href="mailto:leosimons@apache.org">Leo Simons</a> (main author)
  48. *@author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a> (integration in skin)
  49. *@created July 12, 2002
  50. *@version 1.0
  51. */
  52. /**
  53. * IE 5 on Mac doesn't know Array.push.
  54. *
  55. * Implement it - courtesy to fritz.
  56. */
  57. var abc = new Array();
  58. if (!abc.push) {
  59. Array.prototype.push = function(what){this[this.length]=what}
  60. }
  61. /* ========================================================================
  62. CONSTANTS
  63. ======================================================================== */
  64. /**
  65. * Two-dimensional array containing extra crumbs to place at the front of
  66. * the trail. Specify first the name of the crumb, then the URI that belongs
  67. * to it. You'll need to modify this for every domain or subdomain where
  68. * you use this script (you can leave it as an empty array if you wish)
  69. */
  70. var PREPREND_CRUMBS = new Array();
  71. if(!("apache"=="")){
  72. PREPREND_CRUMBS.push( new Array( "apache", "http://www.apache.org/" ) );
  73. }
  74. if(!("xml.apache"=="")){
  75. PREPREND_CRUMBS.push( new Array( "ant.apache", "http://ant.apache.org/" ) );
  76. }
  77. if(!(""=="")){
  78. PREPREND_CRUMBS.push( new Array( "", "" ) );
  79. }
  80. /**
  81. * String to include between crumbs:
  82. */
  83. var DISPLAY_SEPARATOR = " &gt; ";
  84. /**
  85. * String to include at the beginning of the trail
  86. */
  87. var DISPLAY_PREPREND = "";
  88. /**
  89. * String to include at the end of the trail
  90. */
  91. var DISPLAY_POSTPREND = "";
  92. /**
  93. * CSS Class to use for a single crumb:
  94. */
  95. var CSS_CLASS_CRUMB = "breadcrumb";
  96. /**
  97. * CSS Class to use for the complete trail:
  98. */
  99. var CSS_CLASS_TRAIL = "breadcrumbTrail";
  100. /**
  101. * CSS Class to use for crumb separator:
  102. */
  103. var CSS_CLASS_SEPARATOR = "crumbSeparator";
  104. /**
  105. * Array of strings containing common file extensions. We use this to
  106. * determine what part of the url to ignore (if it contains one of the
  107. * string specified here, we ignore it).
  108. */
  109. var FILE_EXTENSIONS = new Array( ".html", ".htm", ".jsp", ".php", ".php3", ".php4" );
  110. /**
  111. * String that separates parts of the breadcrumb trail from each other.
  112. * When this is no longer a slash, I'm sure I'll be old and grey.
  113. */
  114. var PATH_SEPARATOR = "/";
  115. /* ========================================================================
  116. UTILITY FUNCTIONS
  117. ======================================================================== */
  118. /**
  119. * Capitalize first letter of the provided string and return the modified
  120. * string.
  121. */
  122. function sentenceCase( string )
  123. {
  124. var lower = string.toLowerCase();
  125. return lower.substr(0,1).toUpperCase() + lower.substr(1);
  126. }
  127. /**
  128. * Returns an array containing the names of all the directories in the
  129. * current document URL
  130. */
  131. function getDirectoriesInURL()
  132. {
  133. var trail = document.location.pathname.split( PATH_SEPARATOR );
  134. // check whether last section is a file or a directory
  135. var lastcrumb = trail[trail.length-1];
  136. for( var i = 0; i < FILE_EXTENSIONS.length; i++ )
  137. {
  138. if( lastcrumb.indexOf( FILE_EXTENSIONS[i] ) )
  139. {
  140. // it is, remove it and send results
  141. return trail.slice( 1, trail.length-1 );
  142. }
  143. }
  144. // it's not; send the trail unmodified
  145. return trail.slice( 1, trail.length );
  146. }
  147. /* ========================================================================
  148. BREADCRUMB FUNCTIONALITY
  149. ======================================================================== */
  150. /**
  151. * Return a two-dimensional array describing the breadcrumbs based on the
  152. * array of directories passed in.
  153. */
  154. function getBreadcrumbs( dirs )
  155. {
  156. var prefix = "/";
  157. var postfix = "/";
  158. // the array we will return
  159. var crumbs = new Array();
  160. if( dirs != null )
  161. {
  162. for( var i = 0; i < dirs.length; i++ )
  163. {
  164. prefix += dirs[i] + postfix;
  165. crumbs.push( new Array( dirs[i], prefix ) );
  166. }
  167. }
  168. // preprend the PREPREND_CRUMBS
  169. if(PREPREND_CRUMBS.length > 0 )
  170. {
  171. return PREPREND_CRUMBS.concat( crumbs );
  172. }
  173. return crumbs;
  174. }
  175. /**
  176. * Return a string containing a simple text breadcrumb trail based on the
  177. * two-dimensional array passed in.
  178. */
  179. function getCrumbTrail( crumbs )
  180. {
  181. var xhtml = DISPLAY_PREPREND;
  182. for( var i = 0; i < crumbs.length; i++ )
  183. {
  184. xhtml += '<a href="' + crumbs[i][1] + '" >';
  185. xhtml += sentenceCase( crumbs[i][0] ) + '</a>';
  186. if( i != (crumbs.length-1) )
  187. {
  188. xhtml += DISPLAY_SEPARATOR;
  189. }
  190. }
  191. xhtml += DISPLAY_POSTPREND;
  192. return xhtml;
  193. }
  194. /**
  195. * Return a string containing an XHTML breadcrumb trail based on the
  196. * two-dimensional array passed in.
  197. */
  198. function getCrumbTrailXHTML( crumbs )
  199. {
  200. var xhtml = '<span class="' + CSS_CLASS_TRAIL + '">';
  201. xhtml += DISPLAY_PREPREND;
  202. for( var i = 0; i < crumbs.length; i++ )
  203. {
  204. xhtml += '<a href="' + crumbs[i][1] + '" class="' + CSS_CLASS_CRUMB + '">';
  205. xhtml += sentenceCase( crumbs[i][0] ) + '</a>';
  206. if( i != (crumbs.length-1) )
  207. {
  208. xhtml += '<span class="' + CSS_CLASS_SEPARATOR + '">' + DISPLAY_SEPARATOR + '</span>';
  209. }
  210. }
  211. xhtml += DISPLAY_POSTPREND;
  212. xhtml += '</span>';
  213. return xhtml;
  214. }
  215. /* ========================================================================
  216. PRINT BREADCRUMB TRAIL
  217. ======================================================================== */
  218. // check if we're local; if so, only print the PREPREND_CRUMBS
  219. if( document.location.href.toLowerCase().indexOf( "http://" ) == -1 )
  220. {
  221. document.write( getCrumbTrail( getBreadcrumbs() ) );
  222. }
  223. else
  224. {
  225. document.write( getCrumbTrail( getBreadcrumbs( getDirectoriesInURL() ) ) );
  226. }