2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-09-03 14:06:50 +03:00
|
|
|
import getRequestErrorMessage from 'ghost/utils/ajax';
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Mixin, inject} = Ember;
|
|
|
|
|
|
|
|
let defaultPaginationSettings = {
|
2014-07-20 20:42:03 +04:00
|
|
|
page: 1,
|
|
|
|
limit: 15
|
|
|
|
};
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Mixin.create({
|
|
|
|
notifications: inject.service(),
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
paginationModel: null,
|
|
|
|
paginationSettings: null,
|
|
|
|
paginationMeta: null,
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
init() {
|
|
|
|
let paginationSettings = this.get('paginationSettings');
|
|
|
|
let settings = Ember.$.extend({}, defaultPaginationSettings, paginationSettings);
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('paginationSettings', settings);
|
|
|
|
this.set('paginationMeta', {});
|
|
|
|
},
|
|
|
|
|
2014-07-20 20:42:03 +04:00
|
|
|
/**
|
2015-09-03 14:06:50 +03:00
|
|
|
* Takes an ajax response, concatenates any error messages, then generates an error notification.
|
|
|
|
* @param {jqXHR} response The jQuery ajax reponse object.
|
|
|
|
* @return
|
2014-07-20 20:42:03 +04:00
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
reportLoadError(response) {
|
|
|
|
let message = 'A problem was encountered while loading more records';
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
if (response) {
|
|
|
|
// Get message from response
|
2015-10-28 14:36:45 +03:00
|
|
|
message += `: ${getRequestErrorMessage(response, true)}`;
|
2015-09-03 14:06:50 +03:00
|
|
|
} else {
|
|
|
|
message += '.';
|
2014-08-01 00:29:35 +04:00
|
|
|
}
|
2014-07-20 20:42:03 +04:00
|
|
|
|
2015-10-07 17:44:23 +03:00
|
|
|
this.get('notifications').showAlert(message, {type: 'error', key: 'pagination.load.failed'});
|
2015-09-03 14:06:50 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
loadFirstPage() {
|
|
|
|
let paginationSettings = this.get('paginationSettings');
|
|
|
|
let modelName = this.get('paginationModel');
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
paginationSettings.page = 1;
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.get('store').query(modelName, paginationSettings).then((results) => {
|
|
|
|
this.set('paginationMeta', results.meta);
|
2015-09-03 14:06:50 +03:00
|
|
|
return results;
|
2015-10-28 14:36:45 +03:00
|
|
|
}, (response) => {
|
|
|
|
this.reportLoadError(response);
|
2015-09-03 14:06:50 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
loadFirstPage() {
|
2015-09-03 14:06:50 +03:00
|
|
|
return this.loadFirstPage();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the next paginated page of posts into the ember-data store. Will cause the posts list UI to update.
|
|
|
|
* @return
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
loadNextPage() {
|
|
|
|
let store = this.get('store');
|
|
|
|
let modelName = this.get('paginationModel');
|
|
|
|
let metadata = this.get('paginationMeta');
|
|
|
|
let nextPage = metadata.pagination && metadata.pagination.next;
|
|
|
|
let paginationSettings = this.get('paginationSettings');
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
if (nextPage) {
|
|
|
|
this.set('isLoading', true);
|
|
|
|
this.set('paginationSettings.page', nextPage);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
store.query(modelName, paginationSettings).then((results) => {
|
|
|
|
this.set('isLoading', false);
|
|
|
|
this.set('paginationMeta', results.meta);
|
2015-09-03 14:06:50 +03:00
|
|
|
return results;
|
2015-10-28 14:36:45 +03:00
|
|
|
}, (response) => {
|
|
|
|
this.reportLoadError(response);
|
2015-09-03 14:06:50 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
resetPagination() {
|
2015-09-03 14:06:50 +03:00
|
|
|
this.set('paginationSettings.page', 1);
|
|
|
|
}
|
2014-07-20 20:42:03 +04:00
|
|
|
}
|
|
|
|
});
|