summaryrefslogtreecommitdiff
path: root/indra/newview/scripts/lua/require/UI/popup.lua
blob: 8ccf3b87f3cc3d8f0e5612588078f8eeb08bc8eb (plain)
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
74
75
76
77
78
79
80
81
82
local leap = require 'leap'
local mapargs = require 'mapargs'
local util = require 'util'

-- notification is any name defined in notifications.xml as
-- <notification name=>
-- vars is a table providing values for [VAR] substitution keys in the
-- notification body
-- payload prepopulates the response table
-- wait=false means fire and forget, returning nil
-- wait=true waits for user response:
-- * If the viewer returns a table containing exactly one key=true pair,
--   popup() returns just that key. If the key is a string containing an
--   underscore, e.g. 'OK_okcancelbuttons', it's truncated at the first
--   underscore, e.g. 'OK'.
-- * Otherwise the viewer's response is returned unchanged. To suppress the
--   above transformations, pass a non-empty payload table; this will cause
--   the viewer to return a table with at least two keys.
local popup = util.setmetamethods{
    -- this gets called when a consumer calls popup(notification, vars, payload)
    __call = function(self, ...)
        local args = mapargs('notification,vars,payload,wait', ...)
        -- we use convenience argument names different from 'LLNotifications'
        -- listener
        newargs = {op='requestAdd',
                   name=args.notification,
                   substitutions=args.vars,
                   payload=args.payload}
        -- Specifically test (wait == false), NOT (not wait), because we treat
        -- nil (omitted, default true) differently than false (explicitly
        -- DON'T wait).
        if args.wait == false then
            leap.send('LLNotifications', newargs)
        else
            local response = leap.request('LLNotifications', newargs).response
            -- response is typically a table. It might have multiple keys,
            -- e.g. if caller passed non-empty payload. In that case, just
            -- return the whole thing.
            if type(response) ~= 'table' then
                return response
            end
            -- get first key=value pair, if any
            local key, value = next(response)
            if (not key) or next(response, key) then
                -- key == nil means response is empty
                -- next(response, non-nil first key) ~= nil means at least two keys
                return response
            end
            -- Here response is a table containing exactly one key. The
            -- notifications system typically returns a table of the form
            -- {OK_okcancelbuttons=true}, which is tricky to test for because it
            -- varies with each set of buttons.
            if value == true then
                -- change {key=true} to plain key
                response = key
                if type(response) == 'string' then
                    -- change 'OK_okcancelbuttons' to plain 'OK'
                    response = string.split(response, '_')[1]
                end
            end
            return response
        end
    end
}

function popup:alert(message, payload)
    return self('GenericAlert', {MESSAGE=message, payload=payload})
end

function popup:alertOK(message, payload)
    return self('GenericAlertOK', {MESSAGE=message, payload=payload})
end

function popup:alertYesCancel(message, payload)
    return self('GenericAlertYesCancel', {MESSAGE=message, payload=payload})
end

function popup:tip(message, payload)
    self{'SystemMessageTip', {MESSAGE=message, payload=payload}, wait=false}
end

return popup