Add multiple selection

This commit is contained in:
Athozus 2023-04-10 19:41:30 +02:00
parent 67bda9a788
commit 4673e71260
No known key found for this signature in database
GPG key ID: B50895022E8484BF
4 changed files with 162 additions and 57 deletions

View file

@ -23,7 +23,8 @@ mail = {
bcc = {}, bcc = {},
boxtab = {}, boxtab = {},
sortfield = {}, sortfield = {},
sortdirection = {} sortdirection = {},
multipleselection = {}
}, },
message_drafts = {} message_drafts = {}

View file

@ -26,6 +26,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- Store common player configuration for reuse -- Store common player configuration for reuse
mail.selected_idxs.sortfield[name] = sortfieldindex mail.selected_idxs.sortfield[name] = sortfieldindex
mail.selected_idxs.sortdirection[name] = sortdirection mail.selected_idxs.sortdirection[name] = sortdirection
if fields.multipleselection then
mail.selected_idxs.multipleselection[name] = fields.multipleselection == "true"
end
-- split inbox and sent msgs for different tests -- split inbox and sent msgs for different tests
local entry = mail.get_storage_entry(name) local entry = mail.get_storage_entry(name)
@ -36,18 +39,60 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- Hanmdle formspec event -- Hanmdle formspec event
if fields.inbox then -- inbox table if fields.inbox then -- inbox table
local evt = minetest.explode_table_event(fields.inbox) local evt = minetest.explode_table_event(fields.inbox)
mail.selected_idxs.inbox[name] = evt.row - 1 if mail.selected_idxs.multipleselection[name] then
if evt.type == "DCL" and getInbox()[mail.selected_idxs.inbox[name]] then if not mail.selected_idxs.inbox[name] then
mail.show_message(name, getInbox()[mail.selected_idxs.inbox[name]].id) mail.selected_idxs.inbox[name] = {}
end
local selected_id = 0
if mail.selected_idxs.inbox[name] and #mail.selected_idxs.inbox[name] > 0 then
for i, selected_msg in ipairs(mail.selected_idxs.inbox[name]) do
if getInbox()[evt.row-1].id == selected_msg then
selected_id = i
table.remove(mail.selected_idxs.inbox[name], i)
break
end
end
end
if selected_id == 0 then
table.insert(mail.selected_idxs.inbox[name], getInbox()[evt.row-1].id)
end
else
mail.selected_idxs.inbox[name] = { getInbox()[evt.row-1].id }
end
if evt.type == "DCL" and getInbox()[evt.row-1] then
mail.show_message(name, getInbox()[evt.row-1].id)
else
mail.show_mail_menu(name)
end end
return true return true
end end
if fields.sent then -- sent table if fields.sent then -- sent table
local evt = minetest.explode_table_event(fields.sent) local evt = minetest.explode_table_event(fields.sent)
mail.selected_idxs.sent[name] = evt.row - 1 if mail.selected_idxs.multipleselection[name] then
if evt.type == "DCL" and getOutbox()[mail.selected_idxs.sent[name]] then if not mail.selected_idxs.sent[name] then
mail.show_message(name, getOutbox()[mail.selected_idxs.sent[name]].id) mail.selected_idxs.sent[name] = {}
end
local selected_id = 0
if mail.selected_idxs.sent[name] and #mail.selected_idxs.sent[name] > 0 then
for i, selected_msg in ipairs(mail.selected_idxs.sent[name]) do
if getOutbox()[evt.row-1].id == selected_msg then
selected_id = i
table.remove(mail.selected_idxs.sent[name], i)
break
end
end
end
if selected_id == 0 then
table.insert(mail.selected_idxs.sent[name], getOutbox()[evt.row-1].id)
end
else
mail.selected_idxs.sent[name] = { getOutbox()[evt.row-1].id }
end
if evt.type == "DCL" and getOutbox()[evt.row-1] then
mail.show_message(name, getOutbox()[evt.row-1].id)
else
mail.show_mail_menu(name)
end end
return true return true
end end
@ -81,10 +126,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
mail.show_drafts(name) mail.show_drafts(name)
elseif fields.read then elseif fields.read then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then -- inbox table if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then -- inbox table
mail.show_message(name, getInbox()[mail.selected_idxs.inbox[name]].id) mail.show_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then -- sent table elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then -- sent table
mail.show_message(name, getOutbox()[mail.selected_idxs.sent[name]].id) mail.show_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
end end
elseif fields.edit then elseif fields.edit then
@ -100,10 +145,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
elseif fields.delete then elseif fields.delete then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then -- inbox table if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then -- inbox table
mail.delete_mail(name, getInbox()[mail.selected_idxs.inbox[name]].id) for _, msg_id in ipairs(mail.selected_idxs.inbox[name]) do
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then -- sent table mail.delete_mail(name, msg_id)
mail.delete_mail(name, getOutbox()[mail.selected_idxs.sent[name]].id) end
elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then -- sent table
for _, msg_id in ipairs(mail.selected_idxs.sent[name]) do
mail.delete_mail(name, msg_id)
end
elseif formname == "mail:drafts" and messagesDrafts[mail.selected_idxs.drafts[name]] then -- drafts table elseif formname == "mail:drafts" and messagesDrafts[mail.selected_idxs.drafts[name]] then -- drafts table
mail.delete_mail(name, messagesDrafts[mail.selected_idxs.drafts[name]].id) mail.delete_mail(name, messagesDrafts[mail.selected_idxs.drafts[name]].id)
end end
@ -111,46 +160,46 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
mail.show_mail_menu(name, sortfieldindex, sortdirection, filter) mail.show_mail_menu(name, sortfieldindex, sortdirection, filter)
elseif fields.reply then elseif fields.reply then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
local message = getInbox()[mail.selected_idxs.inbox[name]] local message = mail.get_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
mail.reply(name, message) mail.reply(name, message)
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then
local message = getOutbox()[mail.selected_idxs.sent[name]] local message = mail.get_message(name, mail.selected_idxs.sent[name][#mail.selected_idxs.sent[name]])
mail.reply(name, message) mail.reply(name, message)
end end
elseif fields.replyall then elseif fields.replyall then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
local message = getInbox()[mail.selected_idxs.inbox[name]] local message = mail.get_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
mail.replyall(name, message) mail.replyall(name, message)
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then
local message = getOutbox()[mail.selected_idxs.sent[name]] local message = mail.get_message(name, mail.selected_idxs.sent[name][#mail.selected_idxs.sent[name]])
mail.replyall(name, message) mail.replyall(name, message)
end end
elseif fields.forward then elseif fields.forward then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
local message = getInbox()[mail.selected_idxs.inbox[name]] local message = mail.get_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
mail.forward(name, message) mail.forward(name, message)
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then
local message = getOutbox()[mail.selected_idxs.sent[name]] local message = mail.get_message(name, mail.selected_idxs.sent[name][#mail.selected_idxs.sent[name]])
mail.forward(name, message) mail.forward(name, message)
end end
elseif fields.markread then elseif fields.markread then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
mail.mark_read(name, getInbox()[mail.selected_idxs.inbox[name]].id) for _, msg_id in ipairs(mail.selected_idxs.inbox[name]) do
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then mail.mark_read(name, msg_id)
mail.mark_read(name, getOutbox()[mail.selected_idxs.sent[name]].id) end
end end
mail.show_mail_menu(name, sortfieldindex, sortdirection, filter) mail.show_mail_menu(name, sortfieldindex, sortdirection, filter)
elseif fields.markunread then elseif fields.markunread then
if formname == "mail:inbox" and getInbox()[mail.selected_idxs.inbox[name]] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
mail.mark_unread(name, getInbox()[mail.selected_idxs.inbox[name]].id) for _, msg_id in ipairs(mail.selected_idxs.inbox[name]) do
elseif formname == "mail:sent" and getOutbox()[mail.selected_idxs.sent[name]] then mail.mark_unread(name, msg_id)
mail.mark_unread(name, getOutbox()[mail.selected_idxs.sent[name]].id) end
end end
mail.show_mail_menu(name, sortfieldindex, sortdirection, filter) mail.show_mail_menu(name, sortfieldindex, sortdirection, filter)
@ -167,6 +216,35 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
elseif fields.about then elseif fields.about then
mail.show_about(name) mail.show_about(name)
elseif fields.selectall then
if formname == "mail:inbox" then
if not mail.selected_idxs.inbox[name] then
mail.selected_idxs.inbox[name] = {}
end
if #mail.selected_idxs.inbox[name] >= #getInbox() then -- if selection is full
mail.selected_idxs.inbox[name] = {}
else
mail.selected_idxs.multipleselection[name] = true
for _, msg in ipairs(getInbox()) do
table.insert(mail.selected_idxs.inbox[name], msg.id)
end
end
elseif formname == "mail:sent" then
if not mail.selected_idxs.sent[name] then
mail.selected_idxs.sent[name] = {}
end
if #mail.selected_idxs.sent[name] >= #getOutbox() then -- if selection is full
mail.selected_idxs.sent[name] = {}
else
mail.selected_idxs.multipleselection[name] = true
for _, msg in ipairs(getOutbox()) do
table.insert(mail.selected_idxs.sent[name], msg.id)
end
end
end
mail.show_mail_menu(name)
elseif fields.sortfield or fields.sortdirection or fields.filter then elseif fields.sortfield or fields.sortdirection or fields.filter then
mail.show_mail_menu(name, sortfieldindex, sortdirection, filter) mail.show_mail_menu(name, sortfieldindex, sortdirection, filter)
end end

View file

@ -23,15 +23,19 @@ function mail.show_inbox(name, sortfieldindex, sortdirection, filter)
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[] button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[] button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
dropdown[0,9.4;2,0.5;sortfield;]] .. dropdown[0,8.4;2,0.5;sortfield;]] ..
S("From") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. sortfieldindex .. [[;true] S("From") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. sortfieldindex .. [[;true]
dropdown[2.0,9.4;2,0.5;sortdirection;]] .. dropdown[2.0,8.4;2,0.5;sortdirection;]] ..
S("Ascending") .. "," .. S("Descending") .. [[;]] .. sortdirection .. [[;true] S("Ascending") .. "," .. S("Descending") .. [[;]] .. sortdirection .. [[;true]
field[4.25,9.85;1.4,0.5;filter;]] .. S("Filter") .. [[:;]] .. filter .. [[] field[4.25,8.85;1.4,0.5;filter;]] .. S("Filter") .. [[:;]] .. filter .. [[]
button[5.14,9.52;0.85,0.5;search;Q] button[5.14,8.52;0.85,0.5;search;Q]
checkbox[0,9.3;multipleselection;]] .. S("Allow multiple selection") .. [[;]] ..
tostring(mail.selected_idxs.multipleselection[name]) .. [[]
button[3.5,9.5;2.5,0.5;selectall;]] .. S("(Un)select all") .. [[]
tablecolumns[color;text;text] tablecolumns[color;text;text]
table[0,0.7;5.75,8.35;inbox;#999,]] .. S("From") .. "," .. S("Subject") table[0,0.7;5.75,7.35;inbox;#999,]] .. S("From") .. "," .. S("Subject")
local formspec = { inbox_formspec } local formspec = { inbox_formspec }
local entry = mail.get_storage_entry(name) local entry = mail.get_storage_entry(name)
local sortfield = ({"from","subject","time"})[sortfieldindex] local sortfield = ({"from","subject","time"})[sortfieldindex]
@ -41,7 +45,19 @@ function mail.show_inbox(name, sortfieldindex, sortdirection, filter)
if #messages > 0 then if #messages > 0 then
for _, message in ipairs(messages) do for _, message in ipairs(messages) do
if not message.read then local selected_id = 0
-- check if message is in selection list and return its id
if mail.selected_idxs.inbox[name] and #mail.selected_idxs.inbox[name] > 0 then
for i, selected_msg in ipairs(mail.selected_idxs.inbox[name]) do
if message.id == selected_msg then
selected_id = i
break
end
end
end
if selected_id > 0 then
formspec[#formspec + 1] = ",#466432"
elseif not message.read then
if not mail.player_in_list(name, message.to) then if not mail.player_in_list(name, message.to) then
formspec[#formspec + 1] = ",#FFD788" formspec[#formspec + 1] = ",#FFD788"
else else
@ -68,10 +84,6 @@ function mail.show_inbox(name, sortfieldindex, sortdirection, filter)
formspec[#formspec + 1] = S("(No subject)") formspec[#formspec + 1] = S("(No subject)")
end end
end end
if mail.selected_idxs.inbox[name] then
formspec[#formspec + 1] = ";"
formspec[#formspec + 1] = tostring(mail.selected_idxs.inbox[name] + 1)
end
formspec[#formspec + 1] = "]" formspec[#formspec + 1] = "]"
else else
formspec[#formspec + 1] = "]label[2.25,4.5;" .. S("No mail") .. "]" formspec[#formspec + 1] = "]label[2.25,4.5;" .. S("No mail") .. "]"

View file

@ -21,15 +21,19 @@ function mail.show_sent(name, sortfieldindex, sortdirection, filter)
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[] button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[] button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
dropdown[0,9.4;2,0.5;sortfield;]] dropdown[0,8.4;2,0.5;sortfield;]] ..
.. S("To") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. sortfieldindex .. [[;1] S("To") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. sortfieldindex .. [[;true]
dropdown[2.0,9.4;2,0.5;sortdirection;]] dropdown[2.0,8.4;2,0.5;sortdirection;]] ..
.. S("Ascending") .. "," .. S("Descending") .. [[;]] .. sortdirection .. [[;1] S("Ascending") .. "," .. S("Descending") .. [[;]] .. sortdirection .. [[;true]
field[4.25,9.85;1.4,0.5;filter;]].. S("Filter") .. [[:;]] .. filter .. [[] field[4.25,8.85;1.4,0.5;filter;]] .. S("Filter") .. [[:;]] .. filter .. [[]
button[5.14,9.52;0.85,0.5;search;Q] button[5.14,8.52;0.85,0.5;search;Q]
checkbox[0,9.3;multipleselection;]] .. S("Allow multiple selection") .. [[;]] ..
tostring(mail.selected_idxs.multipleselection[name]) .. [[]
button[3.5,9.5;2.5,0.5;selectall;]] .. S("(Un)select all") .. [[]
tablecolumns[color;text;text] tablecolumns[color;text;text]
table[0,0.7;5.75,8.35;sent;#999,]] .. S("To") .. "," .. S("Subject") table[0,0.7;5.75,7.35;sent;#999,]] .. S("To") .. "," .. S("Subject")
local formspec = { sent_formspec } local formspec = { sent_formspec }
local entry = mail.get_storage_entry(name) local entry = mail.get_storage_entry(name)
local sortfield = ({"to","subject","time"})[sortfieldindex] local sortfield = ({"to","subject","time"})[sortfieldindex]
@ -39,7 +43,21 @@ function mail.show_sent(name, sortfieldindex, sortdirection, filter)
if #messages > 0 then if #messages > 0 then
for _, message in ipairs(messages) do for _, message in ipairs(messages) do
formspec[#formspec + 1] = "," local selected_id = 0
-- check if message is in selection list and return its id
if mail.selected_idxs.sent[name] and #mail.selected_idxs.sent[name] > 0 then
for i, selected_msg in ipairs(mail.selected_idxs.sent[name]) do
if message.id == selected_msg then
selected_id = i
break
end
end
end
if selected_id > 0 then
formspec[#formspec + 1] = ",#466432"
else
formspec[#formspec + 1] = ","
end
formspec[#formspec + 1] = "," formspec[#formspec + 1] = ","
formspec[#formspec + 1] = minetest.formspec_escape(message.to) formspec[#formspec + 1] = minetest.formspec_escape(message.to)
formspec[#formspec + 1] = "," formspec[#formspec + 1] = ","
@ -54,10 +72,6 @@ function mail.show_sent(name, sortfieldindex, sortdirection, filter)
formspec[#formspec + 1] = S("(No subject)") formspec[#formspec + 1] = S("(No subject)")
end end
end end
if mail.selected_idxs.sent[name] then
formspec[#formspec + 1] = ";"
formspec[#formspec + 1] = tostring(mail.selected_idxs.sent[name] + 1)
end
formspec[#formspec + 1] = "]" formspec[#formspec + 1] = "]"
else else
formspec[#formspec + 1] = "]label[2.25,4.5;" .. S("No mail") .. "]" formspec[#formspec + 1] = "]label[2.25,4.5;" .. S("No mail") .. "]"