source: Dev/trunk/src/client/dijit/MenuItem.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.6 KB
RevLine 
[483]1define([
2        "dojo/_base/declare", // declare
3        "dojo/dom", // dom.setSelectable
4        "dojo/dom-attr", // domAttr.set
5        "dojo/dom-class", // domClass.toggle
6        "dojo/_base/kernel", // kernel.deprecated
7        "dojo/sniff", // has("ie")
8        "dojo/_base/lang", // lang.hitch
9        "./_Widget",
10        "./_TemplatedMixin",
11        "./_Contained",
12        "./_CssStateMixin",
13        "dojo/text!./templates/MenuItem.html"
14], function(declare, dom, domAttr, domClass, kernel, has, lang,
15                        _Widget, _TemplatedMixin, _Contained, _CssStateMixin, template){
16
17        // module:
18        //              dijit/MenuItem
19
20        var MenuItem = declare("dijit.MenuItem" + (has("dojo-bidi") ? "_NoBidi" : ""),
21                [_Widget, _TemplatedMixin, _Contained, _CssStateMixin], {
22                // summary:
23                //              A line item in a Menu Widget
24
25                // Make 3 columns
26                // icon, label, and expand arrow (BiDi-dependent) indicating sub-menu
27                templateString: template,
28
29                baseClass: "dijitMenuItem",
30
31                // label: String
32                //              Menu text as HTML
33                label: "",
34                _setLabelAttr: function(val){
35                        this._set("label", val);
36                        var shortcutKey = "";
37                        var text;
38                        var ndx = val.search(/{\S}/);
39                        if(ndx >= 0){
40                                shortcutKey = val.charAt(ndx + 1);
41                                var prefix = val.substr(0, ndx);
42                                var suffix = val.substr(ndx + 3);
43                                text = prefix + shortcutKey + suffix;
44                                val = prefix + '<span class="dijitMenuItemShortcutKey">' + shortcutKey + '</span>' + suffix;
45                        }else{
46                                text = val;
47                        }
48                        this.domNode.setAttribute("aria-label", text + " " + this.accelKey);
49                        this.containerNode.innerHTML = val;
50                        this._set('shortcutKey', shortcutKey);
51                },
52
53                /*=====
54                // shortcutKey: [readonly] String
55                //              Single character (underlined when the parent Menu is focused) used to navigate directly to this widget,
56                //              also known as [a mnemonic](http://en.wikipedia.org/wiki/Mnemonics_(keyboard%29).
57                //              This is denoted in the label by surrounding the single character with {}.
58                //              For example, if label="{F}ile", then shortcutKey="F".
59                shortcutKey: "",
60                =====*/
61
62                // iconClass: String
63                //              Class to apply to DOMNode to make it display an icon.
64                iconClass: "dijitNoIcon",
65                _setIconClassAttr: { node: "iconNode", type: "class" },
66
67                // accelKey: String
68                //              Text for the accelerator (shortcut) key combination, a control, alt, etc. modified keystroke meant to
69                //              execute the menu item regardless of where the focus is on the page.
70                //
71                //              Note that although Menu can display accelerator keys, there is no infrastructure to actually catch and
72                //              execute those accelerators.
73                accelKey: "",
74
75                // disabled: Boolean
76                //              If true, the menu item is disabled.
77                //              If false, the menu item is enabled.
78                disabled: false,
79
80                _fillContent: function(/*DomNode*/ source){
81                        // If button label is specified as srcNodeRef.innerHTML rather than
82                        // this.params.label, handle it here.
83                        if(source && !("label" in this.params)){
84                                this._set('label', source.innerHTML);
85                        }
86                },
87
88                buildRendering: function(){
89                        this.inherited(arguments);
90                        var label = this.id + "_text";
91                        domAttr.set(this.containerNode, "id", label); // only needed for backward compat
92                        if(this.accelKeyNode){
93                                domAttr.set(this.accelKeyNode, "id", this.id + "_accel"); // only needed for backward compat
94                        }
95                        dom.setSelectable(this.domNode, false);
96                },
97
98                onClick: function(/*Event*/){
99                        // summary:
100                        //              User defined function to handle clicks
101                        // tags:
102                        //              callback
103                },
104
105                focus: function(){
106                        // summary:
107                        //              Focus on this MenuItem
108                        try{
109                                if(has("ie") == 8){
110                                        // needed for IE8 which won't scroll TR tags into view on focus yet calling scrollIntoView creates flicker (#10275)
111                                        this.containerNode.focus();
112                                }
113                                this.focusNode.focus();
114                        }catch(e){
115                                // this throws on IE (at least) in some scenarios
116                        }
117                },
118
119                _onFocus: function(){
120                        // summary:
121                        //              This is called by the focus manager when focus
122                        //              goes to this MenuItem or a child menu.
123                        // tags:
124                        //              protected
125
126                        this.getParent()._onItemFocus(this);
127
128                        this.inherited(arguments);
129                },
130
131                _setSelected: function(selected){
132                        // summary:
133                        //              Indicate that this node is the currently selected one
134                        // tags:
135                        //              private
136
137                        domClass.toggle(this.domNode, "dijitMenuItemSelected", selected);
138                },
139
140                setLabel: function(/*String*/ content){
141                        // summary:
142                        //              Deprecated.   Use set('label', ...) instead.
143                        // tags:
144                        //              deprecated
145                        kernel.deprecated("dijit.MenuItem.setLabel() is deprecated.  Use set('label', ...) instead.", "", "2.0");
146                        this.set("label", content);
147                },
148
149                setDisabled: function(/*Boolean*/ disabled){
150                        // summary:
151                        //              Deprecated.   Use set('disabled', bool) instead.
152                        // tags:
153                        //              deprecated
154                        kernel.deprecated("dijit.Menu.setDisabled() is deprecated.  Use set('disabled', bool) instead.", "", "2.0");
155                        this.set('disabled', disabled);
156                },
157
158                _setDisabledAttr: function(/*Boolean*/ value){
159                        // summary:
160                        //              Hook for attr('disabled', ...) to work.
161                        //              Enable or disable this menu item.
162
163                        this.focusNode.setAttribute('aria-disabled', value ? 'true' : 'false');
164                        this._set("disabled", value);
165                },
166
167                _setAccelKeyAttr: function(/*String*/ value){
168                        // summary:
169                        //              Hook for attr('accelKey', ...) to work.
170                        //              Set accelKey on this menu item.
171
172                        if(this.accelKeyNode){
173                                this.accelKeyNode.style.display = value ? "" : "none";
174                                this.accelKeyNode.innerHTML = value;
175                                //have to use colSpan to make it work in IE
176                                domAttr.set(this.containerNode, 'colSpan', value ? "1" : "2");
177                        }
178                        this._set("accelKey", value);
179                }
180        });
181
182        if(has("dojo-bidi")){
183                MenuItem = declare("dijit.MenuItem", MenuItem, {
184                        _setLabelAttr: function(val){
185                                this.inherited(arguments);
186                                if(this.textDir === "auto"){
187                                        this.applyTextDir(this.textDirNode);
188                                }
189                        }
190                });
191        }
192
193        return MenuItem;
194});
Note: See TracBrowser for help on using the repository browser.