All files / js/components Component.jsx

16.22% Statements 6/37
13.64% Branches 3/22
25% Functions 1/4
16.67% Lines 6/36
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  1x 1x     9x   3x                                                                                                               3x 3x                                            
'use strict'
import dialogSystem from '../dialogSystem'
import BaseComponent from './BaseComponent'
 
class Component extends BaseComponent {
  constructor (props) {
    super(props)
    this._unlistenBeforeLeavingRoute = null
  }
 
  /* helper function for forwarding props down the tree */
  getRoutingProps () {
    return {
      history: this.props.history,
      location: this.props.location,
      params: this.props.params,
      route: this.props.route,
      routeParams: this.props.routeParams,
      routes: this.props.routes
    }
  }
 
  /* helper that can generate a path to a rule */
  getPathToAdminPage (name, params) {
    let parts = this.props.routes.map((x) => x.name)
    if (name !== null) {
      if (name.substr(0, 1) === '.') {
        parts[parts.length - 1] = name.substr(1)
      } else {
        parts = name.split('.')
      }
    }
 
    const rv = []
    let node = this.props.routes[0]
    if (node.name !== parts.shift()) {
      return null
    }
    rv.push(node.path)
 
    parts.forEach((part) => {
      for (let i = 0; i < node.childRoutes.length; i++) {
        if (node.childRoutes[i].name === part) {
          node = node.childRoutes[i]
          rv.push(node.path)
          return
        }
      }
      node = null
    })
 
    return rv.join('/').replace(/:[a-zA-Z]+/g, (m) => {
      const key = m.substr(1)
      return params[key] || this.props.params[key]
    })
  }
 
  /* helper to transition to a specific page */
  transitionToAdminPage (name, params) {
    this.props.history.pushState(null, this.getPathToAdminPage(name, params))
  }
 
  componentDidMount () {
    super.componentDidMount()
    Iif (this.props.history !== undefined) {
      this._unlistenBeforeLeavingRoute = this.props.history.listenBeforeLeavingRoute(
        this.props.route, this.routerWillLeave.bind(this))
    }
  }
 
  componentWillUnmount () {
    super.componentWillUnmount()
    if (this._unlistenBeforeLeavingRoute) {
      this._unlistenBeforeLeavingRoute()
    }
  }
 
  routerWillLeave (nextLocation) {
    if (dialogSystem.preventNavigation()) {
      return false
    } else {
      dialogSystem.dismissDialog()
    }
  }
}
export default Component