all files / ui/ ResponsiveApplication.js

0% Statements 0/39
0% Branches 0/14
0% Functions 0/14
0% Lines 0/39
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                                                                                                                                                                                                                                                             
import { cloneDeep, platform } from '../util'
import { DefaultDOMElement } from '../dom'
import Component from './Component'
 
class ResponsiveApplication extends Component {
  constructor(...args) {
    super(...args)
 
    this.pages = {}
 
    this.handleActions({
      'navigate': this.navigate
    })
  }
 
  getInitialState() {
    return {
      route: undefined,
      mobile: this._isMobile()
    }
  }
 
  didMount() {
    if (platform.inBrowser) {
      let _window = DefaultDOMElement.getBrowserWindow()
      _window.on('resize', this._onResize, this)
    }
    this.router = this.getRouter()
    this.router.on('route:changed', this._onRouteChanged, this)
    let route = this.router.readRoute()
    // Replaces the current entry without creating new history entry
    // or triggering hashchange
    this.navigate(route, {replace: true})
  }
 
  dispose() {
    this.router.off(this)
    this.router.dispose()
  }
 
  /*
    Used to navigate the app based on given route.
 
    Example route: {documentId: 'example.xml'}
    On app level, never use setState/extendState directly as this may
    lead to invalid states.
  */
  navigate(route, opts) {
    this.extendState({
      route: route
    })
    this.router.writeRoute(route, opts)
  }
 
  _onRouteChanged(route) {
    // console.log('NotesApp._onRouteChanged', route);
    this.navigate(route, {replace: true})
  }
 
  _isMobile() {
    if (platform.inBrowser) {
      return window.innerWidth < 700
    }
  }
 
  _onResize() {
    if (this._isMobile()) {
      // switch to mobile
      if (!this.state.mobile) {
        this.extendState({
          mobile: true
        })
      }
    } else {
      if (this.state.mobile) {
        this.extendState({
          mobile: false
        })
      }
    }
  }
 
  _getPage() {
    return this.state.route.page || this.getDefaultPage()
  }
 
  _getPageClass() {
    let page = this._getPage()
    return this.pages[page]
  }
 
  _getPageProps() {
    let props = cloneDeep(this.state.route)
    delete props.page
    props.mobile = this.state.mobile
    return props
  }
 
  addPage(pageName, PageClass) {
    this.pages[pageName] = PageClass
  }
 
  renderPage($$) {
    let PageClass = this._getPageClass()
    let pageName = this._getPage()
    return $$(PageClass, this._getPageProps()).ref(pageName)
  }
 
  render($$) {
    let el = $$('div').addClass('sc-responsive-application')
 
    if (this.state.route === undefined) {
      // Not yet initialized by router
      return el
    }
 
    el.append(
      this.renderPage($$)
    )
 
    return el
  }
 
}
 
export default ResponsiveApplication