| 1 | define([ |
| 2 | "dojo/_base/array", // forEach() |
| 3 | "dojo/aspect", // after() |
| 4 | "dojo/_base/declare", // declare() |
| 5 | "dojo/_base/lang", // hitch() |
| 6 | "dojo/parser" // parse() |
| 7 | ], function(array, aspect, declare, lang, parser){ |
| 8 | |
| 9 | // module: |
| 10 | // dijit/_WidgetsInTemplateMixin |
| 11 | |
| 12 | return declare("dijit._WidgetsInTemplateMixin", null, { |
| 13 | // summary: |
| 14 | // Mixin to supplement _TemplatedMixin when template contains widgets |
| 15 | |
| 16 | // _earlyTemplatedStartup: Boolean |
| 17 | // A fallback to preserve the 1.0 - 1.3 behavior of children in |
| 18 | // templates having their startup called before the parent widget |
| 19 | // fires postCreate. Defaults to 'false', causing child widgets to |
| 20 | // have their .startup() called immediately before a parent widget |
| 21 | // .startup(), but always after the parent .postCreate(). Set to |
| 22 | // 'true' to re-enable to previous, arguably broken, behavior. |
| 23 | _earlyTemplatedStartup: false, |
| 24 | |
| 25 | // contextRequire: Function |
| 26 | // Used to provide a context require to the dojo/parser in order to be |
| 27 | // able to use relative MIDs (e.g. `./Widget`) in the widget's template. |
| 28 | contextRequire: null, |
| 29 | |
| 30 | _beforeFillContent: function(){ |
| 31 | // Short circuit the parser when the template doesn't contain any widgets. Note that checking against |
| 32 | // this.templateString is insufficient because the data-dojo-type=... may appear through a substitution |
| 33 | // variable, like in ConfirmDialog, where the widget is hidden inside of the ${!actionBarTemplate}. |
| 34 | if(/dojoType|data-dojo-type/i.test(this.domNode.innerHTML)){ |
| 35 | // Before copying over content, instantiate widgets in template |
| 36 | var node = this.domNode; |
| 37 | |
| 38 | if(this.containerNode && !this.searchContainerNode){ |
| 39 | // Tell parse call below not to look for widgets inside of this.containerNode |
| 40 | this.containerNode.stopParser = true; |
| 41 | } |
| 42 | |
| 43 | parser.parse(node, { |
| 44 | noStart: !this._earlyTemplatedStartup, |
| 45 | template: true, |
| 46 | inherited: {dir: this.dir, lang: this.lang, textDir: this.textDir}, |
| 47 | propsThis: this, // so data-dojo-props of widgets in the template can reference "this" to refer to me |
| 48 | contextRequire: this.contextRequire, |
| 49 | scope: "dojo" // even in multi-version mode templates use dojoType/data-dojo-type |
| 50 | }).then(lang.hitch(this, function(widgets){ |
| 51 | this._startupWidgets = widgets; |
| 52 | |
| 53 | // _WidgetBase::destroy() will destroy any supporting widgets under this.domNode. |
| 54 | // If we wanted to, we could call this.own() on anything in this._startupWidgets that was moved outside |
| 55 | // of this.domNode (like Dialog, which is moved to <body>). |
| 56 | |
| 57 | // Hook up attach points and events for nodes that were converted to widgets |
| 58 | for(var i = 0; i < widgets.length; i++){ |
| 59 | this._processTemplateNode(widgets[i], function(n,p){ |
| 60 | // callback to get a property of a widget |
| 61 | return n[p]; |
| 62 | }, function(widget, type, callback){ |
| 63 | // callback to do data-dojo-attach-event to a widget |
| 64 | if(type in widget){ |
| 65 | // back-compat, remove for 2.0 |
| 66 | return widget.connect(widget, type, callback); |
| 67 | }else{ |
| 68 | // 1.x may never hit this branch, but it's the default for 2.0 |
| 69 | return widget.on(type, callback, true); |
| 70 | } |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | // Cleanup flag set above, just in case |
| 75 | if(this.containerNode && this.containerNode.stopParser){ |
| 76 | delete this.containerNode.stopParser; |
| 77 | } |
| 78 | })); |
| 79 | |
| 80 | if(!this._startupWidgets){ |
| 81 | throw new Error(this.declaredClass + ": parser returned unfilled promise (probably waiting for module auto-load), " + |
| 82 | "unsupported by _WidgetsInTemplateMixin. Must pre-load all supporting widgets before instantiation."); |
| 83 | } |
| 84 | } |
| 85 | }, |
| 86 | |
| 87 | _processTemplateNode: function(/*DOMNode|Widget*/ baseNode, getAttrFunc, attachFunc){ |
| 88 | // Override _AttachMixin._processNode to skip DOMNodes with data-dojo-type set. They are handled separately |
| 89 | // in the _beforeFillContent() code above. |
| 90 | |
| 91 | if(getAttrFunc(baseNode, "dojoType") || getAttrFunc(baseNode, "data-dojo-type")){ |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | return this.inherited(arguments); |
| 96 | }, |
| 97 | |
| 98 | startup: function(){ |
| 99 | array.forEach(this._startupWidgets, function(w){ |
| 100 | if(w && !w._started && w.startup){ |
| 101 | w.startup(); |
| 102 | } |
| 103 | }); |
| 104 | this._startupWidgets = null; |
| 105 | this.inherited(arguments); |
| 106 | } |
| 107 | }); |
| 108 | }); |