Pull to refresh work for scroll #56

This commit is contained in:
Max Lynch
2013-11-07 17:38:33 -06:00
parent fd6f400ddd
commit d37ec6fca2
9 changed files with 230 additions and 310 deletions

View File

@ -20,121 +20,6 @@
};
/**
* The Pull To Refresh drag operation handles the well-known
* "pull to refresh" concept seen on various apps. This lets
* the user indicate they want to refresh a given list by dragging
* down.
*
* @param {object} opts the options for the pull to refresh drag.
*/
var PullToRefreshDrag = function(opts) {
this.dragThresholdY = opts.dragThresholdY || 10;
this.onRefreshOpening = opts.onRefreshOpening || function() {};
this.onRefresh = opts.onRefresh || function() {};
this.onRefreshHolding = opts.onRefreshHolding || function() {};
this.el = opts.el;
};
PullToRefreshDrag.prototype = new DragOp();
PullToRefreshDrag.prototype.start = function(e) {
var content, refresher;
content = ionic.DomUtil.getParentOrSelfWithClass(e.target, 'list');
if(!content) { return; }
// Grab the refresher element that will show as you drag down
refresher = content.querySelector('.list-refresher');
if(!refresher) {
refresher = this._injectRefresher();
}
// Disable animations while dragging
refresher.classList.remove('list-refreshing');
this._currentDrag = {
refresher: refresher,
content: content,
isHolding: false
};
};
PullToRefreshDrag.prototype._injectRefresher = function() {
var refresher = document.createElement('div');
refresher.className = 'list-refresher';
this.el.insertBefore(refresher, this.el.firstChild);
return refresher;
};
PullToRefreshDrag.prototype.drag = function(e) {
var _this = this;
window.requestAnimationFrame(function() {
// We really aren't dragging
if(!_this._currentDrag) {
return;
}
// Check if we should start dragging. Check if we've dragged past the threshold,
// or we are starting from the open state.
if(!_this._isDragging && Math.abs(e.gesture.deltaY) > _this.dragThresholdY) {
_this._isDragging = true;
}
if(_this._isDragging) {
var refresher = _this._currentDrag.refresher;
var currentHeight = parseFloat(refresher.style.height);
refresher.style.height = e.gesture.deltaY + 'px';
var newHeight = parseFloat(refresher.style.height);
var firstChildHeight = parseFloat(refresher.firstElementChild.offsetHeight);
if(newHeight > firstChildHeight && !_this._currentDrag.isHolding) {
// The user is holding the refresh but hasn't let go of it
_this._currentDrag.isHolding = true;
_this.onRefreshHolding && _this.onRefreshHolding();
} else {
// Indicate what ratio of opening the list refresh drag is
var ratio = Math.min(1, newHeight / firstChildHeight);
_this.onRefreshOpening && _this.onRefreshOpening(ratio);
}
}
});
};
PullToRefreshDrag.prototype.end = function(e, doneCallback) {
var _this = this;
// There is no drag, just end immediately
if(!this._currentDrag) {
return;
}
var refresher = this._currentDrag.refresher;
var currentHeight = parseFloat(refresher.style.height);
refresher.style.height = e.gesture.deltaY + 'px';
var firstChildHeight = parseFloat(refresher.firstElementChild.offsetHeight);
if(currentHeight > firstChildHeight) {
//this.refreshing();
refresher.classList.add('list-refreshing');
refresher.style.height = firstChildHeight + 'px';
this.onRefresh && _this.onRefresh();
} else {
// Enable animations
refresher.classList.add('list-refreshing');
refresher.style.height = '0px';
this.onRefresh && _this.onRefresh();
}
this._currentDrag = null;
doneCallback && doneCallback();
};
var SlideDrag = function(opts) {
this.dragThresholdX = opts.dragThresholdX || 10;
@ -497,26 +382,6 @@
}
}
// Check if this is a "pull down" drag for pull to refresh
/*
else if(e.gesture.direction == 'down') {
this._dragOp = new PullToRefreshDrag({
el: this.el,
onRefresh: function() {
_this.onRefresh && _this.onRefresh();
},
onRefreshHolding: function() {
_this.onRefreshHolding && _this.onRefreshHolding();
},
onRefreshOpening: function(ratio) {
_this.onRefreshOpening && _this.onRefreshOpening(ratio);
}
});
this._dragOp.start(e);
}
*/
// Or check if this is a swipe to the side drag
else if((e.gesture.direction == 'left' || e.gesture.direction == 'right') && Math.abs(e.gesture.deltaX) > 5) {
this._dragOp = new SlideDrag({ el: this.el });