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-04-11 03:52:18 +00:00
|
|
|
function lib.showContext(id)
|
2022-04-15 11:27:08 +02:00
|
|
|
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]
|
2022-04-15 10:22:28 +02:00
|
|
|
openContextMenu = id
|
2022-04-01 21:22:14 +02:00
|
|
|
SetNuiFocus(true, true)
|
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,
|
|
|
|
|
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-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-04-15 10:22:28 +02:00
|
|
|
function lib.getOpenContextMenu() return openContextMenu end
|
|
|
|
|
|
2022-04-11 16:14:32 +02:00
|
|
|
RegisterNUICallback('openContext', function(id, cb)
|
|
|
|
|
cb(1)
|
2022-04-11 03:52:18 +00:00
|
|
|
lib.showContext(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-04-15 11:18:05 +02:00
|
|
|
SetNuiFocus(false, false)
|
2022-05-28 19:15:35 +02:00
|
|
|
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
|
2022-05-28 19:15:35 +02:00
|
|
|
|
2022-04-15 10:22:28 +02:00
|
|
|
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)
|