| 1 |
import React from "react"; |
| 2 |
|
| 3 |
/** |
| 4 |
* Route default options. |
| 5 |
* |
| 6 |
* @type {Object} |
| 7 |
*/ |
| 8 |
export const defaultRouteOptions = { |
| 9 |
path: null, |
| 10 |
title: "no_title", |
| 11 |
element: null, |
| 12 |
}; |
| 13 |
|
| 14 |
/** |
| 15 |
* Route object. |
| 16 |
* |
| 17 |
* @param {Object} options route options |
| 18 |
* @class |
| 19 |
*/ |
| 20 |
function Route(options) { |
| 21 |
const { path, title, element } = { ...defaultRouteOptions, ...options }; |
| 22 |
/** |
| 23 |
* Get route path. |
| 24 |
* |
| 25 |
* @return {string} path |
| 26 |
*/ |
| 27 |
this.getPath = () => path; |
| 28 |
|
| 29 |
/** |
| 30 |
* Get route title. |
| 31 |
* |
| 32 |
* @return {string} title |
| 33 |
*/ |
| 34 |
this.getTitle = () => title; |
| 35 |
|
| 36 |
/** |
| 37 |
* Get route element. |
| 38 |
* |
| 39 |
* @return {Function} element |
| 40 |
*/ |
| 41 |
this.getElement = () => |
| 42 |
element ?? <div>no element defined for route [{this.getPath()}]</div>; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Generate options array from settings objects. |
| 47 |
* |
| 48 |
* These settings objects should correspond to Route default options. |
| 49 |
* |
| 50 |
* @param {Array<Object>} optionsArray options array |
| 51 |
* @return {Array<Route>} route object array |
| 52 |
*/ |
| 53 |
export const generateRouteArray = optionsArray => { |
| 54 |
return optionsArray.map(options => new Route(options)); |
| 55 |
}; |
| 56 |
|
| 57 |
/** |
| 58 |
* @module Route |
| 59 |
*/ |
| 60 |
export default Route; |
| 61 |
|