Fix some off-by-one issues when drag-and-dropping dashboard panels

Summary: Fixes T5321. There were a couple of off-by-one issues here which could result in inserts into the wrong position.

Test Plan:
  - Dragged panels to the top, bottom, and first position of columns.
  - Dragged panels from one column to another.
  - Reloaded the page after drags, things stayed where I put them.

Reviewers: chad

Reviewed By: chad

Subscribers: epriestley

Maniphest Tasks: T5321

Differential Revision: https://secure.phabricator.com/D9573
This commit is contained in:
epriestley
2014-06-16 09:15:35 -07:00
parent 0efa1ca643
commit 5f52f1f82c

View File

@@ -41,29 +41,32 @@ final class PhabricatorDashboardMovePanelController
$layout_config->removePanel($panel_phid); $layout_config->removePanel($panel_phid);
$panel_location_grid = $layout_config->getPanelLocations(); $panel_location_grid = $layout_config->getPanelLocations();
$panel_columns = idx($panel_location_grid, $column_id, array()); $column_phids = idx($panel_location_grid, $column_id, array());
if ($panel_columns) { $column_phids = array_values($column_phids);
if ($column_phids) {
$insert_at = 0; $insert_at = 0;
$new_panel_columns = $panel_columns; foreach ($column_phids as $index => $phid) {
foreach ($panel_columns as $index => $curr_panel_phid) { if ($phid === $before_phid) {
if ($curr_panel_phid === $before_phid) {
$insert_at = max($index - 1, 0);
break;
}
if ($curr_panel_phid === $after_phid) {
$insert_at = $index; $insert_at = $index;
break; break;
} }
if ($phid === $after_phid) {
$insert_at = $index + 1;
break;
}
} }
$new_column_phids = $column_phids;
array_splice( array_splice(
$new_panel_columns, $new_column_phids,
$insert_at, $insert_at,
0, 0,
array($panel_phid)); array($panel_phid));
} else { } else {
$new_panel_columns = array(0 => $panel_phid); $new_column_phids = array(0 => $panel_phid);
} }
$panel_location_grid[$column_id] = $new_panel_columns;
$panel_location_grid[$column_id] = $new_column_phids;
$layout_config->setPanelLocations($panel_location_grid); $layout_config->setPanelLocations($panel_location_grid);
$dashboard->setLayoutConfigFromObject($layout_config); $dashboard->setLayoutConfigFromObject($layout_config);
$dashboard->save(); $dashboard->save();