PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/skin/breadcrumbs.js

http://github.com/apache/zookeeper
JavaScript | 237 lines | 103 code | 32 blank | 102 comment | 21 complexity | ed5ea2507af2834fb4033eec345dddd5 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  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. * 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. /**
  26. * IE 5 on Mac doesn't know Array.push.
  27. *
  28. * Implement it - courtesy to fritz.
  29. */
  30. var abc = new Array();
  31. if (!abc.push) {
  32. Array.prototype.push = function(what){this[this.length]=what}
  33. }
  34. /* ========================================================================
  35. CONSTANTS
  36. ======================================================================== */
  37. /**
  38. * Two-dimensional array containing extra crumbs to place at the front of
  39. * the trail. Specify first the name of the crumb, then the URI that belongs
  40. * to it. You'll need to modify this for every domain or subdomain where
  41. * you use this script (you can leave it as an empty array if you wish)
  42. */
  43. var PREPREND_CRUMBS = new Array();
  44. var link1 = "@skinconfig.trail.link1.name@";
  45. var link2 = "@skinconfig.trail.link2.name@";
  46. var link3 = "@skinconfig.trail.link3.name@";
  47. var href1 = "@skinconfig.trail.link1.href@";
  48. var href2 = "@skinconfig.trail.link2.href@";
  49. var href3 = "@skinconfig.trail.link3.href@";
  50. if(!(link1=="")&&!link1.indexOf( "@" ) == 0){
  51. PREPREND_CRUMBS.push( new Array( link1, href1 ) );
  52. }
  53. if(!(link2=="")&&!link2.indexOf( "@" ) == 0){
  54. PREPREND_CRUMBS.push( new Array( link2, href2 ) );
  55. }
  56. if(!(link3=="")&&!link3.indexOf( "@" ) == 0){
  57. PREPREND_CRUMBS.push( new Array( link3, href3 ) );
  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 = " &gt; ";
  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. { return string;
  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 += unescape( 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 += unescape( 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. }