jails-js 5.0.0-beta.0 → 5.0.0-beta.11

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/index.js DELETED
@@ -1,185 +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
- morphdom( element, sodajs( this.template, this.view(this.model) ), {
109
- onNodeDiscarded(node) {
110
- Template.scan(node, Template.remove)
111
- return true
112
- },
113
- onBeforeElUpdated(node, toEl) {
114
- if (node.isEqualNode(toEl))
115
- return false
116
- if( node.nodeType == 1 && 'static' in node.dataset )
117
- return false
118
- return true
119
- }
120
- })
121
-
122
- rAF(_ => {
123
- const elements = Array.from(element.querySelectorAll('[data-component]'))
124
- elements.forEach( node => {
125
- const initialState = JSON.parse(node.getAttribute('initialState')) || {}
126
- const item = AST.find( item => item.element == node )
127
- const { global, parent, ...model } = this.model
128
- if( item ) {
129
- const newmodel = Object.assign(initialState, { parent:model, global: SST })
130
- item.update( newmodel, true )
131
- }
132
- })
133
- })
134
- }
135
- }
136
-
137
- AST.push( ElementInterface )
138
-
139
- element.dataset.component.split(/\s/).forEach( name => {
140
-
141
- const C = components[name]
142
-
143
- if( !C ) {
144
- console.warn(`Jails - Module ${name} not registered`)
145
- return
146
- }
147
-
148
- const { module, dependencies } = C
149
- ElementInterface.model = Object.assign({}, module.model, ElementInterface.model )
150
-
151
- const base = Component({ name, element, dependencies, Pubsub, ElementInterface, AST })
152
-
153
- const promise = module.default(base)
154
-
155
- if( promise && promise.then ) {
156
- ElementInterface.promises.push(promise)
157
- }
158
-
159
- base.__initialize()
160
- ElementInterface.view = module.view || ElementInterface.view
161
- ElementInterface.instances[name] = { methods: {} }
162
- })
163
-
164
- ElementInterface.update()
165
- }
166
-
167
- const createTemplate = ( html ) => {
168
- const vhtml = stripTemplateTags( html )
169
- const vroot = document.createElement('div')
170
- vroot.innerHTML = vhtml
171
- const components = Array.from(vroot.querySelectorAll('[data-component]'))
172
- components.forEach( c => {
173
- const cache = AST.find( item => item.tplid === c.getAttribute('tplid') )
174
- if( cache )
175
- c.outerHTML = cache.template
176
- })
177
- return vroot.innerHTML
178
- }
179
-
180
- const saveGlobal = (data) => {
181
- Object.assign(SST, data)
182
- delete SST.parent
183
- delete SST.global
184
- return SST
185
- }
@@ -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
- }
@@ -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
- }
@@ -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
- }