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