@knowark/componarkjs 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Makefile +49 -0
- package/README.md +47 -0
- package/knowarkjs.code-workspace +29 -0
- package/lib/base/component/README.rst +113 -0
- package/lib/base/component/component.js +115 -0
- package/lib/base/component/component.test.js +327 -0
- package/lib/base/component/index.js +3 -0
- package/lib/base/index.js +1 -0
- package/lib/base/styles/index.js +3 -0
- package/lib/base/styles/styles.js +320 -0
- package/lib/base/utils/define.js +21 -0
- package/lib/base/utils/define.test.js +62 -0
- package/lib/base/utils/format.js +24 -0
- package/lib/base/utils/format.test.js +19 -0
- package/lib/base/utils/helpers.js +96 -0
- package/lib/base/utils/helpers.test.js +154 -0
- package/lib/base/utils/index.js +5 -0
- package/lib/base/utils/slots.js +18 -0
- package/lib/base/utils/slots.test.js +52 -0
- package/lib/base/utils/uuid.js +9 -0
- package/lib/base/utils/uuid.test.js +19 -0
- package/lib/components/audio/README.md +22 -0
- package/lib/components/audio/components/audio.js +103 -0
- package/lib/components/audio/components/audio.test.js +127 -0
- package/lib/components/audio/index.js +1 -0
- package/lib/components/audio/styles/ark.css.js +83 -0
- package/lib/components/audio/styles/index.js +2 -0
- package/lib/components/camera/README.md +64 -0
- package/lib/components/camera/components/camera.js +85 -0
- package/lib/components/camera/components/camera.test.js +104 -0
- package/lib/components/camera/index.js +1 -0
- package/lib/components/camera/styles/ark.css.js +17 -0
- package/lib/components/camera/styles/index.js +2 -0
- package/lib/components/capture/components/capture.js +54 -0
- package/lib/components/capture/components/capture.test.js +112 -0
- package/lib/components/capture/index.js +1 -0
- package/lib/components/droparea/README.md +51 -0
- package/lib/components/droparea/components/droparea-preview.js +159 -0
- package/lib/components/droparea/components/droparea-preview.test.js +105 -0
- package/lib/components/droparea/components/droparea.js +165 -0
- package/lib/components/droparea/components/droparea.test.js +320 -0
- package/lib/components/droparea/index.js +1 -0
- package/lib/components/droparea/styles/ark.css.js +235 -0
- package/lib/components/droparea/styles/index.js +3 -0
- package/lib/components/emit/components/emit.js +33 -0
- package/lib/components/emit/components/emit.test.js +138 -0
- package/lib/components/emit/index.js +1 -0
- package/lib/components/index.js +9 -0
- package/lib/components/list/README.md +103 -0
- package/lib/components/list/components/item.test.js +93 -0
- package/lib/components/list/components/list.item.js +22 -0
- package/lib/components/list/components/list.js +96 -0
- package/lib/components/list/components/list.test.js +267 -0
- package/lib/components/list/index.js +2 -0
- package/lib/components/paginator/README.md +32 -0
- package/lib/components/paginator/components/paginator.js +110 -0
- package/lib/components/paginator/components/paginator.test.js +131 -0
- package/lib/components/paginator/index.js +1 -0
- package/lib/components/paginator/styles/ark.css.js +196 -0
- package/lib/components/paginator/styles/index.js +2 -0
- package/lib/components/spinner/README.md +41 -0
- package/lib/components/spinner/components/spinner.js +105 -0
- package/lib/components/spinner/components/spinner.test.js +50 -0
- package/lib/components/spinner/index.js +1 -0
- package/lib/components/spinner/styles/ark.css.js +658 -0
- package/lib/components/spinner/styles/index.js +2 -0
- package/lib/components/splitview/README.md +63 -0
- package/lib/components/splitview/components/splitview.detail.js +46 -0
- package/lib/components/splitview/components/splitview.detail.test.js +92 -0
- package/lib/components/splitview/components/splitview.js +69 -0
- package/lib/components/splitview/components/splitview.master.js +26 -0
- package/lib/components/splitview/components/splitview.master.test.js +55 -0
- package/lib/components/splitview/components/splitview.test.js +76 -0
- package/lib/components/splitview/index.js +3 -0
- package/lib/components/translate/README.md +56 -0
- package/lib/components/translate/components/translate.js +100 -0
- package/lib/components/translate/components/translate.test.js +226 -0
- package/lib/components/translate/index.js +1 -0
- package/lib/index.js +2 -0
- package/package.json +68 -0
- package/showcase/design/.htaccess +8 -0
- package/showcase/design/core/factories/development/development.factory.js +5 -0
- package/showcase/design/core/factories/development/index.js +1 -0
- package/showcase/design/core/factories/index.js +11 -0
- package/showcase/design/core/factories/standard.factory.js +19 -0
- package/showcase/design/index.html +22 -0
- package/showcase/design/index.js +7 -0
- package/showcase/design/screens/base/audio/audioDemo.js +32 -0
- package/showcase/design/screens/base/audio/index.js +25 -0
- package/showcase/design/screens/base/camera/cameraDemo.js +83 -0
- package/showcase/design/screens/base/camera/index.js +25 -0
- package/showcase/design/screens/base/droparea/dropareaDemo.js +88 -0
- package/showcase/design/screens/base/droparea/index.js +25 -0
- package/showcase/design/screens/base/index.js +42 -0
- package/showcase/design/screens/base/list/index.js +25 -0
- package/showcase/design/screens/base/list/listDemo.js +89 -0
- package/showcase/design/screens/base/paginator/index.js +25 -0
- package/showcase/design/screens/base/paginator/paginatorDemo.js +82 -0
- package/showcase/design/screens/base/root.component.js +294 -0
- package/showcase/design/screens/base/root.routes.js +28 -0
- package/showcase/design/screens/base/spinner/index.js +25 -0
- package/showcase/design/screens/base/spinner/spinnerDemo.js +55 -0
- package/showcase/design/screens/base/splitview/detailDemo.js +40 -0
- package/showcase/design/screens/base/splitview/index.js +25 -0
- package/showcase/design/screens/base/splitview/splitViewDemo.js +58 -0
- package/showcase/design/screens/base/translate/index.js +20 -0
- package/showcase/design/screens/base/translate/translateDemo.js +43 -0
- package/showcase/design/screens/main.js +12 -0
- package/showcase/design/screens/screens.routes.js +23 -0
- package/showcase/index.html +86 -0
- package/showcase/index.js +5 -0
- package/showcase/locales/en/default.json +5 -0
- package/showcase/locales/es/default.json +5 -0
- package/showcase/locales/fr/default.json +5 -0
- package/tsconfig.json +23 -0
- package/webpack.config.cjs +118 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import './list.js'
|
|
2
|
+
|
|
3
|
+
describe('List', () => {
|
|
4
|
+
let container = null
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
container = document.createElement('div')
|
|
8
|
+
document.body.appendChild(container)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
container.remove()
|
|
13
|
+
container = null
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('can be instantiated', () => {
|
|
17
|
+
container.innerHTML = `
|
|
18
|
+
<ark-list></ark-list>
|
|
19
|
+
`
|
|
20
|
+
|
|
21
|
+
const list = container.querySelector('ark-list')
|
|
22
|
+
expect(list).toEqual(list.init())
|
|
23
|
+
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('can be instantiated with items', async () => {
|
|
27
|
+
container.innerHTML = `
|
|
28
|
+
<ark-list></ark-list>
|
|
29
|
+
`
|
|
30
|
+
|
|
31
|
+
const list = container.querySelector('ark-list')
|
|
32
|
+
list.source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
33
|
+
list.render()
|
|
34
|
+
|
|
35
|
+
const items = list.selectAll('ark-list-item')
|
|
36
|
+
|
|
37
|
+
expect(items.length).toEqual(4)
|
|
38
|
+
expect(items[0].textContent.trim()).toEqual('Colombia')
|
|
39
|
+
expect(items[1].textContent.trim()).toEqual('Uruguay')
|
|
40
|
+
expect(items[2].textContent.trim()).toEqual('Brasil')
|
|
41
|
+
expect(items[3].textContent.trim()).toEqual('Perú')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('can be instantiated with items click-disabled', () => {
|
|
45
|
+
expect.assertions(5)
|
|
46
|
+
container.innerHTML = `
|
|
47
|
+
<ark-list click-disabled></ark-list
|
|
48
|
+
`
|
|
49
|
+
|
|
50
|
+
const list = container.querySelector('ark-list')
|
|
51
|
+
list.source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
52
|
+
list.render()
|
|
53
|
+
const items = list.selectAll('ark-list-item')
|
|
54
|
+
|
|
55
|
+
expect(items.length).toEqual(4)
|
|
56
|
+
expect(items[0].textContent.trim()).toEqual('Colombia')
|
|
57
|
+
expect(items[1].textContent.trim()).toEqual('Uruguay')
|
|
58
|
+
expect(items[2].textContent.trim()).toEqual('Brasil')
|
|
59
|
+
expect(items[3].textContent.trim()).toEqual('Perú')
|
|
60
|
+
|
|
61
|
+
list.addEventListener('list-selected', event => {
|
|
62
|
+
expect(true).toBe(false) // never reached
|
|
63
|
+
})
|
|
64
|
+
items[0].click()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('can delete', async () => {
|
|
68
|
+
container.innerHTML = `
|
|
69
|
+
<ark-list click-disabled></ark-list
|
|
70
|
+
`
|
|
71
|
+
|
|
72
|
+
const list = container.querySelector('ark-list')
|
|
73
|
+
list.source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
74
|
+
list.render()
|
|
75
|
+
|
|
76
|
+
list.delete(1)
|
|
77
|
+
let items = list.selectAll('ark-list-item')
|
|
78
|
+
expect(items.length).toEqual(3)
|
|
79
|
+
expect(items[1].textContent.trim()).toEqual('Brasil')
|
|
80
|
+
|
|
81
|
+
list.delete(0, 2)
|
|
82
|
+
items = list.selectAll('ark-list-item')
|
|
83
|
+
expect(items.length).toEqual(1)
|
|
84
|
+
expect(items[0].textContent.trim()).toEqual('Perú')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
it('can select an item when it is clicked', (done) => {
|
|
89
|
+
expect.assertions(2)
|
|
90
|
+
container.innerHTML = `
|
|
91
|
+
<ark-list></ark-list
|
|
92
|
+
`
|
|
93
|
+
|
|
94
|
+
const list = container.querySelector('ark-list')
|
|
95
|
+
list.source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
96
|
+
list.render()
|
|
97
|
+
|
|
98
|
+
const items = list.selectAll('ark-list-item')
|
|
99
|
+
expect(items.length).toEqual(4)
|
|
100
|
+
|
|
101
|
+
list.addEventListener('list-selected', event => {
|
|
102
|
+
expect(event['detail'].data).toEqual('Brasil')
|
|
103
|
+
done()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
items[2].click()
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('can be declaratively instantiated with JSON source', async () => {
|
|
110
|
+
const source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
111
|
+
|
|
112
|
+
container.innerHTML = `
|
|
113
|
+
<ark-list>
|
|
114
|
+
<data>${JSON.stringify(source)}</data>
|
|
115
|
+
</ark-list>
|
|
116
|
+
`
|
|
117
|
+
|
|
118
|
+
const list = container.querySelector('ark-list')
|
|
119
|
+
|
|
120
|
+
const items = list.selectAll('ark-list-item')
|
|
121
|
+
|
|
122
|
+
expect(items.length).toEqual(4)
|
|
123
|
+
expect(items[0].textContent.trim()).toEqual('Colombia')
|
|
124
|
+
expect(items[1].textContent.trim()).toEqual('Uruguay')
|
|
125
|
+
expect(items[2].textContent.trim()).toEqual('Brasil')
|
|
126
|
+
expect(items[3].textContent.trim()).toEqual('Perú')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('can be provided with a list item template', async () => {
|
|
130
|
+
const source = [
|
|
131
|
+
{ name: 'Colombia', code: 'CO' },
|
|
132
|
+
{ name: 'Uruguay', code: 'UY' },
|
|
133
|
+
{ name: 'Brasil', code: 'BR' },
|
|
134
|
+
{ name: 'Perú', code: 'PE' },
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
container.innerHTML = `
|
|
138
|
+
<ark-list>
|
|
139
|
+
<data hidden>${JSON.stringify(source)}</data>
|
|
140
|
+
<template>
|
|
141
|
+
<dl>
|
|
142
|
+
<div>
|
|
143
|
+
<dt>Name</dt>
|
|
144
|
+
<dd>\${this.name}</dd>
|
|
145
|
+
</div>
|
|
146
|
+
<div>
|
|
147
|
+
<dt>Code</dt>
|
|
148
|
+
<dd>\${this.code}</dd>
|
|
149
|
+
</div>
|
|
150
|
+
</dl>
|
|
151
|
+
</template>
|
|
152
|
+
</ark-list>
|
|
153
|
+
`
|
|
154
|
+
|
|
155
|
+
const list = container.querySelector('ark-list')
|
|
156
|
+
|
|
157
|
+
const items = list.selectAll('ark-list-item')
|
|
158
|
+
|
|
159
|
+
expect(items.length).toEqual(4)
|
|
160
|
+
expect(items[0].innerHTML.replace(/\s/g, '')).toEqual(
|
|
161
|
+
'<dl><div><dt>Name</dt><dd>Colombia</dd></div>' +
|
|
162
|
+
'<div><dt>Code</dt><dd>CO</dd></div></dl>')
|
|
163
|
+
expect(items[1].innerHTML.replace(/\s/g, '')).toEqual(
|
|
164
|
+
'<dl><div><dt>Name</dt><dd>Uruguay</dd></div>' +
|
|
165
|
+
'<div><dt>Code</dt><dd>UY</dd></div></dl>')
|
|
166
|
+
expect(items[2].innerHTML.replace(/\s/g, '')).toEqual(
|
|
167
|
+
'<dl><div><dt>Name</dt><dd>Brasil</dd></div>' +
|
|
168
|
+
'<div><dt>Code</dt><dd>BR</dd></div></dl>')
|
|
169
|
+
expect(items[3].innerHTML.replace(/\s/g, '')).toEqual(
|
|
170
|
+
'<dl><div><dt>Name</dt><dd>Perú</dd></div>' +
|
|
171
|
+
'<div><dt>Code</dt><dd>PE</dd></div></dl>')
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('can be render simple lists with an item template', async () => {
|
|
175
|
+
const source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
176
|
+
|
|
177
|
+
container.innerHTML = `
|
|
178
|
+
<ark-list>
|
|
179
|
+
<data hidden>${JSON.stringify(source)}</data>
|
|
180
|
+
<template>
|
|
181
|
+
<span><strong>\${this}</strong></span>
|
|
182
|
+
</template>
|
|
183
|
+
</ark-list>
|
|
184
|
+
`
|
|
185
|
+
|
|
186
|
+
const list = container.querySelector('ark-list')
|
|
187
|
+
|
|
188
|
+
const items = list.selectAll('ark-list-item')
|
|
189
|
+
|
|
190
|
+
expect(items.length).toEqual(4)
|
|
191
|
+
expect(items[0].innerHTML.replace(/\s/g, '')).toEqual(
|
|
192
|
+
'<span><strong>Colombia</strong></span>')
|
|
193
|
+
expect(items[1].innerHTML.replace(/\s/g, '')).toEqual(
|
|
194
|
+
'<span><strong>Uruguay</strong></span>')
|
|
195
|
+
expect(items[2].innerHTML.replace(/\s/g, '')).toEqual(
|
|
196
|
+
'<span><strong>Brasil</strong></span>')
|
|
197
|
+
expect(items[3].innerHTML.replace(/\s/g, '')).toEqual(
|
|
198
|
+
'<span><strong>Perú</strong></span>')
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('can be rendered again given a new array through init', async () => {
|
|
202
|
+
let source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
203
|
+
container.innerHTML = `
|
|
204
|
+
<ark-list>
|
|
205
|
+
<data hidden>${JSON.stringify(source)}</data>
|
|
206
|
+
<template>
|
|
207
|
+
<span><strong>\${this}</strong></span>
|
|
208
|
+
</template>
|
|
209
|
+
</ark-list>
|
|
210
|
+
`
|
|
211
|
+
const list = container.querySelector('ark-list')
|
|
212
|
+
let items = list.selectAll('ark-list-item')
|
|
213
|
+
expect(items.length).toEqual(4)
|
|
214
|
+
expect(items[0].innerHTML.replace(/\s/g, '')).toEqual(
|
|
215
|
+
'<span><strong>Colombia</strong></span>')
|
|
216
|
+
expect(items[1].innerHTML.replace(/\s/g, '')).toEqual(
|
|
217
|
+
'<span><strong>Uruguay</strong></span>')
|
|
218
|
+
expect(items[2].innerHTML.replace(/\s/g, '')).toEqual(
|
|
219
|
+
'<span><strong>Brasil</strong></span>')
|
|
220
|
+
expect(items[3].innerHTML.replace(/\s/g, '')).toEqual(
|
|
221
|
+
'<span><strong>Perú</strong></span>')
|
|
222
|
+
|
|
223
|
+
source = ['Ecuador', 'Panama', 'Venezuela', 'USA']
|
|
224
|
+
list.init({ source }).render()
|
|
225
|
+
items = list.selectAll('ark-list-item')
|
|
226
|
+
|
|
227
|
+
expect(items.length).toEqual(4)
|
|
228
|
+
expect(items[0].innerHTML.replace(/\s/g, '')).toEqual(
|
|
229
|
+
'<span><strong>Ecuador</strong></span>')
|
|
230
|
+
expect(items[1].innerHTML.replace(/\s/g, '')).toEqual(
|
|
231
|
+
'<span><strong>Panama</strong></span>')
|
|
232
|
+
expect(items[2].innerHTML.replace(/\s/g, '')).toEqual(
|
|
233
|
+
'<span><strong>Venezuela</strong></span>')
|
|
234
|
+
expect(items[3].innerHTML.replace(/\s/g, '')).toEqual(
|
|
235
|
+
'<span><strong>USA</strong></span>')
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it('listens to delete events to remove its items', () => {
|
|
239
|
+
let source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
240
|
+
container.innerHTML = `
|
|
241
|
+
<ark-list>
|
|
242
|
+
<data hidden>${JSON.stringify(source)}</data>
|
|
243
|
+
<template>
|
|
244
|
+
<span>
|
|
245
|
+
<strong>\${this}</strong></span>
|
|
246
|
+
<button data-\${this} onclick="this.dispatchEvent(
|
|
247
|
+
new CustomEvent('delete', { bubbles: true }))">
|
|
248
|
+
DELETE
|
|
249
|
+
</button>
|
|
250
|
+
</template>
|
|
251
|
+
</ark-list>
|
|
252
|
+
`
|
|
253
|
+
const list = container.querySelector('ark-list')
|
|
254
|
+
let button = list.querySelector('[data-colombia]')
|
|
255
|
+
|
|
256
|
+
let items = list.selectAll('ark-list-item')
|
|
257
|
+
expect(items.length).toEqual(4)
|
|
258
|
+
|
|
259
|
+
button.click()
|
|
260
|
+
|
|
261
|
+
button = list.querySelector('[data-colombia]')
|
|
262
|
+
items = list.selectAll('ark-list-item')
|
|
263
|
+
|
|
264
|
+
expect(items.length).toEqual(3)
|
|
265
|
+
expect(button).toBeNull()
|
|
266
|
+
})
|
|
267
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
PAGINATOR
|
|
2
|
+
---------
|
|
3
|
+
|
|
4
|
+
The ``ark-paginator`` component allows the user to select a specific page from a range of pages.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Examples
|
|
8
|
+
--------
|
|
9
|
+
|
|
10
|
+
**This component needs to initialize with** ``collectionSize`` **and** ``pageSize`` **properties to display the button and pages needed**
|
|
11
|
+
|
|
12
|
+
``` javascript
|
|
13
|
+
const paginator = this.select('ark-paginator')
|
|
14
|
+
paginator.init({ collectionSize: 4, pageSize: 1 }).render()
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**The** ``ark-paginator`` **component receive an** ``on-page-change`` **event that points to the data that's going to be displayed**
|
|
18
|
+
|
|
19
|
+
``` html
|
|
20
|
+
<ark-paginator listen on-page-change="updateList"></ark-paginator>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Properties
|
|
24
|
+
----------
|
|
25
|
+
|
|
26
|
+
| Component | Name | Type | Default | Description |
|
|
27
|
+
| :---------------: | :------------: | :----: | :-----: | :---------------------------------------------: |
|
|
28
|
+
| ``ark-paginator`` | pageSize | number | null | Number of pages displayed in one page |
|
|
29
|
+
| ``ark-paginator`` | collectionSize | number | null | Defines a range of pages in a number of buttons |
|
|
30
|
+
| ``ark-paginator`` | currentPage | number | null | Selects an specific page |
|
|
31
|
+
|
|
32
|
+
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Component } from '../../../base/component/index.js'
|
|
2
|
+
import styles from '../styles/index.js'
|
|
3
|
+
|
|
4
|
+
const tag = 'ark-paginator'
|
|
5
|
+
export class Paginator extends Component {
|
|
6
|
+
init (context = {}) {
|
|
7
|
+
this.binding = 'paginator-listen'
|
|
8
|
+
this.collectionSize = (
|
|
9
|
+
context.collectionSize || this.collectionSize || '120')
|
|
10
|
+
this.pageSize = context.pageSize || this.pageSize || '24'
|
|
11
|
+
this.currentPage = context.currentPage || this.currentPage || '1'
|
|
12
|
+
this.displayedPages = (
|
|
13
|
+
context.displayedPages || this.displayedPages || '12')
|
|
14
|
+
|
|
15
|
+
this.global = context.global || window
|
|
16
|
+
|
|
17
|
+
return super.init()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
reflectedProperties () {
|
|
21
|
+
return ['collectionSize', 'pageSize', 'displayedPages', 'currentPage']
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
render () {
|
|
25
|
+
this.content = /* html */ `
|
|
26
|
+
<div class="ark-paginator__body">
|
|
27
|
+
<div class="ark-paginator__buttons">
|
|
28
|
+
<button paginator-listen on-click="_first"><<</button>
|
|
29
|
+
<button paginator-listen on-click="_prev"><</button>
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<div paginator-listen on-click="_move" class="ark-paginator__pages">
|
|
33
|
+
${this.currentPages.map((page) => (
|
|
34
|
+
`<button ${page == this.currentPage ? 'active' : ''}
|
|
35
|
+
data-page="${page}">${page}</button>`)). join('')}
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
<div class="ark-paginator__buttons">
|
|
39
|
+
<button paginator-listen on-click="_next">></button>
|
|
40
|
+
<button paginator-listen on-click="_last">>></button>
|
|
41
|
+
</div>
|
|
42
|
+
</div>
|
|
43
|
+
`
|
|
44
|
+
|
|
45
|
+
return super.render()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get totalPages () {
|
|
49
|
+
return Math.ceil(this.collectionSize / this.pageSize)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get currentPages () {
|
|
53
|
+
const displayedPages = Math.min(
|
|
54
|
+
parseInt(this.displayedPages), this.totalPages)
|
|
55
|
+
const currentPage = parseInt(this.currentPage)
|
|
56
|
+
let startPage = Math.max(currentPage - Math.trunc(displayedPages / 2), 1)
|
|
57
|
+
startPage = Math.min(1 + this.totalPages - displayedPages, startPage)
|
|
58
|
+
|
|
59
|
+
return Array.from({length: displayedPages}, (_, i) => i + startPage)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_notifyChange () {
|
|
63
|
+
const page = parseInt(this.currentPage)
|
|
64
|
+
const limit = parseInt(this.pageSize)
|
|
65
|
+
const offset = (page - 1) * limit
|
|
66
|
+
this.emit('page-changed', { page, limit, offset })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @param {number} currentPage */
|
|
70
|
+
_setCurrentPage (currentPage) {
|
|
71
|
+
if (currentPage > 0 && currentPage <= this.totalPages) {
|
|
72
|
+
this.currentPage = currentPage
|
|
73
|
+
this.render()
|
|
74
|
+
this._notifyChange()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @param {Event} event */
|
|
79
|
+
_first (event) {
|
|
80
|
+
event.stopPropagation()
|
|
81
|
+
this._setCurrentPage(1)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** @param {Event} event */
|
|
85
|
+
_prev (event) {
|
|
86
|
+
event.stopPropagation()
|
|
87
|
+
this._setCurrentPage(parseInt(this.currentPage) - 1)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @param {Event} event */
|
|
91
|
+
_move (event) {
|
|
92
|
+
event.stopPropagation()
|
|
93
|
+
const page = parseInt(event.target.dataset.page)
|
|
94
|
+
this._setCurrentPage(page)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @param {Event} event */
|
|
98
|
+
_next (event) {
|
|
99
|
+
event.stopPropagation()
|
|
100
|
+
this._setCurrentPage(parseInt(this.currentPage) + 1)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** @param {Event} event */
|
|
104
|
+
_last (event) {
|
|
105
|
+
event.stopPropagation()
|
|
106
|
+
this._setCurrentPage(this.totalPages)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
Component.define(tag, Paginator, styles)
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Paginator } from './paginator.js'
|
|
2
|
+
|
|
3
|
+
describe('Paginator', () => {
|
|
4
|
+
let container = null
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
container = document.createElement('div')
|
|
8
|
+
document.body.appendChild(container)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
container.remove()
|
|
13
|
+
container = null
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('can be instantiated',()=>{
|
|
17
|
+
container.innerHTML = /* html */ `
|
|
18
|
+
<ark-paginator></ark-paginator>
|
|
19
|
+
`
|
|
20
|
+
const paginator = container.querySelector('ark-paginator')
|
|
21
|
+
|
|
22
|
+
expect(paginator).toBe(paginator.init())
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
it('can be instantiated with parameters', () => {
|
|
27
|
+
container.innerHTML = /* html */ `
|
|
28
|
+
<ark-paginator page-size="12" displayed-pages="5"></ark-paginator>
|
|
29
|
+
`
|
|
30
|
+
const paginator = container.querySelector('ark-paginator')
|
|
31
|
+
paginator.init({collectionSize: 120, currentPage: 4})
|
|
32
|
+
|
|
33
|
+
expect(paginator.pageSize).toBe('12')
|
|
34
|
+
expect(paginator.collectionSize).toBe('120')
|
|
35
|
+
expect(paginator.currentPage).toBe('4')
|
|
36
|
+
expect(paginator.displayedPages).toBe('5')
|
|
37
|
+
expect(paginator.totalPages).toBe(10)
|
|
38
|
+
|
|
39
|
+
paginator.init({collectionSize: 120, pageSize: 10})
|
|
40
|
+
expect(paginator.totalPages).toBe(12)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('computes the current shown pages', () => {
|
|
44
|
+
container.innerHTML = /* html */ `
|
|
45
|
+
<ark-paginator collection-size="120" page-size="12"
|
|
46
|
+
displayed-pages="5" current-page="1"></ark-paginator>
|
|
47
|
+
`
|
|
48
|
+
const paginator = container.querySelector('ark-paginator')
|
|
49
|
+
|
|
50
|
+
expect(paginator.currentPages).toEqual([1, 2, 3, 4, 5])
|
|
51
|
+
expect(paginator.totalPages).toEqual(10)
|
|
52
|
+
|
|
53
|
+
paginator.init({currentPage: 3})
|
|
54
|
+
expect(paginator.currentPages).toEqual([1, 2, 3, 4, 5])
|
|
55
|
+
|
|
56
|
+
paginator.init({currentPage: 4})
|
|
57
|
+
expect(paginator.currentPages).toEqual([2, 3, 4, 5, 6])
|
|
58
|
+
|
|
59
|
+
paginator.init({currentPage: 5})
|
|
60
|
+
expect(paginator.currentPages).toEqual([3, 4, 5, 6, 7])
|
|
61
|
+
|
|
62
|
+
paginator.init({currentPage: 7})
|
|
63
|
+
expect(paginator.currentPages).toEqual([5, 6, 7, 8, 9])
|
|
64
|
+
|
|
65
|
+
paginator.init({currentPage: 8})
|
|
66
|
+
expect(paginator.currentPages).toEqual([6, 7, 8, 9, 10])
|
|
67
|
+
|
|
68
|
+
paginator.init({currentPage: 9})
|
|
69
|
+
expect(paginator.currentPages).toEqual([6, 7, 8, 9, 10])
|
|
70
|
+
|
|
71
|
+
paginator.init({currentPage: 10})
|
|
72
|
+
expect(paginator.currentPages).toEqual([6, 7, 8, 9, 10])
|
|
73
|
+
|
|
74
|
+
paginator.init({displayedPages: 6, currentPage: 5})
|
|
75
|
+
expect(paginator.currentPages).toEqual([2, 3, 4, 5, 6, 7])
|
|
76
|
+
|
|
77
|
+
paginator.init({displayedPages: 6, currentPage: 7})
|
|
78
|
+
expect(paginator.currentPages).toEqual([4, 5, 6, 7, 8, 9])
|
|
79
|
+
|
|
80
|
+
paginator.init({displayedPages: 6, currentPage: 9})
|
|
81
|
+
expect(paginator.currentPages).toEqual([5, 6, 7, 8, 9, 10])
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
it('enables and notifies page changes', () => {
|
|
86
|
+
container.innerHTML = /* html */ `
|
|
87
|
+
<ark-paginator collection-size="100" page-size="10"
|
|
88
|
+
displayed-pages="5" current-page="1"></ark-paginator>
|
|
89
|
+
`
|
|
90
|
+
const paginator = container.querySelector('ark-paginator')
|
|
91
|
+
|
|
92
|
+
let pageChangedEvent = null
|
|
93
|
+
paginator.addEventListener(
|
|
94
|
+
'page-changed', (event) => pageChangedEvent = event)
|
|
95
|
+
|
|
96
|
+
paginator._setCurrentPage(4)
|
|
97
|
+
|
|
98
|
+
expect(paginator.currentPage).toEqual('4')
|
|
99
|
+
expect(pageChangedEvent).toBeTruthy()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('moves to the different pages', () => {
|
|
103
|
+
container.innerHTML = /* html */ `
|
|
104
|
+
<ark-paginator collection-size="100" page-size="10"
|
|
105
|
+
displayed-pages="5" current-page="1"></ark-paginator>
|
|
106
|
+
`
|
|
107
|
+
const paginator = container.querySelector('ark-paginator')
|
|
108
|
+
|
|
109
|
+
paginator.select('[data-page="4"]').click()
|
|
110
|
+
expect(paginator.currentPage).toEqual('4')
|
|
111
|
+
|
|
112
|
+
paginator.select('[data-page="6"]').click()
|
|
113
|
+
expect(paginator.currentPage).toEqual('6')
|
|
114
|
+
|
|
115
|
+
paginator.select('[on-click="_prev"]').click()
|
|
116
|
+
expect(paginator.currentPage).toEqual('5')
|
|
117
|
+
|
|
118
|
+
paginator.select('[on-click="_next"]').click()
|
|
119
|
+
expect(paginator.currentPage).toEqual('6')
|
|
120
|
+
|
|
121
|
+
paginator.select('[on-click="_first"]').click()
|
|
122
|
+
expect(paginator.currentPage).toEqual('1')
|
|
123
|
+
|
|
124
|
+
paginator.select('[on-click="_last"]').click()
|
|
125
|
+
expect(paginator.currentPage).toEqual('10')
|
|
126
|
+
|
|
127
|
+
paginator.select('[on-click="_next"]').click()
|
|
128
|
+
expect(paginator.currentPage).toEqual('10')
|
|
129
|
+
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Paginator } from './components/paginator.js'
|