Debian Codegraph @

dijit/tests/_Widget-attr.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5 <title>widget attribute unit test (in constructor and get()/set())</title>
6
7 <script src="boilerplate.js"></script>
8
9 <script type="text/javascript">
10 require([
11 "doh/runner", "dojo/_base/declare",
12 "dojo/dom", "dojo/dom-attr", "dojo/dom-class", "dojo/dom-style", "dojo/sniff",
13 "dijit/_WidgetBase", "dijit/_Widget",
14 "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin",
15 "dojo/domReady!"
16 ], function(doh, declare, dom, domAttr, domClass, domStyle, has,
17 _WidgetBase, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin){
18
19 doh.register("attributes", [
20 // Test attributes mapped to DOMNodes
21 function domMapping(){
22 var IndividualMaps = declare([_WidgetBase, _TemplatedMixin], {
23 // Mapping foo to this.domNode.foo
24 foo:"",
25 _setFooAttr: "",
26
27 // Mapping bar to this.buttonNode.bar
28 bar: "",
29 _setBarAttr: "buttonNode",
30
31 // Mapping plainText to this.plainTextNode.innerHTML
32 plainText: "",
33 _setPlainTextAttr: {node: "plainTextNode", type: "innerText"},
34
35 teeny: false,
36 _setTeenyAttr: {node: "teenyNode", type: "toggleClass"},
37
38 templateString: "<div class='class1' style='border: 1px solid red; width: 456px'>" +
39 "<button data-dojo-attach-point='buttonNode,focusNode'>hi</button>" +
40 '<span><input data-dojo-attach-point="inputNode" value="input"></span>' +
41 "<span data-dojo-attach-point='containerNode'></span>" +
42 "<span data-dojo-attach-point='plainTextNode'>original plain text</span>" +
43 "<span data-dojo-attach-point='teenyNode'>teeny text</span>" +
44 "</div>"
45 });
46
47 var widget = new IndividualMaps({
48 foo:"value1",
49 bar:"value2",
50 "class":"class2",
51 style:"height: 123px",
52 plainText: "hello world <>&;",
53 teeny: true,
54 "name-with-dashes": "name with dashes"
55 }).placeAt(dom.byId("wrapper"));
56
57 // test attributes specified to constructor were copied over to DOM
58 doh.is("value1", widget.domNode.getAttribute("foo"), "widget.domNode.getAttribute('foo')");
59 doh.is("value2", widget.buttonNode.getAttribute("bar"), "widget.domNode.getAttribute('bar')");
60 doh.t(domClass.contains(widget.domNode, "class1"), "class1");
61 doh.t(domClass.contains(widget.domNode, "class2"), "class2");
62 doh.is("123px", widget.domNode.style.height, "height");
63 doh.is("456px", widget.domNode.style.width, "width");
64 doh.is("hello world &lt;&gt;&amp;;", widget.plainTextNode.innerHTML, "innerHTML");
65 doh.t(domClass.contains(widget.teenyNode, "teeny"), "teeny");
66 },
67
68 // Test attributes mapped to subwidgets
69 function subwidgetMapping(){
70 declare("MySubWidget", [_WidgetBase], {
71 _setFooAttr: function(val){
72 this.foo = val;
73 this.gotValue = true; // set flag for testing purposes
74 }
75 });
76 var IndividualMaps = declare(
77 [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
78
79 // Mapping foo to this.subwidget.foo
80 foo:"",
81 _setFooAttr: "subwidget",
82
83 templateString:
84 "<div class='class1' style='border: 1px solid red; width: 456px'>" +
85 "<button data-dojo-type='MySubWidget' data-dojo-attach-point='subwidget'>hi</button>" +
86 "</div>"
87 });
88
89 var widget = new IndividualMaps({
90 foo: "value1"
91 }).placeAt(dom.byId("wrapper"));
92
93 // test attribute specified to constructor was copied over to subwidget
94 doh.is("value1", widget.subwidget.get("foo"), "widget.subwidget.get('foo')");
95 doh.t(widget.subwidget.gotValue, "gotValue");
96 },
97
98 // Test that certain attributes are automatically applied to focusNode or domNode.
99 // These are attributes that aren't mentioned at all in _WidgetBase.
100 function autoDomMapping(){
101 // Mapping to this.domNode
102 var w = new _WidgetBase({
103 title: "dom title",
104 "aria-labelledby": "foo",
105 role: "button"
106 });
107 doh.is("dom title", domAttr.get(w.domNode, "title"), "domNode title");
108 doh.is("foo", w.domNode.getAttribute("aria-labelledby", "domNode labelledby"));
109 doh.is("button", domAttr.get(w.domNode, "role"), "domNode role");
110
111 // Mapping to this.focusNode
112 var fw = new (declare([_WidgetBase, _TemplatedMixin], {
113 templateString: "<div><input data-dojo-attach-point='focusNode'/></div>"
114 }))({
115 title: "my title",
116 "aria-labelledby": "foo"
117 });
118 doh.is("my title", domAttr.get(fw.focusNode, "title"), "focusNode title");
119 doh.is("foo", fw.focusNode.getAttribute("aria-labelledby"), "focusNode labelledby");
120
121 // Mapping attributes with dashes and mixed case
122 var mc = new (declare([_WidgetBase, _TemplatedMixin], {
123 templateString: "<form></form>"
124 }))({
125 "accept-charset": "utf8",
126 "novalidate": "true" // parser delivers as lowercase even though it's noValidate in JS obj
127 });
128 doh.is("utf8", mc.domNode.getAttribute("accept-charset"), "accept-charset");
129 if(has("ff") >= 4 || has("ie") >= 10 || has("webkit")){
130 // only works for HTML5 compliant browsers where novalidate is understood
131 // (also, hasAttribute() is not available on IE6/7)
132 doh.t(mc.domNode.hasAttribute("novalidate"), "noValidate");
133 }
134 },
135
136 // Test that creating a widget with type defined doesn't causes exceptions on IE6-8.
137 // This is really trying to test that markup like <input dojoType=WidgetBase type=text> works.
138 // (Todo: remove for 2.0)
139 function typeException(){
140 var node = document.createElement("input");
141 document.body.appendChild(node);
142 new _WidgetBase({
143 type: "text"
144 }, node);
145 },
146
147 // Test custom setters/getter methods
148 function customSetters(){
149 var CustomSetters = declare([_WidgetBase], {
150 foo: 0,
151 _setFooAttr: function(val){ this._set("foo", val + 5); },
152 _getFooAttr: function(val){ return this._get("foo") - 10; }
153 });
154
155 var widget = new CustomSetters({
156 foo: 100
157 });
158
159 doh.is(105, widget._get("foo"), "custom setter called at initialize time");
160 doh.is(95, widget.get("foo"), "custom getter called");
161
162 widget.set("foo", 50);
163 doh.is(55, widget._get("foo"), "custom setter called dynamically");
164 doh.is(45, widget.get("foo"), "custom getter still called");
165 },
166
167 // Test setters for attribute names with dashes
168 function settersForNamesWithDashes(){
169 var MyWidget = declare([_WidgetBase, _TemplatedMixin], {
170 "name-with-dashes": "",
171 _setNameWithDashesAttr: {node: "dashNode", type: "innerHTML"},
172
173 templateString: "<div>" +
174 "<span data-dojo-attach-point='dashNode'></span>" +
175 "</div>"
176 });
177
178 var widget = new MyWidget({
179 "name-with-dashes": "name with dashes"
180 }).placeAt(dom.byId("wrapper"));
181
182 doh.is("name with dashes", widget.get("name-with-dashes"), "get()");
183 doh.is("name with dashes", widget.dashNode.innerHTML, "innerHTML");
184
185 widget.set("name-with-dashes", "hello");
186 doh.is("hello", widget.dashNode.innerHTML);
187 },
188
189 // Test deprecated attr() method (remove for 2.0)
190 function attr(){
191 var MyWidget = new declare([_Widget, _TemplatedMixin], {
192 templateString: "<div><span data-dojo-attach-point=nameNode></span></div>",
193 _setNameAttr: {node: "nameNode", type: "innerHTML"}
194 });
195
196 var b = new MyWidget();
197
198 // simple setting
199 b.attr("name", "thinger");
200 doh.is("thinger", b.attr("name"), "b.attr('name')");
201 doh.is("thinger", b.nameNode.innerHTML, "innerHTML");
202
203 // hash setting
204 b.attr({
205 name: "bang",
206 foo: "zap"
207 });
208 doh.is("bang", b.attr("name"), "hash set of bang");
209 doh.is("zap", b.attr("foo"), "hash set of zap");
210 },
211
212 // Test deprecated attributeMap (remove for 2.0)
213 function attributeMap(){
214 var AttrMap = declare([_WidgetBase, _TemplatedMixin], {
215 attributeMap: {foo: "", bar: "buttonNode", plainText: {node: "plainTextNode", type: "innerText"}, teeny: {node: "teenyNode", type: "toggleClass"}},
216 templateString: "<div class='class1' style='border: 1px solid red; width: 456px'>" +
217 "<button data-dojo-attach-point='buttonNode,focusNode'>hi</button>" +
218 '<span><input data-dojo-attach-point="inputNode" value="input"></span>' +
219 "<span data-dojo-attach-point='containerNode'></span>" +
220 "<span data-dojo-attach-point='plainTextNode'>original plain text</span>" +
221 "<span data-dojo-attach-point='teenyNode'>teeny text</span>" +
222 "</div>"
223 });
224
225 var widget = new AttrMap({
226 foo:"value1",
227 bar:"value2",
228 "class":"class2",
229 style:"height: 123px",
230 plainText: "hello world <>&;",
231 teeny: true
232 }).placeAt(dom.byId("wrapper"));
233
234 // test that attributes specified to constructor were copied over to the DOM
235 doh.is("value1", widget.domNode.getAttribute("foo"), "widget.domNode.getAttribute('foo')");
236 doh.is("value2", widget.buttonNode.getAttribute("bar"), "widget.domNode.getAttribute('bar')");
237 doh.t(domClass.contains(widget.domNode, "class1"), "class1");
238 doh.t(domClass.contains(widget.domNode, "class2"), "class2");
239 doh.is("123px", widget.domNode.style.height, "height");
240 doh.is("456px", widget.domNode.style.width, "width");
241 doh.is("hello world &lt;&gt;&amp;;", widget.plainTextNode.innerHTML, "innerHTML");
242 doh.t(domClass.contains(widget.teenyNode, "teeny"), "teeny");
243 },
244
245 // Test that attributes set in the ctor when side effects setter exist
246 // are correctly applied
247 function ctorDependentAttributes(){
248 var TestWidget = declare(_WidgetBase, {
249 single: null,
250 multiple: [],
251 _setSingleAttr: function(value){
252 this._set("multiple", value != null ? [value] : null);
253 this._set("single", value);
254 },
255 _setMultipleAttr: function(value){
256 this._set("single", value ? (value.length > 0 ? value[0] : null) : null);
257 this._set("multiple", value);
258 }
259 });
260
261 var w1 = new TestWidget({
262 single : 5
263 });
264 var w2 = new TestWidget({
265 multiple: [5]
266 });
267 doh.is(5, w1.get("single"), "w1.single");
268 doh.is([5], w1.get("multiple"), "w1.multiple");
269 doh.is(5, w2.get("single"), "w2.single");
270 doh.is([5], w2.get("multiple"), "w2.multiple");
271 },
272
273 function moreCorrelatedProperties(){
274 var Widget = declare([_WidgetBase], {
275 foo: 10,
276 _setFooAttr: function(val){
277 this._set("foo", val);
278 this._set("bar", val + 1);
279 },
280
281 bar: 11,
282 _setBarAttr: function(val){
283 this._set("bar", val);
284 this._set("foo", val - 1);
285 }
286 });
287
288 var w1 = new Widget({foo: 30});
289 doh.is(30, w1.get("foo"), "w1.foo");
290 doh.is(31, w1.get("bar"), "w1.bar");
291
292 var w2 = new Widget({bar: 30});
293 doh.is(30, w2.get("bar"), "w2.bar");
294 doh.is(29, w2.get("foo"), "w2.foo");
295
296 var w3 = new Widget({});
297 doh.is(10, w3.get("foo"), "w3.foo");
298 doh.is(11, w3.get("bar"), "w3.bar");
299 },
300
301 function widgetWatch() {
302 var widget = new _WidgetBase({}),
303 expected = [ 'a', NaN ],
304 actual = [];
305
306 widget.watch('foo', function (key, oldValue, value) {
307 actual.push(value);
308 });
309
310 widget.set('foo', 'a');
311 widget.set('foo', 'a');
312 widget.set('foo', NaN);
313 widget.set('foo', NaN);
314
315 doh.is(expected, actual);
316
317 widget.destroyRecursive();
318 }
319 ]); // doh.register()
320
321 doh.run();
322
323 }); // require()
324
325 </script>
326 </head>
327 <body>
328 <h1>Dijit widget.get()/set() Unit Test</h1>
329 <div id="wrapper"></div>
330 </body>
331 </html>