carousel is now a real example

This commit is contained in:
Andreas Rumpf 2017-06-21 17:57:02 +02:00
commit a535ad709b
5 changed files with 4 additions and 10 deletions

View file

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Carousel app</title>
</head>
<body id="body">
<div id="ROOT" />
<script type="text/javascript" src="nimcache/carousel.js"></script>
</body>
</html>

View file

@ -1,76 +0,0 @@
## This demo shows how you can develop your own stateful components with Karax.
import vdom, vstyles, karax, karaxdsl, jdict, jstrutils, kdom
type
Carousel = ref object of VComponent
counter: int
cntdown, myid: int
timer: TimeOut
list: seq[cstring]
const ticksUntilChange = 5
var
images: seq[cstring] = @[cstring"a", "b", "c", "d"]
proc render(x: VComponent): VNode =
let self = Carousel(x)
proc docount() =
dec self.cntdown
if self.cntdown == 0:
self.counter = (self.counter + 1) mod self.list.len
self.cntdown = ticksUntilChange
else:
self.timer = setTimeout(docount, 30)
markDirty(self)
redraw()
proc onclick(ev: Event; n: VNode) =
if self.timer != nil:
clearTimeout(self.timer)
self.timer = setTimeout(docount, 30)
let col =
case self.counter
of 0: cstring"#4d4d4d"
of 1: cstring"#ff00ff"
of 2: cstring"#00ffff"
of 3: cstring"#ffff00"
else: cstring"red"
result = buildHtml(tdiv()):
text self.list[self.counter]
button(onclick = onclick):
text "Next"
tdiv(style = style(StyleAttr.color, col)):
text "This changes its color." & &self.myid
if self.cntdown != ticksUntilChange:
text &self.cntdown
var gid: int
proc carousel(): Carousel =
result = newComponent(Carousel, render)
result.list = images
result.cntdown = ticksUntilChange
result.myid = gid
inc gid
#result.onAttach = proc (_: VComponent) =
# result.cntdown = ticksUntilChange
# kout cstring"attached!"
proc createDom(): VNode =
result = buildHtml(table):
tr:
td:
carousel()
td:
carousel()
tr:
td:
carousel()
td:
carousel()
setRenderer createDom

View file

@ -1 +0,0 @@
--path: "../src"