Debian Codegraph @

dijit/tests/_TemplatedMixin.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
5 <title>_TemplatedMixin tests</title>
6 <script type="text/javascript" src="boilerplate.js"></script>
7 <script type="text/javascript">
8 require([
9 "doh/runner",
10 "dojo/_base/array", "dojo/_base/declare", "dojo/dom", "dojo/query",
11 "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/layout/LayoutContainer",
12 "dojo/domReady!"
13 ], function(doh, array, declare, dom, query, _WidgetBase, _TemplatedMixin){
14
15 function getOuterHTML(/*DomNode*/ node){
16 var wrapper = document.createElement("div");
17 wrapper.appendChild(node);
18 return wrapper.innerHTML.toLowerCase(); // IE prints <BUTTON> rather than <button>; normalize it.
19 }
20
21 // Template with no variables (should be cached as a DOM tree)
22 declare("SimpleTemplate", [_WidgetBase, _TemplatedMixin], {
23 id: "",
24 _setIdAttr: null, // override _WidgetBase to not copy id to domNode
25
26 templateString: "<button type='button'><span>hello &lt; world</span></button>"
27 });
28
29 // Template with variables
30 declare("VariableTemplate", [_WidgetBase, _TemplatedMixin], {
31 id: "test2",
32 _setIdAttr: null, // override _WidgetBase to not copy id to domNode
33
34 num: 5,
35 bool: false,
36 text: "hello <i>&quot;'\" world",
37
38 templateString: "<button><span num=\"${num}\" value=\"${bool}\">${text}</span></button>"
39 });
40
41 // Template with ! variables (for literal substitution)
42 declare("ExclamationVariableTemplate", [_WidgetBase, _TemplatedMixin], {
43 markup: "<span>hello world</span>",
44
45 templateString: "<div>${!markup}</div>"
46 });
47
48 // Template that starts with special node (has to be constructed inside a <tbody>)
49 declare("TableRowTemplate", [_WidgetBase, _TemplatedMixin], {
50 id: "test3",
51 _setIdAttr: null, // override _WidgetBase to not copy id to domNode
52 text: "bar",
53
54 templateString: "<tr><td>${text}</td></tr>"
55 });
56
57 // Illegal substitution variable name
58 declare("IllegalSubstitution", [_WidgetBase, _TemplatedMixin], {
59 templateString: "<tr><td>${fake}</td></tr>"
60 });
61
62 // data-dojo-attach-point
63 declare("AttachPoint", [_WidgetBase, _TemplatedMixin], {
64 templateString: "<div style='border: 1px solid red'>" +
65 "<button data-dojo-attach-point='buttonNode,focusNode'>hi</button>" +
66 '<span><input data-dojo-attach-point="inputNode" value="input"/></span>' +
67 "<span data-dojo-attach-point='containerNode'></span>" +
68 "</div>"
69 });
70
71 // data-dojo-attach-event
72 declare("AttachEvent", [_WidgetBase, _TemplatedMixin], {
73 click: function(){
74 this.clickCalled = true;
75 },
76 onfocus: function(){
77 this.focusCalled = true;
78 },
79 focus2: function(){
80 this.focus2Called = true;
81 },
82 templateString: "<table style='border: 1px solid blue'><tr>" +
83 "<td><button type='button' data-dojo-attach-point='left' data-dojo-attach-event='onclick: click, onfocus'>left</button></td>" +
84 "<td><button type='button' data-dojo-attach-point='right' data-dojo-attach-event='onclick: click, onfocus: focus2'>right</button></td>" +
85 "</tr></table>"
86 });
87
88 var testW;
89
90 doh.register("_TemplatedMixin", [
91 function simple(t){
92 var widget = new SimpleTemplate({id: "test1"});
93 var wrapper = dom.byId("simpleWrapper");
94 wrapper.appendChild(widget.domNode);
95
96 // Different browsers have different orders for type=button and widgetid=... so simplify
97 // by just removing the type=button.
98 t.is('<button widgetid=\"test1\"><span>hello &lt; world</span></button>', wrapper.innerHTML.toLowerCase().replace(/ type="?button"?/, ""));
99 },
100
101 function variables(t){
102 var widget = new VariableTemplate();
103 var span = widget.domNode.getElementsByTagName("span")[0];
104 var text = (span.innerText || span.textContent).replace("&#x27", "'");
105 t.is("5", span.getAttribute("num"), "num attr");
106 t.is("false", span.getAttribute("value"), "value attr");
107 t.is("hello <i>&quot;'\" world", text, "span textContent");
108 },
109 function variables2(t){
110 var widget = new VariableTemplate({id: "myid", num: -5, bool: true, text: ""});
111 var span = widget.domNode.getElementsByTagName("span")[0];
112 var text = span.innerHTML;
113 t.is("-5", span.getAttribute("num"));
114 t.is("true", span.getAttribute("value"));
115 t.is("", text);
116 },
117 function variablesWithExclamation(t){
118 var widget = new ExclamationVariableTemplate();
119
120 // ExclamationVariableTemplate should create markup like
121 // <div><span>hello world</span></div>
122 // The <span> comes from the ${!markup} variable.
123 var span = query("> *", widget.domNode);
124 t.is(1, span.length, "dom node has one child");
125 t.is("SPAN", span[0].nodeName.toUpperCase(), "which is a span");
126 t.is("hello world", span[0].innerHTML, "and the text is set correctly too");
127 },
128
129 function table(t){
130 var widget = new TableRowTemplate({text: "hello"});
131 var wrapper = dom.byId("trWrapper");
132 wrapper.appendChild(widget.domNode);
133 var actual = wrapper.innerHTML.toLowerCase().replace(/\r/g, "").replace(/\n/g, "");
134 t.is('<tr widgetid="test3"><td>hello</td></tr>', actual);
135 },
136 function illegal(t){
137 var hadException = false;
138 try{
139 var widget = new IllegalSubstitution();
140 }catch(e){
141 console.log(e);
142 hadException = true;
143 }
144 t.t(hadException);
145 },
146 function attachPoint(t){
147 var widget = new AttachPoint();
148 var wrapper = dom.byId("attachPointWrapper");
149 wrapper.appendChild(widget.domNode);
150 t.is(widget.containerNode.tagName.toLowerCase(), "span");
151 t.is(widget.buttonNode.tagName.toLowerCase(), "button");
152 t.is(widget.focusNode.tagName.toLowerCase(), "button");
153 t.is(widget.inputNode.tagName.toLowerCase(), "input");
154 },
155 function attachEvent(t){
156 var deferred = new doh.Deferred();
157 var widget = new AttachEvent();
158 var wrapper = dom.byId("attachEventWrapper");
159 wrapper.appendChild(widget.domNode);
160 widget.left.focus();
161 widget.right.focus();
162 setTimeout(deferred.getTestCallback(function(){
163 t.t(widget.focusCalled, "left focused");
164 t.t(widget.focus2Called, "right focused");
165 }), 50);
166 return deferred;
167 },
168
169 // Test IE problem having "length" as data-dojo-attach-point name
170 function length(){
171 var MyWidget = declare([_WidgetBase, _TemplatedMixin], {
172 name: "",
173 templateString: "<div>" +
174 "<input data-dojo-attach-point='focusNode' name='${name}'>" +
175 "<span data-dojo-attach-point='spanNode'>" +
176 "</div>"
177 });
178 var widget = new MyWidget({name: "length"});
179 doh.isNot(undefined, widget.focusNode, "focusNode");
180 doh.isNot(undefined, widget.spanNode, "spanNode");
181 }
182 ]);
183
184 // This is more of a test of _WidgetBase, but putting here for convenience
185 // since it tests the case when srcNodeRef is swapped for a widget's template
186 doh.register("srcNodeRef", [
187 function replaceMe(){
188 var prev = dom.byId("replaceMe").previousSibling;
189
190 // This should swap out the <span id="replaceMe"> w/the widget
191 var widget = new SimpleTemplate({}, "replaceMe");
192
193 // Make sure the swap occurred
194 doh.is(widget.domNode, prev.nextSibling, "swapped");
195 doh.is(null, dom.byId("replaceMe"), "original node removed");
196
197 // For garbage collection widget.srcNodeRef should also be cleared
198 doh.is(null, widget.srcNodeRef, "srcNodeRef cleared");
199 }
200 ]);
201
202 doh.register("_rendered", [
203 function preRendered(){
204 // Testing for when a widget's template is expanded on the server, so _rendered:true is passed
205 // as a parameter to the widget constructor.
206 var MyWidget = declare([_WidgetBase, _TemplatedMixin], {
207 declaredClass: "MyWidget",
208 templateString: "<input>" // shouldn't be used, just listing a template for testing purposes
209 });
210 var widget = new MyWidget({
211 _rendered: true // flag to not do template insertion
212 }, dom.byId("prerendered"));
213 doh.t(widget.heading, "heading attach point setup");
214 doh.is("h2", widget.heading.tagName.toLowerCase());
215 }
216 ]);
217
218 doh.run();
219 });
220 </script>
221 </head>
222 <body>
223 <h1>_TemplatedMixin test</h1>
224
225 <div id="simpleWrapper"></div>
226 <table>
227 <tbody id="trWrapper"></tbody>
228 </table>
229 <div id="attachPointWrapper"></div>
230 <div id="attachEventWrapper"></div>
231 <span id="replaceMe"></span>
232
233 <!-- test for _rendered=true flag -->
234 <div id="prerendered">
235 <h2 data-dojo-attach-point="heading">hi</h2>
236 </div>
237 </body>
238 </html>