Add attachTo module
parent
f1ee781962
commit
24206f430a
@ -0,0 +1,34 @@
|
||||
function insert(vnode) {
|
||||
// Placeholder has been inserted, reset the vnodes
|
||||
// element to the actual element so that updates will
|
||||
// be made to the proper element
|
||||
var attach = vnode.data.attachTo;
|
||||
vnode.elm = attach.elm;
|
||||
}
|
||||
|
||||
function attach(_, vnode) {
|
||||
var data = vnode.data, target = data.attachTo;
|
||||
if (target === undefined) return;
|
||||
var elm = vnode.elm;
|
||||
var placeholder = document.createElement('span');
|
||||
// Replace actual element with dummy placeholder
|
||||
// Snabbdom will then insert placeholder instead
|
||||
vnode.elm = placeholder;
|
||||
target.appendChild(elm);
|
||||
if (vnode.data.hook === undefined) vnode.data.hook = {};
|
||||
data.attachTo = {target: target, placeholder: placeholder, elm: elm};
|
||||
var hook = vnode.data.hook;
|
||||
hook.insert = insert;
|
||||
}
|
||||
|
||||
function destroy(vnode) {
|
||||
var attach = vnode.data.attachTo;
|
||||
if (attach === undefined) return;
|
||||
// Manually remove the actual element from where it was inserted
|
||||
attach.target.removeChild(attach.elm);
|
||||
// Replace actual element with the dummy
|
||||
// placeholder so that Snabbdom removes it
|
||||
vnode.elm = attach.placeholder;
|
||||
}
|
||||
|
||||
module.exports = {create: attach, destroy: destroy};
|
@ -0,0 +1,60 @@
|
||||
var assert = require('assert');
|
||||
var snabbdom = require('../snabbdom');
|
||||
|
||||
var patch = snabbdom.init([
|
||||
require('../modules/attachto'),
|
||||
]);
|
||||
var h = require('../h');
|
||||
|
||||
describe('attachTo', function() {
|
||||
var elm, vnode0;
|
||||
beforeEach(function() {
|
||||
elm = document.createElement('div');
|
||||
vnode0 = elm;
|
||||
});
|
||||
it('adds element to target', function() {
|
||||
patch(vnode0, h('div', [
|
||||
h('div#wrapper', [
|
||||
h('div', 'Some element'),
|
||||
h('div#attached', {attachTo: elm}, 'Test'),
|
||||
]),
|
||||
]));
|
||||
console.log(elm.children);
|
||||
assert.equal(elm.children.length, 2);
|
||||
});
|
||||
it('updates element at target', function() {
|
||||
var vnode1 = h('div', [
|
||||
h('div#wrapper', [
|
||||
h('div', 'Some element'),
|
||||
h('div#attached', {attachTo: elm}, 'First text'),
|
||||
]),
|
||||
]);
|
||||
var vnode2 = h('div', [
|
||||
h('div#wrapper', [
|
||||
h('div', 'Some element'),
|
||||
h('div#attached', {attachTo: elm}, 'New text'),
|
||||
]),
|
||||
]);
|
||||
patch(vnode0, vnode1);
|
||||
assert.equal(elm.children[0].innerHTML, 'First text');
|
||||
patch(vnode1, vnode2);
|
||||
assert.equal(elm.children[0].innerHTML, 'New text');
|
||||
});
|
||||
it('removes element at target', function() {
|
||||
var vnode1 = h('div', [
|
||||
h('div#wrapper', [
|
||||
h('div', 'Some element'),
|
||||
h('div#attached', {attachTo: elm}, 'First text'),
|
||||
]),
|
||||
]);
|
||||
var vnode2 = h('div', [
|
||||
h('div#wrapper', [
|
||||
h('div', 'Some element'),
|
||||
]),
|
||||
]);
|
||||
patch(vnode0, vnode1);
|
||||
assert.equal(elm.children[0].innerHTML, 'First text');
|
||||
patch(vnode1, vnode2);
|
||||
assert.equal(elm.children.length, 1);
|
||||
});
|
||||
});
|
@ -1,3 +1,4 @@
|
||||
require('./core');
|
||||
require('./style');
|
||||
require('./attachto');
|
||||
require('./thunk');
|
||||
|
Loading…
Reference in New Issue