Files
ox_lib/resource/interface/client/context.lua

54 lines
1.3 KiB
Lua
Raw Normal View History

2022-04-01 21:22:14 +02:00
local contextMenus = {}
local openContextMenu = nil
2022-04-01 21:22:14 +02:00
function lib.showContext(id)
if not contextMenus[id] then return error('No context menu of such id found.') end
2022-04-01 21:22:14 +02:00
local data = contextMenus[id]
openContextMenu = id
2022-04-01 21:22:14 +02:00
SetNuiFocus(true, true)
SendNuiMessage(json.encode({
2022-04-01 21:22:14 +02:00
action = 'showContext',
data = {
title = data.title,
menu = data.menu,
options = data.options
}
}, { sort_keys = true }))
2022-04-01 21:22:14 +02:00
end
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
function lib.getOpenContextMenu() return openContextMenu end
2022-04-11 16:14:32 +02:00
RegisterNUICallback('openContext', function(id, cb)
cb(1)
lib.showContext(id)
2022-04-01 21:22:14 +02:00
end)
RegisterNUICallback('clickContext', function(id, cb)
2022-04-11 16:14:32 +02:00
cb(1)
SetNuiFocus(false, false)
local data = contextMenus[openContextMenu].options[id]
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
openContextMenu = nil
2022-04-01 21:22:14 +02:00
SendNUIMessage({
action = 'hideContext'
})
end)
2022-04-11 16:14:32 +02:00
RegisterNUICallback('closeContext', function(_, cb)
cb(1)
2022-04-01 21:22:14 +02:00
SetNuiFocus(false, false)
end)