Files
ox_lib/resource/interface/client/context.lua
DokaDoka d2178ca258 fix(interface/context): correct id if a float is received
- the array part of a mixed options table registers a '1.0' and so on, this is not how the option is recorded on the lua side
2022-06-04 22:12:04 +10:00

66 lines
1.7 KiB
Lua

local contextMenus = {}
local openContextMenu = nil
function lib.showContext(id)
if not contextMenus[id] then return error('No context menu of such id found.') end
local data = contextMenus[id]
openContextMenu = id
SetNuiFocus(true, true)
SendNuiMessage(json.encode({
action = 'showContext',
data = {
title = data.title,
menu = data.menu,
options = data.options
}
}, { sort_keys = true }))
end
function lib.registerContext(context)
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
function lib.getOpenContextMenu() return openContextMenu end
function lib.hideContext()
SetNuiFocus(false, false)
SendNUIMessage({action = 'hideContext'})
openContextMenu = nil
end
RegisterNUICallback('openContext', function(id, cb)
cb(1)
lib.showContext(id)
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 then return end
openContextMenu = nil
SetNuiFocus(false, false)
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', function(_, cb)
cb(1)
SetNuiFocus(false, false)
openContextMenu = nil
end)