@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,138 @@
|
|
|
1
|
+
import './emit.js'
|
|
2
|
+
|
|
3
|
+
describe('Emit', () => {
|
|
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-emit></ark-emit>
|
|
19
|
+
`
|
|
20
|
+
|
|
21
|
+
const emit = container.querySelector('ark-emit')
|
|
22
|
+
expect(emit).toEqual(emit.init())
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('emits a target event when handling the one listened at', () => {
|
|
26
|
+
container.innerHTML = `
|
|
27
|
+
<ark-emit receive="click" dispatch="custom">
|
|
28
|
+
<button>Sending a click event when pressed!</button>
|
|
29
|
+
</ark-emit>
|
|
30
|
+
`
|
|
31
|
+
const emit = container.querySelector('ark-emit')
|
|
32
|
+
let customCalled = false
|
|
33
|
+
emit.addEventListener('custom', (event) => { customCalled = true })
|
|
34
|
+
|
|
35
|
+
container.querySelector('button').click()
|
|
36
|
+
|
|
37
|
+
expect(customCalled).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('listens to click events and dispatches emit events by default', () => {
|
|
41
|
+
container.innerHTML = `
|
|
42
|
+
<ark-emit>
|
|
43
|
+
<button>Sending a click event when pressed!</button>
|
|
44
|
+
</ark-emit>
|
|
45
|
+
`
|
|
46
|
+
const emit = container.querySelector('ark-emit')
|
|
47
|
+
let emitCalled = false
|
|
48
|
+
emit.addEventListener('emit', (event) => { emitCalled = true })
|
|
49
|
+
|
|
50
|
+
container.querySelector('button').click()
|
|
51
|
+
|
|
52
|
+
expect(emitCalled).toBe(true)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('it carries over the json data given to it', () => {
|
|
56
|
+
expect.assertions(2)
|
|
57
|
+
const data = {
|
|
58
|
+
id: '7a792bda-6f8a-44ed-a63a-a48bba1e76bf',
|
|
59
|
+
name: 'John Doe'
|
|
60
|
+
}
|
|
61
|
+
container.innerHTML = `
|
|
62
|
+
<ark-emit>
|
|
63
|
+
<data>${JSON.stringify(data)}</data>
|
|
64
|
+
<button>Sending a click event when pressed!</button>
|
|
65
|
+
</ark-emit>
|
|
66
|
+
`
|
|
67
|
+
|
|
68
|
+
const emit = container.querySelector('ark-emit')
|
|
69
|
+
|
|
70
|
+
const dataElement = emit.querySelector('data')
|
|
71
|
+
expect(dataElement).toBe(null)
|
|
72
|
+
|
|
73
|
+
emit.addEventListener('emit', (event) => {
|
|
74
|
+
const detail = event.detail
|
|
75
|
+
expect(detail).toEqual(data)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
container.querySelector('button').click()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('it merges upstream event details', () => {
|
|
82
|
+
expect.assertions(1)
|
|
83
|
+
const data = {
|
|
84
|
+
id: '7a792bda-6f8a-44ed-a63a-a48bba1e76bf',
|
|
85
|
+
name: 'John Doe'
|
|
86
|
+
}
|
|
87
|
+
container.innerHTML = `
|
|
88
|
+
<ark-emit data-outer receive="inner" dispatch="outer">
|
|
89
|
+
<data>
|
|
90
|
+
{
|
|
91
|
+
"age": 34,
|
|
92
|
+
"job": "programmer"
|
|
93
|
+
}
|
|
94
|
+
</data>
|
|
95
|
+
<ark-emit data-inner dispatch="inner">
|
|
96
|
+
<data>${JSON.stringify(data)}</data>
|
|
97
|
+
<button>Sending a click event when pressed!</button>
|
|
98
|
+
</ark-emit>
|
|
99
|
+
</ark-emit>
|
|
100
|
+
`
|
|
101
|
+
|
|
102
|
+
const emit = container.querySelector('[data-outer]')
|
|
103
|
+
emit.addEventListener('outer', (event) => {
|
|
104
|
+
const detail = event.detail
|
|
105
|
+
expect(detail).toEqual({
|
|
106
|
+
id: '7a792bda-6f8a-44ed-a63a-a48bba1e76bf',
|
|
107
|
+
name: 'John Doe',
|
|
108
|
+
age: 34,
|
|
109
|
+
job: "programmer"
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
container.querySelector('[data-inner]').click()
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('receives the target.value of regular events', async () => {
|
|
117
|
+
expect.assertions(1)
|
|
118
|
+
|
|
119
|
+
container.innerHTML = `
|
|
120
|
+
<ark-emit receive="change">
|
|
121
|
+
<input type="text"></input>
|
|
122
|
+
</ark-emit>
|
|
123
|
+
`
|
|
124
|
+
const input = container.querySelector('input')
|
|
125
|
+
const inputEvent = new Event('change', { bubbles: true })
|
|
126
|
+
const target = { name: 'input', value: 'XYZ' }
|
|
127
|
+
Object.defineProperty(
|
|
128
|
+
inputEvent, 'target', { writable: false, value: target })
|
|
129
|
+
|
|
130
|
+
const emit = container.querySelector('ark-emit')
|
|
131
|
+
emit.addEventListener('emit', (event) => {
|
|
132
|
+
const detail = event.detail
|
|
133
|
+
expect(detail).toEqual({ value: 'XYZ' })
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
input.dispatchEvent(inputEvent)
|
|
137
|
+
})
|
|
138
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Emit } from './components/emit.js'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './audio/index.js'
|
|
2
|
+
export * from './camera/index.js'
|
|
3
|
+
export * from './capture/index.js'
|
|
4
|
+
export * from './emit/index.js'
|
|
5
|
+
export * from './list/index.js'
|
|
6
|
+
export * from './paginator/index.js'
|
|
7
|
+
export * from './spinner/index.js'
|
|
8
|
+
export * from './splitview/index.js'
|
|
9
|
+
export * from './translate/index.js'
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
LIST
|
|
2
|
+
====
|
|
3
|
+
|
|
4
|
+
The ``ark-list`` component provides an indexed vertical
|
|
5
|
+
list of ``ark-list-items``.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Examples
|
|
9
|
+
--------
|
|
10
|
+
|
|
11
|
+
**Default list:**
|
|
12
|
+
|
|
13
|
+
**Assuming ``data`` is the following array:**
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const data = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Then, the following HTML declaration would render it by passing its content
|
|
20
|
+
as JSON within a ``data`` element:**
|
|
21
|
+
|
|
22
|
+
```html
|
|
23
|
+
<ark-list>
|
|
24
|
+
<data>${JSON.stringify(data)}</data>
|
|
25
|
+
</ark-list>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Template list:**
|
|
29
|
+
|
|
30
|
+
**If ``data`` were an array of objects such as:**
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
const dataTemplate = [
|
|
34
|
+
{ first: 'Colombia', second: 'Argentina', year: 2016 },
|
|
35
|
+
{ first: 'Uruguay', second: 'Colombia', year: 2017 },
|
|
36
|
+
{ first: 'Brasil', second: 'Argentina', year: 2018 },
|
|
37
|
+
{ first: 'Perú', second: 'Bolivia', year: 2019 }
|
|
38
|
+
]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Then we could provide a ``template`` to render each of its items:**
|
|
42
|
+
|
|
43
|
+
```html
|
|
44
|
+
<ark-list>
|
|
45
|
+
<data>${JSON.stringify(data)}</data>
|
|
46
|
+
<template>
|
|
47
|
+
<h1>\${this.year}</h1>
|
|
48
|
+
<span data-first>FIRST: \${this.first}</span>
|
|
49
|
+
<span> | </span>
|
|
50
|
+
<span data-second>SECOND: \${this.second}</span>
|
|
51
|
+
</template>
|
|
52
|
+
</ark-list>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**NOTE: remember to escape JavaScript string interpolation expressions
|
|
56
|
+
inside the template tag (i.e. use ``\%{}``).**
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
**Programmatic API:**
|
|
60
|
+
|
|
61
|
+
**The list receives its items as a ** ``data`` **array of objects**
|
|
62
|
+
|
|
63
|
+
**And its item structure in a** ``template`` **property**
|
|
64
|
+
|
|
65
|
+
``` javascript
|
|
66
|
+
const template = item => `
|
|
67
|
+
<h1>${item.year}</h1>
|
|
68
|
+
<span data-first>FIRST: ${item.first}</span>
|
|
69
|
+
<span> | </span>
|
|
70
|
+
<span data-second>SECOND: ${item.second}</span>`_
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**In order to render an** ``ark-list`` **of multiple lines, we assign the
|
|
74
|
+
items in the** ``data`` **property and its content in
|
|
75
|
+
the** ``template`` **property**
|
|
76
|
+
|
|
77
|
+
``` javascript
|
|
78
|
+
const templateList = this.select('[data-template-list]')
|
|
79
|
+
templateList.init({
|
|
80
|
+
data: sourceTemplate,
|
|
81
|
+
template: template
|
|
82
|
+
}).render()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Attributes
|
|
86
|
+
----------
|
|
87
|
+
|
|
88
|
+
| Component | Name | Type | Default | Description |
|
|
89
|
+
| :---------------: | :------------: | :-----: | :-----: | :-----------------------------------: |
|
|
90
|
+
| ``ark-list-item`` | index | string | text | Indicates the ``list-item`` index |
|
|
91
|
+
| ``ark-list`` | click-disabled | boolean | null | Disable the click event for the items |
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
Properties
|
|
95
|
+
----------
|
|
96
|
+
|
|
97
|
+
| Component | Name | Type | Default | Description |
|
|
98
|
+
| :---------------------: | :------: | ------------ | ------: | :------------------------------------------------------------------------------------------: |
|
|
99
|
+
| ``ark-list-item`` | index | string | text | Indicates the ``list-item`` index |
|
|
100
|
+
| ``ark-list`` (default) | source | list | null | A list of strings that represents each item in the list |
|
|
101
|
+
| ``ark-list`` (template) | source | list | null | An list of objects that represents data reflected in each item |
|
|
102
|
+
| ``ark-list-item`` | data | list, object | null | Displays the data defined in the source for a specific item |
|
|
103
|
+
| ``ark-list-item`` | template | string | null | Receives and displays a html in a template literal, the data should render in a ``span`` tag |
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import './list.js'
|
|
2
|
+
import { ListItem } from './list.item.js'
|
|
3
|
+
|
|
4
|
+
describe('List item', () => {
|
|
5
|
+
let container = null
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
container = document.createElement('div')
|
|
9
|
+
document.body.appendChild(container)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
container.remove()
|
|
14
|
+
container = null
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('can be instantiated', () => {
|
|
18
|
+
container.innerHTML = `
|
|
19
|
+
<ark-list></ark-list>
|
|
20
|
+
`
|
|
21
|
+
|
|
22
|
+
const list = container.querySelector('ark-list')
|
|
23
|
+
list.source = ['Colombia', 'Uruguay', 'Brasil', 'Perú']
|
|
24
|
+
list.render()
|
|
25
|
+
const item = list.querySelector('ark-list-item')
|
|
26
|
+
|
|
27
|
+
expect(item).toEqual(item.init())
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('can be rendered with data', () => {
|
|
31
|
+
container.innerHTML = `
|
|
32
|
+
<ark-list></ark-list>
|
|
33
|
+
`
|
|
34
|
+
|
|
35
|
+
const list = container.querySelector('ark-list')
|
|
36
|
+
list.source = ['Colombia']
|
|
37
|
+
list.render()
|
|
38
|
+
const item = list.querySelector('ark-list-item')
|
|
39
|
+
|
|
40
|
+
item.data = 'my data'
|
|
41
|
+
item.render()
|
|
42
|
+
|
|
43
|
+
expect(item.innerHTML.trim()).toEqual('my data')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('can be rendered with template', () => {
|
|
47
|
+
container.innerHTML = `
|
|
48
|
+
<ark-list data-template-list></ark-list>
|
|
49
|
+
`
|
|
50
|
+
|
|
51
|
+
const list = container.querySelector('[data-template-list]')
|
|
52
|
+
list.source = ['Colombia']
|
|
53
|
+
list.render()
|
|
54
|
+
const item = list.querySelector('ark-list-item')
|
|
55
|
+
|
|
56
|
+
item.data = 'my data'
|
|
57
|
+
item.template = data => /* html */ `<span>${data}</span>`
|
|
58
|
+
item.render()
|
|
59
|
+
|
|
60
|
+
expect(item.innerHTML.trim()).toEqual('<span>my data</span>')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('can be rendered with template', () => {
|
|
64
|
+
container.innerHTML = `
|
|
65
|
+
<ark-list data-template-list></ark-list>
|
|
66
|
+
`
|
|
67
|
+
|
|
68
|
+
const list = container.querySelector('[data-template-list]')
|
|
69
|
+
list.source = ['Colombia']
|
|
70
|
+
list.render()
|
|
71
|
+
const item = list.querySelector('ark-list-item')
|
|
72
|
+
|
|
73
|
+
item.data = 'my data'
|
|
74
|
+
item.template = data => /* html */ `<span>${data}</span>`
|
|
75
|
+
item.render()
|
|
76
|
+
|
|
77
|
+
item.addEventListener('list-item:selected', event => {
|
|
78
|
+
expect(event['detail'].data).toEqual('my data')
|
|
79
|
+
})
|
|
80
|
+
item.click()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('can be rendered without template', () => {
|
|
84
|
+
const item = new ListItem()
|
|
85
|
+
item.init({
|
|
86
|
+
data:'my data'
|
|
87
|
+
}).render()
|
|
88
|
+
|
|
89
|
+
expect(item.innerHTML.trim()).toEqual('my data')
|
|
90
|
+
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
})
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Component } from '../../../base/component/index.js'
|
|
2
|
+
|
|
3
|
+
const tag = 'ark-list-item'
|
|
4
|
+
export class ListItem extends Component {
|
|
5
|
+
init (context = {}) {
|
|
6
|
+
this.index = context.index
|
|
7
|
+
this.data = context.data || null
|
|
8
|
+
this.template = context.template || (data => `${data}`)
|
|
9
|
+
|
|
10
|
+
return super.init()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
reflectedProperties () {
|
|
14
|
+
return ['index']
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
render () {
|
|
18
|
+
this.content = this.template(this.data)
|
|
19
|
+
return super.render()
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
Component.define(tag, ListItem)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Component } from "../../../base/component/index.js"
|
|
2
|
+
import { ListItem } from "./list.item.js"
|
|
3
|
+
|
|
4
|
+
const tag = "ark-list"
|
|
5
|
+
|
|
6
|
+
export class List extends Component {
|
|
7
|
+
constructor () {
|
|
8
|
+
super()
|
|
9
|
+
this.addEventListener("click", this._onSelected.bind(this))
|
|
10
|
+
this.addEventListener("delete", this.onDeleted.bind(this))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @param {Object} context */
|
|
14
|
+
init(context = {}) {
|
|
15
|
+
this.source = /** @type {Array} */ (context.source) || this.source || []
|
|
16
|
+
this.template = context.template || this.template || ((data) => `${data}`)
|
|
17
|
+
|
|
18
|
+
return super.init()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
render() {
|
|
22
|
+
const listData = this.select('data')
|
|
23
|
+
const list = JSON.parse(listData?.textContent || null)
|
|
24
|
+
const source = list || this.source
|
|
25
|
+
|
|
26
|
+
const itemTemplate = (this.select('template'))?.innerHTML
|
|
27
|
+
this.template = itemTemplate ? this._format(itemTemplate) : this.template
|
|
28
|
+
|
|
29
|
+
this.content = ""
|
|
30
|
+
|
|
31
|
+
source.forEach((data, index) => {
|
|
32
|
+
const item = new ListItem()
|
|
33
|
+
|
|
34
|
+
if (this.hasAttribute("click-disabled")) {
|
|
35
|
+
item.setAttribute("click-disabled", "")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.appendChild(item)
|
|
39
|
+
|
|
40
|
+
item.init({
|
|
41
|
+
data: data,
|
|
42
|
+
template: this.template,
|
|
43
|
+
index: index,
|
|
44
|
+
}).render()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return super.render()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @param {number} start @param {number?} deleteCount */
|
|
51
|
+
delete(start, deleteCount = 1) {
|
|
52
|
+
this.source.splice(start, deleteCount)
|
|
53
|
+
const deletions = []
|
|
54
|
+
for (let i = start; i < (deleteCount + start); i++) {
|
|
55
|
+
const item = this.select(`ark-list-item:nth-of-type(${i + 1})`)
|
|
56
|
+
item && deletions.push(item)
|
|
57
|
+
}
|
|
58
|
+
deletions.map(item => item.remove())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_format(template) {
|
|
62
|
+
return (data) => Function(`return \`${template}\``).call(data)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @param {MouseEvent} event */
|
|
66
|
+
_onSelected(event) {
|
|
67
|
+
event.stopImmediatePropagation()
|
|
68
|
+
|
|
69
|
+
const target = /** @type {HTMLElement} */ (event.target)
|
|
70
|
+
const item = /** @type {ListItem} */ (target.closest("ark-list-item"))
|
|
71
|
+
|
|
72
|
+
if (!item || item.hasAttribute("click-disabled")) return
|
|
73
|
+
|
|
74
|
+
this.dispatchEvent(
|
|
75
|
+
new CustomEvent("list-selected", {
|
|
76
|
+
bubbles: true,
|
|
77
|
+
detail: {
|
|
78
|
+
index: item.index,
|
|
79
|
+
data: item.data,
|
|
80
|
+
origin: event,
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @param {MouseEvent} event */
|
|
87
|
+
onDeleted(event) {
|
|
88
|
+
event.stopImmediatePropagation()
|
|
89
|
+
|
|
90
|
+
const target = /** @type {HTMLElement} */ (event.target)
|
|
91
|
+
const item = /** @type {ListItem} */ (target.closest("ark-list-item"))
|
|
92
|
+
|
|
93
|
+
this.delete(Number(item.index))
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
Component.define(tag, List)
|