diff --git a/packages/omi-router/examples/spa/package.json b/packages/omi-router/examples/spa/package.json index 2cf91bf82..9db441a46 100644 --- a/packages/omi-router/examples/spa/package.json +++ b/packages/omi-router/examples/spa/package.json @@ -52,7 +52,7 @@ "scripts": { "start": "node scripts/start.js", "_build": "node scripts/build.js", - "build":"PUBLIC_URL=https://tencent.github.io/omi/packages/omi-router/examples/spa/build npm run _build", + "build": "PUBLIC_URL=https://tencent.github.io/omi/packages/omi-router/examples/spa/build npm run _build", "fix": "eslint src --fix" }, "jest": { diff --git a/packages/omi-router/examples/spa/src/elements/app/index.js b/packages/omi-router/examples/spa/src/elements/app/index.js index 602b33600..3e92365c8 100644 --- a/packages/omi-router/examples/spa/src/elements/app/index.js +++ b/packages/omi-router/examples/spa/src/elements/app/index.js @@ -1,11 +1,6 @@ -import route from 'omi-router' import { define, WeElement } from 'omi' -import logo from './logo.png' -import style from './_index.css' -import normalizeStyle from '../../assets/_normalize.css' -import navInfo from './nav' - -import '../views/not-found' +import route from '../../route' +import '../common/app-frame' define('my-app', class extends WeElement { static get data() { @@ -16,247 +11,23 @@ define('my-app', class extends WeElement { data = { pageTitle: '', - pageContainerClass: [], routeParams: null, - navInfo: [], - showPageLoading: false - } - - css() { - return normalizeStyle + style - } - - initRoute(routeInfo) { - let routeList = [] - for (let mainNav of navInfo) { - if (mainNav.children) { - for (let childNav of mainNav.children) { - let childRouteList = [] - if (childNav.children && childNav.children.length) { - for (let pageNav of childNav.children) { - childRouteList.push({ - title: pageNav.label, - labelbList: [mainNav.label, childNav.label, pageNav.label], - path: '/' + [mainNav.code, childNav.code, pageNav.code].join('/'), - tag: [mainNav.code, childNav.code, pageNav.code].join('-') - }) - } - } - if (childRouteList.length) { - if (!mainNav.path) { - mainNav.path = childRouteList[0].path - } - if (!childNav.path) { - childNav.path = childRouteList[0].path - } - routeList = routeList.concat(childRouteList) - } else { - if (!childNav.path) { - childNav.path = '/' + mainNav.code + '/' + childNav.code - routeList.push({ - title: childNav.label, - labelbList: [mainNav.label, childNav.label], - path: childNav.path, - tag: [mainNav.code, childNav.code].join('-') - }) - } - if (!mainNav.path) { - mainNav.path = '/' + mainNav.code + '/' + childNav.code - } - } - } - } else { - mainNav.path = '/' + mainNav.code - routeList.push({ - title: mainNav.label, - labelbList: [mainNav.label], - path: '/' + mainNav.code, - tag: mainNav.code - }) - } - } - route('/', params => { - route.to(routeList[0].path, params) - }) - for (let item of routeList) { - route(item.path, params => { - window.document.title = item.title - this.data.tag = item.tag - this.data.pageContainerClass = [] - this.data.routeParams = params - this.data.pageTitle = item.labelbList - this.updateFramework(this.data.navInfo, item.path.slice(1).split('/')) - }) - } - - route('/home-page', params => { - window.document.title = 'home-page' - this.data.tag = 'home-page' - this.data.pageContainerClass = ['no-page-title', 'no-side-nav'] - this.data.routeParams = null - this.data.pageTitle = '' - this.updateFramework(this.data.navInfo, [], true) - this.data.navInfo[0].selected = true - }) - route('*', () => { - if (location.hash === '') { - this.data.tag = 'home-page' - this.data.pageContainerClass = ['no-page-title', 'no-side-nav'] - } else { - window.document.title = '404 NotFound' - this.data.tag = 'not-found' - this.data.pageContainerClass = ['no-page-title', 'no-side-nav'] - this.data.routeParams = null - this.data.pageTitle = '' - this.updateFramework(this.data.navInfo, [], true) - } - }) - - route.before = event => { - if (event.newURL.match('/#/nav1/module2/page1')) { - this.data.showPageLoading = true - } - } + routeList: [] } install() { - this.data.navInfo = navInfo - this.initRoute(navInfo) - } - - onPageLoaded() { - this.data.showPageLoading = false - } - - updateFramework(navInfo, navPath, isClear = false) { - let foundPath = false - if (!isClear) { - if (!Array.isArray(navPath) || !navPath.length) { - return false - } + this.data.routeList = route(this) + // show home page if hash is empty + if (location.hash === '') { + location.hash = '/' } - if (!Array.isArray(navInfo) || !navInfo.length) { - return false - } - for (let mainNav of navInfo) { - if (isClear || mainNav.code !== navPath[0]) { - mainNav.selected = false - this.updateFramework(mainNav.children, navPath.slice(1), true) - } else { - foundPath = true - if ( - (!Array.isArray(mainNav.children) || mainNav.children.length) && - navPath.length === 1 - ) { - mainNav.selected = true - } else { - mainNav.selected = this.updateFramework( - mainNav.children, - navPath.slice(1) - ) - } - } - } - return foundPath - } - - getRenderedNav(props, data) { - let mainNav = [] - let subNav = [] - for (let nav of this.data.navInfo) { - mainNav.push( -
  • - {nav.label} -
  • - ) - if (nav.selected && nav.children && nav.children.length) { - for (let childNav of nav.children) { - subNav.push( -
  • - {childNav.label} -
  • - ) - } - if (subNav.length) { - subNav = [ - - ] - } - } - } - return { - mainNav, - subNav - } - } - - getRenderedPageTitle(props, data) { - let parentTitle = [] - let currentTitle = '' - if (Array.isArray(this.data.pageTitle) && this.data.pageTitle.length) { - parentTitle = this.data.pageTitle.slice(0, -1) - currentTitle = this.data.pageTitle[this.data.pageTitle.length - 1] - return ( -

    - {parentTitle.join(' / ')} {parentTitle.length > 0 && '/'} {currentTitle} -

    - ) - } - return '' } render(props, data) { - let { mainNav, subNav } = this.getRenderedNav(props, data) - let pageTitle = this.getRenderedPageTitle(props, data) return ( -
    -
    -

    - - - Omi SPA - -

    - - -
    -
    - {subNav} -
    - {pageTitle} -
    - this.onPageLoaded(e)} - /> -
    -

    页面加载中...

    -
    -
    -
    -
    - -
    + + + ) } }) diff --git a/packages/omi-router/examples/spa/src/elements/app/nav.js b/packages/omi-router/examples/spa/src/elements/app/nav.js deleted file mode 100644 index 6a1d2be20..000000000 --- a/packages/omi-router/examples/spa/src/elements/app/nav.js +++ /dev/null @@ -1,71 +0,0 @@ -import '../views/nav1/module1/page1' -import '../views/nav1/module2/page1' -import '../views/nav2/module1/page1' -import '../views/nav2/module2/page1' -import '../views/home-page' - -export default [ - { - label: 'Home', - code: 'home-page', - selected: true - }, - { - label: 'nav one', - code: 'nav1', - selected: false, - children: [ - { - label: 'module one', - code: 'module1', - selected: false, - children: [ - { - label: 'page one', - code: 'page1' - } - ] - }, - { - label: 'module two', - code: 'module2', - selected: false, - children: [ - { - label: 'page one', - code: 'page1' - } - ] - } - ] - }, - { - label: 'nav two', - code: 'nav2', - selected: false, - children: [ - { - label: 'module one', - code: 'module1', - selected: false, - children: [ - { - label: 'page one', - code: 'page1' - } - ] - }, - { - label: 'module two', - code: 'module2', - selected: false, - children: [ - { - label: 'page one', - code: 'page1' - } - ] - } - ] - } -] diff --git a/packages/omi-router/examples/spa/src/elements/app/_index.css b/packages/omi-router/examples/spa/src/elements/common/app-frame/_index.css similarity index 55% rename from packages/omi-router/examples/spa/src/elements/app/_index.css rename to packages/omi-router/examples/spa/src/elements/common/app-frame/_index.css index 87ac7d445..65a1ecb22 100644 --- a/packages/omi-router/examples/spa/src/elements/app/_index.css +++ b/packages/omi-router/examples/spa/src/elements/common/app-frame/_index.css @@ -72,84 +72,17 @@ margin-left: 10px; } -.sub-nav { - float: left; - background-color: white; - width: 200px; -} -.sub-nav a { - display: block; - height: 48px; - line-height: 48px; - padding-left: 30px; - color: rgba(0,0,0,0.65); -} -.sub-nav a:hover { - color: #1890FF; -} -.sub-nav li.selected a { - color: #1890FF; -} -.sub-nav li.selected{ - position: relative; - background: #E6F7FF; -} -.sub-nav li.selected::after{ - content: ''; - position: absolute; - display: block; - top: 0; - right: 0; - bottom: 0; - width: 4px; - background-color: #1890FF; -} - .page-container { - margin-left: 200px; - padding: 0 20px 20px; + padding: 20px; background: #F0F2F5; } -.page-container.no-side-nav { - margin-left: 0; -} -.page-container.no-page-title { - padding-top: 20px; -} -.page-title { - line-height: 48px; - height: 48px; - color: rgba(0,0,0,0.45); -} -.page-title strong { - color: rgba(0,0,0,0.65); -} .page-content { position: relative; background: #fff; min-height: 500px; padding: 20px; } -.page-shade-layer { - display: none; - position: absolute; - top: 0; - left: 0; - right: 0; - bottom: 0; - background-color: #fff; -} -.page-shade-layer.show { - display: block; -} -.page-shade-layer p{ - width: 100%; - text-align: center; - color: rgba(0,0,0,0.45); - position: absolute; - top: 45%; -} footer { clear: both; diff --git a/packages/omi-router/examples/spa/src/elements/common/app-frame/index.js b/packages/omi-router/examples/spa/src/elements/common/app-frame/index.js new file mode 100644 index 000000000..b2b396662 --- /dev/null +++ b/packages/omi-router/examples/spa/src/elements/common/app-frame/index.js @@ -0,0 +1,71 @@ +import { define, WeElement } from 'omi' +import logo from './logo.png' +import style from './_index.css' +import normalizeStyle from '../../../assets/_normalize.css' + +define('app-frame', class extends WeElement { + static get data() { + return { name: '' } + } + + static observe = true + + data = { + name: 'Omi SPA' + } + + css() { + return normalizeStyle + style + } + + install() {} + + getRenderedNav(props, data) { + let mainNav = [] + for (let nav of props.routeList) { + if (nav.path !== '*') { + mainNav.push( +
  • + {nav.label} +
  • + ) + } + } + return mainNav + } + + render(props, data) { + let mainNav = this.getRenderedNav(props, data) + return ( +
    +
    +

    + + + {this.data.name} + +

    + + +
    +
    +
    + +
    +
    + +
    + ) + } +}) diff --git a/packages/omi-router/examples/spa/src/elements/app/logo.png b/packages/omi-router/examples/spa/src/elements/common/app-frame/logo.png similarity index 100% rename from packages/omi-router/examples/spa/src/elements/app/logo.png rename to packages/omi-router/examples/spa/src/elements/common/app-frame/logo.png diff --git a/packages/omi-router/examples/spa/src/elements/views/home-page/index.js b/packages/omi-router/examples/spa/src/elements/views/home-page/index.js index 1dec698b0..805a4e20c 100644 --- a/packages/omi-router/examples/spa/src/elements/views/home-page/index.js +++ b/packages/omi-router/examples/spa/src/elements/views/home-page/index.js @@ -7,10 +7,17 @@ define('home-page', class extends WeElement { css() { return ( normalizeStyle + - 'p { text-align: center; padding-top: 100px; font-size: 20px; }' + 'div { text-align: center; padding-top: 100px; font-size: 20px; }' + + 'a { color: #1890FF; font-size: 14px;}' ) } render() { - return

    Home

    + return ( +
    +

    Hi Welcome to Omi.

    +

    the Next Generation Web Framework in 4kb JavaScript

    + learn more about Omi +
    + ) } }) diff --git a/packages/omi-router/examples/spa/src/elements/views/nav1/module1/page1.js b/packages/omi-router/examples/spa/src/elements/views/nav1/module1/page1.js deleted file mode 100644 index 8afff90d7..000000000 --- a/packages/omi-router/examples/spa/src/elements/views/nav1/module1/page1.js +++ /dev/null @@ -1,15 +0,0 @@ -import { define, WeElement } from 'omi' -import normalizeStyle from '../../../../assets/_normalize.css' - -define('nav1-module1-page1', class extends WeElement { - static observe = true - install() { - this.fire('pageloaded') - } - css() { - return normalizeStyle - } - render() { - return

    page one is here

    - } -}) diff --git a/packages/omi-router/examples/spa/src/elements/views/nav1/module2/page1.js b/packages/omi-router/examples/spa/src/elements/views/nav1/module2/page1.js deleted file mode 100644 index 167aa401d..000000000 --- a/packages/omi-router/examples/spa/src/elements/views/nav1/module2/page1.js +++ /dev/null @@ -1,17 +0,0 @@ -import { define, WeElement } from 'omi' -import normalizeStyle from '../../../../assets/_normalize.css' - -define('nav1-module2-page1', class extends WeElement { - static observe = true - install() { - setTimeout(() => { - this.fire('pageloaded') - }, 3000) - } - css() { - return normalizeStyle - } - render() { - return

    page one is here

    - } -}) diff --git a/packages/omi-router/examples/spa/src/elements/views/nav1/omi-weui-page/index.js b/packages/omi-router/examples/spa/src/elements/views/nav1/omi-weui-page/index.js new file mode 100644 index 000000000..837fbbb37 --- /dev/null +++ b/packages/omi-router/examples/spa/src/elements/views/nav1/omi-weui-page/index.js @@ -0,0 +1,24 @@ +import { define, WeElement } from 'omi' +import normalizeStyle from '../../../../assets/_normalize.css' + +define('omi-weui-page', class extends WeElement { + static observe = true + install() {} + css() { + return ( + normalizeStyle + + 'div { text-align: center; padding-top: 100px; font-size: 20px; }' + + 'a { color: #1890FF; font-size: 14px;}' + ) + } + render() { + return ( +
    +

    WeUI Components build with Omi is coming soon

    + + learn more about omi-weui + +
    + ) + } +}) diff --git a/packages/omi-router/examples/spa/src/elements/views/nav2/module1/page1.js b/packages/omi-router/examples/spa/src/elements/views/nav2/module1/page1.js deleted file mode 100644 index bcd51967f..000000000 --- a/packages/omi-router/examples/spa/src/elements/views/nav2/module1/page1.js +++ /dev/null @@ -1,15 +0,0 @@ -import { define, WeElement } from 'omi' -import normalizeStyle from '../../../../assets/_normalize.css' - -define('nav2-module1-page1', class extends WeElement { - static observe = true - install() { - this.fire('pageloaded') - } - css() { - return normalizeStyle - } - render() { - return

    page one is here

    - } -}) diff --git a/packages/omi-router/examples/spa/src/elements/views/nav2/module2/page1.js b/packages/omi-router/examples/spa/src/elements/views/nav2/module2/page1.js deleted file mode 100644 index 8a7078fa4..000000000 --- a/packages/omi-router/examples/spa/src/elements/views/nav2/module2/page1.js +++ /dev/null @@ -1,15 +0,0 @@ -import { define, WeElement } from 'omi' -import normalizeStyle from '../../../../assets/_normalize.css' - -define('nav2-module2-page1', class extends WeElement { - static observe = true - install() { - this.fire('pageloaded') - } - css() { - return normalizeStyle - } - render() { - return

    page one is here

    - } -}) diff --git a/packages/omi-router/examples/spa/src/elements/views/nav2/omi-router-page/index.js b/packages/omi-router/examples/spa/src/elements/views/nav2/omi-router-page/index.js new file mode 100644 index 000000000..d9476a0fe --- /dev/null +++ b/packages/omi-router/examples/spa/src/elements/views/nav2/omi-router-page/index.js @@ -0,0 +1,29 @@ +import { define, WeElement } from 'omi' +import normalizeStyle from '../../../../assets/_normalize.css' + +define('omi-router-page', class extends WeElement { + static observe = true + install() {} + css() { + return ( + normalizeStyle + + 'div { text-align: center; padding-top: 100px; font-size: 20px; }' + + 'a { color: #1890FF; font-size: 14px;}' + ) + } + render() { + return ( +
    +

    + omi-router is the Omi official router plugin of Omi. +
    + it's lightweight, easy and powerful to build Omi's SPA(Single Page + Application) +

    + + learn more about omi-router + +
    + ) + } +}) diff --git a/packages/omi-router/examples/spa/src/elements/views/not-found/index.js b/packages/omi-router/examples/spa/src/elements/views/not-found-page/index.js similarity index 86% rename from packages/omi-router/examples/spa/src/elements/views/not-found/index.js rename to packages/omi-router/examples/spa/src/elements/views/not-found-page/index.js index f3d1c2c6b..5f89e31f9 100644 --- a/packages/omi-router/examples/spa/src/elements/views/not-found/index.js +++ b/packages/omi-router/examples/spa/src/elements/views/not-found-page/index.js @@ -1,7 +1,7 @@ import { define, WeElement } from 'omi' import normalizeStyle from '../../../assets/_normalize.css' -define('not-found', class extends WeElement { +define('not-found-page', class extends WeElement { static observe = true css() { diff --git a/packages/omi-router/examples/spa/src/route/error.js b/packages/omi-router/examples/spa/src/route/error.js new file mode 100644 index 000000000..f8e4c6641 --- /dev/null +++ b/packages/omi-router/examples/spa/src/route/error.js @@ -0,0 +1,9 @@ +import '../elements/views/not-found-page' + +export default [ + { + label: '404 not-found', + path: '*', + tag: 'not-found-page' + } +] diff --git a/packages/omi-router/examples/spa/src/route/index.js b/packages/omi-router/examples/spa/src/route/index.js new file mode 100644 index 000000000..871227001 --- /dev/null +++ b/packages/omi-router/examples/spa/src/route/index.js @@ -0,0 +1,16 @@ +import route from 'omi-router' +import routeList from './route' + +export default function(omiElement) { + for (let item of routeList) { + route(item.path, params => { + window.document.title = item.label + omiElement.data.pageTitle = item.label + omiElement.data.tag = item.tag + omiElement.data.routeParams = params + routeList.forEach(routeItem => (routeItem.selected = false)) + item.selected = true + }) + } + return routeList +} diff --git a/packages/omi-router/examples/spa/src/route/nav1.js b/packages/omi-router/examples/spa/src/route/nav1.js new file mode 100644 index 000000000..8cb8c8b88 --- /dev/null +++ b/packages/omi-router/examples/spa/src/route/nav1.js @@ -0,0 +1,7 @@ +import '../elements/views/nav1/omi-weui-page' + +export default { + label: 'omi-weui', + path: '/omi-weui', + tag: 'omi-weui-page' +} diff --git a/packages/omi-router/examples/spa/src/route/nav2.js b/packages/omi-router/examples/spa/src/route/nav2.js new file mode 100644 index 000000000..a9bb9226b --- /dev/null +++ b/packages/omi-router/examples/spa/src/route/nav2.js @@ -0,0 +1,9 @@ +import '../elements/views/nav2/omi-router-page' + +export default [ + { + label: 'omi-router', + path: '/omi-router', + tag: 'omi-router-page' + } +] diff --git a/packages/omi-router/examples/spa/src/route/route.js b/packages/omi-router/examples/spa/src/route/route.js new file mode 100644 index 000000000..783ad67c3 --- /dev/null +++ b/packages/omi-router/examples/spa/src/route/route.js @@ -0,0 +1,14 @@ +import '../elements/views/home-page' + +import nav1Route from './nav1' +import nav2Route from './nav2' +import errorRoute from './error' + +let routeList = [ + { + label: 'Home', + path: '/', + tag: 'home-page' + } +] +export default routeList.concat(nav1Route, nav2Route, errorRoute)