]>
Commit | Line | Data |
---|---|---|
f0cfe83e AD |
1 | define("dijit/form/_ListBase", [ |
2 | "dojo/_base/declare", // declare | |
3 | "dojo/on", | |
4 | "dojo/window" // winUtils.scrollIntoView | |
5 | ], function(declare, on, winUtils){ | |
6 | ||
7 | // module: | |
8 | // dijit/form/_ListBase | |
9 | ||
10 | return declare( "dijit.form._ListBase", null, { | |
11 | // summary: | |
12 | // Focus-less menu to handle UI events consistently | |
13 | // Abstract methods that must be defined externally: | |
14 | // | |
15 | // - onSelect: item is active (mousedown but not yet mouseup, or keyboard arrow selected but no Enter) | |
16 | // - onDeselect: cancels onSelect | |
17 | // tags: | |
18 | // private | |
19 | ||
20 | // selected: DOMNode | |
21 | // currently selected node | |
22 | selected: null, | |
23 | ||
24 | _listConnect: function(/*String|Function*/ eventType, /*String*/ callbackFuncName){ | |
25 | // summary: | |
26 | // Connects 'containerNode' to specified method of this object | |
27 | // and automatically registers for 'disconnect' on widget destroy. | |
28 | // description: | |
29 | // Provide widget-specific analog to 'connect'. | |
30 | // The callback function is called with the normal event object, | |
31 | // but also a second parameter is passed that indicates which list item | |
32 | // actually received the event. | |
33 | // returns: | |
34 | // A handle that can be passed to `disconnect` in order to disconnect | |
35 | // before the widget is destroyed. | |
36 | // tags: | |
37 | // private | |
38 | ||
39 | var self = this; | |
40 | return self.own(on(self.containerNode, | |
41 | on.selector( | |
42 | function(eventTarget, selector, target){ | |
43 | return eventTarget.parentNode == target; | |
44 | }, | |
45 | eventType | |
46 | ), | |
47 | function(evt){ | |
48 | evt.preventDefault(); | |
49 | self[callbackFuncName](evt, this); | |
50 | } | |
51 | )); | |
52 | }, | |
53 | ||
54 | selectFirstNode: function(){ | |
55 | // summary: | |
56 | // Select the first displayed item in the list. | |
57 | var first = this.containerNode.firstChild; | |
58 | while(first && first.style.display == "none"){ | |
59 | first = first.nextSibling; | |
60 | } | |
61 | this._setSelectedAttr(first); | |
62 | }, | |
63 | ||
64 | selectLastNode: function(){ | |
65 | // summary: | |
66 | // Select the last displayed item in the list | |
67 | var last = this.containerNode.lastChild; | |
68 | while(last && last.style.display == "none"){ | |
69 | last = last.previousSibling; | |
70 | } | |
71 | this._setSelectedAttr(last); | |
72 | }, | |
73 | ||
74 | selectNextNode: function(){ | |
75 | // summary: | |
76 | // Select the item just below the current selection. | |
77 | // If nothing selected, select first node. | |
78 | var selectedNode = this.selected; | |
79 | if(!selectedNode){ | |
80 | this.selectFirstNode(); | |
81 | }else{ | |
82 | var next = selectedNode.nextSibling; | |
83 | while(next && next.style.display == "none"){ | |
84 | next = next.nextSibling; | |
85 | } | |
86 | if(!next){ | |
87 | this.selectFirstNode(); | |
88 | }else{ | |
89 | this._setSelectedAttr(next); | |
90 | } | |
91 | } | |
92 | }, | |
93 | ||
94 | selectPreviousNode: function(){ | |
95 | // summary: | |
96 | // Select the item just above the current selection. | |
97 | // If nothing selected, select last node (if | |
98 | // you select Previous and try to keep scrolling up the list). | |
99 | var selectedNode = this.selected; | |
100 | if(!selectedNode){ | |
101 | this.selectLastNode(); | |
102 | }else{ | |
103 | var prev = selectedNode.previousSibling; | |
104 | while(prev && prev.style.display == "none"){ | |
105 | prev = prev.previousSibling; | |
106 | } | |
107 | if(!prev){ | |
108 | this.selectLastNode(); | |
109 | }else{ | |
110 | this._setSelectedAttr(prev); | |
111 | } | |
112 | } | |
113 | }, | |
114 | ||
115 | _setSelectedAttr: function(/*DomNode*/ node){ | |
116 | // summary: | |
117 | // Does the actual select. | |
118 | if(this.selected != node){ | |
119 | var selectedNode = this.selected; | |
120 | if(selectedNode){ | |
121 | this.onDeselect(selectedNode); | |
122 | this.selected = null; | |
123 | } | |
124 | if(node){ | |
125 | this.selected = node; | |
126 | winUtils.scrollIntoView(node); | |
127 | this.onSelect(node); | |
128 | } | |
129 | }else if(node){ | |
130 | this.onSelect(node); | |
131 | } | |
132 | } | |
133 | }); | |
134 | ||
135 | }); |