-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (152 loc) · 5.62 KB
/
Copy pathindex.js
File metadata and controls
178 lines (152 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
function getBreadCrumbItems() {
let currentUrl = decodeURIComponent(window.location.pathname).split("/");
let breadcrumbItems = currentUrl.map((elem, j) => ({
index: elem.replace(/\-/g, '_').toString(),
text: elem,
url: "/" + currentUrl.map((url, i) => (i < j + 1 ? url : '')).filter((el) => el !== '').join("/"),
}));
return breadcrumbItems;
}
export default async function BreadcrumbMaster(options) {
await
function () {
//
}
if (!options.wrapper_id || !document.body.contains(document.getElementById(options.wrapper_id))) {
return;
}
//================ INITIAL CONFIGURATION ================
if (!options.splitter) {
options.splitter = '/'
}
var breadcrumb199List = document.getElementById(options.wrapper_id);
breadcrumb199List.textContent = ""
breadcrumb199List.style.listStyle = "none";
breadcrumb199List.style.display = "flex";
//==================================================
function structureBreadcrumb() {
let breadcrumbItems = [...getBreadCrumbItems()];
breadcrumbItems[0] = {
index: "home",
text: "Home",
url: "/",
}
let currentUrl = window.location.pathname.split("/");
if (options.customElements) {
breadcrumbItems = [...options.customElements];
}
if (options.exceptions_list) {
options.exceptions_list.map((ele, index) => {
const pageURL = ele.page.split('/')
const URL = currentUrl.slice(currentUrl.length - pageURL.length).join('/')
if (ele.page === URL) {
breadcrumbItems = [...ele.customElements]
}
}
)
}
let breadcrumbStructure = breadcrumbItems.map((elem, index) => ({
...elem,
pages: []
}));
if (options.strings) {
if (options.strings.home) {
breadcrumbItems[0] = {
index: "home",
text: options.strings.home,
url: "/",
}
}
}
return breadcrumbStructure;
}
let breadcrumbItems = [...structureBreadcrumb()]
// OPTION : replace strings
if (options.strings) {
// Get the size of an object
Object.size = function (obj) {
var size = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var stringsToChange = options.strings;
var size = Object.size(stringsToChange);
if (size > 0) {
for (const key in stringsToChange) {
breadcrumbItems.forEach(element => {
var index = breadcrumbItems.findIndex(p => p.text == key)
if (typeof breadcrumbItems[index] !== 'undefined') {
breadcrumbItems[index] = {
index: breadcrumbItems[index].id,
text: stringsToChange[key],
url: breadcrumbItems[index].url,
}
}
});
}
}
}
function getBaseUrl() {
var getUrl = window.location;
let baseUrl = ""
getUrl.protocol ? baseUrl += getUrl.protocol + "//" : ""
getUrl.host ? baseUrl += getUrl.host : ""
return baseUrl
}
function redirection_handler(elem_url, spa) {
// todo : fix spa behaviour
if (spa) {
history.replaceState(history.state, null, elem_url);
} else {
window.location.href = getBaseUrl() + elem_url;
}
}
for (var i = 0; i < breadcrumbItems.length; i++) {
var breadcrumb199ListItem = document.createElement("li");
breadcrumb199ListItem.className = "breadcrumb199__list--item";
breadcrumb199ListItem.innerHTML = `<a role="button" class='breadcrumb_item__${breadcrumbItems[i].index} ${i === breadcrumbItems.length - 1 ? 'active_breadcrumb_item' : ''}'>${breadcrumbItems[i].text}</a>` +
`<div class='options-splitter splitter__${breadcrumbItems[i].index}'>${options.splitter}</div>`;
(function () {
var url = breadcrumbItems[i].url;
var spa_app = options.spa_app ? options.spa_app : false;
breadcrumb199ListItem.addEventListener('click', function () {
redirection_handler(url, spa_app)
})
}());
breadcrumb199List.appendChild(breadcrumb199ListItem);
}
// configure the Styles of the breadcrumb
if (options.styles) {
if (options.styles.items_gab) {
Array.from(document.querySelectorAll(".breadcrumb199__list--item .options-splitter")).map(elem => {
elem.style.marginRight = options.styles.items_gab;
elem.style.marginLeft = options.styles.items_gab;
})
}
// configure the color_selected of the latest breadcrumb element
if (options.styles.color && document.body.contains(document.querySelector('.breadcrumb199__list--item a'))) {
Array.from(document.querySelectorAll(".breadcrumb199__list--item a")).map(elem => {
elem.style.color = options.styles.color
})
}
// configure the splitter
if (options.styles.splitter_color && document.body.contains(document.querySelector('.breadcrumb199__list--item .options-splitter'))) {
Array.from(document.querySelectorAll(".breadcrumb199__list--item .options-splitter")).map(elem => {
elem.style.color = options.styles.splitter_color
})
}
// configure the color_selected of the latest breadcrumb element
if (options.styles.color_selected && document.body.contains(document.querySelector('.breadcrumb199__list--item a'))) {
document.querySelector(".breadcrumb199__list--item:last-child a").setAttribute('style', 'color:' + options.styles.color_selected);
}
}
// configure Alignment
document.querySelector(".breadcrumb199__list--item:last-child .options-splitter").remove()
Array.from(document.querySelectorAll(".breadcrumb199__list--item")).map(elem => {
elem.style.display = 'flex'
elem.style.cursor = 'pointer'
})
}