z_mirror/TaskList

git clone https://git.raiguard.me/z_mirror/TaskList

Caleb HeuerUpdate require syntaxc5cc477

trunk
2.4 KiB102 linesraw
1local constants = require("constants")
2
3local task = require("scripts.task")
4local util = require("scripts.util")
5
6local actions = {}
7
8--- @param Gui EditTaskGui
9function actions.close(Gui)
10  if Gui.state.just_confirmed then
11    Gui.state.just_confirmed = false
12    Gui.player.opened = Gui.refs.window
13  else
14    Gui:destroy()
15  end
16end
17
18--- @param Gui EditTaskGui
19function actions.recenter(Gui)
20  Gui.refs.window.force_auto_center()
21end
22
23--- @param Gui EditTaskGui
24function actions.update_assignee_dropdown(Gui)
25  local dropdown = Gui.refs.assignee_dropdown
26
27  local is_private = Gui.refs.private_checkbox.state
28  if is_private then
29    dropdown.enabled = false
30    dropdown.selected_index = Gui.state.player_selection_index --[[@as uint]]
31  else
32    dropdown.enabled = true
33  end
34end
35
36--- @param Gui EditTaskGui
37--- @param e EventData.on_gui_confirmed|EventData.CustomInputEvent
38function actions.confirm(Gui, _, e)
39  local refs = Gui.refs
40  local clicked = e.name == defines.events.on_gui_confirmed or e.name == defines.events.on_gui_click
41
42  local title = refs.title_textfield.text
43  if #title == 0 then
44    util.error_text(Gui.player, { "message.tlst-task-must-have-title" })
45    if not clicked then
46      Gui.state.just_confirmed = true
47    end
48    return
49  end
50
51  local assignee
52  local assignee_dropdown = refs.assignee_dropdown
53  local selected_index = assignee_dropdown.selected_index
54  if selected_index > 1 then
55    assignee = game.players[assignee_dropdown.items[selected_index]]
56  end
57
58  local status_index = refs.status_dropdown.selected_index
59  local status
60  for status_name, status_info in pairs(constants.task_status) do
61    if status_info.index == status_index then
62      status = status_name
63    end
64  end
65
66  local Task = Gui.state.task
67  if Task then
68    Task:update(refs.title_textfield.text, refs.description_textfield.text, assignee, status)
69  else
70    --- @type LuaForce|LuaPlayer|Task
71    local owner = Gui.state.parent_task
72    if not owner then
73      local is_private = refs.private_checkbox.state
74      owner = is_private and Gui.player or util.get_force(Gui.player)
75    end
76
77    task.new(
78      refs.title_textfield.text,
79      refs.description_textfield.text,
80      owner,
81      assignee,
82      status,
83      refs.add_to_top_checkbox.state
84    )
85  end
86
87  if clicked then
88    Gui:destroy()
89  end
90end
91
92--- @param Gui EditTaskGui
93function actions.delete(Gui)
94  local Task = Gui.state.task
95  if Task then
96    Task:delete()
97  end
98
99  Gui:destroy()
100end
101
102return actions