aboutsummaryrefslogtreecommitdiff
path: root/javascript/app/components/paginated-list.js
diff options
context:
space:
mode:
authoralexwl <alexey.a.kiryushin@gmail.com>2018-10-02 13:17:04 +0300
committeralexwl <alexey.a.kiryushin@gmail.com>2018-10-02 13:17:04 +0300
commitcf2c56c7061b7ed40fdd3b40a352ddb9c9b7371f (patch)
treeb1de9ada0f1b1cb064e3a9e0d4042d1f519085bd /javascript/app/components/paginated-list.js
Initial commit
Diffstat (limited to 'javascript/app/components/paginated-list.js')
-rw-r--r--javascript/app/components/paginated-list.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/javascript/app/components/paginated-list.js b/javascript/app/components/paginated-list.js
new file mode 100644
index 0000000..9d85610
--- /dev/null
+++ b/javascript/app/components/paginated-list.js
@@ -0,0 +1,48 @@
+import Ember from 'ember';
+function loadItems(store,component,url) {
+ store.loadFromUrlPaginated(url).then((result) => {
+ Ember.run.next(() => {
+ component.set('total',result.total);
+ component.set('items',result.items);
+ component.set('first',result.linkHeader.first);
+ component.set('next',result.linkHeader.next);
+ component.set('prev',result.linkHeader.prev);
+ component.set('last',result.linkHeader.last);
+
+ const pageMatch = url.match(/(&|\?)page=(\d+)/);
+ const perPageMatch = url.match(/(&|\?)per_page=(\d+)/);
+
+ const page = pageMatch ? pageMatch[2] : 1;
+ const perPage = perPageMatch ? perPageMatch[2] : 20;
+
+ if(result.linkHeader.next || result.linkHeader.prev) {
+ component.set('firstItemOnPage',(page - 1) * perPage + 1);
+ if(!result.linkHeader.last) {
+ component.set('lastItemOnPage',result.total);
+ } else {
+ component.set('lastItemOnPage',page * perPage);
+ }
+ }
+ });
+ });
+}
+
+export default Ember.Component.extend({
+ store : Ember.inject.service('store'),
+ init() {
+ this._super(...arguments);
+ if(this.get('url')) {
+ loadItems(this.get('store'),this,this.get('url'));
+ }
+ },
+ urlObserver : Ember.observer('url',function () {
+ loadItems(this.get('store'),this,this.get('url'));
+ this.element.querySelector(".paginated-list-content").scrollTop = 0;
+ }),
+ actions : {
+ update(url) {
+ this.element.querySelector(".paginated-list-content").scrollTop = 0;
+ loadItems(this.get('store'),this,url);
+ }
+ }
+});