2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2016-05-24 15:06:59 +03:00
|
|
|
import getRequestErrorMessage from 'ghost-admin/utils/ajax';
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
Mixin,
|
2016-04-18 19:30:35 +03:00
|
|
|
computed,
|
2016-04-15 17:45:50 +03:00
|
|
|
RSVP,
|
2016-01-19 16:03:27 +03:00
|
|
|
inject: {service}
|
|
|
|
} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
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({
|
2016-01-19 16:03:27 +03:00
|
|
|
notifications: service(),
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
paginationModel: null,
|
|
|
|
paginationSettings: null,
|
2016-04-18 19:30:35 +03:00
|
|
|
|
|
|
|
// add a hook so that routes/controllers can do something with the meta data
|
|
|
|
paginationMeta: computed({
|
|
|
|
get() {
|
|
|
|
return this._paginationMeta;
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
if (this.didReceivePaginationMeta) {
|
|
|
|
this.didReceivePaginationMeta(value);
|
|
|
|
}
|
|
|
|
this._paginationMeta = value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}),
|
2015-09-03 14:06:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
init() {
|
|
|
|
let paginationSettings = this.get('paginationSettings');
|
2016-04-15 17:45:50 +03:00
|
|
|
let settings = Ember.assign({}, 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
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
this.set('paginationSettings.page', 1);
|
2015-09-03 14:06:50 +03:00
|
|
|
|
2016-04-18 19:30:35 +03:00
|
|
|
this.set('isLoading', true);
|
|
|
|
|
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;
|
2016-04-18 19:30:35 +03:00
|
|
|
}).catch((response) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.reportLoadError(response);
|
2016-04-18 19:30:35 +03:00
|
|
|
}).finally(() => {
|
|
|
|
this.set('isLoading', false);
|
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
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
if (nextPage && !this.get('isLoading')) {
|
2015-09-03 14:06:50 +03:00
|
|
|
this.set('isLoading', true);
|
|
|
|
this.set('paginationSettings.page', nextPage);
|
|
|
|
|
2016-04-18 19:30:35 +03:00
|
|
|
return store.query(modelName, paginationSettings).then((results) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('paginationMeta', results.meta);
|
2015-09-03 14:06:50 +03:00
|
|
|
return results;
|
2016-04-18 19:30:35 +03:00
|
|
|
}).catch((response) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.reportLoadError(response);
|
2016-04-18 19:30:35 +03:00
|
|
|
}).finally(() => {
|
|
|
|
this.set('isLoading', false);
|
2015-09-03 14:06:50 +03:00
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
} else {
|
|
|
|
return RSVP.resolve([]);
|
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
|
|
|
}
|
|
|
|
});
|