Skip to content

Commit

Permalink
chore: refactor to use newer frontend extenders
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Apr 29, 2024
1 parent 71723ce commit e5dabbc
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 49 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"flarum/core": "^1.2.0"
"flarum/core": "^1.8.3"
},
"authors": [
{
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion js/src/admin/components/LinksPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from 'flarum/admin/app';

import ExtensionPage from 'flarum/common/components/ExtensionPage';
import ExtensionPage from 'flarum/admin/components/ExtensionPage';
import Button from 'flarum/common/components/Button';
import icon from 'flarum/common/helpers/icon';

Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import commonExtend from '../common/extend';

export default [...commonExtend];
6 changes: 2 additions & 4 deletions js/src/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import app from 'flarum/admin/app';

import Link from '../common/models/Link';
import LinksPage from './components/LinksPage';

export * from './components';
export * from '../common/utils';
export * from '../common/models';

app.initializers.add('fof-links', () => {
app.store.models.links = Link;
export { default as extend } from './extend';

app.initializers.add('fof-links', () => {
app.extensionData
.for('fof-links')
.registerPage(LinksPage)
Expand Down
7 changes: 7 additions & 0 deletions js/src/common/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Extend from 'flarum/common/extenders';
import Link from './models/Link';

export default [
new Extend.Store() //
.add('links', Link),
];
16 changes: 0 additions & 16 deletions js/src/common/models/Link.js

This file was deleted.

47 changes: 47 additions & 0 deletions js/src/common/models/Link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Model from 'flarum/common/Model';

export default class Link extends Model {
title() {
return Model.attribute<string>('title').call(this);
}

icon() {
return Model.attribute<string>('icon').call(this);
}

type() {
return Model.attribute<string>('type').call(this);
}

url() {
return Model.attribute<string>('url').call(this);
}

position() {
return Model.attribute<number | null | undefined>('position').call(this);
}

isInternal() {
return Model.attribute<boolean>('isInternal').call(this);
}

isNewtab() {
return Model.attribute<boolean>('isNewtab').call(this);
}

useRelMe() {
return Model.attribute<boolean>('useRelMe').call(this);
}

isChild() {
return Model.attribute<boolean>('isChild').call(this);
}

parent() {
return Model.hasOne<Link>('parent').call(this);
}

visibility() {
return Model.attribute<string>('visibility').call(this);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions js/src/forum/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import commonExtend from '../common/extend';

export default [...commonExtend];
22 changes: 22 additions & 0 deletions js/src/forum/extendHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import HeaderPrimary from 'flarum/forum/components/HeaderPrimary';
import ItemList from 'flarum/common/utils/ItemList';
import sortLinks from '../common/utils/sortLinks';
import type Mithril from 'mithril';
import Link from '../common/models/Link';
import LinkItem from './components/LinkItem';
import LinkDropdown from './components/LinkDropdown';

export default function extendHeader() {
extend(HeaderPrimary.prototype, 'items', function (items: ItemList<Mithril.Children>) {
const links = app.store.all<Link>('links').filter((link) => !link.isChild());
const addLink = (parent: Link | null | undefined) => {
const hasChildren = app.store.all<Link>('links').some((link) => link.parent() == parent);

items.add(`link${parent?.id()}`, hasChildren ? LinkDropdown.component({ link: parent }) : LinkItem.component({ link: parent }));
};

sortLinks(links).map(addLink);
});
}
27 changes: 0 additions & 27 deletions js/src/forum/index.js

This file was deleted.

12 changes: 12 additions & 0 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import app from 'flarum/forum/app';
import extendHeader from './extendHeader';

export * from './components';
export * from '../common/utils';
export * from '../common/models';

export { default as extend } from './extend';

app.initializers.add('fof-links', () => {
extendHeader();
});

0 comments on commit e5dabbc

Please sign in to comment.