[483] | 1 | define([ |
---|
| 2 | "dojo/_base/array", // array.forEach array.map |
---|
| 3 | "dojo/aspect", // aspect.after |
---|
| 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, aspect, 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(), // remove for 2.0 |
---|
| 48 | aspects = query("> script[type='dojo/aspect']", src).orphan(), |
---|
| 49 | srcType = src.nodeName; |
---|
| 50 | |
---|
| 51 | var propList = this.defaults || {}; |
---|
| 52 | |
---|
| 53 | // For all methods defined like <script type="dojo/method" data-dojo-event="foo">, |
---|
| 54 | // add that method to prototype. |
---|
| 55 | // If there's no "event" specified then it's code to run on instantiation, |
---|
| 56 | // so it becomes a connection to "postscript" (handled below). |
---|
| 57 | array.forEach(methods, function(s){ |
---|
| 58 | var evt = s.getAttribute("event") || s.getAttribute("data-dojo-event"), // remove "event" for 2.0 |
---|
| 59 | func = parser._functionFromScript(s, "data-dojo-"); |
---|
| 60 | if(evt){ |
---|
| 61 | propList[evt] = func; |
---|
| 62 | }else{ |
---|
| 63 | aspects.push(s); |
---|
| 64 | } |
---|
| 65 | }); |
---|
| 66 | |
---|
| 67 | // map array of strings like [ "dijit.form.Button" ] to array of mixin objects |
---|
| 68 | // (note that array.map(this.mixins, lang.getObject) doesn't work because it passes |
---|
| 69 | // a bogus third argument to getObject(), confusing it) |
---|
| 70 | if(this.mixins.length){ |
---|
| 71 | this.mixins = array.map(this.mixins, function(name){ return lang.getObject(name); } ); |
---|
| 72 | }else{ |
---|
| 73 | this.mixins = [ _Widget, _TemplatedMixin, _WidgetsInTemplateMixin ]; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | propList._skipNodeCache = true; |
---|
| 77 | propList.templateString = |
---|
| 78 | "<"+srcType+" class='"+src.className+"'" + |
---|
| 79 | " data-dojo-attach-point='"+ |
---|
| 80 | (src.getAttribute("data-dojo-attach-point") || src.getAttribute("dojoAttachPoint") || '')+ |
---|
| 81 | "' data-dojo-attach-event='"+ |
---|
| 82 | (src.getAttribute("data-dojo-attach-event") || src.getAttribute("dojoAttachEvent") || '')+ |
---|
| 83 | "' >"+src.innerHTML.replace(/\%7B/g,"{").replace(/\%7D/g,"}")+"</"+srcType+">"; |
---|
| 84 | |
---|
| 85 | // create the new widget class |
---|
| 86 | var wc = declare( |
---|
| 87 | this.widgetClass, |
---|
| 88 | this.mixins, |
---|
| 89 | propList |
---|
| 90 | ); |
---|
| 91 | |
---|
| 92 | // Handle <script> blocks of form: |
---|
| 93 | // <script type="dojo/aspect" data-dojo-advice="after" data-dojo-method="foo"> |
---|
| 94 | // and |
---|
| 95 | // <script type="dojo/method"> |
---|
| 96 | // (Note that the second one is just shorthand for a dojo/aspect to postscript) |
---|
| 97 | // Since this is a connect in the declaration, we are actually connection to the method |
---|
| 98 | // in the _prototype_. |
---|
| 99 | array.forEach(aspects, function(s){ |
---|
| 100 | var advice = s.getAttribute("data-dojo-advice") || "after", |
---|
| 101 | method = s.getAttribute("data-dojo-method") || "postscript", |
---|
| 102 | func = parser._functionFromScript(s); |
---|
| 103 | aspect.after(wc.prototype, method, func, true); |
---|
| 104 | }); |
---|
| 105 | |
---|
| 106 | // Handle legacy <script type="dojo/connect" data-dojo-event="foo">. |
---|
| 107 | // Remove for 2.0. |
---|
| 108 | array.forEach(connects, function(s){ |
---|
| 109 | var evt = s.getAttribute("event") || s.getAttribute("data-dojo-event"), |
---|
| 110 | func = parser._functionFromScript(s); |
---|
| 111 | aspect.after(wc.prototype, evt, func, true); |
---|
| 112 | }); |
---|
| 113 | } |
---|
| 114 | }); |
---|
| 115 | }); |
---|