Implements anchor awareness.

This commit is contained in:
Dominik Picheta 2018-05-15 22:13:53 +01:00
commit bc104ff41d
4 changed files with 107 additions and 79 deletions

View file

@ -58,7 +58,14 @@ proc render(): VNode =
), ),
r("/t/@id", r("/t/@id",
(params: Params) => (params: Params) =>
(renderPostList(params["id"].parseInt(), isLoggedIn())) (
let postId = getInt(($state.url.hash).substr(1), 0);
renderPostList(
params["id"].parseInt(),
if postId == 0: none[int]() else: some[int](postId),
isLoggedIn()
)
)
), ),
r("/", (params: Params) => renderThreadList()) r("/", (params: Params) => renderThreadList())
]) ])

View file

@ -1,6 +1,25 @@
import strutils, options, strformat import strutils, options, strformat, parseutils
import dom except window import dom except window
proc parseInt*(s: string, value: var int, validRange: Slice[int]) {.
noSideEffect.} =
## parses `s` into an integer in the range `validRange`. If successful,
## `value` is modified to contain the result. Otherwise no exception is
## raised and `value` is not touched; this way a reasonable default value
## won't be overwritten.
var x = value
try:
discard parseutils.parseInt(s, x, 0)
except OverflowError:
discard
if x in validRange: value = x
proc getInt*(s: string, default = 0): int =
## Safely parses an int and returns it.
result = default
parseInt(s, result, 0..1_000_000_000)
when defined(js):
include karax/prelude include karax/prelude
import karax / [kdom] import karax / [kdom]

View file

@ -12,6 +12,8 @@ type
posts*: seq[Post] posts*: seq[Post]
when defined(js): when defined(js):
from dom import nil
include karax/prelude include karax/prelude
import karax / [vstyles, kajax, kdom] import karax / [vstyles, kajax, kdom]
@ -38,7 +40,7 @@ when defined(js):
var var
state = newState() state = newState()
proc onPostList(httpStatus: int, response: kstring) = proc onPostList(httpStatus: int, response: kstring, postId: Option[int]) =
state.loading = false state.loading = false
state.status = httpStatus.HttpCode state.status = httpStatus.HttpCode
if state.status != Http200: return if state.status != Http200: return
@ -48,6 +50,18 @@ when defined(js):
state.list = some(list) state.list = some(list)
# The anchor should be jumped to once all the posts have been loaded.
if postId.isSome():
discard setTimeout(
() => (
# Would have used scrollIntoView but then the `:target` selector
# isn't activated.
window.location.hash = "";
window.location.hash = "#" & $postId.get()
),
100
)
proc onMorePosts(httpStatus: int, response: kstring, start: int) = proc onMorePosts(httpStatus: int, response: kstring, start: int) =
state.loading = false state.loading = false
state.status = httpStatus.HttpCode state.status = httpStatus.HttpCode
@ -190,13 +204,17 @@ when defined(js):
tdiv(class="information-title"): tdiv(class="information-title"):
text diffStr text diffStr
proc renderPostList*(threadId: int, isLoggedIn: bool): VNode = proc renderPostList*(threadId: int, postId: Option[int],
isLoggedIn: bool): VNode =
if state.status != Http200: if state.status != Http200:
return renderError("Couldn't retrieve posts.") return renderError("Couldn't retrieve posts.")
if state.list.isNone or state.list.get().thread.id != threadId: if state.list.isNone or state.list.get().thread.id != threadId:
let uri = makeUri("posts.json", ("id", $threadId)) var params = @[("id", $threadId)]
ajaxGet(uri, @[], (s: int, r: kstring) => onPostList(s, r)) if postId.isSome():
params.add(("anchor", $postId.get()))
let uri = makeUri("posts.json", params)
ajaxGet(uri, @[], (s: int, r: kstring) => onPostList(s, r, postId))
return buildHtml(tdiv(class="loading loading-lg")) return buildHtml(tdiv(class="loading loading-lg"))

View file

@ -7,24 +7,8 @@ from times import getTime, getGMTime, format
let let
UsernameIdent* = IdentChars # TODO: Double check that everyone follows this. UsernameIdent* = IdentChars # TODO: Double check that everyone follows this.
import redesign/karaxutils
proc parseInt*(s: string, value: var int, validRange: Slice[int]) {. export parseInt
noSideEffect.} =
## parses `s` into an integer in the range `validRange`. If successful,
## `value` is modified to contain the result. Otherwise no exception is
## raised and `value` is not touched; this way a reasonable default value
## won't be overwritten.
var x = value
try:
discard parseutils.parseInt(s, x, 0)
except OverflowError:
discard
if x in validRange: value = x
proc getInt*(s: string, default = 0): int =
## Safely parses an int and returns it.
result = default
parseInt(s, result, 0..1_000_000_000)
proc `%`*[T](opt: Option[T]): JsonNode = proc `%`*[T](opt: Option[T]): JsonNode =
## Generic constructor for JSON data. Creates a new ``JNull JsonNode`` ## Generic constructor for JSON data. Creates a new ``JNull JsonNode``