Ghost/core/client/app/controllers/posts.js
Kevin Ansfield 1c6b208047 Refactor modals
refs #5798, closes #5018
- adds new `gh-fullscreen-modal` component - modals are now specified in-context so that they can have deeper interaction with their surrounding components/controller/route, i.e. a modal component can be a thin confirm/deny wrapper over the underlying controller action keeping all context-sensitive logic in one place
- adds spin-buttons to all modals with async behaviour
- adds/improves behaviour of inline-validation in modals
- improves re-authenticate modal to properly handle validation and authentication errors
2016-01-12 20:53:08 +00:00

97 lines
2.5 KiB
JavaScript

import Ember from 'ember';
const {Controller, compare, computed} = Ember;
const {equal} = computed;
// a custom sort function is needed in order to sort the posts list the same way the server would:
// status: ASC
// published_at: DESC
// updated_at: DESC
// id: DESC
function comparator(item1, item2) {
let updated1 = item1.get('updated_at');
let updated2 = item2.get('updated_at');
let idResult,
publishedAtResult,
statusResult,
updatedAtResult;
// when `updated_at` is undefined, the model is still
// being written to with the results from the server
if (item1.get('isNew') || !updated1) {
return -1;
}
if (item2.get('isNew') || !updated2) {
return 1;
}
idResult = compare(parseInt(item1.get('id')), parseInt(item2.get('id')));
statusResult = compare(item1.get('status'), item2.get('status'));
updatedAtResult = compare(updated1.valueOf(), updated2.valueOf());
publishedAtResult = publishedAtCompare(item1, item2);
if (statusResult === 0) {
if (publishedAtResult === 0) {
if (updatedAtResult === 0) {
// This should be DESC
return idResult * -1;
}
// This should be DESC
return updatedAtResult * -1;
}
// This should be DESC
return publishedAtResult * -1;
}
return statusResult;
}
function publishedAtCompare(item1, item2) {
let published1 = item1.get('published_at');
let published2 = item2.get('published_at');
if (!published1 && !published2) {
return 0;
}
if (!published1 && published2) {
return -1;
}
if (!published2 && published1) {
return 1;
}
return compare(published1.valueOf(), published2.valueOf());
}
export default Controller.extend({
showDeletePostModal: false,
// See PostsRoute's shortcuts
postListFocused: equal('keyboardFocus', 'postList'),
postContentFocused: equal('keyboardFocus', 'postContent'),
sortedPosts: computed('model.@each.status', 'model.@each.published_at', 'model.@each.isNew', 'model.@each.updated_at', function () {
let postsArray = this.get('model').toArray();
return postsArray.sort(comparator);
}),
actions: {
showPostContent(post) {
if (!post) {
return;
}
this.transitionToRoute('posts.post', post);
},
toggleDeletePostModal() {
this.toggleProperty('showDeletePostModal');
}
}
});