forked from dcloudio/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mui.class.scroll.pullrefresh.js
166 lines (163 loc) · 5.2 KB
/
mui.class.scroll.pullrefresh.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
(function($, window, document, undefined) {
var CLASS_VISIBILITY = $.className('visibility');
var CLASS_HIDDEN = $.className('hidden');
var PullRefresh = $.Scroll.extend($.extend({
handleEvent: function(e) {
this._super(e);
if (e.type === 'scrollbottom') {
if (e.target === this.scroller) {
this._scrollbottom();
}
}
},
_scrollbottom: function() {
if (!this.pulldown && !this.loading) {
this.pulldown = false;
this._initPullupRefresh();
this.pullupLoading();
}
},
_start: function(e) {
//仅下拉刷新在start阻止默认事件
if (e.touches && e.touches.length && e.touches[0].clientX > 30) {
e.target && !this._preventDefaultException(e.target, this.options.preventDefaultException) && e.preventDefault();
}
if (!this.loading) {
this.pulldown = this.pullPocket = this.pullCaption = this.pullLoading = false
}
this._super(e);
},
_drag: function(e) {
this._super(e);
if (!this.pulldown && !this.loading && this.topPocket && e.detail.direction === 'down' && this.y >= 0) {
this._initPulldownRefresh();
}
if (this.pulldown) {
this._setCaption(this.y > this.options.down.height ? this.options.down.contentover : this.options.down.contentdown);
}
},
_reLayout: function() {
this.hasVerticalScroll = true;
this._super();
},
//API
resetPosition: function(time) {
if (this.pulldown) {
if (this.y >= this.options.down.height) {
this.pulldownLoading(undefined, time || 0);
return true;
} else {
!this.loading && this.topPocket.classList.remove(CLASS_VISIBILITY);
}
}
return this._super(time);
},
pulldownLoading: function(y, time) {
typeof y === 'undefined' && (y = this.options.down.height); //默认高度
this.scrollTo(0, y, time, this.options.bounceEasing);
if (this.loading) {
return;
}
// if (!this.pulldown) {
this._initPulldownRefresh();
// }
this._setCaption(this.options.down.contentrefresh);
this.loading = true;
this.indicators.map(function(indicator) {
indicator.fade(0);
});
var callback = this.options.down.callback;
callback && callback.call(this);
},
endPulldownToRefresh: function() {
var self = this;
if (self.topPocket && self.loading && this.pulldown) {
self.scrollTo(0, 0, self.options.bounceTime, self.options.bounceEasing);
self.loading = false;
self._setCaption(self.options.down.contentdown, true);
setTimeout(function() {
self.loading || self.topPocket.classList.remove(CLASS_VISIBILITY);
}, 350);
}
},
pullupLoading: function(callback, x, time) {
x = x || 0;
this.scrollTo(x, this.maxScrollY, time, this.options.bounceEasing);
if (this.loading) {
return;
}
this._initPullupRefresh();
this._setCaption(this.options.up.contentrefresh);
this.indicators.map(function(indicator) {
indicator.fade(0);
});
this.loading = true;
callback = callback || this.options.up.callback;
callback && callback.call(this);
},
endPullupToRefresh: function(finished) {
var self = this;
if (self.bottomPocket) { // && self.loading && !this.pulldown
self.loading = false;
if (finished) {
this.finished = true;
self._setCaption(self.options.up.contentnomore);
// self.bottomPocket.classList.remove(CLASS_VISIBILITY);
// self.bottomPocket.classList.add(CLASS_HIDDEN);
self.wrapper.removeEventListener('scrollbottom', self);
} else {
self._setCaption(self.options.up.contentdown);
// setTimeout(function() {
self.loading || self.bottomPocket.classList.remove(CLASS_VISIBILITY);
// }, 300);
}
}
},
disablePullupToRefresh: function() {
this._initPullupRefresh();
this.bottomPocket.className = $.className('pull-bottom-pocket') + ' ' + CLASS_HIDDEN;
this.wrapper.removeEventListener('scrollbottom', this);
},
enablePullupToRefresh: function() {
this._initPullupRefresh();
this.bottomPocket.classList.remove(CLASS_HIDDEN);
this._setCaption(this.options.up.contentdown);
this.wrapper.addEventListener('scrollbottom', this);
},
refresh: function(isReset) {
if (isReset && this.finished) {
this.enablePullupToRefresh();
this.finished = false;
}
this._super();
},
}, $.PullRefresh));
$.fn.pullRefresh = function(options) {
if (this.length === 1) {
var self = this[0];
var pullRefreshApi = null;
options = options || {};
var id = self.getAttribute('data-pullrefresh');
if (!id) {
id = ++$.uuid;
$.data[id] = pullRefreshApi = new PullRefresh(self, options);
self.setAttribute('data-pullrefresh', id);
} else {
pullRefreshApi = $.data[id];
}
if (options.down && options.down.auto) { //如果设置了auto,则自动下拉一次
pullRefreshApi.pulldownLoading(options.down.autoY);
} else if (options.up && options.up.auto) { //如果设置了auto,则自动上拉一次
pullRefreshApi.pullupLoading();
}
//暂不提供这种调用方式吧
// if (typeof options === 'string') {
// var methodValue = pullRefreshApi[options].apply(pullRefreshApi, $.slice.call(arguments, 1));
// if (methodValue !== undefined) {
// return methodValue;
// }
// }
return pullRefreshApi;
}
};
})(mui, window, document);