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,25 +74,32 @@ 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">
<button class="btn btn-sm btn-danger remove-package float-right" type="button" aria-label="Remove"> <div class="row">
<i class="fas fa-trash"></i> <div class="col-auto text-muted pr-2">
</button> <i class="fas fa-bars"></i>
<h5> </div>
<a href="${escapeHtml(url)}" target="_blank"> <div class="col">
${escapeHtml(pkg.title)} by ${escapeHtml(pkg.author)} <button class="btn btn-sm btn-danger remove-package float-right" type="button" aria-label="Remove">
</a> <i class="fas fa-trash"></i>
</h5> </button>
<p class="text-muted"> <h5>
${escapeHtml(pkg.short_description)} <a href="${escapeHtml(url)}" target="_blank">
</p> ${escapeHtml(pkg.title)} by ${escapeHtml(pkg.author)}
<div class="form-group"> </a>
<label for="descriptions-${nextId}">Short Description</label> </h5>
<input class="form-control" id="descriptions-${nextId}" maxlength="500" minlength="0" <p class="text-muted">
name="descriptions-${nextId}" type="text" value=""> ${escapeHtml(pkg.short_description)}
<small class="form-text text-muted">You can replace the description with your own</small> </p>
<div class="form-group">
<label for="descriptions-${nextId}">Short Description</label>
<input class="form-control" id="descriptions-${nextId}" maxlength="500" minlength="0"
name="descriptions-${nextId}" type="text" value="">
<small class="form-text text-muted">You can replace the description with your own</small>
</div>
<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">
</div>
</div> </div>
<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">
</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 %}