|
| 1 | +<link rel="import" href="../polymer/polymer-element.html"> |
| 2 | +<link rel="import" href="../paper-icon-button/paper-icon-button.html"> |
| 3 | +<link rel="import" href="../iron-icons/iron-icons.html"> |
| 4 | + |
| 5 | + |
| 6 | +<dom-module id="cork-pagination"> |
| 7 | + <template> |
| 8 | + <style> |
| 9 | + :host { |
| 10 | + display: inline-block; |
| 11 | + } |
| 12 | + |
| 13 | + #root { |
| 14 | + display: flex; |
| 15 | + align-items: center; |
| 16 | + } |
| 17 | + |
| 18 | + .ellipsis { |
| 19 | + display: none; |
| 20 | + } |
| 21 | + |
| 22 | + paper-icon-button { |
| 23 | + color: var(--cork-color, --default-primary-color); |
| 24 | + } |
| 25 | + paper-icon-button[disabled] { |
| 26 | + color: var(--cork-disabled-color, #ccc); |
| 27 | + } |
| 28 | + |
| 29 | + a { |
| 30 | + color: var(--cork-color, --default-primary-color); |
| 31 | + cursor: pointer; |
| 32 | + text-align: center; |
| 33 | + min-width: 20px; |
| 34 | + border-radius: 20px; |
| 35 | + display: inline-block; |
| 36 | + padding: 4px; |
| 37 | + margin: 0 3px; |
| 38 | + } |
| 39 | + |
| 40 | + a:hover { |
| 41 | + background: var(--cork-background-color-light, var(--default-background-color-light, #eee)); |
| 42 | + } |
| 43 | + |
| 44 | + a[selected] { |
| 45 | + background: var(--cork-background-color, var(--default-background-color, #ccc)); |
| 46 | + color: white; |
| 47 | + } |
| 48 | + </style> |
| 49 | + |
| 50 | + <div id="root"> |
| 51 | + <paper-icon-button disabled$="[[firstPage]]" icon="arrow-back" on-click="previous"></paper-icon-button> |
| 52 | + |
| 53 | + <a selected$="[[firstPage]]" on-click="_selectPage">1</a> |
| 54 | + <span id="startEllipsis" class="ellipsis">...</span> |
| 55 | + |
| 56 | + <template is="dom-repeat" items="[[pages]]"> |
| 57 | + <a selected$="[[item.selected]]" on-click="_selectPage">[[item.index]]</a> |
| 58 | + </template> |
| 59 | + |
| 60 | + <span id="stopEllipsis" class="ellipsis">...</span> |
| 61 | + <a id="lastPage" selected$="[[lastPage]]" on-click="_selectPage">[[lastPageIndex]]</a> |
| 62 | + |
| 63 | + <paper-icon-button disabled$="[[lastPage]]" icon="arrow-forward" on-click="next"></paper-icon-button> |
| 64 | + </div> |
| 65 | + |
| 66 | + |
| 67 | + </template> |
| 68 | + |
| 69 | + <script> |
| 70 | + /** |
| 71 | + * `cork-pagination` |
| 72 | + * Pagination element |
| 73 | + * |
| 74 | + * @customElement |
| 75 | + * @polymer |
| 76 | + * @demo demo/index.html |
| 77 | + */ |
| 78 | + class CorkPagination extends Polymer.Element { |
| 79 | + static get is() { return 'cork-pagination'; } |
| 80 | + |
| 81 | + static get properties() { |
| 82 | + return { |
| 83 | + itemsPerPage : { |
| 84 | + type : Number, |
| 85 | + value : 10 |
| 86 | + }, |
| 87 | + currentIndex : { |
| 88 | + type : Number, |
| 89 | + value : 1 |
| 90 | + }, |
| 91 | + totalResults : { |
| 92 | + type : Number, |
| 93 | + value : 1 |
| 94 | + }, |
| 95 | + numShownPages : { |
| 96 | + type : Number, |
| 97 | + value : 5 |
| 98 | + }, |
| 99 | + pages : { |
| 100 | + type : Array, |
| 101 | + value : function() { |
| 102 | + return [] |
| 103 | + } |
| 104 | + }, |
| 105 | + lastPageIndex: { |
| 106 | + type: Number, |
| 107 | + value: 1 |
| 108 | + }, |
| 109 | + firstPage : { |
| 110 | + type : Boolean, |
| 111 | + value : true |
| 112 | + }, |
| 113 | + lastPage : { |
| 114 | + type: Boolean, |
| 115 | + value: false |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + static get observers() { |
| 121 | + return [ |
| 122 | + // Observer method name, followed by a list of dependencies, in parenthesis |
| 123 | + '_onPageChanged(currentIndex, itemsPerPage, totalResults)' |
| 124 | + ] |
| 125 | + } |
| 126 | + |
| 127 | + _onPageChanged() { |
| 128 | + this.firstPage = false; |
| 129 | + this.lastPage = false; |
| 130 | + |
| 131 | + var pages = []; |
| 132 | + this.currentPage = Math.floor(this.currentIndex / this.itemsPerPage) + 1; |
| 133 | + var offset = Math.floor(this.numShownPages / 2); |
| 134 | + this.lastPageIndex = Math.max(Math.floor(this.totalResults / this.itemsPerPage), 1); |
| 135 | + |
| 136 | + var startPage = this.currentPage - offset; |
| 137 | + var endPage = this.currentPage + offset; |
| 138 | + |
| 139 | + if( startPage < 1 ) { |
| 140 | + endPage = (1 - startPage) + endPage; |
| 141 | + } |
| 142 | + if( endPage > this.lastPageIndex ) { |
| 143 | + startPage = startPage - (endPage - this.lastPageIndex); |
| 144 | + endPage = this.lastPageIndex; |
| 145 | + } |
| 146 | + if( startPage < 1 ) startPage = 1; |
| 147 | + |
| 148 | + this.firstPage = (this.currentPage === 1) ? true : false; |
| 149 | + if( startPage === 1 ) startPage = 2; |
| 150 | + |
| 151 | + this.lastPage = (this.currentPage === this.lastPageIndex) ? true : false; |
| 152 | + if( endPage === this.lastPageIndex ) endPage = this.lastPageIndex - 1; |
| 153 | + |
| 154 | + for( var i = startPage; i <= endPage; i++ ) { |
| 155 | + pages.push({ |
| 156 | + index : i, |
| 157 | + selected : (i === this.currentPage) ? true : false |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + this.$.lastPage.style.display = (this.lastPageIndex > 1) ? 'inline-block' : 'none'; |
| 162 | + |
| 163 | + this.$.startEllipsis.style.display = (startPage > 2) ? 'inline-block' : 'none'; |
| 164 | + this.$.stopEllipsis.style.display = (endPage < (this.lastPageIndex - 1)) ? 'inline-block' : 'none'; |
| 165 | + |
| 166 | + this.pages = pages; |
| 167 | + } |
| 168 | + |
| 169 | + previous() { |
| 170 | + this._fireNav({ |
| 171 | + page : this.currentPage - 1, |
| 172 | + startIndex : (this.currentPage - 2) * this.itemsPerPage |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + next() { |
| 177 | + this._fireNav({ |
| 178 | + page : this.currentPage + 1, |
| 179 | + startIndex : (this.currentPage) * this.itemsPerPage |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + _selectPage(e) { |
| 184 | + var page = parseInt(e.currentTarget.innerHTML); |
| 185 | + |
| 186 | + this._fireNav({ |
| 187 | + page : page, |
| 188 | + startIndex : (page-1) * this.itemsPerPage |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + _fireNav(payload) { |
| 193 | + this.dispatchEvent(new CustomEvent('nav', {detail: payload})); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + window.customElements.define(CorkPagination.is, CorkPagination); |
| 198 | + </script> |
| 199 | +</dom-module> |
0 commit comments