-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeCon.svelte
More file actions
117 lines (104 loc) · 3.58 KB
/
AttributeCon.svelte
File metadata and controls
117 lines (104 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<script lang="ts">
import { ALLOWED_ATTRIBUTE_TYPES } from './consts'
import AttributeValue from './AttributeValue.svelte'
import { _ } from 'svelte-i18n'
import { getContext } from 'svelte'
import AttributeRequest from './AttributeRequest.svelte'
export let i: number
const { policy, signing } = getContext('store')
$: con = $policy[i].con
$: possibleChoices = Object.entries(ALLOWED_ATTRIBUTE_TYPES).flatMap(([group, types]) => {
return types.filter(({ ident }) => !con.some(({ t }) => ident === t))
})
const addAttribute = (t: AttributeRequest) => {
policy.update((p: PolicyListItem[]) => {
p[i].con.push({ t, v: '', focused: true })
return p
})
}
const removeAttribute = (j: number) => {
policy.update((p: PolicyListItem[]) => {
p[i].con = p[i].con.filter((item, idx) => idx !== j)
return p
})
}
// Helper function to get the more information of the type in an AttributeRequest.
function typeOf(ar: AttributeRequest): AttributeType | undefined {
for (const [, groupList] of Object.entries(ALLOWED_ATTRIBUTE_TYPES)) {
for (const typ of groupList) {
if (ar.t === typ.ident) return typ
}
}
}
</script>
<div id="attribute-con">
{#each con as ar, j}
{@const typ = typeOf(ar)}
<AttributeRequest removable={true} onRemove={() => removeAttribute(j)}>
<select
id="attribute-request"
slot="type"
bind:value={ar.t}
on:change={() => {
ar.v = ''
ar.focused = false
console.log('selected: ', ar.t)
}}
on:focus={() => (ar.focused = true)}
style={typ?.img
? `background-image: url("${typ.img}"); padding-left: ${
ar.focused ? '2.25em' : '1.75em'
}`
: ''}
>
{#each possibleChoices as { ident }}
<option value={ident} label={ar.focused ? $_(`attributeTypes.${ident}`) : ''} />
{/each}
</select>
<AttributeValue slot="value" bind:value={ar.v} bind:focused={ar.focused} {typ} />
</AttributeRequest>
{/each}
{#if possibleChoices.length > 0}
<select
id="add-recipient-data"
class="round-btn"
required
on:change={(event) => {
addAttribute(event.target.value)
event.target.value = 'none'
}}
>
<option value="none" disabled selected hidden
>{$_(`addAttribute${signing ? 'Sign' : ''}`)}</option
>
{#each possibleChoices as { ident }}
<option value={ident} label={$_(`attributeTypes.${ident}`)} />
{/each}
</select>
{/if}
</div>
<style lang="scss">
#attribute-con {
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 5px;
}
select#attribute-request {
display: flex;
flex-direction: row;
width: 100%;
height: 100%;
background-color: var(--pg-blue);
color: var(--pg-white);
border: 0px solid black;
width: 3em;
background-repeat: no-repeat;
background-position: left 8px top 50%;
background-size: 20px 20px;
}
select#add-recipient-data {
border-right: 10px solid transparent;
padding-right: 0px;
}
</style>