-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1743 from unboxed/sortable-js
Allow drag and drop sorting
- Loading branch information
Showing
17 changed files
with
268 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.sortable-list { | ||
cursor: move; | ||
} |
39 changes: 39 additions & 0 deletions
39
app/controllers/planning_applications/assessment/informatives/positions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module PlanningApplications | ||
module Assessment | ||
module Informatives | ||
class PositionsController < AuthenticationController | ||
before_action :set_planning_application | ||
before_action :set_informative_set | ||
before_action :set_informative | ||
|
||
def update | ||
if @informative.insert_at(informative_position) | ||
head :no_content | ||
else | ||
render json: @informative.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_informative_set | ||
@informative_set = @planning_application.informative_set | ||
end | ||
|
||
def set_informative | ||
@informative = @informative_set.informatives.find(params[:informative_id]) | ||
end | ||
|
||
def informative_position_params | ||
params.require(:informative).permit(:position) | ||
end | ||
|
||
def informative_position | ||
Integer(informative_position_params[:position]) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import { put } from "@rails/request.js" | ||
import Sortable from "sortablejs" | ||
|
||
export default class extends Controller { | ||
static values = { url: String } | ||
|
||
connect() { | ||
this.initializeSortable() | ||
} | ||
|
||
disconnect() { | ||
this.sortable.destroy() | ||
} | ||
|
||
initializeSortable() { | ||
this.sortable = Sortable.create(this.element, { | ||
animation: 350, | ||
handle: "[data-sortable-handle]", | ||
onEnd: this.onEnd.bind(this), | ||
}) | ||
} | ||
|
||
onEnd(event) { | ||
const { newIndex, item } = event | ||
const url = item.dataset.sortableUrl | ||
|
||
put(url, { | ||
body: JSON.stringify({ | ||
informative: { | ||
position: newIndex, | ||
}, | ||
}), | ||
}) | ||
.then(() => { | ||
const modelName = item.dataset.modelName | ||
|
||
if (modelName) { | ||
this.updatePositions(modelName) | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error("Error updating position:", error) | ||
}) | ||
} | ||
|
||
updatePositions(modelName) { | ||
const items = this.element.querySelectorAll("li") | ||
|
||
items.forEach((item, index) => { | ||
const positionElement = item.querySelector(".govuk-hint") | ||
if (positionElement) { | ||
positionElement.textContent = `${modelName} ${index + 1}` | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddPositionToInformative < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :informatives, :position, :integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.