waibu 1.1.12 → 1.1.14

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,13 +10,33 @@ function buildHomesMenu () {
10
10
  return orderBy(routes, ['title'])
11
11
  }
12
12
 
13
- function buildPagesMenu () {
14
- const { get, orderBy } = this.lib._
15
- const all = [{ icon: 'house', href: '/', title: 'home', level: 1 }]
13
+ function buildPagesMenu (req) {
14
+ const { orderBy } = this.lib._
15
+ const all = [{ icon: 'house', href: '/', level: 1 }]
16
16
  for (const ns of this.app.bajo.pluginNames) {
17
- const pages = get(this, `app.${ns}.config.waibuMpa.pages`, [])
17
+ const items = []
18
+ const pages = this.app[ns].getConfig('waibuMpa.pages', { defValue: [] })
18
19
  if (pages.length === 0) continue
19
- all.push(...pages)
20
+ for (const page of pages) {
21
+ page.level = page.level ?? 1000
22
+ if (page.visible === 'auth' && !req.user) continue
23
+ if (page.visible === 'anon' && req.user) continue
24
+ if (!page.children) {
25
+ items.push(page)
26
+ continue
27
+ }
28
+ const children = []
29
+ for (const child of page.children) {
30
+ if (child.visible === 'auth' && !req.user) continue
31
+ if (child.visible === 'anon' && req.user) continue
32
+ children.push(child)
33
+ }
34
+ if (children.length > 0) {
35
+ page.children = children
36
+ items.push(page)
37
+ }
38
+ }
39
+ all.push(...items)
20
40
  }
21
41
  return orderBy(all, ['level', 'title'])
22
42
  }
@@ -54,8 +74,8 @@ async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
54
74
  if (this.app.bajo.config.env === 'dev') console.log(params.error)
55
75
  }
56
76
  if (reply && req.session && req.flash && !opts.partial) _meta.flash = reply.flash()
57
- set(params, 'menu.homes', buildHomesMenu.call(this))
58
- set(params, 'menu.pages', buildPagesMenu.call(this))
77
+ set(params, 'menu.homes', buildHomesMenu.call(this, req))
78
+ set(params, 'menu.pages', buildPagesMenu.call(this, req))
59
79
  const merged = merge({}, params, { _meta })
60
80
  await runHook(`${this.name}:afterBuildLocals`, merged, req)
61
81
  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.12",
3
+ "version": "1.1.14",
4
4
  "description": "Web Framework for Bajo",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin/factory.js CHANGED
@@ -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)
@@ -173,15 +174,12 @@ async function factory (pkgName) {
173
174
  }
174
175
 
175
176
  getPluginByPrefix = (prefix) => {
176
- const { get } = this.lib._
177
- let plugin
178
- for (const p of this.app.bajo.pluginNames) {
179
- if (get(this, `app.${p}.config.waibu.prefix`) === prefix) {
180
- plugin = this.app[p]
181
- break
182
- }
183
- }
184
- return plugin
177
+ const { get, find } = this.lib._
178
+ const item = find(this.app.waibu.routes, r => {
179
+ return get(r, 'config.prefix') === prefix
180
+ })
181
+ const ns = get(item, 'config.ns')
182
+ if (ns) return this.app[ns]
185
183
  }
186
184
 
187
185
  getPluginPrefix = (base, webApp = 'waibuMpa') => {