-
Notifications
You must be signed in to change notification settings - Fork 16
/
article_metadata_form.js
135 lines (107 loc) · 3.58 KB
/
article_metadata_form.js
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
/*jshint esversion: 6 */
$(document).ready(function() {
function prepAuthorContainer(params) {
var ne = params.element;
var reset = params.reset_value;
var number = params.number;
ne.id = 'authors-' + number + '-container';
ne = $(ne);
ne.find('[id^=authors-]').each(function () {
var ce = $(this);
// reset the value
if (reset) {
ce.val('')
}
// set the id as requestsed
items = ce.attr('id').split('-');
var id = 'authors-' + number + '-' + items[2];
// set both the id and the name to the new id, as per wtforms requirements
ce.attr('id', id);
ce.attr('name', id);
});
// we also need to update the remove button
ne.find("[id^=remove_authors-]").each(function () {
var ce = $(this);
// update the id as above - saving us a closure again
items = ce.attr('id').split('-');
var id = 'remove_authors-' + number;
// set both the id and the name to the new id
ce.attr('id', id);
ce.attr('name', id);
})
}
function showHideFirstRemoveButton() {
// Hide delete button when there's just one author
if ($('[id^=authors-][id$="container"]').length === 1) {
$('#remove_authors-0').css('display', 'none')
} else {
$('#remove_authors-0').css('display', 'inherit')
}
}
function removeAuthor() {
event.preventDefault();
var id = $(this).attr("id");
var short_name = id.split("_")[1];
var container = short_name + "-container";
$("#" + container).remove();
var count = 0;
$('[id^=authors-][id$="container"]').each(function () {
prepAuthorContainer({
element: this,
number: count,
reset_value: false
});
count++;
});
showHideFirstRemoveButton()
}
$('button[name=more_authors]').click(function (event) {
event.preventDefault();
// get the last author div in the list
var all_e = $('[id^=authors-][id$="container"]');
var e = all_e.last();
// make a clone of the last author div
var ne = e.clone()[0];
// extract the last author's number from the div id and increment it
var items = ne.id.split('-');
var number = parseInt(items[1]);
number = number + 1;
// increment all the numbers
prepAuthorContainer({
element: ne,
number: number,
reset_value: true
});
e.after(ne);
var rem_b = $(".remove_field__button");
rem_b.unbind("click");
rem_b.click(removeAuthor);
if (all_e.length === 1) {
$('#remove_authors-1').css('display', 'inherit')
}
showHideFirstRemoveButton();
});
showHideFirstRemoveButton();
$(".remove_field__button").click(removeAuthor);
$("#pissn").select2({
allowClear: false,
width: 'resolve',
newOption: 'false'
});
$("#eissn").select2({
allowClear: false,
width: 'resolve',
newOption: 'false'
});
$("#keywords").select2({
multiple: true,
minimumInputLength: 1,
allowClear: false,
tags: [],
tokenSeparators: [','],
width: 'resolve'
})
$("#article_metadata_form").on("submit", function(event) {
$("button[type=submit]").prop("disabled", true);
})
})