source: Dev/branches/rest-dojo-ui/client/dijit/Declaration.js @ 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: 3.9 KB
RevLine 
[256]1define([
2        "dojo/_base/array", // array.forEach array.map
3        "dojo/_base/connect", // connect.connect
4        "dojo/_base/declare", // declare
5        "dojo/_base/lang", // lang.getObject
6        "dojo/parser", // parser._functionFromScript
7        "dojo/query", // query
8        "./_Widget",
9        "./_TemplatedMixin",
10        "./_WidgetsInTemplateMixin",
11        "dojo/NodeList-dom"
12], function(array, connect, declare, lang, parser, query, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin){
13
14/*=====
15        var _Widget = dijit._Widget;
16        var _TemplatedMixin = dijit._TemplatedMixin;
17        var _WidgetsInTemplateMixin = dijit._WidgetsInTemplateMixin;
18=====*/
19
20        // module:
21        //              dijit/Declaration
22        // summary:
23        //              The Declaration widget allows a developer to declare new widget
24        //              classes directly from a snippet of markup.
25
26        return declare("dijit.Declaration", _Widget, {
27                // summary:
28                //              The Declaration widget allows a developer to declare new widget
29                //              classes directly from a snippet of markup.
30
31                // _noScript: [private] Boolean
32                //              Flag to parser to leave alone the script tags contained inside of me
33                _noScript: true,
34
35                // stopParser: [private] Boolean
36                //              Flag to parser to not try and parse widgets declared inside of me
37                stopParser: true,
38
39                // widgetClass: [const] String
40                //              Name of class being declared, ex: "acme.myWidget"
41                widgetClass: "",
42
43                // propList: [const] Object
44                //              Set of attributes for this widget along with default values, ex:
45                //              {delay: 100, title: "hello world"}
46                defaults: null,
47
48                // mixins: [const] String[]
49                //              List containing the prototype for this widget, and also any mixins,
50                //              ex: ["dijit._Widget", "dijit._Container"]
51                mixins: [],
52
53                buildRendering: function(){
54                        var src = this.srcNodeRef.parentNode.removeChild(this.srcNodeRef),
55                                methods = query("> script[type^='dojo/method']", src).orphan(),
56                                connects = query("> script[type^='dojo/connect']", src).orphan(),
57                                srcType = src.nodeName;
58
59                        var propList = this.defaults || {};
60
61                        // For all methods defined like <script type="dojo/method" data-dojo-event="foo">,
62                        // add that method to prototype.
63                        // If there's no "event" specified then it's code to run on instantiation,
64                        // so it becomes a connection to "postscript" (handled below).
65                        array.forEach(methods, function(s){
66                                var evt = s.getAttribute("event") || s.getAttribute("data-dojo-event"),
67                                        func = parser._functionFromScript(s);
68                                if(evt){
69                                        propList[evt] = func;
70                                }else{
71                                        connects.push(s);
72                                }
73                        });
74
75                        // map array of strings like [ "dijit.form.Button" ] to array of mixin objects
76                        // (note that array.map(this.mixins, lang.getObject) doesn't work because it passes
77                        // a bogus third argument to getObject(), confusing it)
78                        this.mixins = this.mixins.length ?
79                                array.map(this.mixins, function(name){ return lang.getObject(name); } ) :
80                                [ _Widget, _TemplatedMixin, _WidgetsInTemplateMixin ];
81
82                        propList._skipNodeCache = true;
83                        propList.templateString =
84                                "<"+srcType+" class='"+src.className+"'" +
85                                " data-dojo-attach-point='"+
86                                        (src.getAttribute("data-dojo-attach-point") || src.getAttribute("dojoAttachPoint") || '')+
87                                "' data-dojo-attach-event='"+
88                                        (src.getAttribute("data-dojo-attach-event") || src.getAttribute("dojoAttachEvent") || '')+
89                                "' >"+src.innerHTML.replace(/\%7B/g,"{").replace(/\%7D/g,"}")+"</"+srcType+">";
90
91                        // create the new widget class
92                        var wc = declare(
93                                this.widgetClass,
94                                this.mixins,
95                                propList
96                        );
97
98                        // Handle <script> blocks of form:
99                        //              <script type="dojo/connect" data-dojo-event="foo">
100                        // and
101                        //              <script type="dojo/method">
102                        // (Note that the second one is just shorthand for a dojo/connect to postscript)
103                        // Since this is a connect in the declaration, we are actually connection to the method
104                        // in the _prototype_.
105                        array.forEach(connects, function(s){
106                                var evt = s.getAttribute("event") || s.getAttribute("data-dojo-event") || "postscript",
107                                        func = parser._functionFromScript(s);
108                                connect.connect(wc.prototype, evt, func);
109                        });
110                }
111        });
112});
Note: See TracBrowser for help on using the repository browser.