-
Notifications
You must be signed in to change notification settings - Fork 2
/
DropDownList.js
192 lines (138 loc) · 5.43 KB
/
DropDownList.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(
function()
{
$j.RegisterControlType("J:DROPDOWNLIST", DropDownList);
function DropDownList(jelemt) {
if (jelemt) {
var width = jelemt.getAttribute("Width");
var height = jelemt.getAttribute("Height");
var selectChanged = jelemt.getAttribute("SelectChanged");
}
if (width)
this.width = width;
else
this.width = "200px";
if (height)
this.height = height;
else
this.height = "22px";
if (selectChanged)
this.selectChanged = selectChanged;
var dropDown = $j.DropDown();
this.dropDown = dropDown;
this.elemt = dropDown.elemt;
var text = document.createElement("div");
text.style.boxSizing = "border-box";
text.style.width = this.width;
text.style.height = this.height;
text.style.border = "solid 1px lightblue";
text.innerHTML = " "; // 预先给 text div 赋值一个文本,这样 DropDownList 和其它文本放在一起时,对齐方式可以统一。没有文本的 inline-block 的 div 会比旁边的文本高一点,第一次选择 item 给 text div 赋值文本时会和旁边的文本对齐,此时 text div 会产生一个向下的小移动
dropDown.Top(text);
this.text = text;
var list = document.createElement("div");
list.style.boxSizing = "border-box";
list.style.width = this.width;
list.style.border = "solid 1px lightblue";
list.style.backgroundColor = "white";
list.style.cursor = "default";
dropDown.Drop(list);
this.list = list;
this.selectedItem = null;
}
$j.DropDownList = function CreateDropDownList(id) {
var ctrl = new DropDownList();
if (id) {
$j.RegisterControl(ctrl, id);
}
return ctrl;
}
DropDownList.prototype = $j.Control();
DropDownList.prototype.Width = function Width(width) {
if (!width)
return this.width;
this.width = width;
this.text.style.width = width;
}
DropDownList.prototype.Height = function Height(height) {
if (!height)
return this.height;
this.height = height;
this.text.style.height = height;
}
DropDownList.prototype.DataBind = function DataBind(dataSource)
{
var ctrl = this;
var text = this.text;
var list = this.list;
list.innerHTML = "";
for (var i = 0; i < dataSource.length; i++) {
let item = document.createElement("div");
item.className = "jwf_DropDownListItem";
item.innerHTML = dataSource[i];
item.addEventListener("mousedown",
function jwf$DropDownList$ItemMouseDown(e) {
ctrl.mousedownSelf = true;
}
);
item.addEventListener("click",
function jwf$DropDownList$ItemClick() {
text.innerHTML = item.innerHTML;
// 这个 延时 其实也可以不用加,加延时的话体验效果会好一点,不加的话下拉框消失的太快了,当然这个看法因人而异 :)
window.setTimeout(
function jwf$DropDownList$ItemClickCloseDrop() {
ctrl.dropDown.Close();
},
100);
if (item != ctrl.selectedItem)
{
ctrl.selectedItem = item;
RaiseSelectChangedEvent( ctrl );
}
}
);
list.appendChild(item);
}
}
function RaiseSelectChangedEvent( ctrl )
{
var handler;
if (typeof (ctrl.selectChanged) == "string") {
handler = window[ctrl.selectChanged];
}
else if (typeof (ctrl.selectChanged) == "function") {
handler = ctrl.selectChanged;
}
if (handler)
handler(ctrl);
}
DropDownList.prototype.SelectChanged = function SelectChanged(selectChanged) {
if (!selectChanged)
return this.selectChanged;
this.selectChanged = selectChanged;
}
DropDownList.prototype.SelectedText = function SelectedText(text)
{
if (!text)
{
if (!this.selectedItem)
return null;
return this.selectedItem.innerText;
}
var item = this.list.firstChild;
if (!item)
return;
while (true)
{
if (item.innerText == text)
{
this.text.innerText = text;
this.selectedItem = item;
break;
}
if (!item.nextSibling)
break;
item = item.nextSibling;
}
}
}
)();