Browse Source

add ability to set a page as default home page

master
Sajjad Hashemian 2 years ago
parent
commit
2329b27bd0

+ 1
- 0
bootstrap.php View File

@@ -9,4 +9,5 @@ return function (Dispatcher $events) {
$events->subscribe(Listener\AddClientAssets::class);
$events->subscribe(Listener\AddPagesRoute::class);
$events->subscribe(Listener\AddPagesApi::class);
$events->subscribe(Listener\AddApiAttributes::class);
};

+ 21
- 7
js/admin/dist/extension.js View File

@@ -1,6 +1,8 @@
'use strict';

System.register('sijad/pages/addPagesPane', ['flarum/extend', 'flarum/components/AdminNav', 'flarum/components/AdminLinkButton', 'sijad/pages/components/PagesPage'], function (_export, _context) {
"use strict";

var extend, AdminNav, AdminLinkButton, PagesPage;

_export('default', function () {
@@ -36,6 +38,8 @@ System.register('sijad/pages/addPagesPane', ['flarum/extend', 'flarum/components
'use strict';

System.register('sijad/pages/components/EditPageModal', ['flarum/components/Modal', 'flarum/components/Button', 'flarum/utils/string'], function (_export, _context) {
"use strict";

var Modal, Button, slug, EditPageModal;
return {
setters: [function (_flarumComponentsModal) {
@@ -51,13 +55,13 @@ System.register('sijad/pages/components/EditPageModal', ['flarum/components/Moda

function EditPageModal() {
babelHelpers.classCallCheck(this, EditPageModal);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(EditPageModal).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (EditPageModal.__proto__ || Object.getPrototypeOf(EditPageModal)).apply(this, arguments));
}

babelHelpers.createClass(EditPageModal, [{
key: 'init',
value: function init() {
babelHelpers.get(Object.getPrototypeOf(EditPageModal.prototype), 'init', this).call(this);
babelHelpers.get(EditPageModal.prototype.__proto__ || Object.getPrototypeOf(EditPageModal.prototype), 'init', this).call(this);

this.page = this.props.page || app.store.createRecord('pages');

@@ -217,6 +221,8 @@ System.register('sijad/pages/components/EditPageModal', ['flarum/components/Moda
'use strict';

System.register('sijad/pages/components/PagesList', ['flarum/Component', 'flarum/components/LoadingIndicator', 'flarum/components/Placeholder', 'flarum/components/Button', 'sijad/pages/components/PagesListItem'], function (_export, _context) {
"use strict";

var Component, LoadingIndicator, Placeholder, Button, PagesListItem, PagesList;
return {
setters: [function (_flarumComponent) {
@@ -236,7 +242,7 @@ System.register('sijad/pages/components/PagesList', ['flarum/Component', 'flarum

function PagesList() {
babelHelpers.classCallCheck(this, PagesList);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(PagesList).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (PagesList.__proto__ || Object.getPrototypeOf(PagesList)).apply(this, arguments));
}

babelHelpers.createClass(PagesList, [{
@@ -346,7 +352,7 @@ System.register('sijad/pages/components/PagesList', ['flarum/Component', 'flarum
}, {
key: 'refresh',
value: function refresh() {
var clear = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
var clear = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

if (clear) {
this.loading = true;
@@ -404,6 +410,8 @@ System.register('sijad/pages/components/PagesList', ['flarum/Component', 'flarum
'use strict';

System.register('sijad/pages/components/PagesListItem', ['flarum/Component', 'flarum/components/Button', 'sijad/pages/components/EditPageModal'], function (_export, _context) {
"use strict";

var Component, Button, EditPageModal, PagesListItem;
return {
setters: [function (_flarumComponent) {
@@ -419,7 +427,7 @@ System.register('sijad/pages/components/PagesListItem', ['flarum/Component', 'fl

function PagesListItem() {
babelHelpers.classCallCheck(this, PagesListItem);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(PagesListItem).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (PagesListItem.__proto__ || Object.getPrototypeOf(PagesListItem)).apply(this, arguments));
}

babelHelpers.createClass(PagesListItem, [{
@@ -484,6 +492,8 @@ System.register('sijad/pages/components/PagesListItem', ['flarum/Component', 'fl
'use strict';

System.register('sijad/pages/components/PagesPage', ['flarum/components/Page', 'flarum/components/Button', 'flarum/components/LoadingIndicator', 'sijad/pages/components/EditPageModal', 'sijad/pages/components/PagesList'], function (_export, _context) {
"use strict";

var Page, Button, LoadingIndicator, EditPageModal, PagesList, PagesPage;
return {
setters: [function (_flarumComponentsPage) {
@@ -503,7 +513,7 @@ System.register('sijad/pages/components/PagesPage', ['flarum/components/Page', '

function PagesPage() {
babelHelpers.classCallCheck(this, PagesPage);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(PagesPage).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (PagesPage.__proto__ || Object.getPrototypeOf(PagesPage)).apply(this, arguments));
}

babelHelpers.createClass(PagesPage, [{
@@ -555,6 +565,8 @@ System.register('sijad/pages/components/PagesPage', ['flarum/components/Page', '
'use strict';

System.register('sijad/pages/main', ['flarum/extend', 'sijad/pages/models/Page', 'sijad/pages/addPagesPane'], function (_export, _context) {
"use strict";

var extend, Page, addPagesPane;
return {
setters: [function (_flarumExtend) {
@@ -576,6 +588,8 @@ System.register('sijad/pages/main', ['flarum/extend', 'sijad/pages/models/Page',
'use strict';

System.register('sijad/pages/models/Page', ['flarum/Model', 'flarum/utils/mixin', 'flarum/utils/computed', 'flarum/utils/string'], function (_export, _context) {
"use strict";

var Model, mixin, computed, getPlainContent, Page;
return {
setters: [function (_flarumModel) {
@@ -593,7 +607,7 @@ System.register('sijad/pages/models/Page', ['flarum/Model', 'flarum/utils/mixin'

function Page() {
babelHelpers.classCallCheck(this, Page);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Page).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (Page.__proto__ || Object.getPrototypeOf(Page)).apply(this, arguments));
}

return Page;

+ 104
- 17
js/forum/dist/extension.js View File

@@ -1,6 +1,68 @@
'use strict';

System.register('sijad/pages/components/HomePage', ['flarum/components/IndexPage', 'flarum/components/LoadingIndicator', 'flarum/components/Page', 'flarum/helpers/icon', 'sijad/pages/components/PagePage'], function (_export, _context) {
"use strict";

var IndexPage, LoadingIndicator, Page, icon, PagePage, HomePage;
return {
setters: [function (_flarumComponentsIndexPage) {
IndexPage = _flarumComponentsIndexPage.default;
}, function (_flarumComponentsLoadingIndicator) {
LoadingIndicator = _flarumComponentsLoadingIndicator.default;
}, function (_flarumComponentsPage) {
Page = _flarumComponentsPage.default;
}, function (_flarumHelpersIcon) {
icon = _flarumHelpersIcon.default;
}, function (_sijadPagesComponentsPagePage) {
PagePage = _sijadPagesComponentsPagePage.default;
}],
execute: function () {
HomePage = function (_PagePage) {
babelHelpers.inherits(HomePage, _PagePage);

function HomePage() {
babelHelpers.classCallCheck(this, HomePage);
return babelHelpers.possibleConstructorReturn(this, (HomePage.__proto__ || Object.getPrototypeOf(HomePage)).apply(this, arguments));
}

babelHelpers.createClass(HomePage, [{
key: 'init',
value: function init() {
babelHelpers.get(HomePage.prototype.__proto__ || Object.getPrototypeOf(HomePage.prototype), 'init', this).call(this);

app.history.push('homePage', icon('home'));
app.drawer.hide();
app.modal.close();
}
}, {
key: 'show',
value: function show(page) {
this.page = page;
m.redraw();
}
}, {
key: 'hero',
value: function hero() {
return IndexPage.prototype.hero();
}
}, {
key: 'id',
value: function id() {
return app.forum.attribute('pagesHome');
}
}]);
return HomePage;
}(PagePage);

_export('default', HomePage);
}
};
});;
'use strict';

System.register('sijad/pages/components/PageHero', ['flarum/Component', 'flarum/utils/ItemList', 'flarum/helpers/listItems'], function (_export, _context) {
"use strict";

var Component, ItemList, listItems, PageHero;
return {
setters: [function (_flarumComponent) {
@@ -16,7 +78,7 @@ System.register('sijad/pages/components/PageHero', ['flarum/Component', 'flarum/

function PageHero() {
babelHelpers.classCallCheck(this, PageHero);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(PageHero).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (PageHero.__proto__ || Object.getPrototypeOf(PageHero)).apply(this, arguments));
}

babelHelpers.createClass(PageHero, [{
@@ -65,6 +127,8 @@ System.register('sijad/pages/components/PageHero', ['flarum/Component', 'flarum/
'use strict';

System.register('sijad/pages/components/PagePage', ['flarum/components/Page', 'flarum/components/LoadingIndicator', 'sijad/pages/components/PageHero'], function (_export, _context) {
"use strict";

var Page, LoadingIndicator, PageHero, PagePage;
return {
setters: [function (_flarumComponentsPage) {
@@ -80,13 +144,13 @@ System.register('sijad/pages/components/PagePage', ['flarum/components/Page', 'f

function PagePage() {
babelHelpers.classCallCheck(this, PagePage);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(PagePage).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (PagePage.__proto__ || Object.getPrototypeOf(PagePage)).apply(this, arguments));
}

babelHelpers.createClass(PagePage, [{
key: 'init',
value: function init() {
babelHelpers.get(Object.getPrototypeOf(PagePage.prototype), 'init', this).call(this);
babelHelpers.get(PagePage.prototype.__proto__ || Object.getPrototypeOf(PagePage.prototype), 'init', this).call(this);

/**
* The page that is being viewed.
@@ -97,8 +161,6 @@ System.register('sijad/pages/components/PagePage', ['flarum/components/Page', 'f

this.loadPage();

app.history.push('page');

this.bodyClass = 'App--page';
}
}, {
@@ -112,13 +174,13 @@ System.register('sijad/pages/components/PagePage', ['flarum/components/Page', 'f
m(
'div',
{ className: 'Pages-page' },
page ? [PageHero.component({ page: page }), m(
page ? [this.hero(), m(
'div',
{ className: 'container' },
{ className: 'Pages-container container' },
m(
'div',
{ className: 'Post-body' },
m.trust(page.contentHtml())
this.content()
)
)] : LoadingIndicator.component({ className: 'LoadingIndicator--block' })
)
@@ -135,13 +197,30 @@ System.register('sijad/pages/components/PagePage', ['flarum/components/Page', 'f
m.redraw();
}
}, {
key: 'hero',
value: function hero() {
return PageHero.component({ page: this.page });
}
}, {
key: 'content',
value: function content() {
return m.trust(this.page.contentHtml());
}
}, {
key: 'id',
value: function id() {
return m.route.param('id').split('-')[0];
}
}, {
key: 'loadPage',
value: function loadPage() {
this.page = null;

app.store.find('pages', m.route.param('id').split('-')[0]).then(this.show.bind(this));

m.lazyRedraw();
var id = this.id();
var page = app.store.getById('pages', id);
if (page) {
this.show(page);
} else {
app.store.find('pages', id).then(this.show.bind(this));
}
}
}]);
return PagePage;
@@ -153,10 +232,14 @@ System.register('sijad/pages/components/PagePage', ['flarum/components/Page', 'f
});;
'use strict';

System.register('sijad/pages/main', ['sijad/pages/components/PagePage', 'sijad/pages/models/Page'], function (_export, _context) {
var PagePage, Page;
System.register('sijad/pages/main', ['sijad/pages/components/HomePage', 'sijad/pages/components/PagePage', 'sijad/pages/models/Page'], function (_export, _context) {
"use strict";

var HomePage, PagePage, Page;
return {
setters: [function (_sijadPagesComponentsPagePage) {
setters: [function (_sijadPagesComponentsHomePage) {
HomePage = _sijadPagesComponentsHomePage.default;
}, function (_sijadPagesComponentsPagePage) {
PagePage = _sijadPagesComponentsPagePage.default;
}, function (_sijadPagesModelsPage) {
Page = _sijadPagesModelsPage.default;
@@ -164,6 +247,8 @@ System.register('sijad/pages/main', ['sijad/pages/components/PagePage', 'sijad/p
execute: function () {

app.initializers.add('sijad-pages', function (app) {
app.routes.homePage = { path: '/pages/home', component: HomePage.component() };

app.routes.page = { path: '/p/:id', component: PagePage.component() };
app.store.models.pages = Page;
/**
@@ -184,6 +269,8 @@ System.register('sijad/pages/main', ['sijad/pages/components/PagePage', 'sijad/p
'use strict';

System.register('sijad/pages/models/Page', ['flarum/Model', 'flarum/utils/mixin', 'flarum/utils/computed', 'flarum/utils/string'], function (_export, _context) {
"use strict";

var Model, mixin, computed, getPlainContent, Page;
return {
setters: [function (_flarumModel) {
@@ -201,7 +288,7 @@ System.register('sijad/pages/models/Page', ['flarum/Model', 'flarum/utils/mixin'

function Page() {
babelHelpers.classCallCheck(this, Page);
return babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(Page).apply(this, arguments));
return babelHelpers.possibleConstructorReturn(this, (Page.__proto__ || Object.getPrototypeOf(Page)).apply(this, arguments));
}

return Page;

+ 29
- 0
js/forum/src/components/HomePage.js View File

@@ -0,0 +1,29 @@
import IndexPage from 'flarum/components/IndexPage';
import LoadingIndicator from 'flarum/components/LoadingIndicator';
import Page from 'flarum/components/Page';
import icon from 'flarum/helpers/icon';

import PagePage from 'sijad/pages/components/PagePage';

export default class HomePage extends PagePage {
init() {
super.init()

app.history.push('homePage', icon('home'));
app.drawer.hide();
app.modal.close();
}

show(page) {
this.page = page;
m.redraw();
}

hero() {
return IndexPage.prototype.hero();
}

id() {
return app.forum.attribute('pagesHome');
}
}

+ 41
- 10
js/forum/src/components/PagePage.js View File

@@ -16,8 +16,6 @@ export default class PagePage extends Page {

this.loadPage();

app.history.push('page');

this.bodyClass = 'App--page';
}

@@ -29,10 +27,10 @@ export default class PagePage extends Page {
<div className="Pages-page">
{page
? [
PageHero.component({page}),
<div className="container">
this.hero(),
<div className="Pages-container container">
<div className="Post-body">
{m.trust(page.contentHtml())}
{this.content()}
</div>
</div>
]
@@ -43,6 +41,7 @@ export default class PagePage extends Page {
}

/**
* Initilize page.
*
* @param {sijad/pages/Page} page
* @protected
@@ -56,12 +55,44 @@ export default class PagePage extends Page {
m.redraw();
}

loadPage() {
this.page = null;
/**
* Get the hero of current page.
*
* @return {VirtualElement}
*/
hero() {
return PageHero.component({page: this.page});
}

app.store.find('pages', m.route.param('id').split('-')[0])
.then(this.show.bind(this));
/**
* Get the content of page.
*
* @return {VirtualElement}
*/
content() {
return m.trust(this.page.contentHtml());
}

m.lazyRedraw();
/**
* Get current page id from route.
*
* @return string
*/
id() {
return m.route.param('id').split('-')[0];
}

/**
* Load page from the store, or make a request
* if we don't have it yet. Then initialize the page.
*/
loadPage() {
const id = this.id();
const page = app.store.getById('pages', id)
if (page) {
this.show(page);
} else {
app.store.find('pages', id).then(this.show.bind(this));
}
}
}

+ 3
- 0
js/forum/src/main.js View File

@@ -1,7 +1,10 @@
import HomePage from 'sijad/pages/components/HomePage';
import PagePage from 'sijad/pages/components/PagePage';
import Page from 'sijad/pages/models/Page';

app.initializers.add('sijad-pages', app => {
app.routes.homePage = {path: '/pages/home', component: HomePage.component()};

app.routes.page = {path: '/p/:id', component: PagePage.component()};
app.store.models.pages = Page;
/**

+ 1
- 1
less/forum/Pages.less View File

@@ -1,4 +1,4 @@
.Pages-page .container {
.Pages-container {
max-width: 820px;
white-space: pre-wrap;
}

+ 36
- 0
src/Listener/AddApiAttributes.php View File

@@ -0,0 +1,36 @@
<?php

namespace Sijad\Pages\Listener;

use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Event\PrepareApiAttributes;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Settings\SettingsRepositoryInterface;

class AddApiAttributes {
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function subscribe(Dispatcher $events)
{
$events->listen(PrepareApiAttributes::class, [$this, 'prepareApiAttributes']);
}

public function prepareApiAttributes(PrepareApiAttributes $event) {
$id = $this->settings->get('pages_home');
if ($id && $event->isSerializer(ForumSerializer::class)) {
$event->attributes['pagesHome'] = $id;
}
}
}


+ 1
- 0
src/Listener/AddPagesRoute.php View File

@@ -20,6 +20,7 @@ class AddPagesRoute
*/
public function configureForumRoutes(ConfigureForumRoutes $event)
{
$event->get('/pages/home', 'pages.home');
$event->get('/p/{id:\d+(?:-[^/]*)?}', 'pages.page');
}
}

Loading…
Cancel
Save