waibu 1.1.11 → 1.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- function buildHomesMenu () {
1
+ function buildHomesMenu (req) {
2
2
  const { get, find, orderBy } = this.lib._
3
3
  const routes = []
4
4
  for (const ns of this.app.bajo.pluginNames) {
@@ -10,6 +10,35 @@ function buildHomesMenu () {
10
10
  return orderBy(routes, ['title'])
11
11
  }
12
12
 
13
+ function buildPagesMenu (req) {
14
+ const { orderBy } = this.lib._
15
+ const all = [{ icon: 'house', href: '/', level: 1 }]
16
+ for (const ns of this.app.bajo.pluginNames) {
17
+ const items = []
18
+ const pages = this.app[ns].getConfig('waibuMpa.pages', { defValue: [] })
19
+ if (pages.length === 0) continue
20
+ for (const page of pages) {
21
+ if (page.visible === 'auth' && !req.user) continue
22
+ if (page.visible === 'anon' && req.user) continue
23
+ if (!page.children) {
24
+ items.push(page)
25
+ continue
26
+ }
27
+ const children = []
28
+ for (const child of page.children) {
29
+ if (child.visible === 'auth' && !req.user) continue
30
+ if (child.visible === 'anon' && req.user) continue
31
+ children.push(child)
32
+ }
33
+ if (children.length > 0) {
34
+ page.children = children
35
+ items.push(page)
36
+ }
37
+ }
38
+ all.push(...items)
39
+ }
40
+ return orderBy(all, ['level', 'title'])
41
+ }
13
42
  async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
14
43
  const { runHook } = this.app.bajo
15
44
  const { set, merge, pick, get, isEmpty, find } = this.lib._
@@ -17,7 +46,6 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
17
46
 
18
47
  const appTitle = this.app.waibuMpa ? req.t(this.app.waibuMpa.getAppTitle(req.ns)) : ''
19
48
  params.page = merge(params.page ?? {}, { ns: req.ns, appTitle })
20
- set(params, 'menu.homes', buildHomesMenu.call(this))
21
49
 
22
50
  const { site, user, lang, darkMode } = req
23
51
  const theme = pick(find(this.themes, { name: req.theme }) ?? {}, ['name', 'framework'])
@@ -33,6 +61,7 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
33
61
  _meta.template = tpl
34
62
  _meta.hostHeader = req.headers.host
35
63
  _meta.statusCode = 200
64
+ _meta.isAdmin = _meta.user && find(_meta.user.teams, { alias: 'administrator' })
36
65
  if (params.error) {
37
66
  if (params.error.statusCode) _meta.statusCode = params.error.statusCode
38
67
  _meta.errorMessage = params.error.message
@@ -44,6 +73,8 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
44
73
  if (this.app.bajo.config.env === 'dev') console.log(params.error)
45
74
  }
46
75
  if (reply && req.session && req.flash && !opts.partial) _meta.flash = reply.flash()
76
+ set(params, 'menu.homes', buildHomesMenu.call(this, req))
77
+ set(params, 'menu.pages', buildPagesMenu.call(this, req))
47
78
  const merged = merge({}, params, { _meta })
48
79
  await runHook(`${this.name}:afterBuildLocals`, merged, req)
49
80
  if (!isEmpty(routeOpts.ns)) await runHook(`${this.name}.${routeOpts.ns}:afterBuildLocals`, merged, req)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "Web Framework for Bajo",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin/factory.js CHANGED
@@ -22,7 +22,7 @@ async function factory (pkgName) {
22
22
  this.dependencies = ['bajo-logger', 'bajo-extra']
23
23
  this.config = {
24
24
  server: {
25
- host: 'localhost',
25
+ host: '127.0.0.1',
26
26
  port: 7771
27
27
  },
28
28
  factory: {
@@ -142,7 +142,8 @@ async function factory (pkgName) {
142
142
  })
143
143
  }
144
144
 
145
- escape = (text) => {
145
+ escape = (text = '') => {
146
+ if (typeof text !== 'string') return text
146
147
  const { forOwn } = this.lib._
147
148
  forOwn(this.escapeChars, (v, k) => {
148
149
  text = text.replaceAll(k, v)