@knowark/componarkjs 1.11.1 → 1.11.2

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.
@@ -22,26 +22,40 @@ export class Paginator extends Component {
22
22
  }
23
23
 
24
24
  render () {
25
+ this._grabSlots()
25
26
  this.content = /* html */ `
26
27
  <div class="ark-paginator__body">
27
- <div class="ark-paginator__buttons">
28
- <button paginator-listen on-click="_first">&#60;&#60;</button>
29
- <button paginator-listen on-click="_prev">&#60;</button>
28
+ <div data-start class="ark-paginator__buttons">
30
29
  </div>
31
30
 
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('')}
31
+ <div data-middle paginator-listen on-click="_move"
32
+ class="ark-paginator__pages">
36
33
  </div>
37
34
 
38
- <div class="ark-paginator__buttons">
39
- <button paginator-listen on-click="_next">&#62;</button>
40
- <button paginator-listen on-click="_last">&#62;&#62;</button>
35
+ <div data-end class="ark-paginator__buttons">
41
36
  </div>
42
37
  </div>
43
38
  `
44
39
 
40
+ this.querySelector('[data-start]').appendChild(
41
+ this._buildButton(this.firstButton, '_first', '<<'))
42
+ this.querySelector('[data-start]').appendChild(
43
+ this._buildButton(this.previousButton, '_prev', '<'))
44
+
45
+ for (const page of this.currentPages) {
46
+ const attributes = [['data-page', page]]
47
+ if ((Number(page) === Number(this.currentPage))) {
48
+ attributes.unshift(['active', ''])
49
+ }
50
+ this.querySelector('[data-middle]').appendChild(
51
+ this._buildButton(this.pageButton, null, page, attributes))
52
+ }
53
+
54
+ this.querySelector('[data-end]').appendChild(
55
+ this._buildButton(this.nextButton, '_next', '>'))
56
+ this.querySelector('[data-end]').appendChild(
57
+ this._buildButton(this.lastButton, '_last', '>>'))
58
+
45
59
  return super.render()
46
60
  }
47
61
 
@@ -105,6 +119,30 @@ export class Paginator extends Component {
105
119
  event.stopPropagation()
106
120
  this._setCurrentPage(this.totalPages)
107
121
  }
122
+
123
+ _grabSlots() {
124
+ const [pageButton] = [this.slots.general].flat()
125
+ this.pageButton = this.pageButton ?? pageButton
126
+ const [firstButton] = [this.slots.first].flat()
127
+ this.firstButton = this.firstButton ?? firstButton
128
+ const [previousButton] = [this.slots.previous].flat()
129
+ this.previousButton = this.previousButton ?? previousButton
130
+ const [nextButton] = [this.slots.next].flat()
131
+ this.nextButton = this.nextButton ?? nextButton
132
+ const [lastButton] = [this.slots.last].flat()
133
+ this.lastButton = this.lastButton ?? lastButton
134
+ }
135
+
136
+ _buildButton (element, handler, content, attributes = []) {
137
+ const button = element ?? document.createElement('button')
138
+ button.textContent = button.textContent || content
139
+ if (handler) {
140
+ button.setAttribute('paginator-listen', '')
141
+ button.setAttribute('on-click', handler)
142
+ }
143
+ attributes.forEach(([key, value]) => button.setAttribute(key, value))
144
+ return button
145
+ }
108
146
  }
109
147
 
110
148
  Component.define(tag, Paginator, styles)
@@ -125,4 +125,24 @@ describe('Paginator', () => {
125
125
  paginator.select('[on-click="_next"]').click()
126
126
  expect(paginator.currentPage).toEqual('10')
127
127
  })
128
+
129
+ it('can be provided with custom styleable elements', () => {
130
+ container.innerHTML = /* html */ `
131
+ <ark-paginator page-size="12" displayed-pages="5">
132
+ <button data-first slot="first"></button>
133
+ <button data-previous slot="previous"></button>
134
+ <button data-page-general></button>
135
+ <button data-next slot="next"></button>
136
+ <button data-last slot="last"></button>
137
+ </ark-paginator>
138
+ `
139
+
140
+ const paginator = container.querySelector('ark-paginator')
141
+
142
+ expect(paginator.querySelector('[data-first]')).toBeTruthy()
143
+ expect(paginator.querySelector('[data-previous]')).toBeTruthy()
144
+ expect(paginator.querySelector('[data-page-general]')).toBeTruthy()
145
+ expect(paginator.querySelector('[data-next]')).toBeTruthy()
146
+ expect(paginator.querySelector('[data-last]')).toBeTruthy()
147
+ })
128
148
  })
@@ -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">&#60;&#60;</button>
29
+ <button paginator-listen on-click="_prev">&#60;</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">&#62;</button>
40
+ <button paginator-listen on-click="_last">&#62;&#62;</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,128 @@
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
+ it('can be instantiated with parameters', () => {
26
+ container.innerHTML = /* html */ `
27
+ <ark-paginator page-size="12" displayed-pages="5"></ark-paginator>
28
+ `
29
+ const paginator = container.querySelector('ark-paginator')
30
+ paginator.init({ collectionSize: 120, currentPage: 4 })
31
+
32
+ expect(paginator.pageSize).toBe('12')
33
+ expect(paginator.collectionSize).toBe('120')
34
+ expect(paginator.currentPage).toBe('4')
35
+ expect(paginator.displayedPages).toBe('5')
36
+ expect(paginator.totalPages).toBe(10)
37
+
38
+ paginator.init({ collectionSize: 120, pageSize: 10 })
39
+ expect(paginator.totalPages).toBe(12)
40
+ })
41
+
42
+ it('computes the current shown pages', () => {
43
+ container.innerHTML = /* html */ `
44
+ <ark-paginator collection-size="120" page-size="12"
45
+ displayed-pages="5" current-page="1"></ark-paginator>
46
+ `
47
+ const paginator = container.querySelector('ark-paginator')
48
+
49
+ expect(paginator.currentPages).toEqual([1, 2, 3, 4, 5])
50
+ expect(paginator.totalPages).toEqual(10)
51
+
52
+ paginator.init({ currentPage: 3 })
53
+ expect(paginator.currentPages).toEqual([1, 2, 3, 4, 5])
54
+
55
+ paginator.init({ currentPage: 4 })
56
+ expect(paginator.currentPages).toEqual([2, 3, 4, 5, 6])
57
+
58
+ paginator.init({ currentPage: 5 })
59
+ expect(paginator.currentPages).toEqual([3, 4, 5, 6, 7])
60
+
61
+ paginator.init({ currentPage: 7 })
62
+ expect(paginator.currentPages).toEqual([5, 6, 7, 8, 9])
63
+
64
+ paginator.init({ currentPage: 8 })
65
+ expect(paginator.currentPages).toEqual([6, 7, 8, 9, 10])
66
+
67
+ paginator.init({ currentPage: 9 })
68
+ expect(paginator.currentPages).toEqual([6, 7, 8, 9, 10])
69
+
70
+ paginator.init({ currentPage: 10 })
71
+ expect(paginator.currentPages).toEqual([6, 7, 8, 9, 10])
72
+
73
+ paginator.init({ displayedPages: 6, currentPage: 5 })
74
+ expect(paginator.currentPages).toEqual([2, 3, 4, 5, 6, 7])
75
+
76
+ paginator.init({ displayedPages: 6, currentPage: 7 })
77
+ expect(paginator.currentPages).toEqual([4, 5, 6, 7, 8, 9])
78
+
79
+ paginator.init({ displayedPages: 6, currentPage: 9 })
80
+ expect(paginator.currentPages).toEqual([5, 6, 7, 8, 9, 10])
81
+ })
82
+
83
+ it('enables and notifies page changes', () => {
84
+ container.innerHTML = /* html */ `
85
+ <ark-paginator collection-size="100" page-size="10"
86
+ displayed-pages="5" current-page="1"></ark-paginator>
87
+ `
88
+ const paginator = container.querySelector('ark-paginator')
89
+
90
+ let pageChangedEvent = null
91
+ paginator.addEventListener(
92
+ 'page-changed', (event) => pageChangedEvent = event)
93
+
94
+ paginator._setCurrentPage(4)
95
+
96
+ expect(paginator.currentPage).toEqual('4')
97
+ expect(pageChangedEvent).toBeTruthy()
98
+ })
99
+
100
+ it('moves to the different pages', () => {
101
+ container.innerHTML = /* html */ `
102
+ <ark-paginator collection-size="100" page-size="10"
103
+ displayed-pages="5" current-page="1"></ark-paginator>
104
+ `
105
+ const paginator = container.querySelector('ark-paginator')
106
+
107
+ paginator.select('[data-page="4"]').click()
108
+ expect(paginator.currentPage).toEqual('4')
109
+
110
+ paginator.select('[data-page="6"]').click()
111
+ expect(paginator.currentPage).toEqual('6')
112
+
113
+ paginator.select('[on-click="_prev"]').click()
114
+ expect(paginator.currentPage).toEqual('5')
115
+
116
+ paginator.select('[on-click="_next"]').click()
117
+ expect(paginator.currentPage).toEqual('6')
118
+
119
+ paginator.select('[on-click="_first"]').click()
120
+ expect(paginator.currentPage).toEqual('1')
121
+
122
+ paginator.select('[on-click="_last"]').click()
123
+ expect(paginator.currentPage).toEqual('10')
124
+
125
+ paginator.select('[on-click="_next"]').click()
126
+ expect(paginator.currentPage).toEqual('10')
127
+ })
128
+ })
@@ -0,0 +1 @@
1
+ export { Paginator } from './components/paginator.js'
@@ -0,0 +1,196 @@
1
+ const css = String.raw; export default css`
2
+ .ark-paginator {
3
+ background: var(--canvas, white);
4
+ color: var(--ink, black);
5
+ }
6
+
7
+ /*-------------- THEME-------------- */
8
+
9
+ .ark-paginator[background=primary] {
10
+ background: var(--primary);
11
+ color: white;
12
+ }
13
+
14
+ .ark-paginator.ark-paginator[color=primary] {
15
+ color: var(--primary);
16
+ }
17
+
18
+ .ark-paginator[background=secondary] {
19
+ background: var(--secondary);
20
+ color: white;
21
+ }
22
+
23
+ .ark-paginator.ark-paginator[color=secondary] {
24
+ color: var(--secondary);
25
+ }
26
+
27
+ .ark-paginator[background=success] {
28
+ background: var(--success);
29
+ color: white;
30
+ }
31
+
32
+ .ark-paginator.ark-paginator[color=success] {
33
+ color: var(--success);
34
+ }
35
+
36
+ .ark-paginator[background=danger] {
37
+ background: var(--danger);
38
+ color: white;
39
+ }
40
+
41
+ .ark-paginator.ark-paginator[color=danger] {
42
+ color: var(--danger);
43
+ }
44
+
45
+ .ark-paginator[background=warning] {
46
+ background: var(--warning);
47
+ color: white;
48
+ }
49
+
50
+ .ark-paginator.ark-paginator[color=warning] {
51
+ color: var(--warning);
52
+ }
53
+
54
+ .ark-paginator[background=info] {
55
+ background: var(--info);
56
+ color: white;
57
+ }
58
+
59
+ .ark-paginator.ark-paginator[color=info] {
60
+ color: var(--info);
61
+ }
62
+
63
+ .ark-paginator[background=dark] {
64
+ background: var(--dark);
65
+ color: white;
66
+ }
67
+
68
+ .ark-paginator.ark-paginator[color=dark] {
69
+ color: var(--dark);
70
+ }
71
+
72
+ .ark-paginator[background=muted] {
73
+ background: var(--muted);
74
+ color: white;
75
+ }
76
+
77
+ .ark-paginator.ark-paginator[color=muted] {
78
+ color: var(--muted);
79
+ }
80
+
81
+ .ark-paginator[background=light] {
82
+ background: var(--light);
83
+ color: white;
84
+ }
85
+
86
+ .ark-paginator.ark-paginator[color=light] {
87
+ color: var(--light);
88
+ }
89
+
90
+ /*-------------------------------------------*/
91
+
92
+ .ark-paginator__body {
93
+ width: 100%;
94
+ display: grid;
95
+ grid-auto-flow: column;
96
+ justify-content: center;
97
+ align-items: center;
98
+ }
99
+
100
+ .ark-paginator__footer {
101
+ display: grid;
102
+ grid-auto-flow: column;
103
+ justify-content: center;
104
+ }
105
+
106
+ .ark-paginator__footer small[data-info] {
107
+ color: var(--primary);
108
+ }
109
+
110
+ .ark-paginator__buttons {
111
+ margin: 10px;
112
+ }
113
+
114
+ .ark-paginator__buttons button {
115
+ width: 1.8rem;
116
+ height: 1.8rem;
117
+ border: 0;
118
+ margin: 0.1rem;
119
+ background: inherit;
120
+ }
121
+
122
+ .ark-paginator__buttons button:focus {
123
+ outline: none;
124
+ }
125
+
126
+ .ark-paginator__buttons button:active {
127
+ border: 1px solid black;
128
+ }
129
+
130
+ .ark-paginator__buttons button[on-click=_prev],
131
+ .ark-paginator__buttons button[on-click=_next] {
132
+ border: 2px solid rgba(128, 128, 128, 0.411);
133
+ background: white;
134
+ }
135
+
136
+ .ark-paginator__buttons button[on-click=_first],
137
+ .ark-paginator__buttons button[on-click=_last] {
138
+ border: 2px solid rgba(128, 128, 128, 0.411);
139
+ background: white;
140
+ }
141
+
142
+ .ark-paginator__pages {
143
+ display: grid;
144
+ grid-auto-flow: column;
145
+ }
146
+
147
+ .ark-paginator__pages button {
148
+ min-width: 1.8rem;
149
+ width: 1.8rem;
150
+ min-height: 1.8rem;
151
+ height: 1.8rem;
152
+ border: 1px solid var(--primary);
153
+ margin: 0.1rem;
154
+ text-align: center;
155
+ }
156
+
157
+ .ark-paginator__pages button:focus {
158
+ outline: none;
159
+ }
160
+
161
+ .ark-paginator__pages button[active] {
162
+ background: var(--primary, blue);
163
+ color: white;
164
+ }
165
+
166
+ .ark-paginator__buttons button {
167
+ border-radius: 4px;
168
+ transition: transform 0.1s ease-in-out;
169
+ }
170
+
171
+ .ark-paginator__buttons button:hover {
172
+ transform: scale(1.05);
173
+ }
174
+
175
+ .ark-paginator__pages {
176
+ display: grid;
177
+ }
178
+
179
+ .ark-paginator__pages button {
180
+ border-radius: 4px;
181
+ transition: transform 0.1s ease-in-out;
182
+ }
183
+
184
+ .ark-paginator__pages button:focus {
185
+ outline: none;
186
+ }
187
+
188
+ .ark-paginator__pages button[active] {
189
+ background: var(--primary, blue);
190
+ color: white;
191
+ }
192
+
193
+ .ark-paginator__pages button:hover {
194
+ transform: scale(1.05);
195
+ }
196
+ `
@@ -0,0 +1,2 @@
1
+ import styles from './ark.css.js'
2
+ export default styles
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowark/componarkjs",
3
- "version": "1.11.1",
3
+ "version": "1.11.2",
4
4
  "author": "Knowark",
5
5
  "description": "Knowark's Web Components Library",
6
6
  "license": "ISC",
@@ -8,7 +8,7 @@
8
8
  <script type="importmap">
9
9
  {
10
10
  "imports": {
11
- "@knowark/componarkjs": "https://unpkg.com/@knowark/componarkjs@1.7.3/lib/index.js"
11
+ "@knowark/componarkjs": "https://unpkg.com/@knowark/componarkjs@1.11.1/lib/index.js"
12
12
  }
13
13
  }
14
14
  </script>
@@ -11,17 +11,17 @@ module.exports = (env, argv) => {
11
11
  const commonConfig = {
12
12
  mode: argv.mode,
13
13
  entry: {
14
- showcase: './lib/showcase/design/index.js'
14
+ showcase: './showcase/index.js'
15
15
  },
16
16
  plugins: [
17
17
  new CleanWebpackPlugin(),
18
18
  new HtmlWebpackPlugin({
19
19
  chunks: ['showcase', 'runtime'],
20
20
  title: 'Componark',
21
- template: './lib/showcase/design/index.html'
21
+ template: './showcase/index.html'
22
22
  }),
23
23
  new CopyWebpackPlugin({
24
- patterns: ['lib/showcase/design/.htaccess']
24
+ patterns: ['./showcase/.htaccess']
25
25
  }),
26
26
  new DefinePlugin({
27
27
  PRODUCTION: !devMode,
@@ -51,9 +51,9 @@ module.exports = (env, argv) => {
51
51
  },
52
52
  resolve: {
53
53
  alias: {
54
- base: path.resolve(__dirname, './lib/base/'),
55
- components: path.resolve(__dirname, './lib/components/'),
56
- screens: path.resolve(__dirname, './lib/showcase/screens/')
54
+ base: path.resolve(__dirname, './base/'),
55
+ components: path.resolve(__dirname, './components/'),
56
+ screens: path.resolve(__dirname, './showcase/screens/')
57
57
  }
58
58
  }
59
59
  }
@@ -77,7 +77,7 @@ module.exports = (env, argv) => {
77
77
  name: 'root',
78
78
  mode: argv.mode,
79
79
  entry: {
80
- index: './lib/showcase/index.js'
80
+ index: './showcase/index.js'
81
81
  },
82
82
  plugins: [
83
83
  new CleanWebpackPlugin(),
@@ -88,12 +88,12 @@ module.exports = (env, argv) => {
88
88
  }),
89
89
  new CopyWebpackPlugin({
90
90
  patterns: [
91
- { from: 'lib/showcase/locales/', to: 'locales/' }
91
+ { from: './showcase/locales/', to: 'locales/' }
92
92
  ]
93
93
  }),
94
94
  new HtmlWebpackPlugin({
95
95
  title: 'Componark',
96
- template: './lib/showcase/index.html'
96
+ template: './showcase/index.html'
97
97
  })
98
98
  ]
99
99
  }