source: Dev/trunk/src/client/dijit/tests/_Templated-widgetsInTemplate1.x.html @ 483

Last change on this file since 483 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 16.7 KB
Line 
1<html>
2        <head>
3                <title>testing widgetsInTemplate support</title>
4                <style type="text/css">
5                        @import "../themes/claro/document.css";
6                        @import "../themes/claro/claro.css";
7                </style>
8                <script type="text/javascript" src="../../dojo/dojo.js"
9                        djConfig="isDebug: true"></script>
10                <script type="text/javascript">
11                        dojo.require("doh.runner");
12
13                        dojo.require("dojo.parser");
14
15                        dojo.require("dijit._Widget");
16                        dojo.require("dijit._Templated");
17                        dojo.require("dijit.form.Button");
18                        dojo.require("dijit.form.CheckBox");
19                        dojo.require("dijit.ProgressBar");
20                        dojo.require("dijit.layout.ContentPane");
21                        dojo.require("dijit.layout.TabContainer");
22                        dojo.require("dijit._Container");
23                        dojo.require("dijit._Contained");
24                        dojo.require("dijit.layout._LayoutWidget");
25
26                        dojo.ready(function(){
27                                var testW;
28
29                                dojo.declare('Test1Widget',
30                                        [dijit._Widget, dijit._Templated],
31                                {
32                                        widgetsInTemplate: true,
33       
34                                        templateString: dojo.byId('Test1Template').textContent || dojo.byId('Test1Template').innerText,
35                                        onClick: function(e){
36                                                if(e.target){
37                                                        alert('onClick widgetId='+e.target.id);
38                                                }else{
39                                                        if(e._counter == undefined){
40                                                                e._counter = 1;
41                                                        }else{
42                                                                e._counter++;
43                                                        }
44                                                }
45                                        }
46                                });
47
48                                dojo.declare('Test2Widget',
49                                        [dijit._Widget, dijit._Templated],
50                                {
51                                        widgetsInTemplate: true,
52       
53                                        templateString: dojo.byId('Test2Template').textContent || dojo.byId('Test2Template').innerText
54                                });
55
56                                dojo.declare('Test4Widget',
57                                        [dijit._Widget, dijit._Templated],
58                                {
59                                        widgetsInTemplate: true,
60               
61                                        templateString: dojo.byId('Test4Template').textContent || dojo.byId('Test4Template').innerText
62                                });
63               
64                                function validateTest4Widget(t, testW){
65                                        var selectedTab = dojo.query(".dijitTabChecked", testW.domNode)[0];
66                                        var selectedPane = dojo.query(".dijitVisible > .dijitTabPane", testW.domNode)[0];
67                                        var tabBox = selectedTab ? dojo.contentBox(selectedTab) : null;
68                                        var paneBox = selectedPane ? dojo.contentBox(selectedPane) : null;
69                                        t.t(tabBox && tabBox.w > 0 && tabBox.h > 0, "tabBox && tabBox.w > 0 && tabBox.h > 0");
70                                        t.t(paneBox && paneBox.w > 0 && paneBox.h > 0, "paneBox && paneBox.w > 0 && paneBox.h");
71                                        // Check that everything got started
72                                        t.t(testW._started, "testW._started");
73                                        t.t(testW.tabCont._started, "tabCont._started");
74                                        t.t(testW.tab1._started, "tab1._started");
75                                        t.t(testW.tab2._started, "tab2._started");
76                                }
77
78                                dojo.declare('TestLayoutWidget', dijit.layout._LayoutWidget, {
79                                        startup: function(){
80                                                if(this._started){
81                                                        this._doubleStarted = true;
82                                                }
83                                                this.inherited(arguments);
84                                        },
85                                        destroy: function(){
86                                                if(this._destroyed){
87                                                        this._doubleDestroyed = true;
88                                                }
89                                                this.inherited(arguments);
90                                                this._destroyed = true;
91                                        }
92                                });
93                                dojo.declare('TestCtnrWidget', [dijit._Widget, dijit._Container], {
94                                        startup: function(){
95                                                if(this._started){
96                                                        this._doubleStarted = true;
97                                                }
98                                                this.inherited(arguments);
99                                        },
100                                        destroy: function(){
101                                                if(this._destroyed){
102                                                        this._doubleDestroyed = true;
103                                                }
104                                                this.inherited(arguments);
105                                                this._destroyed = true;
106                                        }
107                                });
108                                dojo.declare('TestCtndWidget', [dijit._Widget, dijit._Contained], {
109                                        startup: function(){
110                                                if(this._started){
111                                                        this._doubleStarted = true;
112                                                }
113                                                this.inherited(arguments);
114                                        },
115                                        destroy: function(){
116                                                if(this._destroyed){
117                                                        this._doubleDestroyed = true;
118                                                }
119                                                this.inherited(arguments);
120                                                this._destroyed = true;
121                                        }
122                                });
123                                dojo.declare('TestNonCtnrWidget', [dijit._Widget, dijit._Templated], {
124                                        templateString: "<div dojoAttachPoint=containerNode></div>",
125                                        startup: function(){
126                                                if(this._started){
127                                                        this._doubleStarted = true;
128                                                }
129                                                this.inherited(arguments);
130                                        },
131                                        destroy: function(){
132                                                if(this._destroyed){
133                                                        this._doubleDestroyed = true;
134                                                }
135                                                this.inherited(arguments);
136                                                this._destroyed = true;
137                                        }
138                                });
139                                dojo.declare('TestStubWidget', dijit._Widget, {
140                                        startup: function(){
141                                                if(this._started){
142                                                        this._doubleStarted = true;
143                                                }
144                                                this.inherited(arguments);
145                                        },
146                                        destroy: function(){
147                                                if(this._destroyed){
148                                                        this._doubleDestroyed = true;
149                                                }
150                                                this.inherited(arguments);
151                                                this._destroyed = true;
152                                        }
153                                });
154               
155                                dojo.declare('Test5Widget',
156                                        [dijit._Widget, dijit._Templated],
157                                {
158                                        widgetsInTemplate: true,
159               
160                                        templateString: dojo.byId('Test5Template').textContent || dojo.byId('Test5Template').innerText,
161                                        startup: function(){
162                                                if(this._started){
163                                                        this._doubleStarted = true;
164                                                }
165                                                this.inherited(arguments);
166                                        },
167                                        destroy: function(){
168                                                if(this._destroyed){
169                                                        this._doubleDestroyed = true;
170                                                }
171                                                this.inherited(arguments);
172                                                this._destroyed = true;
173                                        }
174                                });
175               
176                                function getTestWidgets(testW){
177                                        return [
178                                                testW,
179                                                testW.layout,
180                                                testW.layChild1,
181                                                testW.layChild2,
182                                                testW.container,
183                                                testW.contained1,
184                                                testW.contained2,
185                                                testW.nonContainer,
186                                                testW.nonContained1,
187                                                testW.nonContained2,
188                                                testW.threeLevel,
189                                                testW.secondLevel,
190                                                testW.bottomLevel,
191                                                testW.anotherThree,
192                                                testW.anotherSecond,
193                                                testW.anotherBottom,
194                                                testW.stub1
195                                        ];
196                                }
197               
198                                function validateTest5Widget(t, testW){
199                                        // Check that everything got started, but not double-started
200                                        dojo.forEach(getTestWidgets(testW), function(w){
201                                                t.t(w._started, "w._started: " + w);
202                                                t.is(undefined, w._doubleStarted, "w._doubleStarted: " + w);
203                                        });
204                                }
205               
206                                function validateTest5WidgetDestroy(t, testW){
207                                        var savedWidgets = getTestWidgets(testW);
208                                        testW.destroy();
209                                        dojo.forEach(savedWidgets, function(w, idx){
210                                                t.t(w._destroyed, "w._destroyed: " + w);
211                                                t.is(undefined, w._doubleDestroyed, "w._doubleDestroyed: " + w);
212                                        });
213                                }
214
215                                dojo.declare("Missing", [dijit._Widget, dijit._Templated], {
216                                        templateString: '<div>' +
217                                                                                '<div dojoType="dijit.layout.ContentPane">' +
218                                                                                        '<div dojoType="dijit.form.Button" id="missingButtonId" ' +
219                                                                                        'dojoAttachPoint="missingButton">Missing...</div>' +
220                                                                                '</div>' +
221                                                                        '</div>',
222                                        widgetsInTemplate: true
223                                });
224       
225                                doh.register("parse", function(){
226                                        dojo.parser.parse();
227                                });
228
229                                doh.register("_Templated-widgetsInTemplate1.x",
230                                        [
231                                                {
232                                                        name: "dojoAttachPoint",
233                                                        runTest: function(t){
234                                                                var testW = dijit.byId("test1Widget");
235                                                                t.t(testW.normalNode, "normalNode");
236                                                                t.f(isNaN(testW.normalNode.nodeType), "normalNode.nodeType");
237                                                                t.t(testW.buttonWidget instanceof dijit.form.Button, "buttonWidget is Button");
238                                                                t.t(testW.checkboxWidget instanceof dijit.form.CheckBox, "checkboxWidget is CheckBox");
239                                                                t.t(testW.progressBarWidget instanceof dijit.ProgressBar, "progressBarWidget is ProgressBar");
240                                                                testW = dijit.byId("test2Widget");
241                                                                t.t(testW.containerNode, "containerNode");
242                                                                t.f(isNaN(testW.containerNode.nodeType), "containerNode nodeType");
243                                                                t.is(undefined,testW.buttonWidget, "buttonWidget undefined");
244                                                                t.t(testW.checkboxWidget instanceof dijit.form.CheckBox, "checkboxWidget is still CheckBox");
245                                                        }
246                                                },
247                                                {
248                                                        name: "dojoAttachEvent",
249                                                        runTest: function(t){
250                                                                var testW = dijit.byId("test1Widget");
251                                                                testW.buttonWidget._counter=0;
252                                                                testW.buttonWidget.onClick(testW.buttonWidget);
253                                                                testW.checkboxWidget._counter=0;
254                                                                testW.checkboxWidget.onClick(testW.checkboxWidget);
255                                                                testW.progressBarWidget._counter=0;
256                                                                testW.progressBarWidget.onChange(testW.progressBarWidget);
257                                                                t.is(1,testW.buttonWidget._counter, "buttonWidget._counter");
258                                                                t.is(1,testW.checkboxWidget._counter, "checkboxWidget._counter");
259                                                                t.is(1,testW.progressBarWidget._counter, "progressBarWidget._counter");
260                                                        }
261                                                },
262                                                {
263                                                        // Test that getDescendants ()
264                                                        // finds direct descendants but skips widgetsInTemplates
265                                                        // and also nested widgets (if direct==true)
266                                                        name: "destruction",
267                                                        runTest: function(t){
268                                                                var testW = dijit.byId("test3Widget");
269
270
271/*** performance tests
272                                                                var start = new Date();
273                                                                for(var i=0; i<1000; i++)
274                                                                        testW.getChildren();
275                                                                console.log("*** time for getChildren(): " + (new Date()-start));
276                                                                var start = new Date();
277                                                                for(var i=0; i<1000; i++)
278                                                                        testW.getDescendants();
279                                                                console.log("*** time for getDescendants(false): " + (new Date()-start));
280***/
281                                                                var chil = testW.getChildren();
282                                                                t.is(5, chil.length, "number of direct descendants");
283                                                                t.is(chil[0].id, "3.1");
284                                                                t.is(chil[1].id, "3.2");
285                                                                t.is(chil[2].id, "3.3");
286                                                                t.is(chil[3].id, "3.4");
287                                                                t.is(chil[4].id, "3.5");
288
289                                                                var desc = testW.getDescendants();
290                                                                t.is(7, desc.length, "number of descendants (including nested ones)");
291                                                                t.is(desc[0].id, "3.1");
292                                                                t.is(desc[1].id, "3.2");
293                                                                t.is(desc[2].id, "3.3");
294                                                                t.is(desc[3].id, "3.nested");
295                                                                t.is(desc[4].id, "3.nested2");
296                                                                t.is(desc[5].id, "3.4");
297                                                                t.is(desc[6].id, "3.5");
298                                                        }
299                                                },
300                                                {
301                                                        // Check that declarative widget with layout widgets in template is correctly created and rendered
302                                                        name: "declarative widget with layout widgets",
303                                                        runTest: function(t){
304                                                                validateTest4Widget(t, dijit.byId("test4Widget"));
305                                                        }
306                                                },
307                                                {
308                                                        // Check that programmatic widget with layout widgets in template is correctly created and rendered
309                                                        name: "programmatic widget with layout widgets",
310                                                        runTest: function(t){
311                                                                test4WidgetProgrammatic = new Test4Widget({}).placeAt("test4Widget", "after");
312                                                                test4WidgetProgrammatic.startup();
313                                                                validateTest4Widget(t, test4WidgetProgrammatic);
314                                                        }
315                                                },
316                                                {
317                                                        // Compare programmatic and declaratively created widget with layout widgets in template
318                                                        name: "programmatic vs declarative with layout widgets",
319                                                        runTest: function(t){
320                                                                // Focus unrelated element, so that we don't have different classes on our two widgets
321                                                                dojo.byId("plainInput").focus();
322                                                                var declW = dijit.byId("test4Widget");
323                                                                var progW = test4WidgetProgrammatic;
324
325                                                                // Check that generated HTML in DOM is same
326                                                                var declNeutralHtml = declW.domNode.innerHTML.replace(/_\d+/g, "");
327                                                                var progNeutralHtml = progW.domNode.innerHTML.replace(/_\d+/g, "");
328                                                                t.is(declNeutralHtml, progNeutralHtml, "progNeutralHtml");
329
330                                                                // Check that dimensions are same
331                                                                var declBox = dojo.contentBox(declW.domNode);
332                                                                var progBox = dojo.contentBox(progW.domNode);
333                                                                t.is(declBox.h, progBox.h, "progBox.h");
334                                                                t.is(declBox.w, progBox.w, "progBox.w");
335                                                        }
336                                                },
337                                                {
338                                                        // Check that declarative widget with other widgets in template is correctly started
339                                                        name: "declarative widget with many child widgets",
340                                                        runTest: function(t){
341                                                                validateTest5Widget(t, dijit.byId("test5Widget"));
342                                                        }
343                                                },
344                                                {
345                                                        // Check that programmatic widget with other widgets in template is correctly started
346                                                        name: "programmatic widget with many child widgets",
347                                                        runTest: function(t){
348                                                                test5WidgetProgrammatic = new Test5Widget().placeAt("test5Widget", "after");
349                                                                test5WidgetProgrammatic.startup();
350                                                                validateTest5Widget(t, test5WidgetProgrammatic);
351                                                        }
352                                                },
353                                                {
354                                                        // Check that destroying our declarative widget works correctly
355                                                        name: "declarative widget destruction",
356                                                        runTest: function(t){
357                                                                validateTest5WidgetDestroy(t, dijit.byId("test5Widget"));
358                                                        }
359                                                },
360                                                {
361                                                        // Check that destroying our programmatic widget works correctly
362                                                        name: "programmatic widget destruction",
363                                                        runTest: function(t){
364                                                                validateTest5WidgetDestroy(t, test5WidgetProgrammatic);
365                                                        }
366                                                },
367                                                {
368                                                        // Test that dojoAttachPoint inside of a ContentPane (inside of a template) works
369                                                        name: "ContentPane",
370                                                        runTest: function(){
371                                                                var testW = dijit.byId("missing");
372                                                                doh.t(testW, "widget was created");
373                                                                doh.t(testW.missingButton, "dojoAttachPoint created");
374                                                                doh.is("dijit.form.Button", testW.missingButton.declaredClass, "and it's to a widget");
375                                                                doh.t(dijit.byId("missingButtonId"), "nested widget also registered by id");
376                                                        }
377                                                }
378                                        ]
379                                );
380                                doh.run();
381                        });
382                </script>
383        </head>
384        <body class="claro">
385                <h1>testing widgetsInTemplate support</h1>
386                <xmp id="Test1Template" style="display:none;">
387                        <div>
388                                <div dojoAttachPoint="normalNode" >normal node</div>
389                                <button dojoAttachPoint="buttonWidget" dojoAttachEvent="onClick:onClick" dojoType="dijit.form.Button">button #1</button>
390                                <div dojoAttachPoint="checkboxWidget" dojoAttachEvent="onClick:onClick" dojoType="dijit.form.CheckBox"></div> checkbox #1
391                                <div dojoAttachPoint="progressBarWidget" dojoAttachEvent="onChange:onClick" style="width:400px"
392                                        maximum="200" progress="20" dojoType="dijit.ProgressBar"></div>
393                        </div>
394                </xmp>
395        <div dojoType="Test1Widget" id="test1Widget" ></div>
396
397        <!-- TODO: this is a strange test; the containerNode shouldn't have any contents as they will be overwritten -->
398        <xmp id="Test2Template" style="display:none;">
399                <div>
400                        <div dojoAttachPoint="containerNode" >
401                                <div dojoAttachPoint="checkboxWidget" dojoType="dijit.form.CheckBox"></div>
402                                checkbox #2
403                        </div>
404                </div>
405        </xmp>
406        <div dojoType="Test2Widget" id="test2Widget" ><button dojoAttachPoint="buttonWidget" dojoType="dijit.form.Button">button #2</button></div>
407
408
409        <xmp id="Test3Template" style="display:none;">
410                <div>
411                        <div dojoAttachPoint="checkboxWidget" dojoType="dijit.form.CheckBox"></div> checkbox #3
412                        <div dojoAttachPoint="containerNode" ></div>
413                </div>
414        </xmp>
415                <script>
416                        dojo.declare('Test3Widget',
417                                [dijit._Widget, dijit._Templated],
418                        {
419                                widgetsInTemplate: true,
420
421                                templateString: dojo.byId('Test3Template').textContent || dojo.byId('Test3Template').innerText
422                        });
423                </script>
424        <div dojoType="Test3Widget" id="test3Widget" >
425                <span>hello world</span>
426                <b style="border: 1px solid blue;">this is my
427                        <button dojoType="dijit.form.Button" id="3.1">first button</button>
428                </b>
429                <button dojoType="dijit.form.Button" id="3.2">another button</button>
430                <i>and some more</i>
431                <div style="border: 1px solid red;">
432                        <div dojoType="dijit.layout.ContentPane" style="border: 1px solid gray;" id="3.3">
433                                <button dojoType="dijit.form.Button" id="3.nested">a nested button</button>
434                                <button dojoType="dijit.form.Button" id="3.nested2">another nested button</button>
435                        </div>
436                        <button dojoType="dijit.form.Button" id="3.4">yet another button</button>
437                        <button dojoType="dijit.form.Button" id="3.5">yet yet another button</button>
438                </div>
439        </div>
440
441        <!-- Test templated widget containing layout widgets in template -->
442        <xmp id="Test4Template" style="display:none;">
443                <div>
444                        <div dojoType="dijit.layout.TabContainer" dojoAttachPoint="tabCont" style="height: 5em;">
445                                <div dojoType="dijit.layout.ContentPane" dojoAttachPoint="tab1" title="Tab 1">
446                                        pane 1
447                                </div>
448                                <div dojoType="dijit.layout.ContentPane" dojoAttachPoint="tab2" title="Tab 2">
449                                        pane 2
450                                </div>
451                        </div>
452                </div>
453        </xmp>
454        <div dojoType="Test4Widget" id="test4Widget" ></div>
455
456        <!-- Test templated widget containing container and nested widgets in template -->
457        <xmp id="Test5Template" style="display:none;">
458                <div>
459                        <div dojoType="TestLayoutWidget" dojoAttachPoint="layout">
460                                <div dojoType="TestCtndWidget" dojoAttachPoint="layChild1"></div>
461                                <div dojoType="TestCtndWidget" dojoAttachPoint="layChild2"></div>
462                        </div>
463                        <div dojoType="TestCtnrWidget" dojoAttachPoint="container">
464                                <div dojoType="TestCtndWidget" dojoAttachPoint="contained1"></div>
465                                <div dojoType="TestCtndWidget" dojoAttachPoint="contained2"></div>
466                        </div>
467                        <div dojoType="TestStubWidget" dojoAttachPoint="stub1"></div>
468                        <div dojoType="TestNonCtnrWidget" dojoAttachPoint="nonContainer">
469                                <div dojoType="TestStubWidget" dojoAttachPoint="nonContained1"></div>
470                                <div dojoType="TestStubWidget" dojoAttachPoint="nonContained2"></div>
471                        </div>
472                        <div dojoType="TestCtnrWidget" dojoAttachPoint="threeLevel">
473                                <div dojoType="TestNonCtnrWidget" dojoAttachPoint="secondLevel">
474                                        <div dojoType="TestStubWidget" dojoAttachPoint="bottomLevel"></div>
475                                </div>
476                        </div>
477                        <div dojoType="TestNonCtnrWidget" dojoAttachPoint="anotherThree">
478                                <div dojoType="TestCtnrWidget" dojoAttachPoint="anotherSecond">
479                                        <div dojoType="TestCtndWidget" dojoAttachPoint="anotherBottom"></div>
480                                </div>
481                        </div>
482                </div>
483        </xmp>
484        <div dojoType="Test5Widget" id="test5Widget" ></div>
485
486        <div dojoType="Missing" id="missing"></div>
487        <input id="plainInput"/>
488        </body>
489</html>
490
Note: See TracBrowser for help on using the repository browser.