forked from karaxnim/karax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.nim
691 lines (546 loc) · 17.2 KB
/
app.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
import src/karax/prelude
import std/[strformat, dom]
# Tips
# toJSStr converts byte sequence to JS str.
# math, strformat, times, algorithm, parseutils, math
# Concept
# dom,
# import parseutils
template setVNodeAttrs(n: var VNode, attrs: openArray[(string, string)]) =
for (key, value) in attrs:
if value.len > 0:
n.setAttr(key, value)
else:
n.setAttr(key)
type
MenuMode* {.pure.} = enum
horizontal, vertical
BaseMenuItem* = object
index: int
className: string
disabled: bool
OnSelectCallback* = proc(idx: int): EventHandler
BaseMenu* = object
className: string
defaultIdx: int
mode: MenuMode
MenuContext* = ref object
index: int
onSelect: OnSelectCallback
proc initMenuItem*(index: int, className = "", disabled = false): BaseMenuItem =
BaseMenuItem(index: index, className: className, disabled: disabled)
proc menuItemClass*(baseMenuItem: BaseMenuItem, ctx: MenuContext): string =
result = "menu-item"
if baseMenuItem.className.len > 0:
result.add fmt" menu-{baseMenuItem.className}"
if baseMenuItem.disabled:
result.add " is-disabled"
if ctx.index == baseMenuItem.index:
result.add " is-active"
proc build(
baseMenuItem: BaseMenuItem,
ctx: var MenuContext,
attrs: openArray[(string, string)]
): VNode =
let className = menuItemClass(baseMenuItem, ctx)
result = buildHtml:
li(class = className):
proc onClick(ev: Event, n: VNode) =
ctx.index = baseMenuItem.index
if ctx.onSelect != nil and (not baseMenuItem.disabled):
ctx.onSelect(ctx.index)(ev, n)
result.setVNodeAttrs(attrs)
proc registerMenuItem*(
index: int,
ctx: var MenuContext,
className = "",
disabled = false,
attrs: openArray[(string, string)] = {:}
): VNode =
let baseMenuItem = BaseMenuItem(index: index, className: className, disabled: disabled)
build(baseMenuItem, ctx, attrs)
template MenuItem*(className = "", disabled = false, attrs: openArray[(string, string)] = {:}): VNode =
mixin menuCtx
mixin menuInternalIndex
when not declared(menuCtx):
static:
raise newException(ValueError, "MenuItem must be used with Menu!")
inc menuInternalIndex
registerMenuItem(menuInternalIndex, menuCtx, className, disabled, attrs)
proc initMenu*(className = "", mode = MenuMode.horizontal): BaseMenu =
BaseMenu(className: className, mode: mode)
proc menuClass*(baseMenu: BaseMenu): string =
result = "menu"
if baseMenu.className.len > 0:
result.add fmt" menu-{baseMenu.className}"
result.add fmt" menu-{baseMenu.mode}"
proc build*(baseMenu: BaseMenu, attrs: openArray[(string, string)]): VNode =
let className = menuClass(baseMenu)
result = buildHtml:
ul(class = className)
setVNodeAttrs(result, attrs)
proc registerMenu(className = "", mode = MenuMode.horizontal, attrs: openArray[(string, string)] = {:}): VNode =
let baseMenu = BaseMenu(className: className, mode: mode)
build(baseMenu, attrs)
template Menu*(ctx: MenuContext, className = "", mode = MenuMode.horizontal, callback: OnSelectCallback = nil,
attrs: openArray[(string, string)] = {:}): VNode =
var menuCtx {.inject.} = ctx
var menuInternalIndex {.inject.} = -1
registerMenu(className, mode, attrs)
when true:
block:
proc hello(idx: int): EventHandler =
result = proc (ev: Event, n: VNode) =
window.alert $(idx)
var ctx1 = MenuContext(index: 0, onSelect: hello)
var ctx2 = MenuContext(index: 0)
setRenderer proc(): VNode =
result = buildHtml(tdiv):
link(rel="stylesheet", `type` = "text/css", href = "styles/index.css")
Menu(ctx1):
MenuItem:
text "one is one"
MenuItem(disabled = true):
text "two is two"
MenuItem:
text "three is three"
br()
p:
Menu(ctx2, mode = MenuMode.vertical):
MenuItem:
text "one is one"
MenuItem:
text "two is two"
MenuItem:
text "three is three"
type
AlertType* = enum
success, info, warning, error
BaseAlert* = object
className: string
typ: AlertType
message: string
proc initAlert*(
className = "",
typ = AlertType.success,
message = ""
): BaseAlert =
BaseAlert(className: className, typ: typ, message: message)
proc alertClass*(baseAlert: BaseAlert): string =
result = "alert"
if baseAlert.className.len > 0:
result.add fmt" alert-{baseAlert.className}"
result.add fmt" alert-{baseAlert.typ}"
proc build*(baseAlert: BaseAlert, attrs: openArray[(string, string)]): VNode =
let class = alertClass(baseAlert)
let message =
if baseAlert.message.len > 0:
baseAlert.message
else:
case baseAlert.typ
of AlertType.success:
"Success Text"
of AlertType.info:
"Info Text"
of AlertType.warning:
"Warning Text"
of AlertType.error:
"Error Text"
result = buildHtml(tdiv(class = class)):
span: text message
setVNodeAttrs(result, attrs)
proc Alert*(
className = "",
typ = AlertType.success,
message = "",
attrs: openArray[(string, string)] = {:}
): VNode =
let alert = BaseAlert(className: className, typ: typ, message: message)
build(alert, attrs)
when false:
block:
proc createDom(): VNode =
result = buildHtml(tdiv):
link(rel="stylesheet", `type` = "text/css", href = "styles/index.css")
Alert()
setRenderer(createDom)
type
ButtonSize* {.pure.} = enum
base, large, small
ButtonType* {.pure.} = enum
primary, default, danger, link
BaseButton* = object
className: string
disabled: bool
size: ButtonSize
typ: ButtonType
href: string
proc buttonClass(baseButton: BaseButton): string =
result = "btn"
if baseButton.className.len > 0:
result.add " " & baseButton.className
result.add fmt" btn-{baseButton.typ}"
if baseButton.size != ButtonSize.base:
result.add fmt" btn-{baseButton.size}"
if baseButton.typ == ButtonType.link and baseButton.disabled:
result.add fmt" disabled"
proc initButton*(
className = "", disabled = false, size = ButtonSize.base,
typ = ButtonType.primary, href = ""
): BaseButton {.inline.} =
BaseButton(className: className, disabled: disabled,
size: size, typ: typ, href: href)
proc build*(
baseButton: BaseButton,
attrs: openArray[(string, string)]
): VNode =
let class = buttonClass(baseButton)
result = buildHtml:
if baseButton.typ == ButtonType.link and baseButton.href.len > 0:
a(class = class, href = baseButton.href)
else:
if baseButton.disabled:
button(class = class, disabled = $baseButton.disabled)
else:
button(class = class)
setVNodeAttrs(result, attrs)
proc Button*(className = "", disabled = false, size = ButtonSize.base,
typ = ButtonType.primary, href = "", attrs: openArray[(string, string)] = {:}): VNode =
let btn = BaseButton(className: className, disabled: disabled,
size: size, typ: typ, href: href)
build(btn, attrs)
when false:
var attrs = {"autofocus": "true", "setFocus": ""}
block:
proc createDom(): VNode =
result = buildHtml(tdiv):
link(rel="stylesheet", `type` = "text/css", href = "styles/index.css")
p:
Button("Super", false, ButtonSize.large, ButtonType.primary, "", attrs):
text "Click Here"
proc onclick(ev: Event, n: VNode) =
echo "Hallo"
Button("Super", false, ButtonSize.base, ButtonType.danger, "", attrs):
text "Click Here"
proc onclick(ev: Event, n: VNode) =
echo "see"
Button("Super", false, ButtonSize.small, ButtonType.default, "", attrs):
text "Click Here"
proc onclick(ev: Event, n: VNode) =
echo "text"
p:
Button("Super", true, typ = ButtonType.primary):
text "Click Here"
proc onclick(ev: Event, n: VNode) =
echo "seed"
Button("Super", false, ButtonSize.base, ButtonType.link, "http://www.google.com", attrs):
text "Click Here"
proc onclick(ev: Event, n: VNode) =
echo "clean"
Button:
text "hello"
setRenderer createDom
when false:
block:
var count = 0
proc createDom(): VNode =
result = buildHtml(tdiv):
link(rel="stylesheet", `type` = "text/css", href = "styles/index.css")
h1: text "Home"
h2: text "Karax"
hr()
p:
code: text "Nim"
label(id = "demo"):
text $count
br()
button:
text "Click Here"
proc onclick(ev: Event, n: VNode) =
inc count
# echo getVNodeById("demo").dom.outerHTML
# getVNodeById("demo").dom.outerHTML = &"<input id=\"demo\" type=\"number\" value=\"{count}\">"
setRenderer(createDom)
when false:
import times
block:
var message = ""
proc para(): VNode =
result = buildHtml(p):
text "New paragraph"
proc createDom(): VNode =
result = buildHtml(tdiv):
para()
button:
text "Click Here"
proc onclick(ev: Event; n: VNode) =
message = $local(getTime())
proc onclick(ev: Event; n: VNode) =
echo "Hello"
p(id = "time"):
text message
setRenderer(createDom)
when false:
import src/karax/prelude
import dom, times
block:
var message: string
proc createDom(): VNode =
result = buildHtml(tdiv):
p: text "Add Events"
button:
text "Click Here"
proc onclick(ev: Event; n: VNode) =
message = $local(getTime())
proc onclick(ev: Event; n: VNode) =
echo "Hello"
p(id = "time"):
text message
setRenderer(createDom)
when false:
import dom
import src/karax/vstyles
block:
proc createDom(): VNode =
let message = "Mouse over me"
let style = "background-color:#D94A38;width:120px;height:20px;padding:40px;"
result = buildHtml(tdiv(style = style.toCSS, id = "back")):
text message
proc onmouseover(ev: Event, n: VNode) =
# if not nil
n.dom.innerHtml = "Great!"
proc onmouseout(ev: Event, n: VNode) =
# if not nil
n.dom.innerHtml = message
setRenderer(createDom)
when false:
import dom, strformat, times, strutils
block:
proc createDom(): VNode =
result = buildHtml(tdiv):
h1: text "Web Page"
p(id="karax"):
text "Need to change"
input(`type`="text", id="fname"):
proc onchange(ev: Event, n: VNode) =
n.value = n.value.toUpperCase
button:
text "Click Here!"
proc onclick(ev: Event; n: VNode) =
# window.alert($(12 + 8))
document.getElementById("karax").innerHtml = "Hello, Karax!"
# document.write($local(getTime()))
setRenderer(createDom)
# proc createDom(): VNode =
# <div className="App">
# <Router>
# <MuiThemeProvider muiTheme={muiTheme}>
# <div id="container">
# <div id="headContainer">
# <Head />
# </div>
# <div id="bodyContainer">
# <Switch>
# <Route path="/" exact component={Home} />
# <Route path="/login" component={Login} />
# <Route path="/logout" component={Logout} />
# <Route path="/products" exact component={Products} />
# <Route path="/products/import" component={ProductImport} />
# <Route
# path="/products/categories"
# exact
# component={ProductCategories}
# />
# <Route path="/orders" exact component={Orders} />
# <Route
# path="/orders/statuses"
# exact
# component={OrderStatuses}
# />
# <Route path="/order/:orderId" exact component={OrderDetails} />
# <Route path="/customers" exact component={Customers} />
# <Route
# path="/customers/groups"
# exact
# component={CustomerGroups}
# />
# <Route
# path="/customer/:customerId"
# exact
# component={CustomerDetails}
# />
# <Route path="/product/:productId" component={ProductDetails} />
# <Route path="/pages" exact component={Pages} />
# <Route path="/pages/add" exact component={PagesDetails} />
# <Route path="/pages/:pageId" component={PagesDetails} />
# <Route path="/settings" component={Settings} />
# <Route path="/apps" component={Apps} />
# <Route path="/files" exact component={Files} />
# <Route component={NotFound} />
# </Switch>
# </div>
# </div>
# </MuiThemeProvider>
# </Router>
# </div>
when false:
import dom, strformat
block:
var count = 0
proc createDom(): VNode =
result = buildHtml(tdiv):
button:
proc onclick(ev: Event; n: VNode) =
inc count
if (count >= 10):
window.alert("count is dangerously high!")
count = 0
if count > 1:
text fmt"Clicked {count} times"
else:
text fmt"Clicked {count} time"
setRenderer(createDom)
when false:
import random
block button:
var count = 0
proc createDom(): VNode =
result = buildHtml(tdiv):
text $rand(10)
br()
button:
proc onclick(ev: Event; n: VNode) =
inc count
text "Click Here"
setRenderer(createDom)
when false:
import strformat
block button:
var count = 0
proc createDom(): VNode =
result = buildHtml(tdiv):
button:
proc onclick(ev: Event; n: VNode) =
inc count
if count > 1:
text fmt"Clicked {count} times"
else:
text fmt"Clicked {count} time"
setRenderer(createDom)
when false:
import strformat
block hello_world:
let name = "Prologue"
proc createDom(): VNode =
result = buildHtml(tdiv):
text fmt"Hello, {name}!"
setRenderer(createDom)
when false:
import strformat
block dynamic_attributes:
let
src = "assets/benchmark.png"
name = "xflywind"
proc createDom(): VNode =
result = buildHtml(tdiv):
img(src=src, alt=fmt"{name} benchmark")
setRenderer(createDom)
when false:
# count
proc createDom(): proc(): VNode =
var count = 0
result = proc(): VNode =
buildHtml(tdiv):
input(`type` = "number", value = $count)
button:
text "count"
proc onclick(ev: Event; n: VNode) =
inc count
setRenderer(createDom())
when false:
import std/[strformat, dom]
block num_count:
proc createDom(): proc(): VNode =
var count = 0
discard window.setInterval((proc() = inc count), 10)
result = proc(): VNode =
buildHtml(tdiv):
text fmt"{count = }"
setRenderer(createDom())
when false:
block input:
var message = "Hello, Karax"
proc createDom(): VNode =
result = buildHtml(tdiv):
text message
input(placeholder="Hello")
setRenderer createDom
when false:
import algorithm
block button:
proc createButton(message: var string): VNode =
result = buildHtml(tdiv):
button:
proc onclick(ev: Event; n: VNode) =
reverse(message)
text "Reverse Message"
proc createDom(): proc(): VNode =
var message = "Hello, Karax!"
result = proc(): VNode =
buildHtml(tdiv):
p: text message
createButton(message)
setRenderer createDom()
when false:
import times
import strformat
block currentTime:
proc createDom(): VNode =
result = buildHtml(tdiv):
span:
text fmt"Data: {local(getTime())}"
setRenderer createDom
when false:
import src/karax/autocomplete
const suggestions = @[cstring"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Nim",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"]
var s = newAutocomplete(suggestions)
proc main(): VNode =
result = buildHtml(tdiv):
autocomplete(s, proc (s: cstring) = echo "now ", s):
proc onchange(ev: Event; n: VNode) =
echo "now selected ", n.kind
setRenderer(main)
when false:
setRenderer proc(): VNode =
result = buildHtml(tdiv):
text "Select text:"
input(`type` = "text", value = "Hello world!"):
proc onselect(ev: Event, n: VNode) =
window.alert("You have selected some of the text: ")
br()
text "Select text:"
textarea(cols = "20", rows = "5"):
text "Hello world!"
proc onselect() =
window.alert("You have selected some of the text.")