2017-08-22 10:53:26 +03:00
|
|
|
import Mixin from '@ember/object/mixin';
|
2016-06-30 13:21:47 +03:00
|
|
|
import RSVP from 'rsvp';
|
2018-02-28 20:31:43 +03:00
|
|
|
// import {assign} from '@ember/polyfills';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2018-02-28 20:31:43 +03:00
|
|
|
// let defaultPaginationSettings = {
|
|
|
|
// page: 1,
|
|
|
|
// limit: 15
|
|
|
|
// };
|
|
|
|
|
|
|
|
// NOTE: this is DEPRECATED and now _only_ used by the subscribers route.
|
|
|
|
// DO NOT USE - it will disappear soon
|
2014-07-20 20:42:03 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Mixin.create({
|
2017-10-30 12:38:01 +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() {
|
2018-02-28 20:31:43 +03:00
|
|
|
// NOTE: errors in Ember 3.0 because this.paginationSettings.isDescriptor
|
|
|
|
// no longer exists as CPs will be available directly with no getter.
|
|
|
|
// Commented out for now as this whole mixin will soon disappear
|
|
|
|
//
|
2017-01-25 23:05:28 +03:00
|
|
|
// don't merge defaults if paginationSettings is a CP
|
2018-02-28 20:31:43 +03:00
|
|
|
// if (!this.paginationSettings.isDescriptor) {
|
|
|
|
// let paginationSettings = this.get('paginationSettings');
|
|
|
|
// let settings = assign({}, defaultPaginationSettings, paginationSettings);
|
|
|
|
//
|
|
|
|
// this.set('paginationSettings', settings);
|
|
|
|
// }
|
2015-09-03 14:06:50 +03:00
|
|
|
|
|
|
|
this.set('paginationMeta', {});
|
2017-01-25 23:05:28 +03:00
|
|
|
|
|
|
|
this._super(...arguments);
|
2015-09-03 14:06:50 +03:00
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
reportLoadError(error) {
|
|
|
|
this.get('notifications').showAPIError(error, {key: 'pagination.load.failed'});
|
2015-09-03 14:06:50 +03:00
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
loadFirstPage(transition) {
|
2015-10-28 14:36:45 +03:00
|
|
|
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-06-30 17:45:02 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
// if we have a transition we're executing in a route hook so we
|
|
|
|
// want to throw in order to trigger the global error handler
|
|
|
|
if (transition) {
|
|
|
|
throw error;
|
|
|
|
} else {
|
|
|
|
this.reportLoadError(error);
|
|
|
|
}
|
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-06-30 17:45:02 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
this.reportLoadError(error);
|
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
|
|
|
}
|
|
|
|
});
|