mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 15:06:02 +01:00
@@ -1,5 +1,6 @@
|
||||
local contextMenus = {}
|
||||
local openContextMenu = nil
|
||||
local keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
---@class ContextMenuItem
|
||||
---@field title? string
|
||||
@@ -27,11 +28,19 @@ local openContextMenu = nil
|
||||
---@field canClose? boolean
|
||||
---@field options { [string]: ContextMenuItem } | ContextMenuArrayItem[]
|
||||
|
||||
local function resetFocus()
|
||||
SetNuiFocus(false, false)
|
||||
SetNuiFocusKeepInput(keepInput)
|
||||
end
|
||||
|
||||
local function closeContext(_, cb, onExit)
|
||||
if cb then cb(1) end
|
||||
if (cb or onExit) and contextMenus[openContextMenu].onExit then contextMenus[openContextMenu].onExit() end
|
||||
SetNuiFocus(false, false)
|
||||
|
||||
resetFocus()
|
||||
|
||||
if not cb then SendNUIMessage({ action = 'hideContext' }) end
|
||||
|
||||
openContextMenu = nil
|
||||
end
|
||||
|
||||
@@ -40,7 +49,11 @@ function lib.showContext(id)
|
||||
if not contextMenus[id] then error('No context menu of such id found.') end
|
||||
local data = contextMenus[id]
|
||||
openContextMenu = id
|
||||
keepInput = IsNuiFocusKeepingInput()
|
||||
|
||||
SetNuiFocus(true, true)
|
||||
SetNuiFocusKeepInput(false)
|
||||
|
||||
SendNuiMessage(json.encode({
|
||||
action = 'showContext',
|
||||
data = {
|
||||
@@ -78,21 +91,31 @@ end)
|
||||
|
||||
RegisterNUICallback('clickContext', function(id, cb)
|
||||
cb(1)
|
||||
|
||||
if math.type(tonumber(id)) == 'float' then
|
||||
id = math.tointeger(id)
|
||||
elseif tonumber(id) then
|
||||
id += 1
|
||||
end
|
||||
|
||||
local data = contextMenus[openContextMenu].options[id]
|
||||
|
||||
if not data.event and not data.serverEvent and not data.onSelect then return end
|
||||
|
||||
openContextMenu = nil
|
||||
SetNuiFocus(false, false)
|
||||
|
||||
resetFocus()
|
||||
|
||||
if data.onSelect then data.onSelect(data.args) end
|
||||
if data.event then TriggerEvent(data.event, data.args) end
|
||||
if data.serverEvent then TriggerServerEvent(data.serverEvent, data.args) end
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'hideContext'
|
||||
})
|
||||
end)
|
||||
|
||||
RegisterNUICallback('closeContext', closeContext)
|
||||
RegisterNUICallback('closeContext', closeContext)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user