forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_authors.html
More file actions
160 lines (137 loc) · 6.28 KB
/
edit_authors.html
File metadata and controls
160 lines (137 loc) · 6.28 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
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
{% extends "base.html" %}
{# Copyright The IETF Trust 2021, All Rights Reserved #}
{% load origin %}
{% load static %}
{% load bootstrap3 %}
{% block pagehead %}
<link rel="stylesheet" href="{% static 'select2/select2.css' %}">
<link rel="stylesheet" href="{% static 'select2-bootstrap-css/select2-bootstrap.min.css' %}">
{% endblock %}
{% block morecss %}
#empty-author-form {
display: none;
}
{% endblock %}
{% block title %}
Edit authors for {{ titletext }}
{% endblock %}
{% block content %}
{% origin %}
<h1>Edit authors<br><small>{{ titletext }}</small></h1>
<form enctype="multipart/form-data" method="post" id="authors-form">
{% csrf_token %}
{% bootstrap_form change_basis_form %}
{% buttons %}
<button id="add-author-button" type="button" class="btn btn-default" onclick="local_js.add_author()">Add new author</button>
{% endbuttons %}
{% bootstrap_form formset.management_form %}
<div id="authors-list" class="well">
{% for form in formset %}
<div class="panel panel-default author-panel">
<div class="panel-body draggable">
<span class="handle fa fa-reorder"></span>
<div class="form-horizontal">
{% bootstrap_form form layout='horizontal' %}
</div>
</div>
</div>
{% endfor %}
</div>
<div id="empty-author-form" class="template">
<div class="panel panel-default author-panel">
<div class="panel-body draggable">
<span class="handle fa fa-reorder"></span>
<div class="form-horizontal">
{% bootstrap_form formset.empty_form layout='horizontal' %}
</div>
</div>
</div>
</div>
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
<a class="btn btn-default pull-right"
href="{% url "ietf.doc.views_doc.document_main" name=doc.canonical_name %}">Back</a>
{% endbuttons %}
</form>
{% endblock %}
{% block js %}
<script src="{% static 'Sortable/Sortable.min.js' %}"></script>
<script src="{% static 'select2/select2.min.js' %}"></script>
<script src="{% static 'ietf/js/select2-field.js' %}"></script>
<script type="text/javascript">
const local_js = (
function () {
const sortable_list_id = 'authors-list';{# id of the container element for Sortable #}
const prefix = 'author'; {# formset prefix - must match the prefix in the edit_authors() view #}
var list_container;
var form_counter;
var author_template;
var ajax_url = '{% url "ietf.person.ajax.person_email_json" personid="123454321" %}';
var person_select2_input_selector = 'input.select2-field[name^="author-"][name$="-person"]';
function handle_drag_end() {
// after dragging, set order inputs to match new positions in list
$(list_container).find('.draggable input[name^="' + prefix + '"][name$="ORDER"]').each(
function (index, elt) {
$(elt).val(index + 1);
})
}
function add_author() {
// __prefix__ is the unique prefix for each list item, indexed from 0
var new_html = $(author_template).html().replaceAll('__prefix__', form_counter.value);
var new_elt = $(new_html)
$(list_container).append(new_elt);
var new_person_select = new_elt.find(person_select2_input_selector);
setupSelect2Field(new_person_select);
new_person_select.on('change', person_changed);
var form_count = Number(form_counter.value);
form_counter.value = String(form_count + 1);
new_elt[0].scrollIntoView(true);
}
function update_email_options_cb_factory(email_select) {
// factory method creates a closure for the callback
return function(ajax_data) {
// keep the first item - it's the 'blank' option
$(email_select).children().not(':first').remove();
$.each(ajax_data, function(index, email) {
$(email_select).append(
$('<option></option>')
.attr('value', email.address)
.text(email.address)
);
});
if (ajax_data.length > 0) {
$(email_select).val(ajax_data[0].address);
}
}
}
function person_changed(event) {
var person_elt = $(this);
var email_select = $('#' + person_elt.attr('id').replace(/-person$/, '-email'));
$.get(
ajax_url.replace('123454321', $(this).val()),
null,
update_email_options_cb_factory(email_select)
);
}
function initialize() {
list_container = document.getElementById(sortable_list_id)
form_counter = document.getElementsByName(prefix + '-TOTAL_FORMS')[0];
author_template = document.getElementById('empty-author-form');
Sortable.create(
list_container,
{
handle: '.handle',
onEnd: handle_drag_end
});
// register handler
$(person_select2_input_selector).on('change', person_changed);
}
return {
add_author: add_author,
initialize: initialize
}
}
)()
$(document).ready(local_js.initialize);
</script>
{% endblock %}