Fix new items missing drag handles in collection editor

This commit is contained in:
rubenwardy 2023-08-21 00:34:55 +01:00
parent dca6e82594
commit 954826f053
3 changed files with 31 additions and 24 deletions

@ -179,12 +179,12 @@ def handle_create_edit(collection: Collection, form: CollectionForm,
form.populate_obj(collection) form.populate_obj(collection)
collection.name = name collection.name = name
item_lookup = {} link_lookup = {}
for link in collection.items: for link in collection.items:
item_lookup[link.package.get_id()] = link link_lookup[link.package.get_id()] = link
for i, package_id in enumerate(form.package_ids): for i, package_id in enumerate(form.package_ids):
link = next((x for x in collection.items if str(x.package.get_id()) == package_id.data), None) link = link_lookup.get(package_id.data)
to_delete = form.package_removed[i].data == "1" to_delete = form.package_removed[i].data == "1"
if link is None: if link is None:
if to_delete: if to_delete:
@ -198,7 +198,7 @@ def handle_create_edit(collection: Collection, form: CollectionForm,
link.package = package link.package = package
link.collection = collection link.collection = collection
link.description = form.descriptions[i].data link.description = form.descriptions[i].data
item_lookup[link.package.get_id()] = link link_lookup[link.package.get_id()] = link
db.session.add(link) db.session.add(link)
elif to_delete: elif to_delete:
db.session.delete(link) db.session.delete(link)
@ -206,7 +206,7 @@ def handle_create_edit(collection: Collection, form: CollectionForm,
link.description = form.descriptions[i].data link.description = form.descriptions[i].data
for i, package_id in enumerate(form.order.data.split(",")): for i, package_id in enumerate(form.order.data.split(",")):
item_lookup[package_id].order = i + 1 link_lookup[package_id].order = i + 1
add_audit_log(severity, current_user, add_audit_log(severity, current_user,
f"Edited collection {collection.author.username}/{collection.name}", f"Edited collection {collection.author.username}/{collection.name}",

@ -74,6 +74,11 @@ function addPackage(pkg) {
temp.innerHTML = ` temp.innerHTML = `
<article class="card my-3" data-id="${escapeHtml(id)}"> <article class="card my-3" data-id="${escapeHtml(id)}">
<div class="card-body"> <div class="card-body">
<div class="row">
<div class="col-auto text-muted pr-2">
<i class="fas fa-bars"></i>
</div>
<div class="col">
<button class="btn btn-sm btn-danger remove-package float-right" type="button" aria-label="Remove"> <button class="btn btn-sm btn-danger remove-package float-right" type="button" aria-label="Remove">
<i class="fas fa-trash"></i> <i class="fas fa-trash"></i>
</button> </button>
@ -94,6 +99,8 @@ function addPackage(pkg) {
<input id="package_ids-${nextId}" name="package_ids-${nextId}" type="hidden" value="${id}"> <input id="package_ids-${nextId}" name="package_ids-${nextId}" type="hidden" value="${id}">
<input id="package_removed-${nextId}" name="package_removed-${nextId}" type="hidden" value="0"> <input id="package_removed-${nextId}" name="package_removed-${nextId}" type="hidden" value="0">
</div> </div>
</div>
</div>
</article> </article>
`; `;

@ -10,7 +10,7 @@
{% block scriptextra %} {% block scriptextra %}
<script src="/static/libs/jquery-ui.min.js"></script> <script src="/static/libs/jquery-ui.min.js"></script>
<script src="/static/collection_editor.js?v=6"></script> <script src="/static/collection_editor.js?v=7"></script>
{% endblock %} {% endblock %}
{% block content %} {% block content %}