|
1
|
|
|
function init_subform(id, sortable) { |
|
2
|
|
|
var container = $('#' + id), |
|
3
|
|
|
delete_button = $('<a class="button remove-item">-</a>'), |
|
4
|
|
|
add_button = $('<a class="button add-item">+</a>') |
|
5
|
|
|
.on('click', function(e) { |
|
6
|
|
|
e.preventDefault(); |
|
7
|
|
|
add_form(container, add_button, delete_button, sortable); |
|
8
|
|
|
}), |
|
9
|
|
|
index = 0; |
|
10
|
|
|
|
|
11
|
|
|
container.on('click', 'a.remove-item', function(e) { |
|
12
|
|
|
e.preventDefault(); |
|
13
|
|
|
$(this).parent().remove(); |
|
14
|
|
|
if ( container.data('max-count') > 0 |
|
15
|
|
|
&& container.data('max-count') >= container.find('fieldset').length |
|
16
|
|
|
&& container.find('.add-item').length === 0) { |
|
17
|
|
|
container.append(add_button); |
|
18
|
|
|
} |
|
19
|
|
|
}); |
|
20
|
|
|
|
|
21
|
|
|
container.children().each(function() { |
|
22
|
|
|
$(this).prepend(delete_button.clone()); |
|
23
|
|
|
index++; |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
container.data('index', index); |
|
27
|
|
|
if ( container.data('max-count') === 0 |
|
28
|
|
|
|| container.data('max-count') > index) { |
|
29
|
|
|
container.append(add_button); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (sortable === true) { |
|
33
|
|
|
container |
|
34
|
|
|
.sortable({items: '> :not(a.add-item)'}) |
|
35
|
|
|
.on('sortupdate', function() { |
|
36
|
|
|
$($(this).find('> .ui-sortable-handle').get().reverse()).each(function(index, element) { |
|
37
|
|
|
let id = element.id |
|
38
|
|
|
if (!id) { |
|
39
|
|
|
id = $('> .input > *:first-child', element).attr('id'); |
|
40
|
|
|
} |
|
41
|
|
|
$('#' + id + '_score').val(index); |
|
42
|
|
|
}); |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
add_button.on('click', function() { |
|
47
|
|
|
// If there is exactly one file selector, we're probably in some sort of attachment list, |
|
48
|
|
|
// so let's assume the user wants to add a file |
|
49
|
|
|
// (at some point this should probably be made configurable) |
|
50
|
|
|
if ($(this).prev().find('input[type="file"]').length === 1) { |
|
51
|
|
|
$(this).prev().find('input[type="file"]').click(); |
|
52
|
|
|
} |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
function add_form(container, add_button, delete_button, sortable) { |
|
57
|
|
|
var prototype = container.data('prototype'), |
|
58
|
|
|
index = container.data('index'), |
|
59
|
|
|
new_form = prototype.replace(/__name__/g, index); |
|
60
|
|
|
container.data('index', index + 1); |
|
61
|
|
|
$(new_form) |
|
62
|
|
|
.prepend(delete_button.clone()) |
|
63
|
|
|
.insertBefore(add_button); |
|
64
|
|
|
|
|
65
|
|
|
if ( container.data('max-count') > 0 |
|
66
|
|
|
&& container.data('max-count') >= container.find('> :not(.button.add-item)').length) { |
|
67
|
|
|
add_button.detach(); |
|
68
|
|
|
} |
|
69
|
|
|
if (sortable === true) { |
|
70
|
|
|
container.sortable('refresh'); |
|
71
|
|
|
container.trigger('sortupdate'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|