/
sui.js
227 lines (212 loc) · 5.75 KB
/
sui.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*ui.popbox.js*/
!function ($) {
var conf = {
"mask_class" : 'sui-popbox-mask',
'hide_class' : 'sui-disp-none',
'box_sel' : ".sui-popbox-box",
"delay_time" : 100
}
var _timer, _this =this;
$.fn.checkPosition = function () {
setInterval(function () {
$box.data('top') && $box.css('top', parseInt($box.data('top')) + document.body.scrollTop);
}, conf.delay_time);
}
$.fn.trantision = function () {
$(this).css('webkitTransition', 'top 1s');
}
/**
* 显示弹框,并设置高度
*/
$.fn.showPopbox = function () {
var $popbox = $(this),
box = $popbox.find(conf.box_sel)[0];
_this.$popbox = $popbox;
$popbox.suiShow();
//定高
var height = document.body.scrollHeight;
$popbox.height(height);
//定时校验机制, 一次延时校验
function _fixedHeight() {
height = document.body.scrollHeight;
if ( height !== $popbox.height() ) $popbox.height(height);
}
setTimeout(_fixedHeight, 100);
/*根据data配置数据*/
if ( $popbox.data('position') === 'fixed' && $popbox.data('setPosition') !== 'fixed' ) {
$popbox.data('setPosition', 'fixed');
$popbox.fixed();
}
$popbox.data('transition') && box && (box.style['webkitTransition'] = 'top 1s');
}
$.fn.fixed = function (e) {
var $popbox = $(this),
$box = $popbox.find('.sui-popbox-box');
function scrollHandler (event) {
if (!$box.data('top')) {
$box.data('top', $box.offset()['top'])
}
$box.css('top', parseInt($box.data('top')) + document.body.scrollTop);
}
window.addEventListener('scroll', scrollHandler);
scrollHandler();
}
$(document).on('click.sui-popbox>.sui-popbox-mask', function (e) {
var $mask = $(e.target),
$parent = $mask.parent();
if ( !$mask.hasClass(conf.mask_class) ) return;
if ($parent.data('cancel')) {
//防止事件透传, 采用delay隐藏
$parent.delayHide();
}
//防止事件透传,停止默认事件与冒泡
e.preventDefault();
e.stopPropagation();
return false;
});
//停止滚动, 在Android2.1停止scroll会无效
$(document).on('touchmove.sui-popbox', _scrollHandler);
function _scrollHandler (e) {
if ( _this.$popbox
&& !_this.$popbox.isHide()
&& _this.$popbox.data('noscroll') ) {
e.preventDefault();
e.stopPropagation();
return false;
}
}
}(window.$);
/*ui.input.js*/
!function ($) {
var conf = {};
/*Input public method*/
$.fn.flushInp = _fulshInput;
$(document).on('click.sui-inp', _fulshInput);
/*flush Placeholder*/
function _fulshInput (e) {
var $inp = $(e.target);
if( _isEmpty( $inp.val() ) ) {
$( event.target ).val( '' );
}
}
//String is empty
function _isEmpty(val) {
return (val === undefined || val === null || val.length === 0);
}
}(window.$);
/*ui.button.js*/
!function ($) {
var conf = {
"feed_time" : 250,
"feed_class" : 'sui-no-feed',
"disabled" : 'sui-disabled',
"nav_btn" : 'sui-btn-nav',
"nav_on" : 'sui-on',
"button" : '.sui-btn',
"feedback_class" : 'sui-feedback'
}
$.fn.toggle = function (time, callback) {
var $this = this;
$this.on('click', function () {
_toggle.call($this, time, callback)
})
}
$.fn.enabled = function () {
$(this).removeAttr('disabled');
$(this).removeClass(conf.disabled);
}
$.fn.disabled = function () {
$(this).attr('disabled', 'disabled');
$(this).addClass(conf.disabled)
}
/**
* @return bool
**/
$.fn.isdisabled = function () {
return $(this).hasClass(conf.disabled);
}
$.fn.feed = function () {
$(this).removeClass(conf.feed_class);
}
$.fn.unfeed = function () {
$(this).addClass(conf.feed_class)
}
$.fn.feedback = function (className) {
var $this = this;
$this.on('click', function () {
_feedback.call($(this), className);
})
}
function _toggle (time, callback) {
var $this = this;
$this.disabled();
if ( time && time !== 0 ) {
setTimeout (function () {
$this.enabled();
callback && callback.apply($this);
}, time);
}
}
function _feedback (className) {
var $this = this;
className = className || conf.feedback_class;
$this.addClass(className);
setTimeout(function () {
$this.removeClass(className);
}, conf.feed_time);
}
/**
* Button Event
**/
$(document).on('click.sui-btn', function (e) {
var $btn = $(e.target);
$btn.feed();
//按钮处于disabled状态
if ($btn.isdisabled()) return;
//检查按钮的toggle配置
var toggle = $btn.attr('data-toggle'),
feedback = $btn.attr('data-feedback'),
$parent = $btn.parent(),
isNavBtn = $parent ? $btn.parent().hasClass(conf.nav_btn) : false;
//改按钮配置了toggle属性
if (toggle) {
//disabled开关
_toggle.call($btn, parseInt(toggle));
}
if (feedback) {
_feedback.call($btn, feedback);
}
//取消默认反馈
setTimeout(function () {
$btn.removeClass(conf.feed_class);
}, conf.feed_time);
if (isNavBtn) {
$parent.find(conf.button).removeClass(conf.nav_on);
$btn.addClass(conf.nav_on);
}
})
}(window.$);
/*ui.base.js*/
!function ($) {
var conf = {
'hide_class' : 'sui-disp-none',
"delay_time" : 100
}
$.fn.delayHide = function (time) {
time = (typeof time) == 'number' ? time : conf.delay_time;
var $sui = $(this);
setTimeout(function () {
$sui.suiHide();
} , time);
}
$.fn.isHide = function () {
return $(this).hasClass(conf.hide_class);
}
$.fn.suiHide = function () {
var $sui = $(this);
$sui.addClass(conf.hide_class);
}
$.fn.suiShow = function () {
$(this).removeClass(conf.hide_class);
}
}(window.$);