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