Ghost/ghost/admin/app/routes/posts.js
Kevin Ansfield ef0178cd06 Refactored posts and pages controllers to Octane patterns
refs https://github.com/TryGhost/Ghost/issues/14101

- migrated to full native class syntax
- removed loading of snippets as they are not needed on post lists (they are needed on the editor screen which does it's own loading)
- removed `access` query param definition leftover from earlier development
- removed use of `{{action}}` in associated templates
2022-10-07 18:39:34 +01:00

150 lines
4.3 KiB
JavaScript

import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import {action} from '@ember/object';
import {assign} from '@ember/polyfills';
import {isBlank} from '@ember/utils';
import {inject as service} from '@ember/service';
export default class PostsRoute extends AuthenticatedRoute {
@service infinity;
@service router;
@service feature;
queryParams = {
type: {refreshModel: true},
visibility: {refreshModel: true},
author: {refreshModel: true},
tag: {refreshModel: true},
order: {refreshModel: true}
};
modelName = 'post';
perPage = 30;
constructor() {
super(...arguments);
// if we're already on this route and we're transiting _to_ this route
// then the filters are being changed and we shouldn't create a new
// browser history entry
// see https://github.com/TryGhost/Ghost/issues/11057
this.router.on('routeWillChange', (transition) => {
if (transition.to && (this.routeName === 'posts' || this.routeName === 'pages')) {
let toThisRoute = transition.to.find(route => route.name === this.routeName);
if (transition.from && transition.from.name === this.routeName && toThisRoute) {
transition.method('replace');
}
}
});
}
model(params) {
const user = this.session.user;
let queryParams = {};
let filterParams = {tag: params.tag, visibility: params.visibility};
let paginationParams = {
perPageParam: 'limit',
totalPagesParam: 'meta.pagination.pages'
};
assign(filterParams, this._getTypeFilters(params.type));
if (params.type === 'featured') {
filterParams.featured = true;
}
if (user.isAuthor) {
// authors can only view their own posts
filterParams.authors = user.slug;
} else if (user.isContributor) {
// Contributors can only view their own draft posts
filterParams.authors = user.slug;
// filterParams.status = 'draft';
} else if (params.author) {
filterParams.authors = params.author;
}
let filter = this._filterString(filterParams);
if (!isBlank(filter)) {
queryParams.filter = filter;
}
if (!isBlank(params.order)) {
queryParams.order = params.order;
}
let perPage = this.perPage;
let paginationSettings = assign({perPage, startingPage: 1}, paginationParams, queryParams);
return this.infinity.model(this.modelName, paginationSettings);
}
// trigger a background load of all tags and authors for use in filter dropdowns
setupController(controller) {
super.setupController(...arguments);
if (!controller._hasLoadedTags) {
this.store.query('tag', {limit: 'all'}).then(() => {
controller._hasLoadedTags = true;
});
}
if (!this.session.user.isAuthorOrContributor && !controller._hasLoadedAuthors) {
this.store.query('user', {limit: 'all'}).then(() => {
controller._hasLoadedAuthors = true;
});
}
}
@action
queryParamsDidChange() {
// scroll back to the top
let contentList = document.querySelector('.content-list');
if (contentList) {
contentList.scrollTop = 0;
}
super.actions.queryParamsDidChange.call(this, ...arguments);
}
buildRouteInfoMetadata() {
return {
titleToken: 'Posts'
};
}
_getTypeFilters(type) {
let status = '[draft,scheduled,published,sent]';
switch (type) {
case 'draft':
status = 'draft';
break;
case 'published':
status = 'published';
break;
case 'scheduled':
status = 'scheduled';
break;
case 'sent':
status = 'sent';
break;
}
return {
status
};
}
_filterString(filter) {
return Object.keys(filter).map((key) => {
let value = filter[key];
if (!isBlank(value)) {
return `${key}:${filter[key]}`;
}
return undefined;
}).compact().join('+');
}
}