Skip to content

Commit

Permalink
feat: pagination builder
Browse files Browse the repository at this point in the history
  • Loading branch information
swkeep committed Nov 15, 2024
1 parent 021b1c3 commit 0dd1f97
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions interactionMenu/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ client_script {
'lua/client/interact.lua',
'lua/client/drawIndicator.lua',
'lua/client/garbageCollector.lua',
'lua/client/extends/*.lua',

-- providers
'lua/providers/qb-target.lua',
Expand Down
103 changes: 103 additions & 0 deletions interactionMenu/lua/client/extends/pagination.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
local __export = exports['interactionMenu']
local refresh = __export.refresh
local PaginationBuilder = {}
PaginationBuilder.__index = PaginationBuilder

function PaginationBuilder:calculatePages()
self.pages = math.ceil(#self.user_options / self.itemsPerPage)
end

function PaginationBuilder:addHeader()
self.options = {
{
label = "Page: #1 | Total: " .. tostring(self.pages),
bind = function()
return ("Page: #%d | Total: %d"):format(self.currentPage, self.pages)
end,
canInteract = function()
return self.pages > 1
end
},
}
end

function PaginationBuilder:addFooter()
local function updatePage(direction)
self.currentPage = (self.currentPage - 1 + direction + self.pages) % self.pages + 1

for i = 2, #self.options - 2 do
local option = self.options[i]
__export:set {
menuId = self.menu_id,
type = 'hide',
option = i,
value = option.page ~= self.currentPage
}
end

refresh()
end

-- Pagination controls
if self.pages > 1 then
table.insert(self.options, {
label = 'Prev Page',
icon = 'fa fa-arrow-left',
action = function() updatePage(-1) end
})

table.insert(self.options, {
label = 'Next Page',
icon = 'fa fa-arrow-right',
action = function() updatePage(1) end
})
end
end

function PaginationBuilder:generateOptions()
for index, option in ipairs(self.user_options) do
local page = math.ceil(index / self.itemsPerPage)
table.insert(self.options, {
label = option.label,
icon = option.icon,
action = option.action,
bind = option.bind,
event = option.event,
command = option.command,
canInteract = option.canInteract,
progress = option.progress,
dynamic = option.dynamic,
style = option.style,
video = option.video,
picture = option.picture,
page = page,
hide = page ~= 1,
})
end
end

function PaginationBuilder:create(t)
self = setmetatable({}, PaginationBuilder)
self.itemsPerPage = t.itemsPerPage or 6
self.currentPage = 1
self.menu_id = nil
self.options = {}
self.user_options = t.options or {}

self:addHeader()
self:calculatePages()
self:generateOptions()
self:addFooter()

t.options = self.options
self.menu_id = __export:Create(t)

return self.menu_id
end

local function interface(t)
return PaginationBuilder:create(t)
end

exports("paginatedMenu", interface)
exports("paginateMenu", interface)

0 comments on commit 0dd1f97

Please sign in to comment.