beyond-rails 0.0.284 → 0.0.285

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63ba182611ef8ef26ad8589924f920756257dffa44cec1b672f49aae74f00a72
4
- data.tar.gz: bff2df6564e4937dc29c5a52227a8bf4f8c961e9c3733fcaa1d1fbb3c6a504b1
3
+ metadata.gz: 99b44239e1951ebd60eb5f8b80a1609e93172b5fead61ccf423a2df51e782e49
4
+ data.tar.gz: 20114de4f99d04f58660e3eabaac8a3d3adcfb42040dfa2715c36593da0c19bd
5
5
  SHA512:
6
- metadata.gz: 880db176153aaf81da3896873b71b8ed4c05e3b2e29f2b6f41f322b95677ff637c344e62c9114c07d34de76466f3f75da646930d8541cdc55502383486f16a2c
7
- data.tar.gz: 48178537bbff03a5daad5e3a2225234066df79fb0f459c229a9f70ec8693ceca9d99bae1375ce7fa08986ff3401869b22013e9c08340d240799563643bddba6f
6
+ metadata.gz: c07afe4d926ff5303e0a9cb197a207df07cbaf331802dc44eb68b35d38a01205f9d34e2a01b6df9d1ff8c57158a55bc05babaa2bf08ce57eb2e066c13db9837c
7
+ data.tar.gz: 2cf313ed7cdd8a38be5414d89e36ab502831bf95c8869deea2dd69fe4404aa805f7b7a4d230792421afe940fea9ad5fc6492106a62f722e25c3d9f1a6b83e656
@@ -0,0 +1,261 @@
1
+ import supportDom from '../decorators/supportDom'
2
+ import { range, noop, toInt } from '../utils'
3
+ import { $, $$ } from '../utils/dom'
4
+
5
+ const ACTIVE_CLASSNAME = 'js-active'
6
+ const DOTS = '⸱⸱⸱'
7
+
8
+ @supportDom
9
+ export default class Pagination {
10
+
11
+ constructor(config) {
12
+
13
+ this.dom = config.dom
14
+ this.page = config.page || 1
15
+ this.total = config.total || 0
16
+ this.maxVisiblePage = config.maxVisiblePage || 7
17
+ this.currentPageNode = null
18
+ this.change = config.change || noop
19
+
20
+ this.init()
21
+ this.addEvents()
22
+ }
23
+
24
+ init() {
25
+ const { dom } = this
26
+ this.ul = $('[data-pagination]', dom)
27
+ this.prevBtn = $('[data-prev]', dom)
28
+ this.nextBtn = $('[data-next]', dom)
29
+ this.input = $('[data-page-input]', dom)
30
+ this.drawPages()
31
+ }
32
+
33
+ clearPages() {
34
+ const { ul } = this
35
+ $$('[data-page-item]', ul)
36
+ .concat($$('[data-page-dots]', ul))
37
+ .map(item => item.parentNode)
38
+ .forEach(item => item.remove())
39
+ }
40
+
41
+ getLiNode(pageOrDots) {
42
+
43
+ const li = document.createElement('li')
44
+ li.className = 'page-item'
45
+ const anchor = document.createElement('a')
46
+ anchor.className = 'page-link'
47
+
48
+ if (pageOrDots === DOTS) {
49
+ anchor.setAttribute('data-page-dots', '')
50
+ anchor.textContent = DOTS
51
+ }
52
+ else {
53
+ const page = pageOrDots
54
+ anchor.setAttribute('data-page-item', page)
55
+ anchor.textContent = page
56
+ }
57
+
58
+ li.appendChild(anchor)
59
+ return li
60
+ }
61
+
62
+ setActive(page) {
63
+
64
+ const { currentPageNode } = this
65
+
66
+ if (currentPageNode) {
67
+ currentPageNode.classList.remove(ACTIVE_CLASSNAME)
68
+ }
69
+
70
+ const anchor = $(`[data-page-item="${page}"]`)
71
+
72
+ if (! anchor) {
73
+ return
74
+ }
75
+ anchor.classList.add(ACTIVE_CLASSNAME)
76
+ this.currentPageNode = anchor
77
+ this.page = page
78
+ }
79
+
80
+ insertPageLi(li) {
81
+ this.ul.insertBefore(li, this.nextBtn.parentNode)
82
+ }
83
+
84
+ drawRegularPages(currentPage = this.page) {
85
+
86
+ range(1, this.total + 1)
87
+ .forEach(page => {
88
+ const li = this.getLiNode(page)
89
+ this.insertPageLi(li)
90
+ })
91
+ this.setActive(currentPage)
92
+ }
93
+
94
+ drawPagesWithInput(page = this.page) {
95
+
96
+ this.clearPages()
97
+
98
+ const { total } = this
99
+ const beforeLast = total - 1
100
+
101
+ const isFirst = (page === 1)
102
+ const isSecond = (page === 2)
103
+ const isBeforeLast = (page === beforeLast)
104
+ const isLast = (page === total)
105
+
106
+ // Consider the five cases below:
107
+ // [1] 2 ... 9 10
108
+ // 1 [2] 3 ... 10
109
+ // 1 ... [5] ... 10
110
+ // 1 ... 8 [9] 10
111
+ // 1 2 ... 9 [10]
112
+ const firstLi = this.getLiNode(1)
113
+ this.insertPageLi(firstLi)
114
+
115
+ const secondText = ((page <= 3) || isLast) ? 2 : DOTS
116
+ const secondLi = this.getLiNode(secondText)
117
+ this.insertPageLi(secondLi)
118
+
119
+ const thirdText = (function(p) {
120
+ if (isFirst || isLast) {
121
+ return DOTS
122
+ }
123
+ if (isSecond) {
124
+ return 3
125
+ }
126
+ if (isBeforeLast) {
127
+ return beforeLast - 1
128
+ }
129
+ return p
130
+ })(page)
131
+
132
+ const thirdLi = this.getLiNode(thirdText)
133
+ this.insertPageLi(thirdLi)
134
+
135
+ const fourthText = (function(p) {
136
+ if (isFirst || isBeforeLast || isLast) {
137
+ return beforeLast
138
+ }
139
+ if (p === (beforeLast - 1)) {
140
+ return beforeLast
141
+ }
142
+ return DOTS
143
+ })(page)
144
+
145
+ const fourthLi = this.getLiNode(fourthText)
146
+ this.insertPageLi(fourthLi)
147
+
148
+ const lastLi = this.getLiNode(total)
149
+ this.insertPageLi(lastLi)
150
+
151
+ this.setActive(page)
152
+ }
153
+
154
+ isInputView() {
155
+ const { total } = this
156
+ return ((total > this.maxVisiblePage) && (total > 5))
157
+ }
158
+
159
+ drawPages() {
160
+
161
+ this.clearPages()
162
+
163
+ if (this.isInputView()) {
164
+ this.drawPagesWithInput()
165
+ }
166
+ else {
167
+ this.drawRegularPages()
168
+ }
169
+ }
170
+
171
+ isPrevBtn(target) {
172
+ return ('prev' in target.dataset)
173
+ }
174
+
175
+ isNextBtn(target) {
176
+ return ('next' in target.dataset)
177
+ }
178
+
179
+ isPageBtn(target) {
180
+ return ('pageItem' in target.dataset)
181
+ }
182
+
183
+ isDotBtn(target) {
184
+ return ('pageDots' in target.dataset)
185
+ }
186
+
187
+ handlePageClick(target) {
188
+ const page = toInt(target.dataset.pageItem)
189
+ if (page === this.page) {
190
+ return
191
+ }
192
+ this.setActive(page)
193
+ this.change(page)
194
+ }
195
+
196
+ handleDotBtnClick() {
197
+ this.input.focus()
198
+ }
199
+
200
+ setActiveAndChangeInputViewIfNeeded(page) {
201
+ if (this.isInputView()) {
202
+ return this.drawPagesWithInput(page)
203
+ }
204
+ this.setActive(page)
205
+ }
206
+
207
+ isValidPage(page) {
208
+ return (! isNaN(page)) && (page >= 1) && (page <= this.total)
209
+ }
210
+
211
+ setInputDanger() {
212
+ this.input.classList.add('is-invalid')
213
+ }
214
+
215
+ setInputNormal() {
216
+ this.input.classList.remove('is-invalid')
217
+ }
218
+
219
+ addEvents() {
220
+ this.addEvent(this.ul, 'click', event => {
221
+
222
+ let { target } = event
223
+ if (target.tagName === 'I') {
224
+ target = target.parentNode
225
+ }
226
+
227
+ const { page } = this
228
+
229
+ if (this.isNextBtn(target) && (page < this.total)) {
230
+ const nextPage = page + 1
231
+ this.setActiveAndChangeInputViewIfNeeded(nextPage)
232
+ this.change(nextPage)
233
+ }
234
+ else if (this.isPageBtn(target)) {
235
+ this.handlePageClick(target)
236
+ }
237
+ else if (this.isPrevBtn(target) && (page > 1)) {
238
+ const prevPage = page - 1
239
+ this.setActiveAndChangeInputViewIfNeeded(prevPage)
240
+ this.change(prevPage)
241
+ }
242
+ else if (this.isDotBtn(target)) {
243
+ this.handleDotBtnClick()
244
+ }
245
+ })
246
+
247
+ this.addEvent(this.input, 'change', event => {
248
+ const page = toInt(event.target.value)
249
+
250
+ if (! this.isValidPage(page)) {
251
+ return this.setInputDanger()
252
+ }
253
+ this.setInputNormal()
254
+ this.setActiveAndChangeInputViewIfNeeded(page)
255
+ })
256
+ }
257
+
258
+ destroy() {
259
+ this.clearPages()
260
+ }
261
+ }
data/src/js/index.js CHANGED
@@ -19,6 +19,7 @@ import MonthMenu from './components/MonthMenu'
19
19
  import Menu from './components/Menu'
20
20
  import Modal from './components/Modal'
21
21
  import Navbar from './components/Navbar'
22
+ import Pagination from './components/Pagination'
22
23
  import PieChart from './components/PieChart'
23
24
  import Radio from './components/Radio'
24
25
  import SearchDropdown from './components/SearchDropdown'
@@ -58,6 +59,7 @@ export {
58
59
  Menu,
59
60
  Modal,
60
61
  Navbar,
62
+ Pagination,
61
63
  PieChart,
62
64
  Radio,
63
65
  SearchDropdown,
@@ -1,9 +1,11 @@
1
1
  // @superlanding
2
2
  import dateToTimestamp from '@superlanding/datetotimestamp'
3
+ import extend from '@superlanding/extend'
3
4
  import getDomPos from '@superlanding/getdompos'
4
5
  import getScrollLeft from '@superlanding/getscrollleft'
5
6
  import getScrollTop from '@superlanding/getscrolltop'
6
7
  import timestampToDate from '@superlanding/timestamptodate'
8
+ import toInt from '@superlanding/toint'
7
9
  import toPixel from '@superlanding/topixel'
8
10
 
9
11
  // date-fns
@@ -47,10 +49,12 @@ import mem from 'mem'
47
49
  export {
48
50
  // @superlanding
49
51
  dateToTimestamp,
52
+ extend,
50
53
  getDomPos,
51
54
  getScrollLeft,
52
55
  getScrollTop,
53
56
  timestampToDate,
57
+ toInt,
54
58
  toPixel,
55
59
 
56
60
  // date-fns
@@ -1,8 +1,4 @@
1
- .pagination {
2
- display: flex;
3
- list-style: none;
4
- flex-wrap: wrap;
5
- }
1
+ $page-active-color: #3c4258;
6
2
 
7
3
  %page-link {
8
4
  border-radius: 3px;
@@ -15,9 +11,58 @@
15
11
  &:disabled {
16
12
  cursor: not-allowed;
17
13
  }
14
+ &.js-active,
18
15
  &:hover {
19
- background-color: #3c4258;
16
+ background-color: $page-active-color;
17
+ color: #fff;
18
+ }
19
+ }
20
+
21
+ .pagination-wrap {
22
+ display: flex;
23
+ > .input-group {
24
+ width: initial;
25
+ }
26
+ > .input,
27
+ > .input-group > .input {
28
+ width: 7em;
29
+ &.is-invalid:focus {
30
+ box-shadow: 0 0 0 1px #cd3d64;
31
+ color: #cd3d64;
32
+ }
33
+ }
34
+ .input-group-append > .input-group-text {
35
+ background-color: $page-active-color;
20
36
  color: #fff;
37
+ height: 100%;
38
+ }
39
+ @media (max-width: $screen-sm) {
40
+ display: block;
41
+ > .input-group {
42
+ margin-top: 1em;
43
+ }
44
+ > .pagination {
45
+ justify-content: space-between;
46
+ }
47
+ }
48
+ }
49
+
50
+ .pagination {
51
+ display: flex;
52
+ list-style: none;
53
+ flex-wrap: wrap;
54
+ @media (max-width: $screen-sm) {
55
+ .page-link {
56
+ margin-left: 3px;
57
+ margin-right: 3px;
58
+ }
59
+ .page-link,
60
+ .page-link-btn {
61
+ padding: .2em .5em;
62
+ i {
63
+ font-size: 12px;
64
+ }
65
+ }
21
66
  }
22
67
  }
23
68
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beyond-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.284
4
+ version: 0.0.285
5
5
  platform: ruby
6
6
  authors:
7
7
  - kmsheng
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-03-09 00:00:00.000000000 Z
12
+ date: 2021-03-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sassc
@@ -143,6 +143,7 @@ files:
143
143
  - src/js/components/MonthMenu.js
144
144
  - src/js/components/Monthpicker.js
145
145
  - src/js/components/Navbar.js
146
+ - src/js/components/Pagination.js
146
147
  - src/js/components/PieChart.js
147
148
  - src/js/components/Radio.js
148
149
  - src/js/components/SearchDropdown.js