forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropdown.js
152 lines (148 loc) · 5.6 KB
/
dropdown.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
define('dropdown',[ '$css',"./avalon" ], function($){
$.log("已加载dropdown模块",7)
$.ui = $.ui || {};
var defaults = {
parent: "body",
//按钮内的文字
text: "action",
//可供换肤用的类名btn-primary btn-danger btn-warning btn-success btn-info btn-inverse
//可供调整大小的类名btn-mini btn-small btn-large
cls: "",
menucls: "", //使用pull-left pull-right让下拉框相对按钮组对齐
menu: []
}
$.ui.DropDown = $.factory({
init: function(opts) {
opts = opts || [];
this.setOptions ("data", defaults, opts );
var data = this.data;
var list = data.menu;
list.forEach(function(el,i){
if(/^(b|n|s)/.test(typeof el)){
var text = el
list[i] = el = {
text: text
};
}
el.text = el.text || "";
el.cls = el.cls || "";
el.href = el.href || "#";
});
this.preRender = data.preRender || $.noop
delete data.preRender
this.tmpl = //不要使用换行符,这在压缩时很容易出现问题
'<div class="btn-group">'+
' <a class="btn dropdown-toggle" bind="class:cls" data-toggle="dropdown" href="#">'+
' <span bind="text:text">Action</span><span class="caret"></span>'+
' </a>'+
' <ul class="dropdown-menu" bind="foreach:menu,display:menu.length,class:menucls">'+
' <li bind="class:cls"><a bind="text:text,attr:{ href:href }" ></a></li>'+
' </ul>'+
'</div>'
if(opts.split){
this.tmpl = //不要使用换行符,这在压缩时很容易出现问题
'<div class="btn-group">'+
' <a class="btn" bind="class:cls" href="#">'+
' <span bind="text:text">Action</span>'+
' </a>'+
' <a class="btn dropdown-toggle" bind="class:cls" data-toggle="dropdown" href="#">'+
' <span class="caret"></span>'+
' </a>'+
' <ul class="dropdown-menu" bind="foreach:menu, display:menu.length,class:menucls">'+
' <li bind="class:cls"><a bind="text:text,attr:{ href:href }"></a></li>'+
' </ul>'+
'</div>'
}
//在.btn-group 的元素上添加dropup类 ,可以向上展出菜单
//在.dropdown-menu的元素上添加pull-right类可以向右对齐
this.preRender();
var ui = this.ui = $(this.tmpl).appendTo( data.parent )
//插入DOM并绑定数据
this.VM = $.ViewModel( data );
$.View(this.VM, ui[0]);
//绑定独立的事件
var menu = ui.find(".dropdown-menu")
//点击按钮时显示下拉框
ui.on("click",function(){
if (ui.is('.disabled, :disabled'))
return
//要求open加在与btn-group类的同一元素上
ui.toggleClass("open");
if(ui.hasClass("open")){
menu.focus();
}
return false;
});
//点击其他地方时会收起下拉框
ui.flag_can_collapse = true;
ui.mouseleave(function(){
ui.flag_can_collapse = true;
}).mouseenter(function(){
ui.flag_can_collapse = false;
});
$(document).click(function(){
if(ui.flag_can_collapse){
ui.removeClass("open");
}
})
},
size: function(name){
this.VM.cls({
"btn-mini":false,
"btn-small":false,
"btn-large":false
})
this.VM.cls(name)
//只能是这几个:.btn-mini, .btn-small, or .btn-large
}
// 如果想下拉框向上方显示,在.btn-group加个类名dropup
// 如果想并排显示多个.btn-group 那么在它们外面最套个DIV,类名为btn-toolbar
});
function getDropDown($this) {
var selector = $this.attr('data-target'), el
if (!selector) {
selector = $this.attr('href')
selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
}
el = $(selector);
if(!el.length){
el = $(".dropdown-toggle").eq(0)
}
if(el.length){
el = el.parent()
}
return el
}
$(document).keyup(function(e){
var keyCode = e.which;
//27 enter 38 up 40 down
if (!/(38|40|27)/.test(keyCode))
return
e.preventDefault();
e.stopPropagation();
//决定要操作哪一个
var el = $(this)
if (el.is('.disabled, :disabled'))
return
var ui = getDropDown( el );
var isActive = ui.hasClass('open')
var items = ui.find("li:not(.divider) a");
if (!isActive || (isActive && keyCode == 27))
return ui.click()
if (!items.length)
return
//IE7不支持:focus添加样式,使用:active代替
var cur = items.filter(':focus');
var index = items.index( cur )
if ( keyCode == 38){
index--
}
if ( keyCode == 40){
index++
}
if( index == items.length){
index = 0;
}
items.eq(index).focus();
})
})