forked from OpenNHP/opennhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.button.js
executable file
·121 lines (92 loc) · 3.27 KB
/
ui.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
define(function(require, exports, module) {
'use strict';
require('core');
var $ = window.Zepto,
UI = $.AMUI;
// Button Class
var Button = function(element, options) {
this.$element = $(element);
this.options = $.extend({}, Button.DEFAULTS, options);
this.isLoading = false;
this.hasSpinner = false;
};
Button.DEFAULTS = {
loadingText: 'loading...',
loadingClass: 'am-btn-loading',
loadingWithSpinner: '<span class="am-icon-refresh am-icon-spin"></span> loading...'
};
Button.prototype.setState = function(state) {
var d = 'disabled',
$el = this.$element,
val = $el.is('input') ? 'val' : 'html',
data = $el.data();
state = state + 'Text';
if (data.resetText == null) {
$el.data('resetText', $el[val]());
}
// add spinner for element with html()
if (UI.support.animation && !this.hasSpinner && val === 'html') {
this.options.loadingText = this.options.loadingWithSpinner;
this.hasSpinner = true;
}
$el[val](data[state] == null ? this.options[state] : data[state]);
// push to event loop to allow forms to submit
setTimeout($.proxy(function() {
if (state == 'loadingText') {
this.isLoading = true;
$el.addClass(d + ' ' + this.options.loadingClass).attr(d, d);
} else if (this.isLoading) {
this.isLoading = false;
$el.removeClass(d + ' ' + this.options.loadingClass).removeAttr(d);
}
}, this), 0);
};
Button.prototype.toggle = function() {
var changed = true,
$parent = this.$element.parent('.am-btn-group');
if ($parent.length) {
var $input = this.$element.find('input');
if ($input.prop('type') == 'radio') {
if ($input.prop('checked') && this.$element.hasClass('am-active')) {
changed = false;
} else {
$parent.find('.am-active').removeClass('am-active')
}
}
if (changed) {
$input.prop('checked', !this.$element.hasClass('am-active')).trigger('change')
}
}
if (changed) {
this.$element.toggleClass('am-active')
}
};
// Button plugin
function Plugin(option) {
return this.each(function() {
var $this = $(this);
var data = $this.data('amui.button');
var options = typeof option == 'object' && option;
if (!data) {
$this.data('amui.button', (data = new Button(this, options)));
}
if (option == 'toggle') {
data.toggle();
} else if (option) {
data.setState(option)
}
});
}
$.fn.button = Plugin;
// Init code
$(document).on('click.button.amui', '[data-am-button]', function(e) {
var $btn = $(e.target);
if (!$btn.hasClass('am-btn')) {
$btn = $btn.closest('.am-btn');
}
Plugin.call($btn, 'toggle');
e.preventDefault();
});
module.exports = Button;
// TODO: 样式复查
});