-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfirmDialog.lua
73 lines (68 loc) · 2.13 KB
/
ConfirmDialog.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- namespace
local _, ns = ...;
local CONFIRM_DIALOG = "ConfirmDialog"
---
-- The static ConfirmDialog is a pop-up window that can be opened to prompt the
-- user with a message dialog that can contain a question and has two buttons
-- [yes, no] to respond to.
--
-- The dialog will be opened using ConfirmDialog.open(text, action), where the
-- text will be displayed in the dialog and the action represents a function that
-- will be called wit the user choice [true - on accept, false - on cancel].
--
local ConfirmDialog = {
dialog = {
text = "%s",
button1 = "Yes",
button2 = "No",
OnAccept = function(self, action) return action(true) end,
OnCancel = function(self, action) return action(false) end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
showAlert = true,
exclusive = true,
preferredIndex = 3, -- avoid some UI taint
cancels = CONFIRM_DIALOG
}
}
ConfirmDialog.__index = ConfirmDialog
ns.ConfirmDialog = ConfirmDialog
-- register the dialog in the static global array
StaticPopupDialogs[CONFIRM_DIALOG] = ConfirmDialog.dialog
---
-- Opens the ConfirmDialog with the given message text and the given function
-- that will be invoked with the user choice [true, false].
--
-- @param #string text
-- the message text of the dialog
-- @param #function action
-- the function that will be invoked with the user choice
--
ConfirmDialog.open = function(text, action)
local frame = StaticPopup_Show(CONFIRM_DIALOG, text)
if (frame) then
ConfirmDialog.dialog.frame = frame
frame.data = action
end
end
---
-- Closes the ConfirmDialog.
--
ConfirmDialog.close = function()
StaticPopup_Hide(CONFIRM_DIALOG)
end
---
-- Indicates whether the ConfirmDialog is currently shown.
--
-- @return #boolean
-- true if the dialog is currently shown, false otherwise
--
ConfirmDialog.isShown = function()
local frame = ConfirmDialog.dialog.frame
if (frame) then
return frame:IsShown()
else
return false
end
end