Skip to content

Commit

Permalink
Merge pull request #177 from lona-web-org/fscherf/bugfixes
Browse files Browse the repository at this point in the history
small bugfixes
  • Loading branch information
fscherf authored Nov 5, 2021
2 parents 3bd54e4 + 1545535 commit 63ff0c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lona/client/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Lona.LonaContext = function(settings) {
this.settings.follow_http_redirects = true;
};

if(typeof(this.settings.scroll_to_top_on_view_start) == 'undefined') {
this.settings.scroll_to_top_on_view_start = true;
};

// state ------------------------------------------------------------------
this._windows = {};
this._connect_hooks = [];
Expand Down
9 changes: 9 additions & 0 deletions lona/client/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,15 @@ Lona.LonaWindow = function(lona_context, root, window_id) {
this._clear();
};

// scroll to top
// If the a new view gets started with the page scrolled down, and
// the page has a fixed height set in css, for example the min-height
// of the body is set to 100%, the new view starts scrolled to the
// bottom, which is counterintuitive to end users.
if(this.lona_context.settings.scroll_to_top_on_view_start) {
window.scrollTo(0, 0);
};

// encode message
var message = [
this._window_id,
Expand Down
23 changes: 11 additions & 12 deletions lona/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,26 @@ def __init__(self, raw_pattern, view, name='', interactive=True,

# string or regex
else:
if self.raw_pattern.endswith(OPTIONAL_TRAILING_SLASH_PATTERN):
raw_pattern = self.raw_pattern

if raw_pattern.endswith(OPTIONAL_TRAILING_SLASH_PATTERN):
self.optional_trailing_slash = True

groups = ABSTRACT_ROUTE_RE.findall(self.raw_pattern)
raw_pattern = \
raw_pattern[:-len(OPTIONAL_TRAILING_SLASH_PATTERN)]

groups = ABSTRACT_ROUTE_RE.findall(raw_pattern)

# path is no pattern but simple string
if not groups:
self.path = self.raw_pattern
self.format_string = self.raw_pattern

if self.optional_trailing_slash:
suffix_len = len(OPTIONAL_TRAILING_SLASH_PATTERN) * -1

self.path = self.path[:suffix_len]
self.format_string = self.path[:suffix_len]
self.path = raw_pattern
self.format_string = raw_pattern

return

pattern_names = [i[0] for i in groups]
patterns = [(i[0], i[2] or DEFAULT_PATTERN) for i in groups]
cleaned_pattern = ABSTRACT_ROUTE_RE.sub('{}', self.raw_pattern)
cleaned_pattern = ABSTRACT_ROUTE_RE.sub('{}', raw_pattern)

# setup format string
self.format_string = cleaned_pattern.format(
Expand All @@ -68,7 +67,7 @@ def __init__(self, raw_pattern, view, name='', interactive=True,
*[ROUTE_PART_FORMAT_STRING.format(*i)
for i in patterns],
),
(OPTIONAL_TRAILING_SLASH_PATTERN
(r'(/)?'
if self.optional_trailing_slash else ''),
),
)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def setup(self):
self.routes = [
Route('/foo(/)', None),
Route('/bar/', None),
Route('/foobar/<arg>(/)', None),
]

self.router = Router()
Expand All @@ -98,6 +99,20 @@ def test_optional_slash_resolves_with_slash(self):
assert route == self.routes[0]
assert match_info == {}

def test_optional_slash_resolves_without_slash_and_argument(self):
match, route, match_info = self.router.resolve('/foobar/foobar')

assert match
assert route == self.routes[2]
assert match_info == {'arg': 'foobar'}

def test_optional_slash_resolves_with_slash_and_argument(self):
match, route, match_info = self.router.resolve('/foobar/foobar/')

assert match
assert route == self.routes[2]
assert match_info == {'arg': 'foobar'}

def test_required_slash_doesnt_resolve_without_slash(self):
match, route, match_info = self.router.resolve('/bar')

Expand All @@ -117,6 +132,7 @@ def setup(self):
routes = [
Route('/foo/<arg>/', None, name='foo'),
Route('/bar/', None, name='bar'),
Route('/foobar/<arg>(/)', None, name='foobar'),
]
self.router = Router()
self.router.add_routes(*routes)
Expand All @@ -126,6 +142,11 @@ def test_reverse_with_arg(self):

assert url == '/foo/bar/'

def test_reverse_with_arg_and_optional_slash(self):
url = self.router.reverse('foobar', arg='foobar')

assert url == '/foobar/foobar'

def test_reverse_without_arg(self):
url = self.router.reverse('bar')

Expand Down

0 comments on commit 63ff0c0

Please sign in to comment.