Skip to content

Commit b390105

Browse files
committed
wip versions ui
1 parent 15da289 commit b390105

3 files changed

Lines changed: 69 additions & 11 deletions

File tree

elixir/web.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import datetime
2828
from collections import OrderedDict, namedtuple
2929
from re import search, sub
30-
from typing import Any, Callable, NamedTuple, Tuple
30+
from typing import Any, Callable, NamedTuple, Optional, Tuple
3131
from urllib import parse
3232
import falcon
3333
import jinja2
@@ -107,7 +107,7 @@ def get_project_error_page(req, resp, exception: ElixirProjectError):
107107

108108
versions_raw = get_versions_cached(query, req.context, project)
109109
get_url_with_new_version = lambda v: stringify_source_path(project, v, '/')
110-
versions, current_version_path = get_versions(versions_raw, get_url_with_new_version, version)
110+
versions, current_version_path = get_versions(versions_raw, get_url_with_new_version, None, version)
111111

112112
template_ctx = {
113113
**template_ctx,
@@ -192,6 +192,10 @@ def validate_project_and_version(ctx, project, version):
192192
def get_source_base_url(project: str, version: str) -> str:
193193
return f'/{ parse.quote(project, safe="") }/{ parse.quote(version, safe="") }/source'
194194

195+
def stringify_diff_path(project: str, version: str, version_other: str, path: str) -> str:
196+
return f'/{ parse.quote(project, safe="") }/{ parse.quote(version, safe="") }/diff/' + \
197+
f'{ parse.quote(version_other, safe="") }/{ path }'
198+
195199
# Converts ParsedSourcePath to a string with corresponding URL path
196200
def stringify_source_path(project: str, version: str, path: str) -> str:
197201
if not path.startswith('/'):
@@ -403,7 +407,7 @@ def get_projects(basedir: str) -> list[ProjectEntry]:
403407

404408
# Tuple of version name and URL to chosen resource with that version
405409
# Used to render version list in the sidebar
406-
VersionEntry = namedtuple('VersionEntry', 'version, url')
410+
VersionEntry = namedtuple('VersionEntry', 'version, url, diff_url')
407411

408412
# Takes result of Query.query('version') and prepares it for the sidebar template.
409413
# Returns an OrderedDict with version information and optionally a triple with
@@ -416,6 +420,7 @@ def get_projects(basedir: str) -> list[ProjectEntry]:
416420
# current_version: string with currently browsed version
417421
def get_versions(versions: OrderedDict[str, OrderedDict[str, str]],
418422
get_url: Callable[[str], str],
423+
get_diff_url: Optional[Callable[[str], str]],
419424
current_version: str) -> Tuple[dict[str, dict[str, list[VersionEntry]]], Tuple[str|None, str|None, str|None]]:
420425

421426
result = OrderedDict()
@@ -427,13 +432,14 @@ def get_versions(versions: OrderedDict[str, OrderedDict[str, str]],
427432
result[major] = OrderedDict()
428433
if minor not in result[major]:
429434
result[major][minor] = []
430-
result[major][minor].append(VersionEntry(v, get_url(v)))
435+
result[major][minor].append(
436+
VersionEntry(v, get_url(v), get_diff_url(v) if get_diff_url is not None else None)
437+
)
431438
if v == current_version:
432439
current_version_path = (major, minor, v)
433440

434441
return result, current_version_path
435442

436-
# Caches get_versions result in a context object
437443
def get_versions_cached(q, ctx, project):
438444
with ctx.versions_cache_lock:
439445
if project not in ctx.versions_cache:
@@ -452,9 +458,9 @@ def get_versions_cached(q, ctx, project):
452458
# project: name of the project
453459
# version: version of the project
454460
def get_layout_template_context(q: Query, ctx: RequestContext, get_url_with_new_version: Callable[[str], str],
455-
project: str, version: str) -> dict[str, Any]:
461+
get_diff_url: Optional[Callable[[str], str]], project: str, version: str) -> dict[str, Any]:
456462
versions_raw = get_versions_cached(q, ctx, project)
457-
versions, current_version_path = get_versions(versions_raw, get_url_with_new_version, version)
463+
versions, current_version_path = get_versions(versions_raw, get_url_with_new_version, get_diff_url, version)
458464

459465
return {
460466
'projects': get_projects(ctx.config.project_dir),
@@ -731,14 +737,16 @@ def generate_source_page(ctx: RequestContext, q: Query,
731737
title_path = f'{ path_split[-1] } - { "/".join(path_split) } - '
732738

733739
get_url_with_new_version = lambda v: stringify_source_path(project, v, path)
740+
get_diff_url = lambda v_other: stringify_diff_path(project, version, v_other, path)
734741

735742
# Create template context
736743
data = {
737-
**get_layout_template_context(q, ctx, get_url_with_new_version, project, version),
744+
**get_layout_template_context(q, ctx, get_url_with_new_version, get_diff_url, project, version),
738745

739746
'title_path': title_path,
740747
'path': path,
741748
'breadcrumb_links': breadcrumb_links,
749+
'diff_mode_available': True,
742750

743751
**template_ctx,
744752
}
@@ -788,19 +796,23 @@ def generate_diff_page(ctx: RequestContext, q: Query,
788796
title_path = f'{ path_split[-1] } - { "/".join(path_split) } - '
789797

790798
get_url_with_new_version = lambda v: stringify_source_path(project, v, path)
799+
get_diff_url = lambda v_other: stringify_diff_path(project, version, v_other, path)
791800

792801
template = ctx.jinja_env.get_template('diff.html')
793802

794803
code, code_other = generate_diff(q, project, version, version_other, path)
795804
# Create template context
796805
data = {
797-
**get_layout_template_context(q, ctx, get_url_with_new_version, project, version),
806+
**get_layout_template_context(q, ctx, get_url_with_new_version, get_diff_url, project, version),
798807

799808
'code': code,
800809
'code_other': code_other,
801810
'title_path': title_path,
802811
'path': path,
803812
'breadcrumb_links': breadcrumb_links,
813+
'diff_mode_available': True,
814+
'diff_checked': True,
815+
'diff_exit_url': stringify_source_path(project, version, path),
804816
}
805817

806818
return (status, template.render(data))
@@ -883,7 +895,7 @@ def generate_ident_page(ctx: RequestContext, q: Query,
883895
get_url_with_new_version = lambda v: stringify_ident_path(project, v, family, ident)
884896

885897
data = {
886-
**get_layout_template_context(q, ctx, get_url_with_new_version, project, version),
898+
**get_layout_template_context(q, ctx, get_url_with_new_version, None, project, version),
887899

888900
'searched_ident': ident,
889901
'current_family': family,

static/style.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,38 @@ h2 {
462462
padding: 0;
463463
}
464464

465+
#diff-checkbox-label {
466+
color: #888;
467+
}
468+
469+
#diff-exit {
470+
color: #888;
471+
padding-left: 1em;
472+
}
473+
474+
#diff-exit:hover {
475+
color: #eee;
476+
color: #6d7dd2;
477+
font-weight: 700;
478+
}
479+
480+
#diff-checkbox {
481+
padding: 1em;
482+
margin-bottom: 0.5em;
483+
}
484+
485+
#diff-checkbox:checked ~ li .version-link-source {
486+
display: none;
487+
}
488+
489+
.version-link-diff {
490+
display: none;
491+
}
492+
493+
#diff-checkbox:checked ~ li .version-link-diff {
494+
display: inline;
495+
}
496+
465497
/* reference popup */
466498

467499
#reference-popup-wrapper {

templates/sidebar.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ <h3 class="screenreader">Projects</h3>
1919

2020
<h3 class="screenreader">Versions</h3>
2121
<ul class="versions">
22+
{% if diff_mode_available %}
23+
<input id="diff-checkbox" type="checkbox" {% if diff_checked %} checked {% endif %} />
24+
<label id="diff-checkbox-label" for="diff-checkbox">Diff mode</label>
25+
{% if diff_exit_url %}
26+
<a id="diff-exit" href="{{ diff_exit_url }}">Exit diff mode</a>
27+
{% endif %}
28+
{% endif %}
29+
2230
{% set current_major, current_minor, current_version = current_version_path %}
2331
{% for major, major_versions in (versions|default({})).items() %}
2432
<li>
2533
<span class="{{ 'active' if current_major == major else '' }}">{{ major }}</span>
34+
2635
<ul>
2736
{% for minor, minor_versions in major_versions.items() %}
2837
{% if minor == minor_versions[0] and minor_versions|length == 1 %}
@@ -35,9 +44,14 @@ <h3 class="screenreader">Versions</h3>
3544
<ul>
3645
{% for v in minor_versions %}
3746
<li class="li-link {{ 'active' if v.version == current_tag else '' }}">
38-
<a href="{{ v.url }}">
47+
<a class="version-link-source" href="{{ v.url }}">
3948
{{ v.version }}
4049
</a>
50+
{% if v.diff_url is not none %}
51+
<a class="version-link-diff" href="{{ v.diff_url }}">
52+
{{ v.version }}
53+
</a>
54+
{% endif %}
4155
</li>
4256
{% endfor %}
4357
</ul>

0 commit comments

Comments
 (0)