Add drag and drop sorting for slides
Slides can be reordered on the dashboard by dragging the handle (mouse, touch and pen via Pointer Events) or with the arrow keys on the focused handle. The new order is posted as ids[] to slides/reorder, which only applies a complete, valid order of the poll's own slides. The up/down buttons stay as no-JS fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50010484be
commit
9983d6c01b
8 files changed
+301
-8
No files matched your search
@@ -120,6 +120,27 @@ class SlidesController extends AdminAppController
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a complete new slide order (drag & drop), expects `ids[]` and answers with JSON.
|
||||
*
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function reorder(): Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$ids = $this->request->getData('ids');
|
||||
$ids = is_array($ids) ? array_values(array_map('intval', array_filter($ids, 'is_numeric'))) : [];
|
||||
|
||||
if (!$this->Slides->reorder($this->poll->id, $ids)) {
|
||||
return $this->json([
|
||||
'ok' => false,
|
||||
'message' => __('Die Reihenfolge konnte nicht gespeichert werden. Bitte lade die Seite neu.'),
|
||||
], 409);
|
||||
}
|
||||
|
||||
return $this->json(['ok' => true, 'message' => __('Reihenfolge gespeichert.')]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all responses of a slide.
|
||||
*
|
||||
|
||||
@@ -294,6 +294,40 @@ class SlidesTable extends Table
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a complete new order (e.g. from drag & drop).
|
||||
*
|
||||
* The ids must be exactly the slides of the poll, each once – otherwise
|
||||
* (stale page, foreign ids) nothing is changed. Repeating the same order is a no-op.
|
||||
*
|
||||
* @param int $pollId Poll id
|
||||
* @param list<int> $slideIds Slide ids in their new order
|
||||
* @return bool Whether the order was valid and is now applied
|
||||
*/
|
||||
public function reorder(int $pollId, array $slideIds): bool
|
||||
{
|
||||
$current = $this->find('list', valueField: 'position')
|
||||
->where(['poll_id' => $pollId])
|
||||
->toArray();
|
||||
$given = $slideIds;
|
||||
$existing = array_map('intval', array_keys($current));
|
||||
sort($given);
|
||||
sort($existing);
|
||||
if ($given !== $existing) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->getConnection()->transactional(function () use ($slideIds, $current): bool {
|
||||
foreach ($slideIds as $index => $id) {
|
||||
if ($current[$id] !== $index + 1) {
|
||||
$this->updateAll(['position' => $index + 1], ['id' => $id]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Renumbers positions 1..n (closes gaps after deletions).
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user