/**
* Construct breadcrumb links.
*
* Usage:
* document.write(breadcrumbs(opt));
* -or-
* document.getElementById('element_id').innerHTML = breadcrumbs(opt);
*
* Options: an object map having the following properties.
* start_title: Title of the left-most component.
* start_index: The beginning index of path components.
* page_title: The title of the right-most component.
* path: Optional path to use in lieu of current document path.
*
* Defaults:
* If start_title is not specified, and the url is not an ip number, then the
* first component of the url is used as the title. For example, for the url
* http://intranet.company.com, the start_title is set to "intranet". The
* start_index default value is 1, corresponding to the top level path to
* start breadcrumbs. If you want to set your home to a subdirectory of the
* domain, increase the start_index. If page_title is not specified it is
* taken from the document title set in the
tag. Page titles
* specified in the HTML are assumed to be of the format: A Page Title - Company Name
* so an attempt is made to remove the Company Name from the page title in
* the breadcrumb.
*
* Copyright (c) 2010,2015 Mack Pexton (mackpexton.com)
* License: http://www.opensource.org/licenses/mit-license.php
*/
(function(){
this.breadcrumbs = function(opt) {
if (opt == null) opt = {};
var crumbs =(opt['path'] ? opt['path'] : location.pathname).split('/'), // array of path components
page_title = opt['page_title'] ? opt['page_title'] : document.title.replace(/(\w*) \W .*/,'$1'),// trim non-title part of page title
start_title = opt['start_title'] ? opt['start_title']
: /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/.test(location.hostname)
? location.hostname // ip address
: location.hostname.replace(/([^\d])\..*/,'$1').toUpperCase(), // first component of url
start_index = opt['start_index'] ? opt['start_index'] : 1, // starting path component of crumbs, increase if starting in subdirectory
stop_index = location.pathname.substr(-1) == '/' ? crumbs.length-2 : crumbs.length-1, // check for empty file component (index pages)
link = '/', html = '', i,
encodeHTML = function(html){
// Replace & < > and a double quote " but not the single quote '
return html.replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"');
};
if (page_title == '') page_title = location.pathname.replace(/.*\//,''); // use file name
for (i = 1; i < start_index; i++) { link += crumbs[i]+'/'; } // start link
html += '';
if (start_title) html += '- '+start_title+'
'; // file: schemes don't have start_title
for (i=start_index; i'+encodeHTML(decodeURIComponent(crumbs[i]).replace(/[-_]/g,' ').capitalize())+' ';
}
html += '- '+encodeHTML(page_title)+'
';
html += '
\n';
return html;
}
})();
if (!String.prototype.capitalize) String.prototype.capitalize = function() { return this.replace(/\b\w/g,function(s){return s.toUpperCase();}); };