-
Notifications
You must be signed in to change notification settings - Fork 10
/
SCxmlProcessors.js
76 lines (72 loc) · 2.23 KB
/
SCxmlProcessors.js
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
SCxml.EventProcessors={
SCXML:{
name:"http://www.w3.org/TR/scxml/#SCXMLEventProcessor",
createEvent: function(name, sc, data)
{
return new SCxml.ExternalEvent(name, "#_scxml_"+sc.sid,
SCxml.EventProcessors.SCXML.name, undefined, data)
},
send: function(event, target, element, sc)
{
if(sc.sendNoMore) return;
target=target||"#_scxml_"+sc.sid
var sid
if((sid=target.match(/^#_scxml_(.+)$/)) && (sid=sid[1]))
{
if(sid in SCxml.sessions && SCxml.sessions[sid])
SCxml.sessions[sid].onEvent(event)
else sc.error("communication",element,
new Error('target session "'+target+'" does not exist'))
}
else if(target.match(/^#_parent$/))
{
if(sc.parent){
event.invokeid=sc.iid
sc.parent.onEvent(event)
}
else sc.error("communication",element,
new Error('this session has no #_parent'))
}
else if((sid=target.match(/^#_(.+)$/)) && (sid=sid[1]))
{
if(sid in sc.invoked)
sc.invoked[sid].onEvent(event)
else sc.error("communication",element,
new Error('invoked target "'+target+'" does not exist'))
}
else {
sc.error("execution",element,
new Error('unsupported target "'+target+'" for SCXML events'))
} // TODO: remote targets
}
},
DOM:{
name:"http://www.w3.org/TR/scxml/#DOMEventProcessor",
createEvent: function(name, sc, data)
{
// this is the ugly DOM 3 version to improve compatibility
var e=document.createEvent("CustomEvent")
e.initCustomEvent(name, true, false, data)
return e
},
send: function(event, target, element, sc)
{
if(sc.sendNoMore) return;
var obj
// heuristics to determine target syntax (XPath or CSS)
if(!target) obj=sc.html
if(!obj) try{
if(target[0]=="/") obj=document.evaluate(
target, sc.html, null, 9, null).singleNodeValue
if(target[0]=="#") obj=document.querySelector(target)
}catch(err){ sc.error("execution.DOM",element,err) }
if(!obj) try{ obj=document.evaluate(
target, sc.html, null, 9, null).singleNodeValue
}catch(err){}
if(!obj) try{ obj=document.querySelector(target) }catch(err){}
if(!obj) sc.error("execution.DOM",element,
new Error('Failed to evaluate "'+target+'" to an existing element'))
obj.dispatchEvent(event)
}
}
}