2023-08-10 19:59:02 +08:00
|
|
|
/**
|
|
|
|
* {wkitd}
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2023/08/10 10:02:15
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './init.js'
|
|
|
|
import { html, css, Component } from 'wkit'
|
|
|
|
import { noop } from './utils.js'
|
2023-08-15 14:43:53 +08:00
|
|
|
import { __ROUTER__, __STORE__, __ROUTER_VIEW__ } from './constants.js'
|
2023-08-10 19:59:02 +08:00
|
|
|
|
|
|
|
export * from './router/index.js'
|
|
|
|
export { createStore } from './store.js'
|
|
|
|
|
|
|
|
class App extends Component {}
|
|
|
|
|
|
|
|
export function createApp({
|
|
|
|
data = {},
|
|
|
|
methods = {},
|
|
|
|
mounted = noop,
|
|
|
|
render
|
|
|
|
} = {}) {
|
|
|
|
//
|
|
|
|
|
|
|
|
return new (function () {
|
|
|
|
App.props = data
|
|
|
|
Object.assign(App.prototype, methods, { mounted })
|
|
|
|
|
|
|
|
this.use = function (plugin = noop, ...args) {
|
2023-08-15 14:43:53 +08:00
|
|
|
plugin.apply(App.prototype, args)
|
2023-08-10 19:59:02 +08:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
this.mount = function () {
|
2023-08-15 14:43:53 +08:00
|
|
|
let $router = window.wkitd.get(__ROUTER__)
|
2023-08-10 19:59:02 +08:00
|
|
|
if (render) {
|
|
|
|
App.prototype.render = render
|
|
|
|
} else {
|
|
|
|
if ($router) {
|
|
|
|
App.prototype.render = function () {
|
|
|
|
return html`<router-view></router-view>`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
App.styles = css`
|
|
|
|
:host {
|
|
|
|
font-family: monospace;
|
|
|
|
color: #647889;
|
|
|
|
}
|
|
|
|
.code {
|
|
|
|
margin: 16px 0;
|
|
|
|
background: #f7f8fb;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
App.prototype.render = function () {
|
|
|
|
return html`
|
|
|
|
<h1>It works!!!</h1>
|
|
|
|
<cite>
|
|
|
|
If you don't use router, you may define the
|
|
|
|
<b>render</b> property.
|
|
|
|
</cite>
|
|
|
|
<div class="code">
|
|
|
|
<pre><code> createApp({</code></pre>
|
|
|
|
<pre><code> render() {</code></pre>
|
|
|
|
<pre><code> return html\`<wc-home></wc-home>\`</code></pre>
|
|
|
|
<pre><code> }</code></pre>
|
|
|
|
<pre><code> })</code></pre>
|
|
|
|
<pre><code> .mount()</code></pre>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($router) {
|
|
|
|
App.prototype.mounted = function (...args) {
|
2023-08-15 14:43:53 +08:00
|
|
|
let $view = window.wkitd.get(__ROUTER_VIEW__)
|
2023-08-11 19:19:48 +08:00
|
|
|
if ($view) {
|
2023-08-14 17:56:00 +08:00
|
|
|
$view.sync($router.views)
|
|
|
|
$router.init() // mounted 时正式初始化路由
|
2023-08-11 19:19:48 +08:00
|
|
|
mounted.call(this, ...args)
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`<router-view /> not found, "Router" works Unexpected.`
|
|
|
|
)
|
|
|
|
}
|
2023-08-10 19:59:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
App.reg('app')
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
}
|
2023-08-14 19:12:30 +08:00
|
|
|
|
|
|
|
export function getStore() {
|
2023-08-15 14:43:53 +08:00
|
|
|
return window.wkitd.get(__STORE__)
|
2023-08-14 19:12:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getRouter() {
|
2023-08-15 14:43:53 +08:00
|
|
|
return window.wkitd.get(__ROUTER__)
|
2023-08-14 19:12:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentPage() {
|
2023-08-15 14:43:53 +08:00
|
|
|
return window.wkitd.get(__ROUTER__)?.route
|
2023-08-14 19:12:30 +08:00
|
|
|
}
|