Skip to content

Commit dfaf351

Browse files
author
Cerem Cem ASLAN
committed
minor improvements
1 parent 1d13419 commit dfaf351

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

components/router/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ Add the `router` component in your webapp:
88

99
# Anchors
1010

11+
Creating anchor:
12+
13+
anchor my-anchor
14+
1115
There are 4 types of anchors:
1216

13-
1. Scroll to an anchor in current page:
17+
1. Scroll in current page:
1418

1519
a(href="#my-anchor") Scroll to my-anchor
1620

17-
2. Switch page in current webapp:
21+
2. Switch page:
1822

1923
a(href="#/other-page") Go to the "other-page"
2024

@@ -26,19 +30,15 @@ There are 4 types of anchors:
2630

2731
a(href="http://example.com") Go to example.com (in new tab, by default)
2832

29-
a(href="http://example.com" curr-window) Go to example.com (in current window)
33+
a(curr-window href="http://example.com") Go to example.com (in current window)
3034
31-
4. Download an asset:
35+
4. Download a static file:
3236

33-
a.icon(href="sgw-example.json" download tooltip="Download the example project")
37+
a.icon(download href="sgw-example.json" tooltip="Download the example project")
3438
icon.download.pink
3539

36-
# Creating in-app anchor target
40+
# Navigating Programmatically
3741

38-
1. Use `anchor` component to create in-app anchor target:
42+
`window.location.hash` is listened by the `router` component.
3943

40-
anchor my-anchor
41-
42-
2. Then create link to scroll to the anchor:
43-
44-
a(href="#my-anchor") Scroll to my-anchor
44+
If the hash is set by any method, `router` will handle the navigation.

components/router/index.ls

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ get-offset = ->
1010
scroll-to = (anchor) ->
1111
offset = $ "span[data-id='#{anchor}']" .offset!
1212
if offset
13-
$ 'html, body' .animate do
14-
scroll-top: (offset?.top or 0) - (window.top-offset or 0)
15-
, 200ms
13+
dist = (offset?.top or 0) - (window.top-offset or 0)
14+
x= $ 'html, body' .animate {scrollTop: dist}, 200ms, ->
15+
#console.log "Animation is completed?"
1616

1717
make-link = (scene, anchor) ->
1818
curr = parse-link get-window-hash!
@@ -108,7 +108,8 @@ Ractive.components['a'] = Ractive.extend do
108108
return
109109
else if link
110110
generated-link = make-link link.scene, link.anchor
111-
#console.log "setting window hash by '<a>' to ", generated-link
111+
if @get \debug
112+
console.log "setting window hash by '<a>' to ", generated-link
112113

113114
# WORKAROUND: jquery.scrollTop! is somehow
114115
# set to 0 when `set-window-hash` is called.
@@ -122,7 +123,7 @@ Ractive.components['a'] = Ractive.extend do
122123

123124
Ractive.components['anchor'] = Ractive.extend do
124125
template: require('./anchor.html')
125-
isolated: yes
126+
isolated: no
126127

127128

128129
Ractive.components['router'] = Ractive.extend do
@@ -194,6 +195,7 @@ Ractive.components['router'] = Ractive.extend do
194195

195196
unless opts.noscroll
196197
sleep 50ms, -> scroll-to curr.anchor
198+
null
197199

198200
actor.send 'app.router.changes', {change}
199201
@set \@shared.router, change
@@ -209,9 +211,10 @@ Ractive.components['router'] = Ractive.extend do
209211
#console.log "fired hash listener! hash is: #{hash}"
210212
handle-hash!
211213

212-
$ window .on \hashchange, ->
214+
window.addEventListener 'hashchange', (->
213215
console.log "this is hashchange run: #{window.location.hash}"
214216
handle-hash!
217+
), false
215218

216219
# on app-load
217220
handle-hash!

0 commit comments

Comments
 (0)