[483] | 1 | define([ |
---|
| 2 | "dojo/_base/declare", // declare |
---|
| 3 | "dojo/_base/lang", // hitch |
---|
| 4 | "dojo/query", // query |
---|
| 5 | "../registry", // registry.byNode |
---|
| 6 | "../popup", // dijit.popup2.hide |
---|
| 7 | "./Button", |
---|
| 8 | "../_Container", |
---|
| 9 | "../_HasDropDown", |
---|
| 10 | "dojo/text!./templates/DropDownButton.html" |
---|
| 11 | ], function(declare, lang, query, registry, popup, Button, _Container, _HasDropDown, template){ |
---|
| 12 | |
---|
| 13 | // module: |
---|
| 14 | // dijit/form/DropDownButton |
---|
| 15 | |
---|
| 16 | return declare("dijit.form.DropDownButton", [Button, _Container, _HasDropDown], { |
---|
| 17 | // summary: |
---|
| 18 | // A button with a drop down |
---|
| 19 | // |
---|
| 20 | // example: |
---|
| 21 | // | <button data-dojo-type="dijit/form/DropDownButton"> |
---|
| 22 | // | Hello world |
---|
| 23 | // | <div data-dojo-type="dijit/Menu">...</div> |
---|
| 24 | // | </button> |
---|
| 25 | // |
---|
| 26 | // example: |
---|
| 27 | // | var button1 = new DropDownButton({ label: "hi", dropDown: new dijit.Menu(...) }); |
---|
| 28 | // | win.body().appendChild(button1); |
---|
| 29 | // |
---|
| 30 | |
---|
| 31 | baseClass: "dijitDropDownButton", |
---|
| 32 | |
---|
| 33 | templateString: template, |
---|
| 34 | |
---|
| 35 | _fillContent: function(){ |
---|
| 36 | // Overrides Button._fillContent(). |
---|
| 37 | // |
---|
| 38 | // My inner HTML contains both the button contents and a drop down widget, like |
---|
| 39 | // <DropDownButton> <span>push me</span> <Menu> ... </Menu> </DropDownButton> |
---|
| 40 | // The first node is assumed to be the button content. The widget is the popup. |
---|
| 41 | |
---|
| 42 | if(this.srcNodeRef){ // programatically created buttons might not define srcNodeRef |
---|
| 43 | //FIXME: figure out how to filter out the widget and use all remaining nodes as button |
---|
| 44 | // content, not just nodes[0] |
---|
| 45 | var nodes = query("*", this.srcNodeRef); |
---|
| 46 | this.inherited(arguments, [nodes[0]]); |
---|
| 47 | |
---|
| 48 | // save pointer to srcNode so we can grab the drop down widget after it's instantiated |
---|
| 49 | this.dropDownContainer = this.srcNodeRef; |
---|
| 50 | } |
---|
| 51 | }, |
---|
| 52 | |
---|
| 53 | startup: function(){ |
---|
| 54 | if(this._started){ |
---|
| 55 | return; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // the child widget from srcNodeRef is the dropdown widget. Insert it in the page DOM, |
---|
| 59 | // make it invisible, and store a reference to pass to the popup code. |
---|
| 60 | if(!this.dropDown && this.dropDownContainer){ |
---|
| 61 | var dropDownNode = query("[widgetId]", this.dropDownContainer)[0]; |
---|
| 62 | if(dropDownNode){ |
---|
| 63 | this.dropDown = registry.byNode(dropDownNode); |
---|
| 64 | } |
---|
| 65 | delete this.dropDownContainer; |
---|
| 66 | } |
---|
| 67 | if(this.dropDown){ |
---|
| 68 | popup.hide(this.dropDown); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | this.inherited(arguments); |
---|
| 72 | }, |
---|
| 73 | |
---|
| 74 | isLoaded: function(){ |
---|
| 75 | // Returns whether or not we are loaded - if our dropdown has an href, |
---|
| 76 | // then we want to check that. |
---|
| 77 | var dropDown = this.dropDown; |
---|
| 78 | return (!!dropDown && (!dropDown.href || dropDown.isLoaded)); |
---|
| 79 | }, |
---|
| 80 | |
---|
| 81 | loadDropDown: function(/*Function*/ callback){ |
---|
| 82 | // Default implementation assumes that drop down already exists, |
---|
| 83 | // but hasn't loaded it's data (ex: ContentPane w/href). |
---|
| 84 | // App must override if the drop down is lazy-created. |
---|
| 85 | var dropDown = this.dropDown; |
---|
| 86 | var handler = dropDown.on("load", lang.hitch(this, function(){ |
---|
| 87 | handler.remove(); |
---|
| 88 | callback(); |
---|
| 89 | })); |
---|
| 90 | dropDown.refresh(); // tell it to load |
---|
| 91 | }, |
---|
| 92 | |
---|
| 93 | isFocusable: function(){ |
---|
| 94 | // Overridden so that focus is handled by the _HasDropDown mixin, not by |
---|
| 95 | // the _FormWidget mixin. |
---|
| 96 | return this.inherited(arguments) && !this._mouseDown; |
---|
| 97 | } |
---|
| 98 | }); |
---|
| 99 | }); |
---|