forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avalon.select.js
173 lines (165 loc) · 7.31 KB
/
avalon.select.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
(function(avalon) {
//判定是否触摸界面
var defaults = {
minWidth: 225,
height: 175,
toggle: false,
caption: "请选择",
selectedIndex: 0,
checkAllText: "全选",
unCheckAllText: "全不选",
onChange: avalon.noop,
onOpen: avalon.noop,
onClose: avalon.noop
};
var domParser = document.createElement("div");
avalon.ui["select"] = function(element, id, opts, model) {
var $element = avalon(element);
var options = avalon.mix({}, defaults);
if (typeof opts === "object") {
for (var i in opts) {
if (i === "$id")
continue;
options[i] = opts[i];
}
}
avalon.mix(options, $element.data());
domParser.innerHTML = '<button type="button" ms-hover="ui-state-hover" ms-active="ui-state-focus" ms-click="toggleMenu" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" >' +
'<span class="ui-icon ui-icon-triangle-2-n-s"></span><span>{{caption}}</span></button>';
var button = domParser.removeChild(domParser.firstChild);
button.style.minWidth = options.minWidth + "px";
button.style.width = Math.max(options.minWidth, element.offsetWidth) + "px";
button.title = element.title;
$element.addClass("ui-helper-hidden-accessible");
domParser.innerHTML = '<div class="ui-multiselect-menu ui-widget ui-widget-content ui-corner-all"'
+ ' ms-visible="toggle" tabindex="-1">'
+ '<div class="ui-widget-header ui-corner-all ui-multiselect-header ui-helper-clearfix">'
+ '<ul class="ui-helper-reset">'
+ '<span ms-if="!multiple">' + options.caption + '</span>'
+ '<li ms-if="multiple"><a class="ui-multiselect-all" href="return false" ms-click="checkAll"><span class="ui-icon ui-icon-check"></span><span>{{checkAllText}}</span></a></li>'
+ '<li ms-if="multiple"><a class="ui-multiselect-none" href="return false" ms-click="unCheckAll"><span class="ui-icon ui-icon-closethick"></span><span>{{unCheckAllText}}</span></a></li>'
+ '<li class="ui-multiselect-close"><a href="#" class="ui-multiselect-close" ms-click="closeMenu"><span class="ui-icon ui-icon-circle-close"></span></a></li>'
+ '</ul></div>'
+ '<ul class="ui-multiselect-checkboxes ui-helper-reset" ms-css-height="height" ms-each-el="list" >'
+ '<li ms-class-ui-multiselect-optgroup-label="!el.isOption" >'
+ '<a href="#" ms-if="!el.isOption" >{{el.text}}</a>'
+ '<label for="rubylouvre" ms-if="el.isOption" ms-hover="ui-state-hover" ms-class-ui-state-disabled="el.disabled" ms-click="changeState" class="ui-corner-all">'
+ '<input ms-visible="multiple" ms-disabled="el.disabled" ms-checked="el.selected" type="checkbox"><span>{{el.text}}</span></label></li>'
+ '</ul></div>';
var list = [], index = 0, els = [];
function getOptions(i, el) {
if (el.tagName === "OPTION") {
list.push({
isOption: true,
text: el.text,
index: index++,
selected: !el.disabled && el.selected,
disabled: el.disabled
});
els.push(el);
} else if (el.tagName === "OPTGROUP") {
list.push({
isOption: false,
text: el.label,
index: 0,
selected: false,
disabled: true
});
els.push(el);
avalon.each(el.childNodes, getOptions);
}
}
avalon.each(element.childNodes, getOptions);
var menu = domParser.removeChild(domParser.firstChild);
menu.style.width = button.style.width;
var curCaption = options.caption;
var canClose = false;
avalon.bind(button, "mouseenter", function(e) {
canClose = false;
});
avalon.bind(menu, "mouseenter", function(e) {
canClose = false;
});
avalon.bind(menu, "mouseleave", function(e) {
canClose = true;
});
avalon.bind(document, "click", function(e) {
if (canClose) {
model.toggle = false;
}
});
model = avalon.define(id, function(vm) {
avalon.mix(vm, options);
vm.list = list;
vm.multiple = element.multiple;
function getCaption() {
if (vm.multiple) {
var l = vm.list.filter(function(el) {
return el.isOption && el.selected && !el.disabled;
}).length;
return l ? l + " selected" : curCaption;
} else {
return element[element.selectedIndex].text;
}
}
vm.caption = getCaption();
vm.toggleMenu = function() {
vm.toggle = !vm.toggle;
};
vm.$watch("toggle", function(v) {
if (v) {
var offset = avalon(button).offset();
menu.style.top = offset.top + button.offsetHeight + "px";
menu.style.left = offset.left + "px";
options.onOpen.call(element);
} else {
options.onClose.call(element);
}
});
vm.closeMenu = function(e) {
e.preventDefault();
vm.toggle = false;
};
vm.checkAll = function(e, val) {
e.preventDefault();
val = !val;
vm.list.forEach(function(el) {
if (el.isOption && !el.disabled) {
el.selected = val;
}
});
vm.caption = getCaption();
};
vm.unCheckAll = function(e) {
vm.checkAll(e, true);
};
vm.changeState = function(e) {
var obj = this.$scope.el;
if (!obj.disabled) {//重要技巧,通过e.target == this排除冒泡上来的事件
var index = obj.index;
var option = els[index];
if (vm.multiple) {
var a = obj.selected;
option.selected = obj.selected = !a
} else {
element.selectedIndex = vm.selectedIndex = index;
option.selected = true;
setTimeout(function() {
vm.toggle = false;
}, 250);
}
options.onChange.call(element);
vm.caption = getCaption();
}
};
});
avalon.ready(function() {
element.parentNode.insertBefore(button, element.nextSibling);
avalon.scan(button, model);
document.body.appendChild(menu);
avalon.scan(menu, model);
});
return model;
};
})(window.avalon);
//http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#single