2016-05-24 15:06:59 +03:00
|
|
|
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
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
|
|
|
|
2018-07-02 16:38:27 +03:00
|
|
|
export default AuthenticatedRoute.extend({
|
|
|
|
infinity: service(),
|
2019-08-29 13:10:09 +03:00
|
|
|
router: service(),
|
2017-02-23 21:47:52 +03:00
|
|
|
|
2017-03-02 21:35:09 +03:00
|
|
|
queryParams: {
|
2019-08-29 13:10:09 +03:00
|
|
|
type: {refreshModel: true},
|
|
|
|
author: {refreshModel: true},
|
|
|
|
tag: {refreshModel: true},
|
|
|
|
order: {refreshModel: true}
|
2017-03-02 21:35:09 +03:00
|
|
|
},
|
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
modelName: 'post',
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
perPage: 30,
|
|
|
|
|
2019-08-29 13:10:09 +03:00
|
|
|
init() {
|
|
|
|
this._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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-02-23 21:47:52 +03:00
|
|
|
model(params) {
|
2019-02-22 06:17:33 +03:00
|
|
|
return this.session.user.then((user) => {
|
2018-07-20 13:57:53 +03:00
|
|
|
let queryParams = {};
|
2017-03-02 21:35:09 +03:00
|
|
|
let filterParams = {tag: params.tag};
|
2018-03-12 13:43:53 +03:00
|
|
|
let paginationParams = {
|
|
|
|
perPageParam: 'limit',
|
|
|
|
totalPagesParam: 'meta.pagination.pages'
|
|
|
|
};
|
2017-03-02 21:35:09 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
assign(filterParams, this._getTypeFilters(params.type));
|
|
|
|
|
2017-11-10 20:36:27 +03:00
|
|
|
if (params.type === 'featured') {
|
|
|
|
filterParams.featured = true;
|
|
|
|
}
|
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
if (user.isAuthor) {
|
2017-03-02 21:35:09 +03:00
|
|
|
// authors can only view their own posts
|
2019-02-22 06:17:33 +03:00
|
|
|
filterParams.authors = user.slug;
|
|
|
|
} else if (user.isContributor) {
|
2018-02-07 12:42:46 +03:00
|
|
|
// Contributors can only view their own draft posts
|
2019-02-22 06:17:33 +03:00
|
|
|
filterParams.authors = user.slug;
|
2018-07-20 13:57:53 +03:00
|
|
|
filterParams.status = 'draft';
|
2017-03-02 21:35:09 +03:00
|
|
|
} else if (params.author) {
|
2018-03-27 22:57:59 +03:00
|
|
|
filterParams.authors = params.author;
|
2017-03-02 21:35:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let filter = this._filterString(filterParams);
|
|
|
|
if (!isBlank(filter)) {
|
|
|
|
queryParams.filter = filter;
|
2017-03-07 20:36:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isBlank(params.order)) {
|
|
|
|
queryParams.order = params.order;
|
2017-02-23 21:47:52 +03:00
|
|
|
}
|
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
let perPage = this.perPage;
|
2018-03-12 13:43:53 +03:00
|
|
|
let paginationSettings = assign({perPage, startingPage: 1}, paginationParams, queryParams);
|
2017-02-23 21:47:52 +03:00
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
return this.infinity.model(this.modelName, paginationSettings);
|
2017-02-23 21:47:52 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
// trigger a background load of all tags and authors for use in the filter dropdowns
|
|
|
|
setupController(controller) {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
this.session.user.then((user) => {
|
|
|
|
if (!user.isAuthorOrContributor && !controller._hasLoadedAuthors) {
|
|
|
|
this.store.query('user', {limit: 'all'}).then(() => {
|
2018-01-11 20:43:23 +03:00
|
|
|
controller._hasLoadedAuthors = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
queryParamsDidChange() {
|
|
|
|
// scroll back to the top
|
2019-02-22 06:17:33 +03:00
|
|
|
let contentList = document.querySelector('.content-list');
|
|
|
|
if (contentList) {
|
|
|
|
contentList.scrollTop = 0;
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
this._super(...arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-05-20 18:16:19 +03:00
|
|
|
buildRouteInfoMetadata() {
|
|
|
|
return {
|
|
|
|
titleToken: 'Posts'
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
_getTypeFilters(type) {
|
|
|
|
let status = '[draft,scheduled,published]';
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-02-22 06:17:33 +03:00
|
|
|
status
|
2017-02-23 21:47:52 +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
|
|
|
}
|
2014-03-02 18:30:35 +04:00
|
|
|
});
|