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