forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avalon.button.js
152 lines (141 loc) · 5.89 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 widget = avalon.ui.button = function(element, data, vmodels, opts) {
var $element = avalon(element), title = element.title,
html = element.innerHTML, model, el, checkbox;
element.title = "";
var fragment = document.createDocumentFragment()
//处理配置
var options = data.buttonOptions //valon.mix({}, defaults, opts, $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")
//如果使用了buttonset
if (typeof options.cornerClass === "string") {
$element.addClass(options.cornerClass)
} else if (options.cornerClass !== false) {
$element.addClass("ui-corner-all")
}
//创建按钮的内部,将它原来的内部放到一个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)
}
if (isCheckbox) {
$element.bind("click", function() {
model.checked = !model.checked
})
}
if (isRadio) {
$element.bind("click", function() {
model.radioActived = radioIndex
})
}
if (!model) {
model = avalon.define(data.buttonId, 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-0", "ui-state-disabled:disabled")
element.setAttribute("ms-active", activeClass + ":!disabled")
if (isCheckbox) {
element.setAttribute("ms-class-1", "ui-state-active:checked")
checkbox.setAttribute("ms-checked", "checked")
}
if (isRadio) {
element.setAttribute("ms-class-2", "ui-state-active:radioActived == " + radioIndex)
element.setAttribute("ms-checked", "radioActived == " + radioIndex)
}
if (toggleButton) {
avalon.scan(checkbox, model)
}
avalon.scan(element, [model].concat(vmodels))
})
return model
}
widget.defaults = {
disabled: false
}
avalon.ui.buttonset = function(element) {
var $element = avalon(element)
$element.addClass("ui-buttonset")
var children = element.children;
for (var i = 0, el; el = children[i++]; ) {
el.setAttribute("data-button-corner-class", "true")
}
children[0].setAttribute("data-button-corner-class", "ui-corner-left")
children[children.length - 1].setAttribute("data-button-corner-class", "ui-corner-right")
}
return avalon
})
/**
data-primary="ui-icon-gear" 用于指定左边的ICON
data-secondary="ui-icon-triangle-1-s" 用于指定右边的ICON
data-corner-class="false" 不添加ui-corner-all圆角类名
data-corner-class="conrer" 添加你指定的这个conrer圆角类名
不写data-corner-class 添加ui-corner-all圆角类名
button, a, span等标签,取其innerHTML作为UI内容,否则需要取其title
data-text = false 决定其内部是否只显示图标
*
*
*/
//X-tag和Web组件帮你提速应用开发 http://mozilla.com.cn/post/51451/