Skip to content
This repository has been archived by the owner on Jun 29, 2019. It is now read-only.

Add deferred on refresh #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 57 additions & 38 deletions jquery.p2r.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// Class Definition
var PullToRefresh = function (element, options) {
this.$element = $(element);

this.options = $.extend({}, self.DEFAULTS, options);

this.$scroll = $(options.scroll);


this.flags = {
prevented: false,
moving: false,
Expand All @@ -22,7 +22,9 @@
startY: 0,
startX: 0,
lastStep: 0,
}
};

this.deferredFunctionOnRefresh = null;
};


Expand Down Expand Up @@ -50,7 +52,7 @@
eventName,
PullToRefresh.key
].join(".");
}
};

// support detection on touch events
PullToRefresh.support = {
Expand All @@ -70,7 +72,7 @@
start: PullToRefresh.namespace('touchstart'),
move: PullToRefresh.namespace('touchmove'),
end: PullToRefresh.namespace('touchend')
}
};
}

var events = {
Expand Down Expand Up @@ -143,7 +145,7 @@
if (has_bind) {
return function _pulltorefresh__bind(fn, context) {
return fn.bind(context);
}
};
} else {
// if lib has proxy
if ($.proxy) {
Expand Down Expand Up @@ -176,7 +178,7 @@
proxy.guid = fn.guid = fn.guid || jQuery.guid++;

return proxy;
}
};
}
}

Expand All @@ -193,9 +195,9 @@
style.webkitTransform = 'translate(0, ' + value + 'px) ' + 'translateZ(0)';
style.msTransform =
style.MsTransform =
style.MozTransform =
style.OTransform =
style.transform = 'translateY(' + value + 'px)';
style.MozTransform =
style.OTransform =
style.transform = 'translateY(' + value + 'px)';
};


Expand All @@ -208,9 +210,9 @@
PullToRefresh.prototype.transition = function _pullToRefresh__transition(style, ms) {
style.webkitTransitionDuration =
style.MozTransitionDuration =
style.msTransitionDuration =
style.OTransitionDuration =
style.transitionDuration = ms;
style.msTransitionDuration =
style.OTransitionDuration =
style.transitionDuration = ms;
};

/**
Expand All @@ -221,9 +223,9 @@
PullToRefresh.prototype.remove_transition = function _pullToRefresh__remove_transition(style) {
style.webkitTransitionDuration =
style.MozTransitionDuration =
style.msTransitionDuration =
style.OTransitionDuration =
style.transitionDuration = null;
style.msTransitionDuration =
style.OTransitionDuration =
style.transitionDuration = null;
};

/**
Expand All @@ -234,10 +236,10 @@
PullToRefresh.prototype.remove_transform = function _pulltorefresh__remove_transform(style) {
style.webkitTransform =
style.msTransform =
style.MsTransform =
style.MozTransform =
style.OTransform =
style.transform = null;
style.MsTransform =
style.MozTransform =
style.OTransform =
style.transform = null;
};


Expand All @@ -252,9 +254,9 @@
return {
x: isTouchEvent ? (event.targetTouches || event.originalEvent.targetTouches)[0].pageX : (event.pageX || event.clientX),
y: isTouchEvent ? (event.targetTouches || event.originalEvent.targetTouches)[0].pageY : (event.pageY || event.clientY)
}
};

}
};

/**
* method to listen event start
Expand Down Expand Up @@ -282,7 +284,7 @@
this.positions.startY = axis.y;
this.positions.startX = axis.x;

this.$element.trigger(PullToRefresh.namespace('start'), [axis.y])
this.$element.trigger(PullToRefresh.namespace('start'), [axis.y]);

this.transition(this.$element[0].style, "0ms");

Expand Down Expand Up @@ -323,7 +325,9 @@

// reset on horizontal scroll threshold fail
if (Math.abs(axis.x - this.positions.startX) > this.options.threshold) {
this.reset();
this.reset({
refresh_fail: true
});
return;
}

Expand All @@ -344,7 +348,9 @@

// if configured to reset on refresh, do it
if (this.options.resetRefresh) {
this.reset();
this.reset({
refresh_fail: false
});
return;
}

Expand Down Expand Up @@ -374,9 +380,20 @@
* Method to listen the end of user action
* @method
*/
PullToRefresh.prototype.reset = function _pulltorefresh__reset() {
this.transition(this.$element[0].style, this.options.resetSpeed);
this.transform(this.$element[0].style, 0);
PullToRefresh.prototype.reset = function _pulltorefresh__reset(data) {
if (this.deferredFunctionOnRefresh == null || data.refresh_fail) {
this.transition(this.$element[0].style, this.options.resetSpeed);
this.transform(this.$element[0].style, 0);
} else {
var that = this;

this.transition(this.$element[0].style, this.options.resetSpeed);
this.transform(this.$element[0].style, this.options.refresh);
that.deferredFunctionOnRefresh.done(function (response) {
that.transform(that.$element[0].style, 0);
that.deferredFunctionOnRefresh = $.Deferred();
});
}
this.flags.touched = false;
this.flags.isTouch = false;
this.flags.refreshed = false;
Expand All @@ -399,7 +416,9 @@
this.positions.startY = 0;
this.positions.startX = 0;

this.reset();
this.reset({
refresh_fail: true
});

this.$element.trigger(PullToRefresh.namespace('end'));

Expand All @@ -420,13 +439,13 @@
var $this = $(this);
var data = $this.data(PullToRefresh.key);

var options = $.extend({}, PullToRefresh.DEFAULTS, $this.data(), typeof option == 'object' && option)
var options = $.extend({}, PullToRefresh.DEFAULTS, $this.data(), typeof option == 'object' && option);

if (!data && option == 'destroy') return PullToRefresh.destroy();

if (!data) {

$this.data(PullToRefresh.key, (data = new PullToRefresh(this, options)))
$this.data(PullToRefresh.key, (data = new PullToRefresh(this, options)));

if (options.autoInit) {

Expand All @@ -435,7 +454,7 @@
}

if (typeof option == 'string') {
data[option].apply(data)
data[option].apply(data);
}

});
Expand All @@ -448,8 +467,8 @@
// ==================

$.fn.pullToRefresh.noConflict = function () {
$.fn.pullToRefresh = old
return this
}
$.fn.pullToRefresh = old;
return this;
};

})(window.jQuery || window.Zepto, document);
})(window.jQuery || window.Zepto, document);