forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avalon.button.js
152 lines (143 loc) · 5.68 KB
/
avalon.button.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(["avalon"], function(avalon) {
var defaults = {
disabled: false
};
avalon.ui.button = function(element, id, vmodels, opts) {
var $element = avalon(element),
model, el, checkbox;
var title = element.title,
html = element.innerHTML;
var fragment = document.createDocumentFragment();
element.title = "";
//处理配置
var options = avalon.mix({}, defaults, $element.data());
//处理radio, checkbox
var isRadio = element.type === "radio";
var isCheckbox = element.type === "checkbox";
var radios = [];
if (isRadio && element.parentNode.$radio) {
model = element.parentNode.$radio;
radios = model.$radios;
}
var radioIndex = radios.length;
var toggleButton = isCheckbox || isRadio;
var activeClass = !toggleButton ? "ui-state-active" : "";
if (toggleButton) { //偷天换日,用label代替原来的input[type=checkbox],input[type=checkbox]
var label = document.createElement("label");
checkbox = element;
label.innerHTML = options.label || checkbox.value;
checkbox.parentNode.insertBefore(label, checkbox.nextSibling);
$element.addClass("ui-helper-hidden-accessible");
element = label;
$element = avalon(element);
}
while (el = element.firstChild) {
fragment.appendChild(el);
}
$element.addClass("ui-button ui-widget ui-state-default");
element.title = title;
//如果使用了buttonset
if (!options.cornerClass) {
$element.addClass("ui-corner-all");
}
if (typeof options.cornerClass === "string") {
$element.addClass(options.cornerClass);
}
//创建按钮的内部,将它原来的内部放到一个span.ui-button-text
if (fragment.childNodes.length) {
var span = document.createElement("span");
span.className = "ui-button-text";
while (fragment.firstChild) {
span.appendChild(fragment.firstChild);
}
$element.addClass("ui-button-text-only");
fragment.appendChild(span);
}
//如果指定了icon, icon也占用一个span
var iconClass = options.text === false ? "ui-button-icon-only" : typeof options.secondary === "string" ? "ui-button-text-icons" : typeof options.primary === "string" ? "ui-button-text-icon-primary" : ""
if (options.text === false) {
element.title = title || html;
}
if (iconClass) {
$element.addClass(iconClass);
}
if (options.primary) {
$element.removeClass("ui-button-text-only");
var span = document.createElement("span");
span.className = options.primary + " ui-button-icon-primary ui-icon";
fragment.insertBefore(span, fragment.firstChild);
}
if (options.secondary) {
$element.removeClass("ui-button-text-only");
var span = document.createElement("span");
span.className = options.secondary + " ui-button-icon-secondary ui-icon";
fragment.appendChild(span);
}
$element.bind("mousedown", function(e) {
if (model.disabled) {
return false;
}
$element.addClass(activeClass);
});
$element.bind("mouseup", function(e) {
if (model.disabled) {
return false;
}
$element.removeClass(activeClass);
});
if (isCheckbox) {
$element.bind("click", function() {
model.checked = !model.checked
});
}
if (isRadio) {
$element.bind("click", function() {
model.radioActived = radioIndex;
});
}
if (!model) {
model = avalon.define(id, function(vm) {
vm.disabled = options.disabled;
vm.radioActived = 0;
vm.checked = !!(checkbox || {}).checked;
vm.$radios = [];
});
}
if (isRadio) {
element.parentNode.$radio = model;
model.$radios.push(element);
}
avalon.nextTick(function() {
if (element.tagName !== "INPUT") {
element.appendChild(fragment);
}
element.setAttribute("ms-hover", "ui-state-hover");
element.setAttribute("ms-class-ui-state-disabled", "disabled");
if (isCheckbox) {
element.setAttribute("ms-class-ui-state-active", "checked");
checkbox.setAttribute("ms-checked", "checked");
}
if (isRadio) {
element.setAttribute("ms-class-ui-state-active", "radioActived == " + radioIndex);
element.setAttribute("ms-checked", "radioActived == " + radioIndex);
}
if (toggleButton) {
avalon.scan(checkbox, model);
}
avalon.scan(element, model);
});
return model
};
avalon.ui.buttonset = function(element, id) {
var $element = avalon(element);
$element.addClass("ui-buttonset");
var children = element.children;
for (var i = 0, el; el = children[i++]; ) {
el.setAttribute("data-corner-class", "true");
}
children[0].setAttribute("data-corner-class", "ui-corner-left");
children[children.length - 1].setAttribute("data-corner-class", "ui-corner-right");
};
return avalon
})
//X-tag和Web组件帮你提速应用开发 http://mozilla.com.cn/post/51451/