2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2014-05-24 07:25:20 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Controller, compare, computed} = Ember;
|
|
|
|
const {equal} = computed;
|
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
// a custom sort function is needed in order to sort the posts list the same way the server would:
|
|
|
|
// status: ASC
|
2016-01-23 21:12:22 +03:00
|
|
|
// publishedAt: DESC
|
|
|
|
// updatedAt: DESC
|
2015-06-13 17:34:09 +03:00
|
|
|
// id: DESC
|
|
|
|
function comparator(item1, item2) {
|
2016-01-23 21:12:22 +03:00
|
|
|
let updated1 = item1.get('updatedAt');
|
|
|
|
let updated2 = item2.get('updatedAt');
|
2015-10-28 14:36:45 +03:00
|
|
|
let idResult,
|
|
|
|
publishedAtResult,
|
2015-06-13 17:34:09 +03:00
|
|
|
statusResult,
|
2015-10-28 14:36:45 +03:00
|
|
|
updatedAtResult;
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2016-01-23 21:12:22 +03:00
|
|
|
// when `updatedAt` is undefined, the model is still
|
2015-06-13 17:34:09 +03:00
|
|
|
// being written to with the results from the server
|
|
|
|
if (item1.get('isNew') || !updated1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item2.get('isNew') || !updated2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
idResult = compare(parseInt(item1.get('id')), parseInt(item2.get('id')));
|
|
|
|
statusResult = compare(item1.get('status'), item2.get('status'));
|
|
|
|
updatedAtResult = compare(updated1.valueOf(), updated2.valueOf());
|
2015-06-13 17:34:09 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:15:55 +04:00
|
|
|
function publishedAtCompare(item1, item2) {
|
2016-01-23 21:12:22 +03:00
|
|
|
let published1 = item1.get('publishedAt');
|
|
|
|
let published2 = item2.get('publishedAt');
|
2014-08-02 19:15:55 +04:00
|
|
|
|
|
|
|
if (!published1 && !published2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!published1 && published2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!published2 && published1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return compare(published1.valueOf(), published2.valueOf());
|
2014-08-02 19:15:55 +04:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Controller.extend({
|
2015-09-03 14:06:50 +03:00
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
showDeletePostModal: false,
|
|
|
|
|
2014-10-16 05:56:32 +04:00
|
|
|
// See PostsRoute's shortcuts
|
2015-10-28 14:36:45 +03:00
|
|
|
postListFocused: equal('keyboardFocus', 'postList'),
|
|
|
|
postContentFocused: equal('keyboardFocus', 'postContent'),
|
2014-06-28 07:50:31 +04:00
|
|
|
|
2016-01-23 21:12:22 +03:00
|
|
|
sortedPosts: computed('model.@each.status', 'model.@each.publishedAt', 'model.@each.isNew', 'model.@each.updatedAt', function () {
|
2015-10-28 14:36:45 +03:00
|
|
|
let postsArray = this.get('model').toArray();
|
2014-06-28 07:50:31 +04:00
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
return postsArray.sort(comparator);
|
|
|
|
}),
|
2014-06-17 23:17:08 +04:00
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
actions: {
|
2015-11-18 13:50:48 +03:00
|
|
|
toggleDeletePostModal() {
|
|
|
|
this.toggleProperty('showDeletePostModal');
|
2015-06-13 17:34:09 +03:00
|
|
|
}
|
2014-05-24 07:25:20 +04:00
|
|
|
}
|
|
|
|
});
|