Skip to content

Commit

Permalink
fix: links
Browse files Browse the repository at this point in the history
chore: proper router config
  • Loading branch information
YUCLing committed Dec 7, 2024
1 parent 65b66e2 commit f839449
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion framework/core/js/src/admin/AdminApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default class AdminApplication extends Application {
// we need to go to https://example.com/admin#/ explicitly.
if (!document.location.hash) document.location.hash = '#/';

super.mount('#');
super.mount('', '#');

m.mount(document.getElementById('app-navigation')!, () => <Navigation className="App-backControl" drawer />);
m.mount(document.getElementById('header-navigation')!, () => <Navigation />);
Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/common/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export default class Application {
});
}

protected mount(routePrefix = '/', basePath: string = '') {
protected mount(basePath: string = '', routePrefix: string = '') {
// An object with a callable view property is used in order to pass arguments to the component; see https://mithril.js.org/mount.html
this.redraw.modal = m.mount(document.getElementById('modal')!, () => <ModalManager state={this.modal} />);
this.redraw.alert = m.mount(document.getElementById('alerts')!, () => <AlertManager state={this.alerts} />);
Expand Down
42 changes: 37 additions & 5 deletions framework/core/js/src/common/utils/patchMithril.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ function Trust(attrs) {
];
}

// from mithril, modified
const Link = () => {
var href, opts, setRoute
var listener = (ev) => {
if (
!ev.defaultPrevented &&
(ev.button === 0 || ev.which === 0 || ev.which === 1) &&
(!ev.currentTarget.target || ev.currentTarget.target === "_self") &&
!ev.ctrlKey && !ev.metaKey && !ev.shiftKey && !ev.altKey
) {
setRoute(href, opts)
return m.capture(ev)
}
}

return function (attrs, old) {
setRoute = app.routing.set
href = attrs.h
opts = attrs.o
return [
m.layout((dom) => {
dom.href = app.routing.prefix + href
if (!old) dom.addEventListener("click", listener)
}),
m.remove((dom) => {
dom.removeEventListener("click", listener)
}),
]
}
}

function getOrCreateComponentFunc(comp) {
let func = componentFuncs.get(comp);
if (!func) {
Expand All @@ -131,12 +162,13 @@ export default function patchMithril(global) {

if (comp === '__LINK__') {
const attrs = args[0];
const className = attrs.className ?? attrs.class ?? '';
const classes = className != '' ? className.split(' ') : [];
return defaultMithril(
'a' + classes.map((v) => '.' + v).join(''),
// this doesn't work since other roots won't have a router.
/*defaultMithril.link(attrs.href, attrs.options)*/ { ...attrs, className: undefined, class: undefined },
'a',
{
className: attrs.className,
class: attrs.class
},
m(Link, {h: `${attrs.href}`, o: attrs.options}),
args.slice(1)
);
}
Expand Down

0 comments on commit f839449

Please sign in to comment.