Skip to content

Commit

Permalink
Merge branch 'v5/refactory'
Browse files Browse the repository at this point in the history
  • Loading branch information
Javiani committed Jan 9, 2022
2 parents bcc270a + fa908b0 commit c1b47e0
Show file tree
Hide file tree
Showing 18 changed files with 280 additions and 313 deletions.
16 changes: 0 additions & 16 deletions .babelrc

This file was deleted.

2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

20 changes: 0 additions & 20 deletions .eslintrc

This file was deleted.

28 changes: 0 additions & 28 deletions .eslintrc.js

This file was deleted.

4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ files to ignore
*.swp
*.out
node_modules/
main.js
index.htm
example.js
*.cache
3 changes: 2 additions & 1 deletion dist/jails.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/jails.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jails-js",
"version": "5.0.0-beta.2",
"version": "5.0.0-beta.3",
"description": "A Modern Javascript Library",
"main": "dist/jails.js",
"scripts": {
Expand Down Expand Up @@ -29,6 +29,8 @@
"@babel/preset-env": "^7.2.3",
"babel-loader": "^8.0.5",
"babel-preset-env": "^1.7.0",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4",
"webpack": "^5.59.1",
"webpack-cli": "^3.2.1"
},
Expand Down
8 changes: 5 additions & 3 deletions src/component.js → src/Component.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as Pubsub from './utils/pubsub'
import { on, off, trigger } from './utils/events'
import { rAF } from './utils'

export default function Component ({
export const Component = ({

name,
element,
dependencies,
Pubsub,
ElementInterface
}) {

}) => {

const subscriptions = []

Expand Down
117 changes: 117 additions & 0 deletions src/Element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { stripTemplateTag, dup, rAF, createTemplate, uuid } from './utils'
import morphdom from 'morphdom'
import { setSodaConfig } from './soda-config'

const sodajs = setSodaConfig()
const templates = {}

export const Element = ( el:HTMLElement ) => {

stripTemplateTag( el )

let updates = []

const model = Object.assign({}, JSON.parse(el.dataset.initialState || '{}'))
const morphdomOptions = lifecycle(el)
const { template, tplid } = getTemplateData(el)

const api = {

tplid,
el,
template,
model,
parent: {},
view : _ => _,
instances:{},
destroyers:[],
promises: [],
parentUpdate: data => null,

dispose(){
if( api.promises.length ){
Promise.all(api.promises)
.then(_ => api.destroyers.forEach( destroy => destroy(api) ))
}else {
api.destroyers.forEach( destroy => destroy(api) )
}
},

update( data = {}, isParentUpdate ) {

if( !document.body.contains(el) )
return

updates.push( data )

rAF( _ => {

if( updates.length ) {

const newdata = {}
updates.forEach( d => Object.assign(newdata, d ) )
updates = []

api.model = Object.assign( api.model, newdata )

if( isParentUpdate ) {
api.parentUpdate( api.model )
}

const newhtml = sodajs(template, api.view(dup(api.model)))
morphdom( el, newhtml, morphdomOptions )

Array
.from( el.querySelectorAll('[data-component]') )
.forEach( node => {
if( !node.__instance__ ) return
const { parent, ...model } = api.model
const initialState = node.dataset.initialState? JSON.parse(node.dataset.initialState): {}
const newmodel = Object.assign(initialState, { parent:model })
node.__instance__.update( newmodel, true )
})
}
})
}
}

el.__instance__ = api

return api
}

const lifecycle = ( element: HTMLElement ) => {
return {
onBeforeElUpdated: update(element),
onBeforeElChildrenUpdated: update(element),
getNodeKey(node) {
if( node.nodeType === 1 && node.dataset.tplid )
return node.dataset.key || node.dataset.tplid
return false
}
}
}

const update = ( element: HTMLElement ) => ( node: HTMLElement, toEl: HTMLElement ) => {
if ( node.isEqualNode(toEl) )
return false
if( node.nodeType == 1 ) {
if( 'static' in node.dataset )
return false
}
return true
}

const getTemplateData = ( element: HTMLElement ) => {
if( element.getAttribute('tplid') ) {
const tplid = element.getAttribute('tplid')
const template = templates[tplid]
return { tplid, template }
}else {
const tplid = uuid()
element.setAttribute('tplid', tplid)
templates[tplid] = createTemplate(element.outerHTML, templates)
const template = templates[tplid]
return { tplid, template }
}
}
26 changes: 26 additions & 0 deletions src/Scanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

export const Scanner = {

scan( node: HTMLElement, callback ) {
if( node.nodeType === 1 ) {
const list : Array<HTMLElement> = Array.from( node.querySelectorAll('[data-component]') )
const elements : Array<HTMLElement> = node.dataset.component? [node].concat(list) : list
if( elements.length ) {
elements.reverse().forEach( callback )
}
}
},

observe( target: HTMLElement, onAdd, onRemove ) {
const observer = new MutationObserver(mutations => mutations.forEach( mutation => {
if (mutation.type === 'childList') {
if (mutation.addedNodes.length) {
Array.from( mutation.addedNodes ).forEach( node => Scanner.scan(node, onAdd) )
} else if (mutation.removedNodes.length) {
Array.from( mutation.removedNodes ).forEach( node => Scanner.scan(node, onRemove) )
}
}
}))
observer.observe(target, { childList: true, subtree: true })
}
}
Loading

0 comments on commit c1b47e0

Please sign in to comment.