mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-12-16 13:48:33 +08:00
Fix step position adjusting on move and destroy [SCI-4880]
This commit is contained in:
parent
86bcd9b13b
commit
a206890a8c
5 changed files with 82 additions and 105 deletions
|
|
@ -162,29 +162,28 @@
|
||||||
|
|
||||||
// Set callback for click on move step
|
// Set callback for click on move step
|
||||||
function applyMoveStepCallBack() {
|
function applyMoveStepCallBack() {
|
||||||
$("[data-action='move-step']").off("ajax:success");
|
$('#steps').on('ajax:success', "[data-action='move-step']", function(e, data) {
|
||||||
$("[data-action='move-step']")
|
var $step = $(this).closest('.step');
|
||||||
.on("ajax:success", function(e, data) {
|
|
||||||
var $step = $(this).closest(".step");
|
|
||||||
var stepUpPosition = data.step_up_position;
|
var stepUpPosition = data.step_up_position;
|
||||||
var stepDownPosition = data.step_down_position;
|
var stepDownPosition = data.step_down_position;
|
||||||
var $stepDown, $stepUp;
|
var $stepDown, $stepUp;
|
||||||
switch (data.move_direction) {
|
|
||||||
case "up":
|
switch ($(this).data('direction')) {
|
||||||
$stepDown = $step.prev(".step");
|
case 'up':
|
||||||
|
$stepDown = $step.prev('.step');
|
||||||
$stepUp = $step;
|
$stepUp = $step;
|
||||||
break;
|
break;
|
||||||
case "down":
|
case 'down':
|
||||||
$stepDown = $step;
|
$stepDown = $step;
|
||||||
$stepUp = $step.next(".step");
|
$stepUp = $step.next('.step');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Switch position of top and bottom steps
|
// Switch position of top and bottom steps
|
||||||
if (!_.isUndefined($stepDown) && !_.isUndefined($stepUp)) {
|
if (!_.isUndefined($stepDown) && !_.isUndefined($stepUp)) {
|
||||||
$stepDown.insertAfter($stepUp);
|
$stepDown.insertAfter($stepUp);
|
||||||
$stepDown.find(".step-number").html(stepDownPosition + 1);
|
$stepDown.find('.step-number').html(`${stepDownPosition + 1}.`);
|
||||||
$stepUp.find(".step-number").html(stepUpPosition + 1);
|
$stepUp.find('.step-number').html(`${stepUpPosition + 1}.`);
|
||||||
$("html, body").animate({ scrollTop: $step.offset().top - window.innerHeight / 2 });
|
$('html, body').animate({ scrollTop: $step.offset().top - window.innerHeight / 2 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof refreshProtocolStatusBar === 'function') refreshProtocolStatusBar();
|
if (typeof refreshProtocolStatusBar === 'function') refreshProtocolStatusBar();
|
||||||
|
|
|
||||||
|
|
@ -80,9 +80,6 @@ class StepsController < ApplicationController
|
||||||
else
|
else
|
||||||
log_activity(:add_step_to_protocol_repository, nil, protocol: @protocol.id)
|
log_activity(:add_step_to_protocol_repository, nil, protocol: @protocol.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update protocol timestamp
|
|
||||||
update_protocol_ts(@step)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
|
@ -194,9 +191,6 @@ class StepsController < ApplicationController
|
||||||
log_activity(:edit_step_in_protocol_repository, nil, protocol: @protocol.id)
|
log_activity(:edit_step_in_protocol_repository, nil, protocol: @protocol.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update protocol timestamp
|
|
||||||
update_protocol_ts(@step)
|
|
||||||
|
|
||||||
format.json {
|
format.json {
|
||||||
render json: {
|
render json: {
|
||||||
html: render_to_string({
|
html: render_to_string({
|
||||||
|
|
@ -226,11 +220,6 @@ class StepsController < ApplicationController
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
if @step.can_destroy?
|
if @step.can_destroy?
|
||||||
# Update position on other steps of this module
|
|
||||||
@protocol.steps.where('position > ?', @step.position).each do |step|
|
|
||||||
step.position = step.position - 1
|
|
||||||
step.save
|
|
||||||
end
|
|
||||||
|
|
||||||
# Calculate space taken by this step
|
# Calculate space taken by this step
|
||||||
team = @protocol.team
|
team = @protocol.team
|
||||||
|
|
@ -250,9 +239,6 @@ class StepsController < ApplicationController
|
||||||
team.release_space(previous_size)
|
team.release_space(previous_size)
|
||||||
team.save
|
team.save
|
||||||
|
|
||||||
# Update protocol timestamp
|
|
||||||
update_protocol_ts(@step)
|
|
||||||
|
|
||||||
flash[:success] = t(
|
flash[:success] = t(
|
||||||
'protocols.steps.destroy.success_flash',
|
'protocols.steps.destroy.success_flash',
|
||||||
step: (@step.position_plus_one).to_s
|
step: (@step.position_plus_one).to_s
|
||||||
|
|
@ -367,57 +353,26 @@ class StepsController < ApplicationController
|
||||||
|
|
||||||
def move_up
|
def move_up
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @step.position.positive?
|
|
||||||
step_down = @step.protocol.steps.find_by(position: @step.position - 1)
|
|
||||||
@step.position -= 1
|
|
||||||
@step.save
|
|
||||||
|
|
||||||
if step_down
|
|
||||||
step_down.position += 1
|
|
||||||
step_down.save
|
|
||||||
|
|
||||||
# Update protocol timestamp
|
|
||||||
update_protocol_ts(@step)
|
|
||||||
|
|
||||||
format.json do
|
format.json do
|
||||||
render json: { move_direction: 'up',
|
@step.move_up!
|
||||||
|
|
||||||
|
render json: {
|
||||||
step_up_position: @step.position,
|
step_up_position: @step.position,
|
||||||
step_down_position: step_down.position },
|
step_down_position: @step.position + 1
|
||||||
status: :ok
|
}
|
||||||
end
|
|
||||||
else
|
|
||||||
format.json { render json: {}, status: :forbidden }
|
|
||||||
end
|
|
||||||
else
|
|
||||||
format.json { render json: {}, status: :forbidden }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_down
|
def move_down
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @step.position < @step.protocol.steps.count - 1
|
|
||||||
step_up = @step.protocol.steps.find_by(position: @step.position + 1)
|
|
||||||
@step.position += 1
|
|
||||||
@step.save
|
|
||||||
|
|
||||||
if step_up
|
|
||||||
step_up.position -= 1
|
|
||||||
step_up.save
|
|
||||||
|
|
||||||
# Update protocol timestamp
|
|
||||||
update_protocol_ts(@step)
|
|
||||||
|
|
||||||
format.json do
|
format.json do
|
||||||
render json: { move_direction: 'down',
|
@step.move_down!
|
||||||
step_up_position: step_up.position,
|
|
||||||
step_down_position: @step.position }
|
render json: {
|
||||||
end
|
step_up_position: @step.position - 1,
|
||||||
else
|
step_down_position: @step.position
|
||||||
format.json { render json: {}, status: :forbidden }
|
}
|
||||||
end
|
|
||||||
else
|
|
||||||
format.json { render json: {}, status: :forbidden }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -517,12 +472,6 @@ class StepsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_protocol_ts(step)
|
|
||||||
if step.present? && step.protocol.present?
|
|
||||||
step.protocol.update(updated_at: Time.now)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_checklist_items_without_callback(params)
|
def update_checklist_items_without_callback(params)
|
||||||
params.dig('checklists_attributes')&.values&.each do |cl|
|
params.dig('checklists_attributes')&.values&.each do |cl|
|
||||||
ck = @step.checklists.find_by(id: cl[:id])
|
ck = @step.checklists.find_by(id: cl[:id])
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class Step < ApplicationRecord
|
||||||
presence: true,
|
presence: true,
|
||||||
length: { maximum: Constants::NAME_MAX_LENGTH }
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
||||||
validates :description, length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
|
validates :description, length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
|
||||||
validates :position, presence: true
|
validates :position, presence: true, uniqueness: { scope: :protocol }
|
||||||
validates :completed, inclusion: { in: [true, false] }
|
validates :completed, inclusion: { in: [true, false] }
|
||||||
validates :user, :protocol, presence: true
|
validates :user, :protocol, presence: true
|
||||||
validates :completed_on, presence: true, if: proc { |s| s.completed? }
|
validates :completed_on, presence: true, if: proc { |s| s.completed? }
|
||||||
|
|
@ -17,11 +17,11 @@ class Step < ApplicationRecord
|
||||||
before_validation :set_completed_on, if: :completed_changed?
|
before_validation :set_completed_on, if: :completed_changed?
|
||||||
before_save :set_last_modified_by
|
before_save :set_last_modified_by
|
||||||
before_destroy :cascade_before_destroy
|
before_destroy :cascade_before_destroy
|
||||||
before_destroy :adjust_positions_on_destroy
|
after_destroy :adjust_positions_after_destroy
|
||||||
|
|
||||||
belongs_to :user, inverse_of: :steps
|
belongs_to :user, inverse_of: :steps
|
||||||
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true
|
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true
|
||||||
belongs_to :protocol, inverse_of: :steps
|
belongs_to :protocol, inverse_of: :steps, touch: true
|
||||||
has_many :checklists, inverse_of: :step, dependent: :destroy
|
has_many :checklists, inverse_of: :step, dependent: :destroy
|
||||||
has_many :step_comments, foreign_key: :associated_id, dependent: :destroy
|
has_many :step_comments, foreign_key: :associated_id, dependent: :destroy
|
||||||
has_many :step_assets, inverse_of: :step, dependent: :destroy
|
has_many :step_assets, inverse_of: :step, dependent: :destroy
|
||||||
|
|
@ -90,6 +90,36 @@ class Step < ApplicationRecord
|
||||||
protocol.present? ? protocol.my_module : nil
|
protocol.present? ? protocol.my_module : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def move_up!
|
||||||
|
return unless position.positive?
|
||||||
|
|
||||||
|
step_above = protocol.steps.find_by(position: position - 1)
|
||||||
|
|
||||||
|
return unless step_above
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
step_above.position = position
|
||||||
|
update!(position: -1)
|
||||||
|
step_above.save!
|
||||||
|
update!(position: step_above.position - 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def move_down!
|
||||||
|
return unless position < protocol.steps.count - 1
|
||||||
|
|
||||||
|
step_below = protocol.steps.find_by(position: position + 1)
|
||||||
|
|
||||||
|
return unless step_below
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
step_below.position = position
|
||||||
|
update!(position: -1)
|
||||||
|
step_below.save!
|
||||||
|
update!(position: step_below.position + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def position_plus_one
|
def position_plus_one
|
||||||
position + 1
|
position + 1
|
||||||
end
|
end
|
||||||
|
|
@ -125,9 +155,9 @@ class Step < ApplicationRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def adjust_positions_on_destroy
|
def adjust_positions_after_destroy
|
||||||
protocol.steps.where('position > ?', position).find_each do |step|
|
protocol.steps.where('position > ?', position).find_each do |step|
|
||||||
step.update(position: step.position - 1)
|
step.update!(position: step.position - 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,29 +49,28 @@
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if (can_manage_protocol_in_module?(@protocol) ||
|
<% if (can_manage_protocol_in_module?(@protocol) || can_manage_protocol_in_repository?(@protocol)) && !(preview) %>
|
||||||
can_manage_protocol_in_repository?(@protocol)) && !(preview) %>
|
<%= link_to(move_up_step_path(step),
|
||||||
<a data-action="move-step"
|
class: 'btn btn-light icon-btn',
|
||||||
class="btn btn-light icon-btn"
|
title: t('protocols.steps.options.up_arrow_title'),
|
||||||
href="<%= move_up_step_path(step, format: :json) %>"
|
remote: true,
|
||||||
title="<%= t("protocols.steps.options.up_arrow_title") %>"
|
method: :put,
|
||||||
data-remote="true">
|
data: { action: 'move-step', direction: :up }) do %>
|
||||||
<span class="fas fa-arrow-up"></span></a>
|
<span class="fas fa-arrow-up"></span>
|
||||||
<a data-action="move-step"
|
<% end %>
|
||||||
class="btn btn-light icon-btn"
|
<%= link_to(move_down_step_path(step),
|
||||||
href="<%= move_down_step_path(step, format: :json) %>"
|
class: 'btn btn-light icon-btn',
|
||||||
title="<%= t("protocols.steps.options.down_arrow_title") %>"
|
title: t('protocols.steps.options.down_arrow_title'),
|
||||||
data-remote="true">
|
remote: true,
|
||||||
<span class="fas fa-arrow-down"></span></a>
|
method: :put,
|
||||||
<a data-action="edit-step"
|
data: { action: 'move-step', direction: :down }) do %>
|
||||||
class="btn btn-light icon-btn"
|
<span class="fas fa-arrow-down"></span>
|
||||||
title="<%= t("protocols.steps.options.edit_title") %>"
|
<% end %>
|
||||||
href="<%= edit_step_path(step, format: :json) %>"
|
<%= link_to(step_path(step),
|
||||||
data-remote="true" >
|
title: t('protocols.steps.options.delete_title'),
|
||||||
<span class="fas fa-pencil-alt"></span>
|
method: :delete,
|
||||||
</a>
|
class: 'btn btn-light icon-btn',
|
||||||
<%= link_to(step_path(step), title: t("protocols.steps.options.delete_title"), method: "delete", class: "btn btn-light icon-btn",
|
data: { action: 'delete-step', confirm: t('protocols.steps.destroy.confirm', step: step.name) }) do %>
|
||||||
data: {action: "delete-step", confirm: t("protocols.steps.destroy.confirm", step: step.name)}) do %>
|
|
||||||
<span class="fas fa-trash-alt"></span>
|
<span class="fas fa-trash-alt"></span>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
||||||
|
|
@ -448,8 +448,8 @@ Rails.application.routes.draw do
|
||||||
member do
|
member do
|
||||||
post 'checklistitem_state'
|
post 'checklistitem_state'
|
||||||
post 'toggle_step_state'
|
post 'toggle_step_state'
|
||||||
get 'move_down'
|
put 'move_down'
|
||||||
get 'move_up'
|
put 'move_up'
|
||||||
post 'update_view_state'
|
post 'update_view_state'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue