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