mirror of
https://github.com/daylinmorgan/nimpkgs.git
synced 2024-12-21 18:50:43 -06:00
kcall
This commit is contained in:
parent
77dbe8af05
commit
00e38fe2a5
3 changed files with 63 additions and 33 deletions
84
src/lib.nim
84
src/lib.nim
|
@ -1,4 +1,4 @@
|
||||||
import std/[strutils, uri]
|
import std/[strutils, uri, macros]
|
||||||
import std/jsffi except `&`
|
import std/jsffi except `&`
|
||||||
import jsconsole
|
import jsconsole
|
||||||
export jsconsole
|
export jsconsole
|
||||||
|
@ -20,26 +20,60 @@ proc currentUri*(): Uri {.inline.} =
|
||||||
func replace*(c: kstring, sub: string, by = " "): kstring =
|
func replace*(c: kstring, sub: string, by = " "): kstring =
|
||||||
($c).replace(sub, by).jss
|
($c).replace(sub, by).jss
|
||||||
|
|
||||||
|
macro kcall*(p: typed) =
|
||||||
|
## make procedure return another procedure that takes no arguments
|
||||||
|
##
|
||||||
|
## used for generating more succint callbacks compatible with karax
|
||||||
|
runnableExamples:
|
||||||
|
proc example(a: string) {.kcall.} =
|
||||||
|
echo a
|
||||||
|
example("hello world")()
|
||||||
|
|
||||||
|
expectKind p, nnkProcDef
|
||||||
|
if p.params[0].kind != nnkEmpty:
|
||||||
|
error "proc must return void"
|
||||||
|
var updated = copy(p)
|
||||||
|
let returnType = nnkProcTy.newTree(nnkFormalParams.newTree(newEmptyNode()), newEmptyNode())
|
||||||
|
updated.params = nnkFormalParams
|
||||||
|
.newTree(returnType)
|
||||||
|
.add(p.params[1 ..^ 1])
|
||||||
|
updated.body = nnkStmtList.newTree(
|
||||||
|
nnkLambda.newTree(
|
||||||
|
newEmptyNode(),
|
||||||
|
newEmptyNode(),
|
||||||
|
newEmptyNode(),
|
||||||
|
nnkFormalParams.newTree(newEmptyNode()),
|
||||||
|
newEmptyNode(),
|
||||||
|
newEmptyNode(),
|
||||||
|
p.body,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
result = nnkStmtList.newTree()
|
||||||
|
result.add updated
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
SortMethod* = enum
|
SortMethod* = enum
|
||||||
smAlphabetical = "smAlphabetical", smCommitAge = "commit", smVersionAge = "version"
|
smAlphabetical = "smAlphabetical", smCommitAge = "commit", smVersionAge = "version"
|
||||||
|
|
||||||
proc setSearchUrl*(searchQuery: kstring, sortMethod = smAlphabetical): proc() =
|
proc setSearchUrl*(
|
||||||
proc() =
|
searchQuery: kstring,
|
||||||
var
|
sortMethod = smAlphabetical
|
||||||
url = currentUri()
|
) {.kcall.} =
|
||||||
params: seq[(string, string)]
|
var
|
||||||
if searchQuery != "":
|
url = currentUri()
|
||||||
params.add ("query", $searchQuery)
|
params: seq[(string, string)]
|
||||||
if sortMethod != smAlphabetical:
|
if searchQuery != "":
|
||||||
params.add ("sort", $sortMethod)
|
params.add ("query", $searchQuery)
|
||||||
url.anchor = "/search"
|
if sortMethod != smAlphabetical:
|
||||||
url = url ? params
|
params.add ("sort", $sortMethod)
|
||||||
window.history.pushState(js{}, "".jss, url.jss)
|
url.anchor = "/search"
|
||||||
let d = getVNodeById("search")
|
url = url ? params
|
||||||
let node = d.dom
|
window.history.pushState(js{}, "".jss, url.jss)
|
||||||
scrollIntoView(node)
|
let d = getVNodeById("search")
|
||||||
redraw()
|
let node = d.dom
|
||||||
|
scrollIntoView(node)
|
||||||
|
redraw()
|
||||||
|
|
||||||
proc getSearchInput*() =
|
proc getSearchInput*() =
|
||||||
let searchInput = getVNodeById("search").getInputText
|
let searchInput = getVNodeById("search").getInputText
|
||||||
|
@ -50,15 +84,13 @@ proc getSearchInput*() =
|
||||||
)
|
)
|
||||||
setSearchUrl(searchInput, sortMethod)()
|
setSearchUrl(searchInput, sortMethod)()
|
||||||
|
|
||||||
|
proc setSearchInput*(q: kstring) {.kcall.} =
|
||||||
proc setSearchInput*(q: kstring): proc() =
|
let sortNode = getVNodeById("sort-select")
|
||||||
result = proc() =
|
let sortMethod = SortMethod(
|
||||||
let sortNode = getVNodeById("sort-select")
|
if sortNode != nil: parseInt($sortNode.getInputText)
|
||||||
let sortMethod = SortMethod(
|
else: 0
|
||||||
if sortNode != nil: parseInt($sortNode.getInputText)
|
)
|
||||||
else: 0
|
setSearchUrl(q, sortMethod)()
|
||||||
)
|
|
||||||
setSearchUrl(q, sortMethod)()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,8 @@ import ../components/[tag, package]
|
||||||
import ../lib
|
import ../lib
|
||||||
import notfound
|
import notfound
|
||||||
|
|
||||||
proc openLink(link: kstring): proc() =
|
proc openLink(link: kstring) {.kcall.} =
|
||||||
result = proc() =
|
discard open(window, link, "_self")
|
||||||
discard open(window, link, "_self")
|
|
||||||
|
|
||||||
proc versionTable(pkg: NimPackage): VNode =
|
proc versionTable(pkg: NimPackage): VNode =
|
||||||
var versions = pkg.versions
|
var versions = pkg.versions
|
||||||
|
|
|
@ -17,10 +17,9 @@ type
|
||||||
|
|
||||||
var pgCtx = PageContext()
|
var pgCtx = PageContext()
|
||||||
|
|
||||||
proc scrollToAnchor(a: string): proc() =
|
proc scrollToAnchor(a: string){.kcall.} =
|
||||||
result = proc() =
|
let d = getVNodeById(a)
|
||||||
let d = getVNodeById(a)
|
scrollIntoView(d.dom)
|
||||||
scrollIntoView(d.dom)
|
|
||||||
|
|
||||||
proc letterlink(activeLinks: seq[char]): VNode = buildHtml:
|
proc letterlink(activeLinks: seq[char]): VNode = buildHtml:
|
||||||
tdiv(
|
tdiv(
|
||||||
|
|
Loading…
Reference in a new issue