source: Dev/branches/rest-dojo-ui/client/dijit/tests/_Templated-widgetsInTemplate1.x.html @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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