| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var homeIconSVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="14" height="14"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>'; |
| 5 |
|
| 6 |
function getSep(separator, custom) { |
| 7 |
if (separator === 'chevron') return '›'; |
| 8 |
if (separator === 'arrow') return '→'; |
| 9 |
if (separator === 'slash') return '/'; |
| 10 |
if (separator === 'dot') return '·'; |
| 11 |
if (separator === 'dash') return '–'; |
| 12 |
return custom || '/'; |
| 13 |
} |
| 14 |
|
| 15 |
function escHtml(str) { |
| 16 |
if (!str) return ''; |
| 17 |
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); |
| 18 |
} |
| 19 |
|
| 20 |
function buildCrumbHTML(crumbs, sep, showHomeIcon, showCurrent, withSchema) { |
| 21 |
var listItems = crumbs.map(function (c, i) { |
| 22 |
var isLast = i === crumbs.length - 1; |
| 23 |
var schema = withSchema ? ' itemscope itemprop="itemListElement" itemtype="https://schema.org/ListItem"' : ''; |
| 24 |
var posAttr = withSchema ? ' itemprop="position" content="' + (i + 1) + '"' : ''; |
| 25 |
|
| 26 |
var inner; |
| 27 |
if (isLast && !showCurrent) return ''; |
| 28 |
if (isLast) { |
| 29 |
var label = isLast ? '<span class="bkbg-bc-text"' + (withSchema ? ' itemprop="name"' : '') + '>' + escHtml(c.label) + '</span>' : ''; |
| 30 |
inner = label; |
| 31 |
} else { |
| 32 |
var iconHtml = (c.isHome && showHomeIcon) ? '<span class="bkbg-bc-home-icon">' + homeIconSVG + '</span>' : ''; |
| 33 |
var nameAttr = withSchema ? ' itemprop="name"' : ''; |
| 34 |
var itemId = withSchema ? ' itemprop="item"' : ''; |
| 35 |
inner = '<a href="' + escHtml(c.url) + '" class="bkbg-bc-link"' + itemId + '>' + |
| 36 |
iconHtml + '<span' + nameAttr + '>' + (c.isHome && showHomeIcon ? '' : escHtml(c.label)) + '</span>' + |
| 37 |
'</a>'; |
| 38 |
if (withSchema) inner += '<meta' + posAttr + '/>'; |
| 39 |
} |
| 40 |
|
| 41 |
var sepHtml = !isLast ? '<span class="bkbg-bc-sep" aria-hidden="true">' + sep + '</span>' : ''; |
| 42 |
|
| 43 |
return '<li class="bkbg-bc-item' + (isLast ? ' bkbg-bc-item--current' : '') + '"' + schema + '>' + inner + '</li>' + sepHtml; |
| 44 |
}).join(''); |
| 45 |
|
| 46 |
var schemaList = withSchema |
| 47 |
? ' itemscope itemtype="https://schema.org/BreadcrumbList"' |
| 48 |
: ''; |
| 49 |
return '<ol class="bkbg-bc-list"' + schemaList + '>' + listItems + '</ol>'; |
| 50 |
} |
| 51 |
|
| 52 |
function initBreadcrumbs(nav) { |
| 53 |
var d = nav.dataset; |
| 54 |
var showHome = d.showHome !== '0'; |
| 55 |
var showCurrent = d.showCurrent !== '0'; |
| 56 |
var showIcon = d.showIcon !== '0'; |
| 57 |
var withSchema = d.schema !== '0'; |
| 58 |
var separator = d.separator || 'chevron'; |
| 59 |
var custom = d.sepCustom || '/'; |
| 60 |
var homeLabel = d.homeLabel || 'Home'; |
| 61 |
var homeUrl = d.homeUrl || '/'; |
| 62 |
var sep = getSep(separator, custom); |
| 63 |
|
| 64 |
/* Build crumb trail from current path */ |
| 65 |
var crumbs = []; |
| 66 |
if (showHome) { |
| 67 |
crumbs.push({ label: homeLabel, url: homeUrl, isHome: true }); |
| 68 |
} |
| 69 |
|
| 70 |
/* Parse current path segments */ |
| 71 |
var path = window.location.pathname; |
| 72 |
var parts = path.replace(/^\/|\/$/g, '').split('/').filter(Boolean); |
| 73 |
|
| 74 |
/* Try to use document title for current page */ |
| 75 |
var pageTitle = document.title.split(/[|\-–—]/)[0].trim(); |
| 76 |
|
| 77 |
/* Build intermediate crumbs from URL segments (heuristic) */ |
| 78 |
var accumulated = ''; |
| 79 |
parts.forEach(function (part, idx) { |
| 80 |
accumulated += '/' + part; |
| 81 |
var label = part.replace(/[-_]/g, ' ').replace(/\b\w/g, function(c){ return c.toUpperCase(); }); |
| 82 |
var isLast = idx === parts.length - 1; |
| 83 |
if (isLast && pageTitle) label = pageTitle; |
| 84 |
crumbs.push({ |
| 85 |
label: label, |
| 86 |
url: isLast ? '' : accumulated + '/' |
| 87 |
}); |
| 88 |
}); |
| 89 |
|
| 90 |
/* Fallback: no path parts found, just show Home + Page Title */ |
| 91 |
if (!parts.length && pageTitle) { |
| 92 |
crumbs.push({ label: pageTitle, url: '' }); |
| 93 |
} |
| 94 |
|
| 95 |
if (crumbs.length === 0) return; |
| 96 |
|
| 97 |
nav.innerHTML = buildCrumbHTML(crumbs, sep, showIcon, showCurrent, withSchema); |
| 98 |
} |
| 99 |
|
| 100 |
document.addEventListener('DOMContentLoaded', function () { |
| 101 |
document.querySelectorAll('.bkbg-bc-wrap[data-separator]').forEach(initBreadcrumbs); |
| 102 |
}); |
| 103 |
})(); |
| 104 |
|