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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
<template>
<div class="input-array">
<span aria-live="assertive" class="sr-only">{{ assistiveLive }}</span>
<table class="default nohover">
<colgroup>
<col style="width: 16px">
<col>
<col v-for="i in additionalColspan" :key="`colspan-${i}`">
<col style="width: 24px">
</colgroup>
<thead>
<tr>
<th class="dragcolumn"></th>
<th>{{ labelPlural }}</th>
<slot name="header-cells" />
<th class="actions"></th>
</tr>
</thead>
<Draggable v-model="options" handle=".dragarea" tag="tbody" class="statements">
<tr v-for="(option, index) in options" :key="index">
<td class="dragcolumn">
<a class="dragarea"
tabindex="0"
:title="$gettextInterpolate($gettext(`Sortierelement für %{label} %{option}. Drücken Sie die Tasten Pfeil-nach-oben oder Pfeil-nach-unten, um dieses Element in der Liste zu verschieben.`), {option, label}, true)"
@keydown="keyHandler($event, index)"
ref="draghandle">
<span class="drag-handle"></span>
</a>
</td>
<td>
<input type="text"
ref="inputs"
:placeholder="label"
@paste="(ev) => onPaste(ev, index)"
v-model="options[index]">
</td>
<slot name="body-cells" />
<td class="actions">
<StudipIcon name="delete"
shape="trash"
:size="20"
@click.prevent="deleteOption(index)"
:title="$gettextInterpolate($gettext('%{label} löschen'), {label}, true)"
/>
</td>
</tr>
</Draggable>
<tfoot>
<tr>
<td :colspan="3 + additionalColspan">
<button class="as-link"
:title="$gettextInterpolate($gettext('%{label} hinzufügen'), {label}, true)"
@click.prevent="addOption()">
<StudipIcon shape="add" :size="20" alt="" />
</button>
</td>
</tr>
</tfoot>
</table>
</div>
</template>
<script>
import Draggable from 'vuedraggable';
import { $gettext } from '../../../assets/javascripts/lib/gettext';
export default {
name: 'input-array',
components: { Draggable },
props: {
additionalColspan: {
type: Number,
default: 0,
},
label: {
type: String,
default: $gettext('Option'),
},
labelPlural: {
type: String,
default: $gettext('Optionen'),
},
value: Array,
},
data() {
return {
options: [],
assistiveLive: '',
};
},
methods: {
addOption(val = '', position = this.options.length) {
this.$set(this.options, position, val.trim());
this.$nextTick(() => {
this.$refs.inputs[position].focus();
});
},
deleteOption(index) {
const question = this.options[index] ? this.$gettext('Wirklich löschen?') : true;
STUDIP.Dialog.confirm(question).done(() => {
this.$delete(this.options, index);
});
},
onPaste(ev, position) {
ev.clipboardData
.getData('text')
.split("\n")
.filter(str => str.trim().length > 0)
.forEach((value, index) => this.addOption(value, position + index));
ev.preventDefault();
},
keyHandler(e, index) {
if (e.keyCode !== 38 && e.keyCode !== 40) {
return;
}
e.preventDefault();
const moveUp = e.keyCode === 38;
this.moveElement(index, moveUp ? -1 : 1).then((newIndex) => {
this.assistiveLive = this.$gettextInterpolate(
this.$gettext('Aktuelle Position in der Liste: %{pos} von %{listLength}.'),
{pos: newIndex + 1, listLength: this.options.length}
);
this.$nextTick(() => {
this.$refs['draghandle'][newIndex].focus();
});
})
},
moveElement(index, direction) {
if (this.options[index + direction] === undefined) {
return Promise.resolve(index);
}
const indices = [index, index + direction].sort();
this.options.splice(
Math.min(...indices),
2,
...indices.reverse().map(idx => this.options[idx])
);
return Promise.resolve(index + direction);
}
},
watch: {
options: {
handler(current) {
this.$emit('input', current);
},
deep: true
},
value: {
handler(current) {
this.options = current;
},
immediate: true
}
}
}
</script>
<style scoped>
.input-array input[type="text"] {
max-width: unset;
}
</style>
|