Method: OpenC3::ToolModel.set_position

Defined in:
lib/openc3/models/tool_model.rb

.set_position(name:, position:, scope:) ⇒ Object

The ToolsTab.vue calls the ToolsController which uses this method to reorder the tools Position is index in the list starting with 0 = first



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/openc3/models/tool_model.rb', line 95

def self.set_position(name:, position:, scope:)
  moving = from_json(get(name: name, scope: scope), scope: scope)
  old_pos = moving.position
  new_pos = Integer(position)
  direction = :down
  if (old_pos == new_pos)
    return # we're not doing anything
  elsif (new_pos > old_pos)
    direction = :up
  end

  # Go through all the tools and reorder
  all(scope: scope).each do |_tool_name, tool|
    tool_model = from_json(tool, scope: scope)
    # Update the requested model to the new position
    if tool_model.name == name
      tool_model.position = position
    elsif direction == :down
      if tool_model.position >= new_pos && tool_model.position < old_pos
        tool_model.position += 1
      end
    else # up
      if tool_model.position > old_pos && tool_model.position <= new_pos
        tool_model.position -= 1
      end
    end
    tool_model.update
  end
end