This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 520
/
interact.coffee
61 lines (54 loc) · 1.97 KB
/
interact.coffee
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
class Interaction
constructor: (browser)->
# Collects all prompts (alert, confirm, prompt).
prompts = []
alertFns = []
# When alert displayed to user, call this function.
this.onalert = (fn)-> alertFns.push fn
confirmFns = []
confirmCanned = {}
# When prompted with a question, return the response. First argument
# may be a function.
this.onconfirm = (question, response)->
if typeof question == "function"
confirmFns.push question
else
confirmCanned[question] = !!response
promptFns = []
promptCanned = {}
# When prompted with message, return response or null if response is
# false. First argument may be a function.
this.onprompt = (message, response)->
if typeof message == "function"
promptFns.push message
else
promptCanned[message] = response
this.prompted = (message)-> prompts.indexOf(message) >= 0
this.extend = (window)->
# Implements window.alert: show message.
window.alert = (message)->
prompts.push message
fn message for fn in alertFns
return
# Implements window.confirm: show question and return true/false.
window.confirm = (question)->
prompts.push question
response = confirmCanned[question]
unless response || response == false
for fn in confirmFns
response = fn(question)
break if response || response == false
return !!response
# Implements window.prompt: show message and return value of null.
window.prompt = (message, def)->
prompts.push message
response = promptCanned[message]
unless response || response == false
for fn in promptFns
response = fn(message, def)
break if response || response == false
return response.toString() if response
return null if response == false
return def || ""
exports.use = (browser)->
return new Interaction(browser)