jails-js 6.0.1-beta.4 → 6.0.1-beta.5

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.
package/src/component.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { safe, rAF, g } from './utils'
1
+ import { safe, rAF, g, dup } from './utils'
2
2
  import { Idiomorph } from 'idiomorph/dist/idiomorph.esm'
3
3
  import { publish, subscribe } from './utils/pubsub'
4
4
 
@@ -7,14 +7,13 @@ export const Component = ({ name, module, dependencies, node, templates, signal
7
7
  const _model = module.model || {}
8
8
  const initialState = (new Function( `return ${node.getAttribute('html-model') || '{}'}`))()
9
9
  const tplid = node.getAttribute('tplid')
10
- const scopeid = node.getAttribute('html-scope-id')
10
+ const scopeid = node.getAttribute('html-scopeid')
11
11
  const tpl = templates[ tplid ]
12
- const data = g.scope[ scopeid ]
13
- const model = module?.model?.apply ? _model({ elm:node, initialState }) : _model
14
- const state = Object.assign({}, data, model, initialState)
12
+ const scope = g.scope[ scopeid ]
13
+ const model = dup(module?.model?.apply ? _model({ elm:node, initialState }) : _model)
14
+ const state = Object.assign({}, scope, model, initialState)
15
15
  const view = module.view? module.view : (data) => data
16
-
17
- let updates = []
16
+ let preserve = []
18
17
 
19
18
  const base = {
20
19
  name,
@@ -34,6 +33,14 @@ export const Component = ({ name, module, dependencies, node, templates, signal
34
33
  */
35
34
  state : {
36
35
 
36
+ protected( list ) {
37
+ if( list ) {
38
+ preserve = list
39
+ } else {
40
+ return preserve
41
+ }
42
+ },
43
+
37
44
  save(data) {
38
45
  if( data.constructor === Function ) {
39
46
  data( state )
@@ -53,20 +60,11 @@ export const Component = ({ name, module, dependencies, node, templates, signal
53
60
  Object.assign(state, data)
54
61
  }
55
62
 
56
- const newstate = Object.assign({}, state)
63
+ const newstate = Object.assign({}, state, scope)
57
64
 
58
- updates.push(data)
65
+ render(newstate)
59
66
 
60
- return new Promise((resolve) => {
61
- rAF(() => {
62
- Object.assign.apply(null, [newstate, ...updates ])
63
- if( updates.length ){
64
- render(newstate)
65
- resolve(newstate)
66
- updates = []
67
- }
68
- })
69
- })
67
+ return Promise.resolve(newstate)
70
68
  },
71
69
 
72
70
  get() {
@@ -148,20 +146,11 @@ export const Component = ({ name, module, dependencies, node, templates, signal
148
146
 
149
147
  rAF(() => {
150
148
  node.querySelectorAll('[tplid]')
151
- .forEach((element) => {
152
- if(!element.base) return
153
- const base = element.base
154
- const props = Object.keys(base.model).reduce((acc, key) => {
155
- if( key in data ) {
156
- if( !acc ) acc = {}
157
- acc[key] = data[key]
158
- }
159
- return acc
160
- }, null)
161
- if( props ) {
162
- base.state.set( props )
163
- }
164
- })
149
+ .forEach((element) => {
150
+ if(!element.base) return
151
+ element.base.state.protected().forEach( key => delete data[key] )
152
+ element.base.state.set(data)
153
+ })
165
154
  rAF(() => g.scope = {})
166
155
  })
167
156
 
package/src/index.ts CHANGED
@@ -21,7 +21,7 @@ export const start = ( target = document.body ) => {
21
21
  .values( components )
22
22
  .forEach(({ name, module, dependencies }) => {
23
23
  if( !customElements.get(name) ) {
24
- customElements.define( name, Element({ component: { name, module, dependencies }, templates, start }) )
24
+ customElements.define( name, Element({ component: { name, module, dependencies }, templates, start }))
25
25
  }
26
26
  })
27
27
  }
@@ -22,9 +22,8 @@ export const template = ( target, { components }) => {
22
22
  return templates
23
23
  }
24
24
 
25
- export const compile = ( outerHTML ) => {
25
+ export const compile = ( html ) => {
26
26
 
27
- const html = transformAttributes( outerHTML )
28
27
  const parsedHtml = JSON.stringify( html )
29
28
 
30
29
  return new Function('$element', 'safe', '$g',`
@@ -64,7 +63,7 @@ const transformAttributes = ( html ) => {
64
63
  .replace(/html-(allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|inert|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|selected)=\"(.*?)\"/g, `%%_if(safe(function(){ return $2 })){_%%$1%%_}_%%`)
65
64
  // The rest
66
65
  .replace(/html-(.*?)=\"(.*?)\"/g, (all, key, value) => {
67
- if (key === 'key' || key === 'model' || key === 'scope-id' ) {
66
+ if (key === 'key' || key === 'model' || key === 'scopeid' ) {
68
67
  return all
69
68
  }
70
69
  if (value) {
@@ -90,11 +89,12 @@ const transformTemplate = ( clone ) => {
90
89
 
91
90
  element.removeAttribute('html-for')
92
91
 
93
- const split = htmlFor.match(/(.*)\sin\s(.*)/) || ''
94
- const varname = split[1]
95
- const object = split[2]
96
- const open = document.createTextNode(`%%_ ;(function(){ var $index = 0; for(var $key in safe(function(){ return ${object} }) ){ var $scopeid = Math.random().toString(36).substring(2, 9); var ${varname} = ${object}[$key]; $g.scope[$scopeid] = { ${varname} :${varname}, ${object}: ${object}, $index: $index, $key: $key }; _%%`)
97
- const close = document.createTextNode(`%%_ $index++; } })() _%%`)
92
+ const split = htmlFor.match(/(.*)\sin\s(.*)/) || ''
93
+ const varname = split[1]
94
+ const object = split[2]
95
+ const objectname = object.split(/\./).shift()
96
+ const open = document.createTextNode(`%%_ ;(function(){ var $index = 0; for(var $key in safe(function(){ return ${object} }) ){ var $scopeid = Math.random().toString(36).substring(2, 9); var ${varname} = ${object}[$key]; $g.scope[$scopeid] = Object.assign({}, { ${objectname}: ${objectname} }, { ${varname} :${varname}, $index: $index, $key: $key }); _%%`)
97
+ const close = document.createTextNode(`%%_ $index++; } })() _%%`)
98
98
 
99
99
  wrap(open, element, close)
100
100
  }
@@ -130,27 +130,15 @@ const setTemplates = ( clone, components ) => {
130
130
 
131
131
  const tplid = node.getAttribute('tplid')
132
132
  const name = node.localName
133
- node.setAttribute('html-scope-id', 'jails___scope-id')
133
+ node.setAttribute('html-scopeid', 'jails___scope-id')
134
134
 
135
135
  if( name in components && components[name].module.template ) {
136
136
  const children = node.innerHTML
137
137
  const html = components[name].module.template({ elm:node, children })
138
-
139
- if( html.constructor === Promise ) {
140
- html.then( htmlstring => {
141
- node.innerHTML = htmlstring
142
- const html = node.outerHTML
143
- templates[tplid] = {
144
- template: html,
145
- render: compile(html)
146
- }
147
- })
148
- } else {
149
- node.innerHTML = html
150
- }
138
+ node.innerHTML = html
151
139
  }
152
140
 
153
- const html = node.outerHTML
141
+ const html = transformAttributes(node.outerHTML)
154
142
 
155
143
  templates[ tplid ] = {
156
144
  template: html,