waibu-mpa 2.1.0 → 2.1.2

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.
Files changed (42) hide show
  1. package/extend/bajoTemplate/template/wmpa.js +12 -8
  2. package/extend/waibuMpa/route/component/render.js +1 -1
  3. package/index.js +8 -1
  4. package/lib/build-page/concat-resources.js +1 -0
  5. package/lib/build-page/inject-elements/link.js +16 -0
  6. package/lib/build-page/inject-elements/meta.js +1 -1
  7. package/lib/build-page/inject-elements.js +7 -4
  8. package/lib/class/component.js +286 -282
  9. package/lib/class/iconset.js +41 -35
  10. package/lib/class/theme.js +24 -19
  11. package/lib/class/tools.js +9 -0
  12. package/lib/class/view-engine.js +33 -27
  13. package/lib/class/widget/any.js +12 -0
  14. package/lib/class/widget/datalist.js +14 -0
  15. package/lib/class/widget/icon.js +28 -0
  16. package/lib/class/widget/link.js +19 -0
  17. package/lib/class/widget/page-end.js +66 -0
  18. package/lib/class/widget/page-start.js +25 -0
  19. package/lib/class/widget/script.js +35 -0
  20. package/lib/class/widget/style.js +20 -0
  21. package/lib/class/widget/t.js +16 -0
  22. package/lib/class/widget/template.js +12 -0
  23. package/lib/class/widget/tree.js +37 -0
  24. package/lib/class/widget.js +95 -0
  25. package/lib/collect-iconsets.js +6 -4
  26. package/lib/collect-themes.js +15 -28
  27. package/lib/collect-view-engines.js +3 -2
  28. package/lib/decorate.js +6 -4
  29. package/package.json +1 -1
  30. package/wiki/CHANGES.md +4 -0
  31. package/lib/class/base-factory.js +0 -90
  32. package/lib/class/factory/any.js +0 -10
  33. package/lib/class/factory/datalist.js +0 -12
  34. package/lib/class/factory/icon.js +0 -26
  35. package/lib/class/factory/link.js +0 -19
  36. package/lib/class/factory/page-end.js +0 -64
  37. package/lib/class/factory/page-start.js +0 -24
  38. package/lib/class/factory/script.js +0 -33
  39. package/lib/class/factory/style.js +0 -19
  40. package/lib/class/factory/t.js +0 -15
  41. package/lib/class/factory/template.js +0 -11
  42. package/lib/class/factory/tree.js +0 -36
@@ -98,9 +98,13 @@ class Wmpa {
98
98
  return result
99
99
  }
100
100
 
101
- fetchRender = async (body) => {
101
+ fetchRender = async (body, qs = {}) => {
102
102
  if (_.isArray(body)) body = body.join('\n')
103
- const resp = await fetch(this.renderUrl, {
103
+ let url = this.renderUrl + '?'
104
+ _.forOwn(qs, (v, k) => {
105
+ url += '&' + k + '=' + v
106
+ })
107
+ const resp = await fetch(url, {
104
108
  method: 'POST',
105
109
  headers: { 'Content-Type': 'text/plain', 'Waibu-Referer': window.location.href },
106
110
  body
@@ -187,9 +191,9 @@ class Wmpa {
187
191
  return selector instanceof HTMLElement ? selector : document.querySelector(selector)
188
192
  }
189
193
 
190
- createComponent = async (body, wrapper) => {
194
+ createComponent = async (body, wrapper, qs = {}) => {
191
195
  if (_.isArray(body)) body = body.join('\n')
192
- const html = await this.fetchRender(body)
196
+ const html = await this.fetchRender(body, qs)
193
197
  return this.createComponentFromHtml(html, wrapper)
194
198
  }
195
199
 
@@ -201,9 +205,9 @@ class Wmpa {
201
205
  return cmp.getAttribute('id')
202
206
  }
203
207
 
204
- replaceWithComponent = async (body, selector, wrapper) => {
208
+ replaceWithComponent = async (body, selector, wrapper, qs = {}) => {
205
209
  let cmp
206
- if (_.isString(body) || _.isArray(body)) cmp = await this.createComponent(body, wrapper)
210
+ if (_.isString(body) || _.isArray(body)) cmp = await this.createComponent(body, wrapper, qs)
207
211
  else cmp = body
208
212
  const el = this.getElement(selector)
209
213
  if (!el) return
@@ -221,9 +225,9 @@ class Wmpa {
221
225
  return cmp.getAttribute('id')
222
226
  }
223
227
 
224
- addComponent = async (body, selector = 'body', wrapper, checkChild) => {
228
+ addComponent = async (body, selector = 'body', wrapper, checkChild, qs = {}) => {
225
229
  let cmp
226
- if (_.isString(body) || _.isArray(body)) cmp = await this.createComponent(body, wrapper)
230
+ if (_.isString(body) || _.isArray(body)) cmp = await this.createComponent(body, wrapper, qs)
227
231
  else cmp = body
228
232
  const el = this.getElement(selector)
229
233
  if (!el) return
@@ -5,7 +5,7 @@ const component = {
5
5
  const { ext = '.html' } = req.body
6
6
  reply.header('Content-Type', `text/html; charset=${this.config.page.charset}`)
7
7
  reply.header('Content-Language', req.lang)
8
- const opts = { req, reply, partial: true, ext }
8
+ const opts = { req, reply, partial: true, ext, theme: req.params.theme ?? req.query.theme, iconset: req.params.iconset ?? req.query.iconset }
9
9
  return this.renderString(req.body, req.query, opts)
10
10
  }
11
11
  }
package/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { stripHtml } from 'string-strip-html'
2
2
  import iconsetMappings from './lib/iconset-mappings.js'
3
+ import toolsFactory from './lib/class/tools.js'
4
+ import widgetFactory from './lib/class/widget.js'
3
5
  import path from 'path'
4
6
 
5
7
  // taken from: https://stackoverflow.com/questions/52928550/js-get-list-of-all-available-standard-html-tags
@@ -68,6 +70,7 @@ async function factory (pkgName) {
68
70
  css: true,
69
71
  meta: true,
70
72
  scripts: true,
73
+ links: true,
71
74
  inlineScript: true,
72
75
  inlineCss: true
73
76
  },
@@ -81,6 +84,7 @@ async function factory (pkgName) {
81
84
  autoInsert: {
82
85
  css: true,
83
86
  scripts: true,
87
+ links: true,
84
88
  inlineScript: true,
85
89
  inlineCss: true
86
90
  }
@@ -89,7 +93,8 @@ async function factory (pkgName) {
89
93
  cacheMaxAge: 0,
90
94
  excluded: [],
91
95
  css: false,
92
- scripts: false
96
+ scripts: false,
97
+ links: false
93
98
  },
94
99
  cheerio: {
95
100
  loadOptions: {
@@ -130,6 +135,8 @@ async function factory (pkgName) {
130
135
  init = async () => {
131
136
  const { trim } = this.app.lib._
132
137
  this.config.waibu.prefix = trim(this.config.waibu.prefix, '/')
138
+ await toolsFactory.call(this)
139
+ await widgetFactory.call(this)
133
140
  }
134
141
 
135
142
  arrayToAttr = (array = [], delimiter = ' ') => {
@@ -63,6 +63,7 @@ async function concatResources (options) {
63
63
  if (!(this.app.bajoExtra && this.app.bajoCache)) return
64
64
  if (this.config.concatResource.cacheMaxAge < 1) return
65
65
  if (this.config.concatResource.css) await apply.call(this, { $, req, tag: 'link', attr: 'href', type: 'css' })
66
+ if (this.config.concatResource.links) await apply.call(this, { $, req, tag: 'link', attr: 'href', type: 'css' })
66
67
  if (this.config.concatResource.scripts) await apply.call(this, { $, req, tag: 'script', attr: 'src', type: 'js' })
67
68
  }
68
69
 
@@ -0,0 +1,16 @@
1
+ import { collectRegular } from './script.js'
2
+
3
+ export function printLink (link) {
4
+ const { routePath } = this.app.waibu
5
+ const { isString } = this.app.lib._
6
+ const item = isString(link) ? { href: link, rel: 'stylesheet', type: 'text/css' } : link
7
+ return `<link href="${routePath(item.href)}" rel="${item.rel}" type="${item.type}" />`
8
+ }
9
+
10
+ async function link (options) {
11
+ const { $ } = options ?? {}
12
+ const regular = await collectRegular.call(this, 'links', printLink, options)
13
+ if (regular.length > 0) $('head').prepend(regular.join('\n'))
14
+ }
15
+
16
+ export default link
@@ -1,4 +1,4 @@
1
- const omitted = ['css', 'style', 'scripts', 'ns', 'appTitle', 'title', 'fullTitle']
1
+ const omitted = ['css', 'style', 'links', 'scripts', 'ns', 'appTitle', 'title', 'fullTitle']
2
2
  const names = ['description', 'keywords', 'robots', 'viewport', 'author', 'publisher',
3
3
  'application-name', 'generator', 'referrer', 'theme-color', 'googlebot'
4
4
  ]
@@ -1,6 +1,7 @@
1
1
  import injectCss from './inject-elements/css.js'
2
2
  import injectMeta from './inject-elements/meta.js'
3
3
  import injectScript from './inject-elements/script.js'
4
+ import injectLink from './inject-elements/link.js'
4
5
 
5
6
  async function injectElements (options) {
6
7
  const { $, req, cmp, reply } = options ?? {}
@@ -11,10 +12,10 @@ async function injectElements (options) {
11
12
  if (req.darkMode) $('body').attr('data-bs-theme', 'dark')
12
13
  const rsc = {}
13
14
  for (const tag of reply.ctags ?? []) {
14
- let Builder = get(cmp, `factory.${tag}`)
15
+ let Builder = get(cmp, `widget.${tag}`)
15
16
  if (!isFunction(Builder)) continue
16
17
  if (!isClass(Builder)) Builder = await Builder.call(cmp)
17
- for (const key of ['scripts', 'css']) {
18
+ for (const key of ['scripts', 'css', 'links']) {
18
19
  let item = Builder[key] ?? []
19
20
  if (isString(item)) item = [item]
20
21
  if (isFunction(item)) item = await item.call(cmp, req)
@@ -32,11 +33,13 @@ async function injectElements (options) {
32
33
  }
33
34
  }
34
35
  }
35
- options.scripts = rsc.scripts ?? []
36
- options.css = rsc.css ?? []
36
+ for (const key of ['scripts', 'css', 'links']) {
37
+ options[key] = rsc[key] ?? []
38
+ }
37
39
  options.inlineScript = rsc.inlineScript
38
40
  options.inlineCss = rsc.inlineCss
39
41
  await runHook(`${this.ns}:beforeBuildPageInjectElement`, options)
42
+ await injectLink.call(this, options)
40
43
  await injectCss.call(this, options)
41
44
  await injectMeta.call(this, options)
42
45
  await injectScript.call(this, options)