[176] | 1 | /* |
---|
| 2 | * Copyright 2002-2004 The 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 | * This script, when included in a html file, builds a neat breadcrumb trail |
---|
| 18 | * based on its url. That is, if it doesn't contains bugs (I'm relatively |
---|
| 19 | * sure it does). |
---|
| 20 | * |
---|
| 21 | * Typical usage: |
---|
| 22 | * <script type="text/javascript" language="JavaScript" src="breadcrumbs.js"></script> |
---|
| 23 | */ |
---|
| 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 | /* ======================================================================== |
---|
| 36 | CONSTANTS |
---|
| 37 | ======================================================================== */ |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Two-dimensional array containing extra crumbs to place at the front of |
---|
| 41 | * the trail. Specify first the name of the crumb, then the URI that belongs |
---|
| 42 | * to it. You'll need to modify this for every domain or subdomain where |
---|
| 43 | * you use this script (you can leave it as an empty array if you wish) |
---|
| 44 | */ |
---|
| 45 | var PREPREND_CRUMBS = new Array(); |
---|
| 46 | |
---|
| 47 | var link1 = "@skinconfig.trail.link1.name@"; |
---|
| 48 | var link2 = "@skinconfig.trail.link2.name@"; |
---|
| 49 | var link3 = "@skinconfig.trail.link3.name@"; |
---|
| 50 | |
---|
| 51 | var href1 = "@skinconfig.trail.link1.href@"; |
---|
| 52 | var href2 = "@skinconfig.trail.link2.href@"; |
---|
| 53 | var href3 = "@skinconfig.trail.link3.href@"; |
---|
| 54 | |
---|
| 55 | if(!(link1=="")&&!link1.indexOf( "@" ) == 0){ |
---|
| 56 | PREPREND_CRUMBS.push( new Array( link1, href1 ) ); |
---|
| 57 | } |
---|
| 58 | if(!(link2=="")&&!link2.indexOf( "@" ) == 0){ |
---|
| 59 | PREPREND_CRUMBS.push( new Array( link2, href2 ) ); |
---|
| 60 | } |
---|
| 61 | if(!(link3=="")&&!link3.indexOf( "@" ) == 0){ |
---|
| 62 | PREPREND_CRUMBS.push( new Array( link3, href3 ) ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * String to include between crumbs: |
---|
| 67 | */ |
---|
| 68 | var DISPLAY_SEPARATOR = " > "; |
---|
| 69 | /** |
---|
| 70 | * String to include at the beginning of the trail |
---|
| 71 | */ |
---|
| 72 | var DISPLAY_PREPREND = " > "; |
---|
| 73 | /** |
---|
| 74 | * String to include at the end of the trail |
---|
| 75 | */ |
---|
| 76 | var DISPLAY_POSTPREND = ""; |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * CSS Class to use for a single crumb: |
---|
| 80 | */ |
---|
| 81 | var CSS_CLASS_CRUMB = "breadcrumb"; |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * CSS Class to use for the complete trail: |
---|
| 85 | */ |
---|
| 86 | var CSS_CLASS_TRAIL = "breadcrumbTrail"; |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * CSS Class to use for crumb separator: |
---|
| 90 | */ |
---|
| 91 | var CSS_CLASS_SEPARATOR = "crumbSeparator"; |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Array of strings containing common file extensions. We use this to |
---|
| 95 | * determine what part of the url to ignore (if it contains one of the |
---|
| 96 | * string specified here, we ignore it). |
---|
| 97 | */ |
---|
| 98 | var FILE_EXTENSIONS = new Array( ".html", ".htm", ".jsp", ".php", ".php3", ".php4" ); |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * String that separates parts of the breadcrumb trail from each other. |
---|
| 102 | * When this is no longer a slash, I'm sure I'll be old and grey. |
---|
| 103 | */ |
---|
| 104 | var PATH_SEPARATOR = "/"; |
---|
| 105 | |
---|
| 106 | /* ======================================================================== |
---|
| 107 | UTILITY FUNCTIONS |
---|
| 108 | ======================================================================== */ |
---|
| 109 | /** |
---|
| 110 | * Capitalize first letter of the provided string and return the modified |
---|
| 111 | * string. |
---|
| 112 | */ |
---|
| 113 | function sentenceCase( string ) |
---|
| 114 | { return string; |
---|
| 115 | //var lower = string.toLowerCase(); |
---|
| 116 | //return lower.substr(0,1).toUpperCase() + lower.substr(1); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * Returns an array containing the names of all the directories in the |
---|
| 121 | * current document URL |
---|
| 122 | */ |
---|
| 123 | function getDirectoriesInURL() |
---|
| 124 | { |
---|
| 125 | var trail = document.location.pathname.split( PATH_SEPARATOR ); |
---|
| 126 | |
---|
| 127 | // check whether last section is a file or a directory |
---|
| 128 | var lastcrumb = trail[trail.length-1]; |
---|
| 129 | for( var i = 0; i < FILE_EXTENSIONS.length; i++ ) |
---|
| 130 | { |
---|
| 131 | if( lastcrumb.indexOf( FILE_EXTENSIONS[i] ) ) |
---|
| 132 | { |
---|
| 133 | // it is, remove it and send results |
---|
| 134 | return trail.slice( 1, trail.length-1 ); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // it's not; send the trail unmodified |
---|
| 139 | return trail.slice( 1, trail.length ); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* ======================================================================== |
---|
| 143 | BREADCRUMB FUNCTIONALITY |
---|
| 144 | ======================================================================== */ |
---|
| 145 | /** |
---|
| 146 | * Return a two-dimensional array describing the breadcrumbs based on the |
---|
| 147 | * array of directories passed in. |
---|
| 148 | */ |
---|
| 149 | function getBreadcrumbs( dirs ) |
---|
| 150 | { |
---|
| 151 | var prefix = "/"; |
---|
| 152 | var postfix = "/"; |
---|
| 153 | |
---|
| 154 | // the array we will return |
---|
| 155 | var crumbs = new Array(); |
---|
| 156 | |
---|
| 157 | if( dirs != null ) |
---|
| 158 | { |
---|
| 159 | for( var i = 0; i < dirs.length; i++ ) |
---|
| 160 | { |
---|
| 161 | prefix += dirs[i] + postfix; |
---|
| 162 | crumbs.push( new Array( dirs[i], prefix ) ); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // preprend the PREPREND_CRUMBS |
---|
| 167 | if(PREPREND_CRUMBS.length > 0 ) |
---|
| 168 | { |
---|
| 169 | return PREPREND_CRUMBS.concat( crumbs ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return crumbs; |
---|
| 173 | } |
---|
| 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 | |
---|
| 183 | for( var i = 0; i < crumbs.length; i++ ) |
---|
| 184 | { |
---|
| 185 | xhtml += '<a href="' + crumbs[i][1] + '" >'; |
---|
| 186 | xhtml += unescape( crumbs[i][0] ) + '</a>'; |
---|
| 187 | if( i != (crumbs.length-1) ) |
---|
| 188 | { |
---|
| 189 | xhtml += DISPLAY_SEPARATOR; |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | xhtml += DISPLAY_POSTPREND; |
---|
| 194 | |
---|
| 195 | return xhtml; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Return a string containing an XHTML breadcrumb trail based on the |
---|
| 200 | * two-dimensional array passed in. |
---|
| 201 | */ |
---|
| 202 | function getCrumbTrailXHTML( crumbs ) |
---|
| 203 | { |
---|
| 204 | var xhtml = '<span class="' + CSS_CLASS_TRAIL + '">'; |
---|
| 205 | xhtml += DISPLAY_PREPREND; |
---|
| 206 | |
---|
| 207 | for( var i = 0; i < crumbs.length; i++ ) |
---|
| 208 | { |
---|
| 209 | xhtml += '<a href="' + crumbs[i][1] + '" class="' + CSS_CLASS_CRUMB + '">'; |
---|
| 210 | xhtml += unescape( crumbs[i][0] ) + '</a>'; |
---|
| 211 | if( i != (crumbs.length-1) ) |
---|
| 212 | { |
---|
| 213 | xhtml += '<span class="' + CSS_CLASS_SEPARATOR + '">' + DISPLAY_SEPARATOR + '</span>'; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | xhtml += DISPLAY_POSTPREND; |
---|
| 218 | xhtml += '</span>'; |
---|
| 219 | |
---|
| 220 | return xhtml; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /* ======================================================================== |
---|
| 224 | PRINT BREADCRUMB TRAIL |
---|
| 225 | ======================================================================== */ |
---|
| 226 | |
---|
| 227 | // check if we're local; if so, only print the PREPREND_CRUMBS |
---|
| 228 | if( document.location.href.toLowerCase().indexOf( "http://" ) == -1 ) |
---|
| 229 | { |
---|
| 230 | document.write( getCrumbTrail( getBreadcrumbs() ) ); |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
| 234 | document.write( getCrumbTrail( getBreadcrumbs( getDirectoriesInURL() ) ) ); |
---|
| 235 | } |
---|
| 236 | |
---|