Skip to content

Commit efcf005

Browse files
authored
Merge pull request #12 from heapwolf/v7.3.0
V7.3.0
2 parents c7e96e9 + f0a9a4d commit efcf005

File tree

5 files changed

+3123
-10
lines changed

5 files changed

+3123
-10
lines changed

API.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
| Method | Description |
66
| :--- | :--- |
7-
| `add(Class, Object)` | Register a class as a new custom-tag and provide optional options for it. |
7+
| `add(Class, Object)` | Register a class as a new custom-tag and provide options for it. |
88
| `escape(String)` | Escapes HTML characters from a string (based on [he][3]). |
99
| `sanitize(Object)` | Escapes all the strings found in an object literal. |
1010
| `match(Node, Selector)` | Match the given node against a selector or any matching parent of the given node. This is useful when trying to locate a node from the actual node that was interacted with. |
@@ -17,10 +17,16 @@
1717
| `getProps()` | Get the properties of a component instance. |
1818
| <code>setState(Object &#124; Function)</code> | Set the state of a component instance. Can also take a function which will receive the current props as an argument. |
1919
| `stylesheet()` | Returns a string of css to be lazily added to a `style` tag in the head. |
20-
| `styles()` | Returns an object that represents inline-styles. Styles are applied by adding a keys from the object to the `styles` attribute of an html tag in the render function, for example `styles="key1 key2"`. Each object's key-value pair are added to the element's style object. |
20+
| `styles()` | Returns an object that represents inline-styles to be applied to the component. Styles are applied by adding a keys from the object to the `styles` attribute of an html tag in the render function, for example `styles="key1 key2"`. Each object's key-value pair are added to the element's style object. |
2121
| `render()` | Returns HTML to be parsed or a dom node that will overwrite. There is usually no need to call this directly, prefer `componentInstance.reRender({ ... })`. |
2222
| html\`...\` | Tidy up an HTML string (use as a [tagged template][2]). |
2323

24+
## INSTANCE PROPERTIES
25+
26+
| Name | Description |
27+
| :--- | :--- |
28+
| <code>children</code> | An array of nodes, the original child nodes of the component. |
29+
2430
## "LIFECYCLE" INSTANCE METHODS
2531

2632
| Method | Description |

index.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Tonic {
7676
const reduce = (a, b) => a.concat(b, strings.shift())
7777
const filter = s => s && (s !== true || s === 0)
7878
const ref = v => {
79+
if (typeof v === 'object' && v.__children__) return this._children(v)
7980
if (typeof v === 'object' || typeof v === 'function') return this._prop(v)
8081
if (typeof v === 'number') return `${v}__float`
8182
return v
@@ -103,11 +104,15 @@ class Tonic {
103104
return this.props
104105
}
105106

107+
handleEvent (e) {
108+
this[e.type](e)
109+
}
110+
106111
_bindEventListeners () {
107112
const hp = Object.getOwnPropertyNames(window.HTMLElement.prototype)
108113
for (const p of this._props) {
109114
if (hp.indexOf('on' + p) === -1) continue
110-
this.root.addEventListener(p, e => this[p](e))
115+
this.root.addEventListener(p, this)
111116
}
112117
}
113118

@@ -131,6 +136,15 @@ class Tonic {
131136
el.getAttribute('styles').split(/\s+/).forEach(s =>
132137
Object.assign(el.style, styles[s.trim()])))
133138
}
139+
140+
Array.from(target.querySelectorAll('tonic-children')).forEach(el => {
141+
const root = Tonic._elements[this.root._id]
142+
Array.from(root[el.id]).forEach(node => {
143+
el.parentNode.insertBefore(node, el)
144+
})
145+
delete root[el.id]
146+
el.parentNode.removeChild(el)
147+
})
134148
} else {
135149
while (target.firstChild) target.removeChild(target.firstChild)
136150
target.appendChild(content.cloneNode(true))
@@ -147,6 +161,14 @@ class Tonic {
147161
return p
148162
}
149163

164+
_children (r) {
165+
const id = this.root._id
166+
const ref = Tonic._createId()
167+
if (!Tonic._elements[id]) Tonic._elements[id] = {}
168+
Tonic._elements[id][ref] = r
169+
return `<tonic-children id="${ref}"/></tonic-children>`
170+
}
171+
150172
_connect () {
151173
for (let { name, value } of this.root.attributes) {
152174
name = name.replace(/-(.)/g, (_, m) => m.toUpperCase())
@@ -168,7 +190,8 @@ class Tonic {
168190
}
169191

170192
this.willConnect && this.willConnect()
171-
this.children = this.children || this.root.innerHTML
193+
this.children = [...this.root.childNodes].map(node => node.cloneNode(true))
194+
this.children.__children__ = true
172195
this._setContent(this.root, this.render())
173196
Tonic._constructTags(this.root)
174197
const style = this.stylesheet && this.stylesheet()
@@ -184,6 +207,7 @@ class Tonic {
184207
_disconnect (index) {
185208
this.disconnected && this.disconnected()
186209
delete Tonic._data[this.root._id]
210+
delete Tonic._elements[this.root._id]
187211
delete this.root
188212
Tonic.refs.splice(index, 1)
189213
}
@@ -192,6 +216,7 @@ class Tonic {
192216
Tonic.tags = []
193217
Tonic.refs = []
194218
Tonic._data = {}
219+
Tonic._elements = {}
195220
Tonic.registry = {}
196221
Tonic.escapeRe = /["&'<>`]/g
197222
Tonic.escapeMap = { '"': '&quot;', '&': '&amp;', '\'': '&#x27;', '<': '&lt;', '>': '&gt;', '`': '&#x60;' }

0 commit comments

Comments
 (0)