-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
!!(function(document) { | ||
|
||
const template = `<div currentState=""></div>` | ||
|
||
document.registerElement('s-states', class extends SlimBaseElement { | ||
|
||
get updateOnAttributes() { | ||
return ['current-state'] | ||
} | ||
|
||
get currentState() { | ||
return this.getAttribute('current-state'); | ||
} | ||
|
||
set currentState(value) { | ||
if (value !== this.currentState) { | ||
this.setAttribute('current-state') | ||
} | ||
} | ||
|
||
beforeRender() { | ||
this.virtual = document.createDocumentFragment() | ||
this.actual = this.__bindingTree | ||
this.currentState = this.getAttribute('current-state') || '' | ||
} | ||
|
||
render() { | ||
this.update() | ||
} | ||
|
||
update() { | ||
for (let i = this.actual.children.length; i > 0; i--) { | ||
let child = this.actual.children[i - 1]; | ||
if (child.getAttribute('in-state') !== this.currentState) { | ||
this.actual.removeChild(child) | ||
this.virtual.appendChild(child) | ||
} | ||
} | ||
|
||
for (let i = this.virtual.children.length; i > 0; i--) { | ||
let child = this.virtual.children[i - 1]; | ||
if (child.getAttribute('in-state') === this.currentState) { | ||
this.virtual.removeChild(child) | ||
this.actual.appendChild(child) | ||
} | ||
} | ||
} | ||
|
||
}) | ||
}(document)) |
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,38 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>SlimJS - States Demo</title> | ||
|
||
<script src="../../SlimElement.js"></script> | ||
<script src="../../components/s-states.js"></script> | ||
</head> | ||
<body> | ||
|
||
|
||
<s-states current-state="init"> | ||
|
||
<div in-state="init">Init</div> | ||
<div in-state="hello">Hello</div> | ||
<div in-state="hello">World</div> | ||
<div in-state="bye">Goodbye</div> | ||
|
||
|
||
</s-states> | ||
<button onclick="nextState()" value="next..."></button> | ||
|
||
<script> | ||
|
||
const states = ['init','hello','bye']; | ||
var x = 0; | ||
|
||
function nextState() { | ||
document.querySelector('s-states').setAttribute('current-state', states[x++ % states.length]) | ||
console.log(x) | ||
} | ||
|
||
|
||
</script> | ||
|
||
</body> | ||
</html> |