2016-05-24 15:06:59 +03:00
|
|
|
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
2022-01-17 12:34:55 +03:00
|
|
|
import {action} from '@ember/object';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {assign} from '@ember/polyfills';
|
|
|
|
import {isBlank} from '@ember/utils';
|
2018-07-02 16:38:27 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2014-03-02 18:30:35 +04:00
|
|
|
|
2022-01-17 12:34:55 +03:00
|
|
|
export default class PostsRoute extends AuthenticatedRoute {
|
|
|
|
@service infinity;
|
|
|
|
@service router;
|
2017-02-23 21:47:52 +03:00
|
|
|
|
2022-01-17 12:34:55 +03:00
|
|
|
queryParams = {
|
2019-08-29 13:10:09 +03:00
|
|
|
type: {refreshModel: true},
|
2020-06-09 14:19:25 +03:00
|
|
|
visibility: {refreshModel: true},
|
|
|
|
access: {refreshModel: true},
|
2019-08-29 13:10:09 +03:00
|
|
|
author: {refreshModel: true},
|
|
|
|
tag: {refreshModel: true},
|
|
|
|
order: {refreshModel: true}
|
2022-01-17 12:34:55 +03:00
|
|
|
};
|
2017-03-02 21:35:09 +03:00
|
|
|
|
2022-01-17 12:34:55 +03:00
|
|
|
modelName = 'post';
|
|
|
|
perPage = 30;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-01-17 12:34:55 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2019-08-29 13:10:09 +03:00
|
|
|
|
|
|
|
// 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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-01-17 12:34:55 +03:00
|
|
|
}
|
2019-08-29 13:10:09 +03:00
|
|
|
|
2017-02-23 21:47:52 +03:00
|
|
|
model(params) {
|
2021-07-08 16:37:31 +03:00
|
|
|
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;
|
|
|
|
}
|
2017-11-10 20:36:27 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
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;
|
2022-02-01 20:59:20 +03:00
|
|
|
// filterParams.status = 'draft';
|
2021-07-08 16:37:31 +03:00
|
|
|
} else if (params.author) {
|
|
|
|
filterParams.authors = params.author;
|
|
|
|
}
|
2017-03-02 21:35:09 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let filter = this._filterString(filterParams);
|
|
|
|
if (!isBlank(filter)) {
|
|
|
|
queryParams.filter = filter;
|
|
|
|
}
|
2017-03-07 20:36:28 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
if (!isBlank(params.order)) {
|
|
|
|
queryParams.order = params.order;
|
|
|
|
}
|
2017-02-23 21:47:52 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let perPage = this.perPage;
|
|
|
|
let paginationSettings = assign({perPage, startingPage: 1}, paginationParams, queryParams);
|
2017-02-23 21:47:52 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
return this.infinity.model(this.modelName, paginationSettings);
|
2022-01-17 12:34:55 +03:00
|
|
|
}
|
2017-02-23 21:47:52 +03:00
|
|
|
|
2020-10-15 20:03:18 +03:00
|
|
|
// trigger a background load of all tags, authors, and snipps for use in filter dropdowns and card menu
|
2018-01-11 20:43:23 +03:00
|
|
|
setupController(controller) {
|
2022-01-17 12:34:55 +03:00
|
|
|
super.setupController(...arguments);
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
if (!controller._hasLoadedTags) {
|
2019-02-22 06:17:33 +03:00
|
|
|
this.store.query('tag', {limit: 'all'}).then(() => {
|
2018-01-11 20:43:23 +03:00
|
|
|
controller._hasLoadedTags = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
if (!this.session.user.isAuthorOrContributor && !controller._hasLoadedAuthors) {
|
|
|
|
this.store.query('user', {limit: 'all'}).then(() => {
|
|
|
|
controller._hasLoadedAuthors = true;
|
|
|
|
});
|
|
|
|
}
|
2020-10-15 20:03:18 +03:00
|
|
|
|
|
|
|
if (!controller._hasLoadedSnippets) {
|
|
|
|
this.store.query('snippet', {limit: 'all'}).then(() => {
|
|
|
|
controller._hasLoadedSnippets = true;
|
|
|
|
});
|
|
|
|
}
|
2022-01-17 12:34:55 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-01-17 12:34:55 +03:00
|
|
|
@action
|
|
|
|
queryParamsDidChange() {
|
|
|
|
// scroll back to the top
|
|
|
|
let contentList = document.querySelector('.content-list');
|
|
|
|
if (contentList) {
|
|
|
|
contentList.scrollTop = 0;
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
2022-01-17 12:34:55 +03:00
|
|
|
|
|
|
|
super.actions.queryParamsDidChange.call(this, ...arguments);
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2019-05-20 18:16:19 +03:00
|
|
|
buildRouteInfoMetadata() {
|
|
|
|
return {
|
|
|
|
titleToken: 'Posts'
|
|
|
|
};
|
2022-01-17 12:34:55 +03:00
|
|
|
}
|
2019-05-20 18:16:19 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
_getTypeFilters(type) {
|
2021-08-13 16:16:57 +03:00
|
|
|
let status = '[draft,scheduled,published,sent]';
|
2017-02-23 21:47:52 +03:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'draft':
|
|
|
|
status = 'draft';
|
|
|
|
break;
|
|
|
|
case 'published':
|
|
|
|
status = 'published';
|
|
|
|
break;
|
|
|
|
case 'scheduled':
|
|
|
|
status = 'scheduled';
|
|
|
|
break;
|
2021-08-13 16:16:57 +03:00
|
|
|
case 'sent':
|
|
|
|
status = 'sent';
|
|
|
|
break;
|
2017-02-23 21:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-02-22 06:17:33 +03:00
|
|
|
status
|
2017-02-23 21:47:52 +03:00
|
|
|
};
|
2022-01-17 12:34:55 +03:00
|
|
|
}
|
2017-03-02 21:35:09 +03:00
|
|
|
|
|
|
|
_filterString(filter) {
|
|
|
|
return Object.keys(filter).map((key) => {
|
|
|
|
let value = filter[key];
|
|
|
|
|
|
|
|
if (!isBlank(value)) {
|
|
|
|
return `${key}:${filter[key]}`;
|
|
|
|
}
|
|
|
|
}).compact().join('+');
|
2017-02-23 21:47:52 +03:00
|
|
|
}
|
2022-01-17 12:34:55 +03:00
|
|
|
}
|