2022-04-01 21:22:14 +02:00
|
|
|
local contextMenus = {}
|
2022-04-15 10:22:28 +02:00
|
|
|
local openContextMenu = nil
|
2022-04-01 21:22:14 +02:00
|
|
|
|
2022-09-18 09:06:32 +10:00
|
|
|
---@class ContextMenuItem
|
|
|
|
|
---@field title? string
|
|
|
|
|
---@field menu? string
|
2023-03-10 19:38:34 +00:00
|
|
|
---@field icon? string | {[1]: IconProp, [2]: string};
|
2022-09-18 09:06:32 +10:00
|
|
|
---@field iconColor? string
|
|
|
|
|
---@field onSelect? fun(args: any)
|
|
|
|
|
---@field arrow? boolean
|
|
|
|
|
---@field description? string
|
|
|
|
|
---@field metadata? string | { [string]: any } | string[]
|
2022-11-06 11:01:39 +01:00
|
|
|
---@field disabled? boolean
|
2022-09-18 09:06:32 +10:00
|
|
|
---@field event? string
|
|
|
|
|
---@field serverEvent? string
|
|
|
|
|
---@field args? any
|
|
|
|
|
|
|
|
|
|
---@class ContextMenuArrayItem : ContextMenuItem
|
|
|
|
|
---@field title string
|
|
|
|
|
|
|
|
|
|
---@class ContextMenuProps
|
|
|
|
|
---@field id string
|
|
|
|
|
---@field title string
|
|
|
|
|
---@field menu? string
|
|
|
|
|
---@field onExit? fun()
|
2022-10-11 12:48:59 +01:00
|
|
|
---@field onBack? fun()
|
2022-09-18 09:06:32 +10:00
|
|
|
---@field canClose? boolean
|
|
|
|
|
---@field options { [string]: ContextMenuItem } | ContextMenuArrayItem[]
|
|
|
|
|
|
2022-06-04 14:42:35 +02:00
|
|
|
local function closeContext(_, cb, onExit)
|
|
|
|
|
if cb then cb(1) end
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2023-03-01 07:25:28 +11:00
|
|
|
lib.resetNuiFocus()
|
|
|
|
|
|
2023-04-21 12:31:33 +10:00
|
|
|
if not openContextMenu then return end
|
|
|
|
|
|
2023-03-01 07:25:28 +11:00
|
|
|
if (cb or onExit) and contextMenus[openContextMenu].onExit then contextMenus[openContextMenu].onExit() end
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-09-18 09:06:32 +10:00
|
|
|
if not cb then SendNUIMessage({ action = 'hideContext' }) end
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-06-04 14:42:35 +02:00
|
|
|
openContextMenu = nil
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-18 09:06:32 +10:00
|
|
|
---@param id string
|
2022-04-11 03:52:18 +00:00
|
|
|
function lib.showContext(id)
|
2022-09-18 09:06:32 +10:00
|
|
|
if not contextMenus[id] then error('No context menu of such id found.') end
|
2023-03-01 07:25:28 +11:00
|
|
|
|
2022-04-01 21:22:14 +02:00
|
|
|
local data = contextMenus[id]
|
2022-04-15 10:22:28 +02:00
|
|
|
openContextMenu = id
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2023-03-01 07:25:28 +11:00
|
|
|
lib.setNuiFocus(false)
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-04-16 11:48:24 +02:00
|
|
|
SendNuiMessage(json.encode({
|
2022-04-01 21:22:14 +02:00
|
|
|
action = 'showContext',
|
|
|
|
|
data = {
|
|
|
|
|
title = data.title,
|
2022-09-08 10:05:44 +02:00
|
|
|
canClose = data.canClose,
|
2022-04-01 21:22:14 +02:00
|
|
|
menu = data.menu,
|
|
|
|
|
options = data.options
|
|
|
|
|
}
|
2022-05-28 19:15:35 +02:00
|
|
|
}, { sort_keys = true }))
|
2022-04-01 21:22:14 +02:00
|
|
|
end
|
|
|
|
|
|
2022-09-18 09:06:32 +10:00
|
|
|
---@param context ContextMenuProps | ContextMenuProps[]
|
2022-04-11 03:52:18 +00:00
|
|
|
function lib.registerContext(context)
|
2022-04-01 21:22:14 +02:00
|
|
|
for k, v in pairs(context) do
|
|
|
|
|
if type(k) == 'number' then
|
|
|
|
|
contextMenus[v.id] = v
|
|
|
|
|
else
|
|
|
|
|
contextMenus[context.id] = context
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-18 09:06:32 +10:00
|
|
|
---@return string?
|
2022-04-15 10:22:28 +02:00
|
|
|
function lib.getOpenContextMenu() return openContextMenu end
|
|
|
|
|
|
2022-09-18 09:06:32 +10:00
|
|
|
---@param onExit boolean?
|
|
|
|
|
function lib.hideContext(onExit) closeContext(nil, nil, onExit) end
|
2022-05-30 16:39:02 +02:00
|
|
|
|
2022-10-11 12:48:59 +01:00
|
|
|
RegisterNUICallback('openContext', function(data, cb)
|
|
|
|
|
if data.back and contextMenus[openContextMenu].onBack then contextMenus[openContextMenu].onBack() end
|
2022-04-11 16:14:32 +02:00
|
|
|
cb(1)
|
2022-10-11 12:48:59 +01:00
|
|
|
lib.showContext(data.id)
|
2022-04-01 21:22:14 +02:00
|
|
|
end)
|
|
|
|
|
|
2022-05-28 19:15:35 +02:00
|
|
|
RegisterNUICallback('clickContext', function(id, cb)
|
2022-04-11 16:14:32 +02:00
|
|
|
cb(1)
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-06-04 13:49:15 +10:00
|
|
|
if math.type(tonumber(id)) == 'float' then
|
|
|
|
|
id = math.tointeger(id)
|
|
|
|
|
elseif tonumber(id) then
|
|
|
|
|
id += 1
|
|
|
|
|
end
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-05-28 19:15:35 +02:00
|
|
|
local data = contextMenus[openContextMenu].options[id]
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-08-17 21:41:02 +02:00
|
|
|
if not data.event and not data.serverEvent and not data.onSelect then return end
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-05-29 15:13:29 +02:00
|
|
|
openContextMenu = nil
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2023-04-21 12:31:33 +10:00
|
|
|
SendNUIMessage({ action = 'hideContext' })
|
2022-12-13 08:37:56 +11:00
|
|
|
|
2022-08-17 21:41:02 +02:00
|
|
|
if data.onSelect then data.onSelect(data.args) end
|
2022-04-01 21:22:14 +02:00
|
|
|
if data.event then TriggerEvent(data.event, data.args) end
|
|
|
|
|
if data.serverEvent then TriggerServerEvent(data.serverEvent, data.args) end
|
|
|
|
|
end)
|
|
|
|
|
|
2022-12-13 08:37:56 +11:00
|
|
|
RegisterNUICallback('closeContext', closeContext)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|