forked from kamicane/art
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added two new APIs: injectBefore() and empty()
- Loading branch information
1 parent
ad1af16
commit 988f190
Showing
23 changed files
with
417 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Object Model</title> | ||
</head> | ||
<body> | ||
<script src="../../lib/link/Source/Web/link.js" data-main="objectmodel.js" data-nocache></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require('../mode'); | ||
var ART = require('../../art'); | ||
|
||
var art = ART.Surface(1000, 600); | ||
|
||
var group = ART.Group() | ||
.inject(art); | ||
|
||
var text = ART.Text('DOM', 'bold 60px "Arial"') | ||
.move(0, 0) | ||
.fill('red') | ||
.inject(group); | ||
|
||
var green = ART.Rectangle(100, 100) | ||
.move(10, 10) | ||
.fill('green') | ||
.inject(group); | ||
|
||
var group2 = ART.Group() | ||
.move(10,10) | ||
.rotate(5) | ||
.inject(group); | ||
|
||
var blue = ART.Rectangle(100, 100) | ||
.move(10,10) | ||
.rotate(-5) | ||
.fill('blue') | ||
.inject(group2); | ||
|
||
function eq(){ | ||
var a = arguments[0]; | ||
for (var i = 1; i < arguments.length; i++){ | ||
var b = arguments[i]; | ||
if (a !== b){ debugger; throw new Error('Assertion failed'); } | ||
} | ||
} | ||
|
||
function verifyState(){ | ||
// art | ||
// group | ||
// text | ||
// green | ||
// group2 | ||
// blue | ||
|
||
eq(art.firstChild, art.lastChild, group); | ||
eq(group.nextSibling, group.previousSibling, null); | ||
|
||
eq(group.firstChild, text); | ||
eq(group.lastChild, group2); | ||
|
||
eq(text.previousSibling, group2.nextSibling, null); | ||
|
||
eq(text.nextSibling, green); | ||
eq(green.previousSibling, text); | ||
|
||
eq(green.nextSibling, group2); | ||
eq(group2.previousSibling, green); | ||
|
||
eq(group2.firstChild, group2.lastChild, blue); | ||
eq(blue.nextSibling, blue.previousSibling, null); | ||
|
||
eq(blue.parentNode, group2); | ||
eq(text.parentNode, green.parentNode, group2.parentNode, group); | ||
eq(group.parentNode, art); | ||
} | ||
|
||
verifyState(); | ||
|
||
var alt = true; | ||
|
||
var timer = setInterval(function(){ | ||
alt = !alt; | ||
if (alt) | ||
green.eject(); | ||
else { | ||
green.injectBefore(group2); | ||
console.log('injecting' + green.parentNode); | ||
verifyState(); | ||
//green.injectBefore(group2); | ||
//group2.injectBefore(green); | ||
} | ||
group2.rotate(1, 50, 50); | ||
}, 500); | ||
|
||
group2.subscribe('click', function(){ | ||
clearInterval(timer); | ||
group.empty(); | ||
art.empty(); | ||
blue.inject(art); | ||
}); | ||
|
||
art.inject(document.body); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var Class = require('../core/class'); | ||
|
||
module.exports = Class({ | ||
|
||
grab: function(){ | ||
for (var i = 0; i < arguments.length; i++) arguments[i].inject(this); | ||
return this; | ||
}, | ||
|
||
empty: function(){ | ||
var node; | ||
while (node = this.firstChild) node.eject(); | ||
return this; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
var Class = require('../core/class'); | ||
|
||
module.exports = Class({ | ||
|
||
// placement | ||
|
||
_resetPlacement: function(){ | ||
var container = this.parentNode; | ||
if (container){ | ||
var previous = this.previousSibling, next = this.nextSibling; | ||
if (previous){ | ||
previous.nextSibling = next; | ||
} else { | ||
container.firstChild = next; | ||
} | ||
if (next){ | ||
next.previousSibling = previous; | ||
} else { | ||
container.lastChild = this.previousSibling; | ||
} | ||
} | ||
this.previousSibling = null; | ||
this.nextSibling = null; | ||
this.parentNode = null; | ||
return this; | ||
}, | ||
|
||
inject: function(container){ | ||
this._resetPlacement(); | ||
var last = container.lastChild; | ||
if (last){ | ||
last.nextSibling = this; | ||
this.previousSibling = last; | ||
} else { | ||
container.firstChild = this; | ||
} | ||
container.lastChild = this; | ||
this.parentNode = container; | ||
this._place(); | ||
return this; | ||
}, | ||
|
||
injectBefore: function(sibling){ | ||
this._resetPlacement(); | ||
var container = sibling.parentNode; | ||
if (!container) return this; | ||
var previous = sibling.previousSibling; | ||
if (previous){ | ||
previous.nextSibling = this; | ||
this.previousSibling = previous; | ||
} else { | ||
container.firstChild = this; | ||
} | ||
sibling.previousSibling = this; | ||
this.nextSibling = sibling; | ||
this.parentNode = container; | ||
this._place(); | ||
return this; | ||
}, | ||
|
||
eject: function(){ | ||
this._resetPlacement(); | ||
this._place(); | ||
return this; | ||
}, | ||
|
||
_place: function(){}, | ||
|
||
// events | ||
|
||
dispatch: function(event){ | ||
var events = this._events, | ||
listeners = events && events[event.type]; | ||
if (listeners){ | ||
listeners = listeners.slice(0); | ||
for (var i = 0, l = listeners.length; i < l; i++){ | ||
var fn = listeners[i], result; | ||
if (typeof fn == 'function') | ||
result = fn.call(this, event); | ||
else | ||
result = fn.handleEvent(event); | ||
if (result === false) event.preventDefault(); | ||
} | ||
} | ||
if (this.parentNode && this.parentNode.dispatch){ | ||
this.parentNode.dispatch(event); | ||
} | ||
}, | ||
|
||
subscribe: function(type, fn, bind){ | ||
if (typeof type != 'string'){ // listen type / fn with object | ||
var subscriptions = []; | ||
for (var t in type) subscriptions.push(this.subscribe(t, type[t])); | ||
return function(){ // unsubscribe | ||
for (var i = 0, l = subscriptions.length; i < l; i++) | ||
subscriptions[i](); | ||
return this; | ||
}; | ||
} else { // listen to one | ||
var bound = typeof fn === 'function' ? fn.bind(bind || this) : fn, | ||
events = this._events || (this._events = {}), | ||
listeners = events[type] || (events[type] = []); | ||
listeners.push(bound); | ||
return function(){ | ||
// unsubscribe | ||
for (var i = 0, l = listeners.length; i < l; i++){ | ||
if (listeners[i] === bound){ | ||
listeners.splice(i, 1); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var Class = require('../core/class'); | ||
var Dummy = require('./dummy'); | ||
var Native = require('./native'); | ||
|
||
module.exports = Class(Dummy, Native, { | ||
|
||
dummy_inject: Dummy.prototype.inject, | ||
dummy_injectBefore: Dummy.prototype.injectBefore, | ||
dummy_eject: Dummy.prototype.eject, | ||
native_inject: Native.prototype.inject, | ||
native_injectBefore: Native.prototype.injectBefore, | ||
native_eject: Native.prototype.eject, | ||
|
||
inject: function(container){ | ||
this.dummy_inject(container); | ||
this.native_inject(container); | ||
return this; | ||
}, | ||
|
||
injectBefore: function(sibling){ | ||
this.dummy_injectBefore(sibling); | ||
this.native_injectBefore(sibling); | ||
return this; | ||
}, | ||
|
||
eject: function(){ | ||
this.dummy_eject(); | ||
this.native_eject(); | ||
return this; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.