-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray.js
More file actions
69 lines (63 loc) · 1.46 KB
/
array.js
File metadata and controls
69 lines (63 loc) · 1.46 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
import Setting from './setting.js';
export default class ArrayType extends Setting {
constructor(name = 'array') {
super(name);
}
value(data) {
if (typeof data === 'string') return JSON.parse(data);
return data;
}
default() {
return [];
}
element(value, update, {
key,
container,
}) {
let index = 0;
function add(text) {
$(container).append(createArrayItem(text, `${key}.${index}`, value, update));
index += 1;
}
value.forEach(add);
return $('<input type="text">').css({
'background-color': 'transparent',
}).on('keydown.script', function keydown(e) {
if (e.key !== 'Enter') return;
e.preventDefault();
value.push(this.value);
add(this.value);
this.value = '';
update(value);
});
}
styles() {
return [
'input.array-remove { display: none; }',
'input.array-remove:checked + label:before { content: "× "; color: red; }',
];
}
}
function createArrayItem(text, key, value, update) {
const ret = $('<div>')
.on('change.script', () => {
const i = value.indexOf(text);
if (i > -1) {
value.splice(i, 1);
update(value);
}
ret.remove();
});
const el = $('<input>')
.addClass('array-remove')
.attr({
type: 'checkbox',
id: key,
}).prop('checked', '1');
const label = $(`<label>`).html(text)
.attr({
for: key,
});
ret.append(el, ' ', label);
return ret;
}