source: other-projects/trunk/realistic-books/packages/AntInstaller/web/manual1.7.0/breadcrumbs.js@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

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