behavior.html
3.26 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Testing dojo.behavior</title>
<style type="text/css">
@import "../resources/dojo.css";
</style>
<script type="text/javascript" src="../dojo.js" data-dojo-config="isDebug: true, popup: true"></script>
<script type="text/javascript">
require(["dojo", "doh", "dojo/behavior", "dojo/domReady!"], function(dojo, doh){
var applyCount = 0;
var behaviorObj = {
".bar":function(elem){
dojo.style(elem, "opacity", 0.5);
applyCount++;
},
".foo > span":function(elem){
elem.style.fontStyle = "italic";
applyCount++;
}
};
topicCount = 0;
dojo.subscribe("/foo", function(){ topicCount++; });
// no behaviors should be executed when onload fires
doh.register("t",
[
function add(t){
t.f(dojo.behavior._behaviors[".bar"]);
t.f(dojo.behavior._behaviors[".foo > span"]);
dojo.behavior.add(behaviorObj);
// make sure they got plopped in
t.t(dojo.behavior._behaviors[".bar"]);
t.is(1, dojo.behavior._behaviors[".bar"].length);
t.t(dojo.behavior._behaviors[".foo > span"]);
t.is(1, dojo.behavior._behaviors[".foo > span"].length);
},
function apply(t){
t.is(0, applyCount);
dojo.behavior.apply();
t.is(2, applyCount);
// reapply and make sure we only match once
dojo.behavior.apply();
t.is(2, applyCount);
},
function reapply(t){
t.is(2, applyCount);
// add the rules again
dojo.behavior.add(behaviorObj);
dojo.behavior.apply();
t.is(4, applyCount);
// dojo.behavior.apply();
// t.is(4, applyCount);
// dojo.query(".bar").styles("opacity", 1.0);
},
function topics(t){
var d = new doh.Deferred();
t.is(0, topicCount);
dojo.behavior.add({ ".foo": "/foo" });
dojo.behavior.apply();
t.is(2, topicCount);
// We are going to catch focus events on "thinger", so first move focus
// somewhere else.
dojo.byId("another").focus();
// use timeout because blur/focus event generation isn't synchronous on IE
setTimeout(d.getTestErrback(function(){
// setup listener for focus events
dojo.behavior.add({ ".foo": {
"onfocus": "/foo"
}
});
dojo.behavior.apply();
t.is(2, topicCount);
// focus blah, publishing /foo
dojo.byId("blah").focus();
setTimeout(d.getTestErrback(function(){
t.is(3, topicCount);
// blur and then refocus blah, publishing /foo again
dojo.byId("another").focus();
dojo.byId("blah").focus();
setTimeout(d.getTestCallback(function(){
t.is(4, topicCount);
}), 10);
}), 10);
}), 10);
return d;
}
]
);
doh.runOnLoad();
});
</script>
</head>
<body>
<div class="foo" id="fooOne">
<span>.foo > span</span>
<div class="bar">
<span>.foo > .bar > span</span>
</div>
</div>
<!--for focus/topic tests -->
<input id="another" value="another">
<input type="text" id="blah" class="foo blah" name="thinger" value="thinger" tabIndex="0">
</body>
</html>