2016-05-24 15:06:59 +03:00
|
|
|
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
2017-02-23 21:47:52 +03:00
|
|
|
import ShortcutsRoute from 'ghost-admin/mixins/shortcuts-route';
|
|
|
|
import InfinityRoute from 'ember-infinity/mixins/route';
|
|
|
|
import {assign} from 'ember-platform';
|
2017-03-02 21:35:09 +03:00
|
|
|
import {isBlank} from 'ember-utils';
|
2017-02-23 21:47:52 +03:00
|
|
|
import $ from 'jquery';
|
2014-03-02 18:30:35 +04:00
|
|
|
|
2017-02-23 21:47:52 +03:00
|
|
|
export default AuthenticatedRoute.extend(InfinityRoute, ShortcutsRoute, {
|
|
|
|
titleToken: 'Content',
|
|
|
|
|
|
|
|
perPage: 30,
|
|
|
|
perPageParam: 'limit',
|
|
|
|
totalPagesParam: 'meta.pagination.pages',
|
|
|
|
|
2017-03-02 21:35:09 +03:00
|
|
|
queryParams: {
|
|
|
|
type: {
|
|
|
|
refreshModel: true,
|
|
|
|
replace: true
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
refreshModel: true,
|
|
|
|
replace: true
|
|
|
|
},
|
|
|
|
tag: {
|
|
|
|
refreshModel: true,
|
|
|
|
replace: true
|
2017-03-07 20:36:28 +03:00
|
|
|
},
|
|
|
|
order: {
|
|
|
|
refreshModel: true,
|
|
|
|
replace: true
|
2017-03-02 21:35:09 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-02-23 21:47:52 +03:00
|
|
|
_type: null,
|
|
|
|
_selectedPostIndex: null,
|
|
|
|
|
|
|
|
model(params) {
|
|
|
|
return this.get('session.user').then((user) => {
|
2017-03-02 21:35:09 +03:00
|
|
|
let queryParams = this._typeParams(params.type);
|
|
|
|
let filterParams = {tag: params.tag};
|
|
|
|
|
2017-02-23 21:47:52 +03:00
|
|
|
if (user.get('isAuthor')) {
|
2017-03-02 21:35:09 +03:00
|
|
|
// authors can only view their own posts
|
|
|
|
filterParams.author = user.get('slug');
|
|
|
|
} else if (params.author) {
|
|
|
|
filterParams.author = params.author;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
let perPage = this.get('perPage');
|
2017-03-02 21:35:09 +03:00
|
|
|
let paginationSettings = assign({perPage, startingPage: 1}, queryParams);
|
2017-02-23 21:47:52 +03:00
|
|
|
|
|
|
|
return this.infinityModel('post', paginationSettings);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-02 21:35:09 +03:00
|
|
|
_typeParams(type) {
|
2017-02-23 21:47:52 +03:00
|
|
|
let status = 'all';
|
|
|
|
let staticPages = 'all';
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'draft':
|
|
|
|
status = 'draft';
|
|
|
|
staticPages = false;
|
|
|
|
break;
|
|
|
|
case 'published':
|
|
|
|
status = 'published';
|
|
|
|
staticPages = false;
|
|
|
|
break;
|
|
|
|
case 'scheduled':
|
|
|
|
status = 'scheduled';
|
|
|
|
staticPages = false;
|
|
|
|
break;
|
|
|
|
case 'page':
|
|
|
|
staticPages = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
staticPages
|
|
|
|
};
|
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('+');
|
|
|
|
},
|
|
|
|
|
|
|
|
// trigger a background load of all tags and authors for use in the filter dropdowns
|
|
|
|
setupController(controller) {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (!controller._hasLoadedTags) {
|
|
|
|
this.get('store').query('tag', {limit: 'all'}).then(() => {
|
|
|
|
controller._hasLoadedTags = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('session.user').then((user) => {
|
|
|
|
if (!user.get('isAuthor') && !controller._hasLoadedAuthors) {
|
|
|
|
this.get('store').query('user', {limit: 'all'}).then(() => {
|
|
|
|
controller._hasLoadedAuthors = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2017-02-23 21:47:52 +03:00
|
|
|
|
|
|
|
stepThroughPosts(step) {
|
|
|
|
let currentPost = this.get('controller.selectedPost');
|
|
|
|
let posts = this.get('controller.model');
|
|
|
|
let length = posts.get('length');
|
|
|
|
let newPosition;
|
|
|
|
|
|
|
|
// when the currentPost is deleted we won't be able to use indexOf.
|
|
|
|
// we keep track of the index locally so we can select next after deletion
|
|
|
|
if (this._selectedPostIndex !== null && length) {
|
|
|
|
newPosition = this._selectedPostIndex + step;
|
|
|
|
} else {
|
|
|
|
newPosition = posts.indexOf(currentPost) + step;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we are on the first or last item
|
|
|
|
// just do nothing (desired behavior is to not
|
|
|
|
// loop around)
|
|
|
|
if (newPosition >= length) {
|
|
|
|
return;
|
|
|
|
} else if (newPosition < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._selectedPostIndex = newPosition;
|
|
|
|
this.set('controller.selectedPost', posts.objectAt(newPosition));
|
|
|
|
},
|
|
|
|
|
|
|
|
shortcuts: {
|
|
|
|
'up, k': 'moveUp',
|
|
|
|
'down, j': 'moveDown',
|
|
|
|
'enter': 'editPost',
|
|
|
|
'c': 'newPost',
|
|
|
|
'command+backspace, ctrl+backspace': 'deletePost'
|
|
|
|
},
|
|
|
|
|
|
|
|
resetController() {
|
|
|
|
this.set('controller.selectedPost', null);
|
|
|
|
this.set('controller.showDeletePostModal', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
willTransition() {
|
|
|
|
this._selectedPostIndex = null;
|
|
|
|
|
|
|
|
if (this.get('controller')) {
|
|
|
|
this.resetController();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
queryParamsDidChange() {
|
|
|
|
// scroll back to the top
|
|
|
|
$('.content-list').scrollTop(0);
|
2017-03-02 21:35:09 +03:00
|
|
|
|
|
|
|
this._super(...arguments);
|
2017-02-23 21:47:52 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
newPost() {
|
|
|
|
this.transitionTo('editor.new');
|
|
|
|
},
|
|
|
|
|
|
|
|
moveUp() {
|
|
|
|
this.stepThroughPosts(-1);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveDown() {
|
|
|
|
this.stepThroughPosts(1);
|
|
|
|
},
|
|
|
|
|
|
|
|
editPost() {
|
|
|
|
let selectedPost = this.get('controller.selectedPost');
|
|
|
|
|
|
|
|
if (selectedPost) {
|
|
|
|
this.transitionTo('editor.edit', selectedPost.get('id'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deletePost() {
|
|
|
|
this.get('controller').send('toggleDeletePostModal');
|
|
|
|
},
|
|
|
|
|
|
|
|
onPostDeletion() {
|
|
|
|
// select next post (re-select the current index)
|
|
|
|
this.stepThroughPosts(0);
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 18:30:35 +04:00
|
|
|
});
|