jails-js 5.0.0-beta.2 → 5.0.0-beta.22
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/.babelrc +3 -13
- package/.editorconfig +2 -0
- package/.github/FUNDING.yml +2 -0
- package/README.md +107 -71
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/logo.svg +29 -0
- package/package.json +42 -37
- package/src/component.ts +147 -0
- package/src/element.ts +52 -0
- package/src/index.ts +29 -0
- package/src/template-system.ts +86 -0
- package/src/utils/{events.js → events.ts} +1 -1
- package/src/utils/index.ts +80 -0
- package/src/utils/pubsub.ts +20 -0
- package/tsconfig.json +106 -0
- package/types/component.d.ts +32 -0
- package/types/element.d.ts +311 -0
- package/types/index.d.ts +5 -0
- package/types/index.ts +41 -0
- package/types/template-system.d.ts +1 -0
- package/types/utils/events.d.ts +3 -0
- package/types/utils/index.d.ts +8 -0
- package/types/utils/pubsub.d.ts +2 -0
- package/webpack.config.js +35 -0
- package/.eslintignore +0 -2
- package/.eslintrc +0 -20
- package/.eslintrc.js +0 -28
- package/dist/jails.js +0 -1
- package/src/component.js +0 -127
- package/src/index.js +0 -187
- package/src/soda-config.js +0 -82
- package/src/utils/index.js +0 -27
- package/src/utils/pubsub.js +0 -25
- package/webpack.config.babel.js +0 -16
package/src/index.js
DELETED
@@ -1,187 +0,0 @@
|
|
1
|
-
import morphdom from 'morphdom'
|
2
|
-
import sodajs from 'sodajs'
|
3
|
-
|
4
|
-
import { uuid, stripTemplateTags, stripTemplateTag, rAF } from './utils'
|
5
|
-
import * as Pubsub from './utils/pubsub'
|
6
|
-
import sodaSetConfig from './soda-config'
|
7
|
-
|
8
|
-
import Component from './component'
|
9
|
-
|
10
|
-
sodaSetConfig( sodajs )
|
11
|
-
|
12
|
-
let SST = {}
|
13
|
-
let AST = []
|
14
|
-
const components = {}
|
15
|
-
|
16
|
-
export default {
|
17
|
-
|
18
|
-
start() {
|
19
|
-
Template.start()
|
20
|
-
Template.observe()
|
21
|
-
},
|
22
|
-
|
23
|
-
register( name, module, dependencies = {} ) {
|
24
|
-
components[name] = { name, module, dependencies }
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
const Template = {
|
29
|
-
|
30
|
-
start() {
|
31
|
-
stripTemplateTag( document.body )
|
32
|
-
Template.scan( document.body, Element )
|
33
|
-
},
|
34
|
-
|
35
|
-
scan( root, callback ) {
|
36
|
-
if( root.nodeType === 1 ) {
|
37
|
-
const list = Array.from( root.querySelectorAll('[data-component]') )
|
38
|
-
const components = root.dataset.component? [root].concat(list) : list
|
39
|
-
components.reverse().forEach( callback )
|
40
|
-
}
|
41
|
-
},
|
42
|
-
|
43
|
-
observe() {
|
44
|
-
const observer = new MutationObserver(mutations => mutations.forEach( mutation => {
|
45
|
-
if (mutation.type === 'childList') {
|
46
|
-
if (mutation.addedNodes.length) {
|
47
|
-
Array.from(mutation.addedNodes).forEach( node => Template.scan(node, Element) )
|
48
|
-
} else if (mutation.removedNodes.length) {
|
49
|
-
Array.from(mutation.removedNodes).forEach( node => Template.scan(node, Template.remove) )
|
50
|
-
}
|
51
|
-
}
|
52
|
-
}))
|
53
|
-
observer.observe(document.body, { childList: true, subtree: true })
|
54
|
-
},
|
55
|
-
|
56
|
-
remove( node ) {
|
57
|
-
const item = AST.find( item => item.element == node )
|
58
|
-
if( item ){
|
59
|
-
item.dispose()
|
60
|
-
}
|
61
|
-
}
|
62
|
-
}
|
63
|
-
|
64
|
-
const Element = ( element ) => {
|
65
|
-
|
66
|
-
let tplid
|
67
|
-
let template
|
68
|
-
|
69
|
-
if( element.getAttribute('tplid') ) {
|
70
|
-
tplid = element.getAttribute('tplid')
|
71
|
-
const item = AST.find( item => item.tplid == tplid )
|
72
|
-
template = item.template
|
73
|
-
}else {
|
74
|
-
tplid = uuid()
|
75
|
-
element.setAttribute('tplid', tplid)
|
76
|
-
template = createTemplate(element.outerHTML)
|
77
|
-
}
|
78
|
-
|
79
|
-
const ElementInterface = {
|
80
|
-
tplid,
|
81
|
-
element,
|
82
|
-
template,
|
83
|
-
instances:{},
|
84
|
-
destroyers:[],
|
85
|
-
promises: [],
|
86
|
-
view: data => data,
|
87
|
-
parentUpdate: data => null,
|
88
|
-
dispose(){
|
89
|
-
if( ElementInterface.promises.length ){
|
90
|
-
Promise.all(ElementInterface.promises).then(_ => {
|
91
|
-
this.destroyers.forEach( destroy => destroy(ElementInterface) )
|
92
|
-
})
|
93
|
-
}else {
|
94
|
-
this.destroyers.forEach( destroy => destroy(ElementInterface) )
|
95
|
-
}
|
96
|
-
},
|
97
|
-
|
98
|
-
model: Object.assign({}, JSON.parse(element.getAttribute('initialState'))),
|
99
|
-
|
100
|
-
update( data, isParentUpdate = false ) {
|
101
|
-
|
102
|
-
this.model = Object.assign( { global: SST }, this.model, data )
|
103
|
-
SST = saveGlobal(data)
|
104
|
-
|
105
|
-
if( isParentUpdate )
|
106
|
-
this.parentUpdate( this.model )
|
107
|
-
|
108
|
-
const dupdata = JSON.parse(JSON.stringify(this.model))
|
109
|
-
|
110
|
-
morphdom( element, sodajs( this.template, this.view(dupdata) ), {
|
111
|
-
onNodeDiscarded(node) {
|
112
|
-
Template.scan(node, Template.remove)
|
113
|
-
return true
|
114
|
-
},
|
115
|
-
onBeforeElUpdated(node, toEl) {
|
116
|
-
if (node.isEqualNode(toEl))
|
117
|
-
return false
|
118
|
-
if( node.nodeType == 1 && 'static' in node.dataset )
|
119
|
-
return false
|
120
|
-
return true
|
121
|
-
}
|
122
|
-
})
|
123
|
-
|
124
|
-
rAF(_ => {
|
125
|
-
const elements = Array.from(element.querySelectorAll('[data-component]'))
|
126
|
-
elements.forEach( node => {
|
127
|
-
const initialState = JSON.parse(node.getAttribute('initialState')) || {}
|
128
|
-
const item = AST.find( item => item.element == node )
|
129
|
-
const { global, parent, ...model } = this.model
|
130
|
-
if( item ) {
|
131
|
-
const newmodel = Object.assign(initialState, { parent:model, global: SST })
|
132
|
-
item.update( newmodel, true )
|
133
|
-
}
|
134
|
-
})
|
135
|
-
})
|
136
|
-
}
|
137
|
-
}
|
138
|
-
|
139
|
-
AST.push( ElementInterface )
|
140
|
-
|
141
|
-
element.dataset.component.split(/\s/).forEach( name => {
|
142
|
-
|
143
|
-
const C = components[name]
|
144
|
-
|
145
|
-
if( !C ) {
|
146
|
-
console.warn(`Jails - Module ${name} not registered`)
|
147
|
-
return
|
148
|
-
}
|
149
|
-
|
150
|
-
const { module, dependencies } = C
|
151
|
-
ElementInterface.model = Object.assign({}, module.model, ElementInterface.model )
|
152
|
-
|
153
|
-
const base = Component({ name, element, dependencies, Pubsub, ElementInterface, AST })
|
154
|
-
|
155
|
-
const promise = module.default(base)
|
156
|
-
|
157
|
-
if( promise && promise.then ) {
|
158
|
-
ElementInterface.promises.push(promise)
|
159
|
-
}
|
160
|
-
|
161
|
-
base.__initialize()
|
162
|
-
ElementInterface.view = module.view || ElementInterface.view
|
163
|
-
ElementInterface.instances[name] = { methods: {} }
|
164
|
-
})
|
165
|
-
|
166
|
-
ElementInterface.update()
|
167
|
-
}
|
168
|
-
|
169
|
-
const createTemplate = ( html ) => {
|
170
|
-
const vhtml = stripTemplateTags( html )
|
171
|
-
const vroot = document.createElement('div')
|
172
|
-
vroot.innerHTML = vhtml
|
173
|
-
const components = Array.from(vroot.querySelectorAll('[data-component]'))
|
174
|
-
components.forEach( c => {
|
175
|
-
const cache = AST.find( item => item.tplid === c.getAttribute('tplid') )
|
176
|
-
if( cache )
|
177
|
-
c.outerHTML = cache.template
|
178
|
-
})
|
179
|
-
return vroot.innerHTML
|
180
|
-
}
|
181
|
-
|
182
|
-
const saveGlobal = (data) => {
|
183
|
-
Object.assign(SST, data)
|
184
|
-
delete SST.parent
|
185
|
-
delete SST.global
|
186
|
-
return SST
|
187
|
-
}
|
package/src/soda-config.js
DELETED
@@ -1,82 +0,0 @@
|
|
1
|
-
export default function sodaSetConfig (sodajs) {
|
2
|
-
|
3
|
-
sodajs.prefix('v-')
|
4
|
-
|
5
|
-
sodajs.directive('repeat', {
|
6
|
-
|
7
|
-
priority: 10,
|
8
|
-
|
9
|
-
link({ scope, el, expression, getValue, compileNode }) {
|
10
|
-
|
11
|
-
let itemName
|
12
|
-
let valueName
|
13
|
-
let trackName
|
14
|
-
|
15
|
-
const trackReg = /\s+by\s+([^\s]+)$/
|
16
|
-
const inReg = /([^\s]+)\s+in\s+([^\s]+)|\(([^,]+)\s*,\s*([^)]+)\)\s+in\s+([^\s]+)/
|
17
|
-
|
18
|
-
const opt = expression.replace(trackReg, (item, $1) => {
|
19
|
-
if ($1)
|
20
|
-
trackName = ($1 || '').trim()
|
21
|
-
return ''
|
22
|
-
})
|
23
|
-
|
24
|
-
const r = inReg.exec(opt)
|
25
|
-
|
26
|
-
if (r) {
|
27
|
-
if (r[1] && r[2]) {
|
28
|
-
itemName = (r[1] || '').trim()
|
29
|
-
valueName = (r[2] || '').trim()
|
30
|
-
|
31
|
-
if (!(itemName && valueName)) {
|
32
|
-
return
|
33
|
-
}
|
34
|
-
} else if (r[3] && r[4] && r[5]) {
|
35
|
-
trackName = (r[3] || '').trim()
|
36
|
-
itemName = (r[4] || '').trim()
|
37
|
-
valueName = (r[5] || '').trim()
|
38
|
-
}
|
39
|
-
} else {
|
40
|
-
return
|
41
|
-
}
|
42
|
-
|
43
|
-
trackName = trackName || '$index'
|
44
|
-
|
45
|
-
const repeatObj = getValue(scope, valueName) || []
|
46
|
-
|
47
|
-
const repeatFunc = (i) => {
|
48
|
-
|
49
|
-
const itemNode = el.cloneNode(true)
|
50
|
-
const itemScope = Object.create(scope)
|
51
|
-
|
52
|
-
itemScope[trackName] = i
|
53
|
-
itemScope[itemName] = repeatObj[i]
|
54
|
-
|
55
|
-
itemNode.removeAttribute(`${this._prefix}repeat`)
|
56
|
-
el.parentNode.insertBefore(itemNode, el)
|
57
|
-
|
58
|
-
Array.from(itemNode.querySelectorAll('[data-component]'))
|
59
|
-
.forEach(node => node.setAttribute('initialState', JSON.stringify(itemScope)))
|
60
|
-
|
61
|
-
compileNode(itemNode, itemScope)
|
62
|
-
}
|
63
|
-
|
64
|
-
if ('length' in repeatObj) {
|
65
|
-
for (var i = 0; i < repeatObj.length; i++) {
|
66
|
-
repeatFunc(i)
|
67
|
-
}
|
68
|
-
} else {
|
69
|
-
for (var i in repeatObj) {
|
70
|
-
if (repeatObj.hasOwnProperty(i)) {
|
71
|
-
repeatFunc(i)
|
72
|
-
}
|
73
|
-
}
|
74
|
-
}
|
75
|
-
|
76
|
-
el.parentNode.removeChild(el)
|
77
|
-
|
78
|
-
if (el.childNodes && el.childNodes.length)
|
79
|
-
el.innerHTML = ''
|
80
|
-
}
|
81
|
-
})
|
82
|
-
}
|
package/src/utils/index.js
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
|
2
|
-
export const rAF = (fn) => {
|
3
|
-
(requestAnimationFrame || setTimeout)(fn, 1000 / 60)
|
4
|
-
}
|
5
|
-
|
6
|
-
export const nextFrame = (fn) => {
|
7
|
-
rAF(() => rAF(fn))
|
8
|
-
}
|
9
|
-
|
10
|
-
export const uuid = () => {
|
11
|
-
return 'xxxxxxxx'.replace(/[xy]/g, (c) => {
|
12
|
-
const r = Math.random() * 8 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
|
13
|
-
return v.toString(8)
|
14
|
-
})
|
15
|
-
}
|
16
|
-
|
17
|
-
export const stripTemplateTags = ( html ) => {
|
18
|
-
return html.replace(/<template.*?>|<\/template>/g, '')
|
19
|
-
}
|
20
|
-
|
21
|
-
export const stripTemplateTag = ( element ) => {
|
22
|
-
const templates = Array.from(element.querySelectorAll('template'))
|
23
|
-
// https://gist.github.com/harmenjanssen/07e425248779c65bc5d11b02fb913274
|
24
|
-
templates.forEach( template => {
|
25
|
-
template.parentNode.replaceChild(template.content, template )
|
26
|
-
})
|
27
|
-
}
|
package/src/utils/pubsub.js
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
const topics = {}
|
2
|
-
const _async = {}
|
3
|
-
|
4
|
-
export const publish = (name, params) => {
|
5
|
-
_async[name] = Object.assign({}, _async[name], params)
|
6
|
-
if( topics[name] )
|
7
|
-
topics[name].forEach(topic => topic(params))
|
8
|
-
}
|
9
|
-
|
10
|
-
export const subscribe = (name, method) => {
|
11
|
-
topics[name] = topics[name] || []
|
12
|
-
topics[name].push(method)
|
13
|
-
if ( name in _async ) {
|
14
|
-
method(_async[name])
|
15
|
-
}
|
16
|
-
}
|
17
|
-
|
18
|
-
export const unsubscribe = (topic) => {
|
19
|
-
topics[topic.name] = (topics[topic.name] || [])
|
20
|
-
.filter(t => t != topic.method)
|
21
|
-
if (!topics[topic.name].length) {
|
22
|
-
delete topics[topic.name]
|
23
|
-
delete _async[topic.name]
|
24
|
-
}
|
25
|
-
}
|
package/webpack.config.babel.js
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
const path = require('path')
|
2
|
-
|
3
|
-
module.exports = {
|
4
|
-
|
5
|
-
entry: {
|
6
|
-
'jails': './src/index.js'
|
7
|
-
},
|
8
|
-
|
9
|
-
output: {
|
10
|
-
path : path.resolve(__dirname, './dist'),
|
11
|
-
filename : '[name].js',
|
12
|
-
libraryTarget : 'umd',
|
13
|
-
library : 'jails',
|
14
|
-
umdNamedDefine: true
|
15
|
-
}
|
16
|
-
}
|