forked from robbestad/react-breadcrumbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
328 lines (287 loc) · 9.96 KB
/
index.jsx
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* @class Breadcrumbs
* @description New breadcrumbs class based on ES6 structure.
* @exports Breadcrumbs
* @version 1.1.11
* @extends component
* @requires react
* @requires react-router
*
*/
import React from 'react'
import {Router, Route, Link} from 'react-router'
import ExecutionEnvironment from 'exenv';
class Breadcrumbs extends React.Component {
constructor(props) {
super(props);
this.displayName = "Breadcrumbs";
}
_getDisplayName(route) {
let name = null;
if (typeof route.getDisplayName === 'function') {
name = route.getDisplayName();
}
if(route.indexRoute) {
name = name || route.indexRoute.displayName || null;
} else {
name = name || route.displayName || null;
}
//check to see if a custom name has been applied to the route
if (!name && !!route.name) {
name = route.name;
}
//if the name exists and it's in the excludes list exclude this route
//if (name && this.props.excludes.some(item => item === name)) return null;
if (!name && this.props.displayMissing) {
name = this.props.displayMissingText;
}
return name;
}
_addKeyToElement (el) {
return (el && !el.key && el.type)
? Object.assign({}, el, { key: Math.random()*100 })
: el;
}
_addKeyToArrayElements (item) {
return item.map((el) => this._addKeyToElement(el));
}
_processCustomElements (items) {
return items.map((el) => {
if (!el) return null;
if (Array.isArray(el)) return this._addKeyToArrayElements(el);
return this._addKeyToElement(el);
});
}
_appendAndPrependElements(originalBreadCrumbs){
let crumbs = [];
let appendAndPrepend = this._processCustomElements([ originalBreadCrumbs.shift(), originalBreadCrumbs.pop() ]);
if (appendAndPrepend[0]) crumbs.unshift(appendAndPrepend[0]);
crumbs.push(originalBreadCrumbs[0]);
if (appendAndPrepend[1]) crumbs.push(appendAndPrepend[1]);
return crumbs.reduce( ( acc, cur ) => acc.concat(cur), [] ).filter((e) => e);
}
_resolveRouteName(route){
let name = this._getDisplayName(route);
if(!name && route.breadcrumbName) name=route.breadcrumbName;
if(!name && route.name) name=route.name;
return name;
}
_processRoute(route,routesLength,crumbsLength,isRoot,createElement) {
//if there is no route path defined and we are set to hide these then do so
if(!route.path && this.props.hideNoPath) return null;
let separator = "";
let paramName="";
let pathValue="";
let name = this._resolveRouteName(route);
if (name &&
'excludes' in this.props &&
this.props.excludes.some(item => item === name)) return null;
let makeLink=true;
// don't make link if route doesn't have a child route
if(makeLink){
makeLink = route.childRoutes ? true : false;
makeLink = routesLength !== (crumbsLength+1);
}
// set up separator
separator = routesLength !== (crumbsLength+1) ? this.props.separator : "";
if(!makeLink) separator = "";
// don't make link if route has a disabled breadcrumblink prop
if(route.hasOwnProperty("breadcrumblink")){
makeLink = route.breadcrumblink;
}
// find param name (if provided)
if(this.props.params){
paramName = Object.keys(this.props.params).map((param) => {
pathValue=param;
return this.props.params[param];
})
}
// Replace route param with real param (if provided)
let currentKey = route.path.split("/")[route.path.split("/").length-1];
let keyValue;
route.path.split("/").map((link)=>{
if(link.substring(0,1)==":"){
if(this.props.params){
keyValue = Object.keys(this.props.params).map((param) => {
return this.props.params[param];
});
let pathWithParam = route.path.split("/").map((link)=>{
if(link.substring(0,1)==":"){
return keyValue.shift();
} else {
return link;
}
})
route.path=pathWithParam.reduce((start,link)=>{return start+"/"+link;})
if(!route.staticName && currentKey.substring(0,1)==":")
name=pathWithParam.reduce((start,link)=>{return link;});
if (typeof route.prettifyParam === 'function'){
name = route.prettifyParam(name);
}
}
}
})
if (name) {
if(this.props.prettify){
// Note: this could be replaced with a more complex prettifier
console.log('prettifying')
name = name.replace(/-/g, ' ');
name = name.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
var itemClass = this.props.itemClass;
if(makeLink){
var link = !createElement ? name:
React.createElement(this.props.Link || Link, {
to: route.path,
}, name);
} else {
link = name;
itemClass += ' ' + this.props.activeItemClass;
}
return !createElement ? link:
React.createElement(this.props.itemElement, { className: itemClass, key: Math.random()*100 }, link, separator);
}
return null;
}
_buildRoutes(routes, createElement, prepend, append) {
let crumbs = [];
let isRoot = routes[1] && routes[1].hasOwnProperty("path");
let parentPath = '/';
let routesWithExclude = [];
routes.forEach((_route, index) => {
let route = Object.assign({}, _route);
if (typeof _route.prettifyParam === 'function'){
route.prettifyParam = _route.prettifyParam;
}
if('props' in route && 'path' in route.props){
route.path=route.props.path;
route.children=route.props.children;
route.name=route.props.name;
route.prettifyParam=route.props.prettifyParam;
}
if (route.path) {
if(route.path.charAt(0) === '/') {
parentPath = route.path;
} else {
if (parentPath.charAt(parentPath.length-1) !== '/') {
parentPath += '/';
}
parentPath += route.path;
}
}
if (0 < index && route.path && route.path.charAt(0) !== '/') {
route.path = parentPath;
}
let name = this._resolveRouteName(route);
if ((this.props.displayMissing || name) && route.path && !('excludes' in this.props && this.props.excludes.some(item => item === name)))
routesWithExclude.push(route);
});
routes=routesWithExclude;
routes.map((route, index) => {
if(!route) return null;
if('props' in route && 'path' in route.props){
route.path=route.props.path;
route.children=route.props.children;
route.name=route.props.name;
}
if (route.path) {
if(route.path.charAt(0) === '/') {
parentPath = route.path;
} else {
if (parentPath.charAt(parentPath.length-1) !== '/') {
parentPath += '/';
}
parentPath += route.path;
}
}
if (0 < index && route.path && route.path.charAt(0) !== '/') {
route.path = parentPath;
}
let result = this._processRoute(route,routes.length,crumbs.length,isRoot,createElement);
if (result) {
crumbs.push(result);
}
});
if (ExecutionEnvironment.canUseDOM){
if(window && window.document){
if('setDocumentTitle' in this.props && this.props.setDocumentTitle && crumbs.length > 0) {
window.document.title = crumbs[crumbs.length-1].props.children[0];
}
}
}
if (prepend || append) crumbs = this._appendAndPrependElements([ prepend, crumbs, append ]);
return !createElement ? crumbs:
React.createElement(
this.props.wrapperElement,
{ className: this.props.customClass || this.props.wrapperClass },
crumbs
);
}
render() {
return this._buildRoutes(this.props.routes, this.props.createElement, this.props.prepend, this.props.append);
}
}
/**
* @property PropTypes
* @description Property types supported by this component
* @type {{separator: *, createElement: *, displayMissing: *, displayName: *, breadcrumbName: *, wrapperElement: *, wrapperClass: *, itemElement: *, itemClass: *, activeItemClass: *, customClass: *,excludes: *, append: *, prepend: *}}
*/
Breadcrumbs.propTypes = {
prepend: React.PropTypes.oneOfType([
React.PropTypes.node,
React.PropTypes.bool,
]),
append:React.PropTypes.oneOfType([
React.PropTypes.node,
React.PropTypes.bool,
]),
separator: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.string
]),
createElement: React.PropTypes.bool,
displayMissing: React.PropTypes.bool,
prettify: React.PropTypes.bool,
displayMissingText: React.PropTypes.string,
displayName: React.PropTypes.string,
breadcrumbName: React.PropTypes.string,
wrapperElement: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.string
]),
wrapperClass: React.PropTypes.string,
itemElement: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.string
]),
itemClass: React.PropTypes.string,
customClass: React.PropTypes.string,
activeItemClass: React.PropTypes.string,
excludes: React.PropTypes.arrayOf(React.PropTypes.string),
hideNoPath: React.PropTypes.bool,
routes: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
setDocumentTitle: React.PropTypes.bool
};
/**
* @property defaultProps
* @description sets the default values for propTypes if they are not provided
* @type {{separator: string, displayMissing: boolean, wrapperElement: string, itemElement: string, wrapperClass: string, customClass: string, prepend: false, append: false}}
*/
Breadcrumbs.defaultProps = {
prepend: false,
append: false,
separator: " > ",
createElement: true,
displayMissing: true,
displayMissingText: "Missing name prop from Route",
wrapperElement: "div",
wrapperClass: "breadcrumbs",
itemElement: "span",
itemClass: "",
activeItemClass: "",
excludes: [''],
prettify: false,
hideNoPath: true,
setDocumentTitle: false
};
module.exports = Breadcrumbs;