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