jails-js 5.0.0-beta.1 → 5.0.0-beta.12

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.js DELETED
@@ -1,128 +0,0 @@
1
- import { on, off, trigger } from './utils/events'
2
- import { rAF } from './utils'
3
-
4
- export default function Component ({
5
- name,
6
- element,
7
- dependencies,
8
- Pubsub,
9
- ElementInterface,
10
- AST
11
- }) {
12
-
13
- const subscriptions = []
14
-
15
- let stateSubscriptions = []
16
- let resolver
17
- let promise = new Promise(resolve => resolver = resolve)
18
-
19
- const base = {
20
-
21
- name,
22
- dependencies,
23
- elm: element,
24
- publish: Pubsub.publish,
25
- unsubscribe: Pubsub.unsubscribe,
26
-
27
- __initialize() {
28
- resolver(base)
29
- },
30
-
31
- main(fn) {
32
- promise
33
- .then( _ => fn().forEach(lambda => lambda(base)))
34
- .catch( err => console.error( err) )
35
- },
36
-
37
- expose(methods) {
38
- ElementInterface.instances[name].methods = methods
39
- },
40
-
41
- state: {
42
- set( state ) {
43
- if( state.constructor === Function ){
44
- const model = ElementInterface.model
45
- state(model)
46
- ElementInterface.update(model)
47
- stateSubscriptions.forEach( fn => fn(model) )
48
- } else {
49
- ElementInterface.update(state)
50
- stateSubscriptions.forEach( fn => fn(state) )
51
- }
52
- return new Promise((resolve) => rAF(resolve))
53
- },
54
- get() {
55
- return ElementInterface.model
56
- },
57
- subscribe(fn){
58
- stateSubscriptions.push(fn)
59
- },
60
- unsubscribe(fn){
61
- stateSubscriptions = stateSubscriptions.filter( item => item !== fn )
62
- }
63
- },
64
-
65
- destroy(callback) {
66
- ElementInterface.destroyers.push(callback)
67
- },
68
-
69
- on(name, selectorOrCallback, callback) {
70
- on(element, name, selectorOrCallback, callback)
71
- },
72
-
73
- off(name, callback) {
74
- off(element, name, callback)
75
- },
76
-
77
- trigger(ev, target, args) {
78
- if (target.constructor === String)
79
- trigger(element.querySelector(target), ev, { args: args })
80
- else trigger(element, ev, { args: target })
81
- },
82
-
83
- emit(n, params) {
84
- const args = Array.prototype.slice.call(arguments)
85
- trigger(element, args.shift(), { args: args })
86
- },
87
-
88
- update(fn) {
89
- ElementInterface.parentUpdate = fn
90
- },
91
-
92
- get(name, query) {
93
-
94
- return function () {
95
- rAF(_ => {
96
- const args = Array.prototype.slice.call(arguments),
97
- method = args.shift(),
98
- selector = `[data-component*=${name}]`
99
- query = query ? selector + query : selector
100
-
101
- Array.from(element.querySelectorAll(query))
102
- .forEach(el => {
103
- const item = AST.find( item => item.element == el )
104
- if( item ) {
105
- const instance = item.instances[name]
106
- if (instance && (method in instance.methods))
107
- instance.methods[method].apply(null, args)
108
- }
109
- })
110
-
111
- if (element.matches(query)) {
112
- const item = AST.find( item => item.element == element )
113
- const instance = item.instances[name]
114
- if (instance && method in instance.methods)
115
- instance.methods[method].apply(null, args)
116
- }
117
- })
118
- }
119
- },
120
-
121
- subscribe(name, method) {
122
- subscriptions.push({ name, method })
123
- Pubsub.subscribe(name, method)
124
- }
125
- }
126
-
127
- return base
128
- }
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
- }
@@ -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
- }