Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-9354] Fixed bug with events firing twice. Also fixed a bug with Ti.UI.Slider #2337

Merged
merged 2 commits into from
Jun 6, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions mobileweb/titanium/Ti/UI/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ define(["Ti/_/declare", "Ti/_/UI/Widget", "Ti/_/dom", "Ti/_/css", "Ti/_/style",
on(self, "postlayout", self, "_updatePosition");
},

_constrainedUpdate: function() {
this.value = this._constrainValue(this.value);
_constrainedUpdate: function(value) {
this.properties.__values__.value = this._constrainValue(value);
this._updatePosition();
},

Expand All @@ -55,6 +55,11 @@ define(["Ti/_/declare", "Ti/_/UI/Widget", "Ti/_/dom", "Ti/_/css", "Ti/_/style",
setStyle(this._thumb, "pointerEvents", cssVal);
},

_handleTouchEvent: function(type, e) {
e.value = this.value;
Widget.prototype._handleTouchEvent.call(this, type, e);
},

_getContentSize: function() {
return {
width: 200,
Expand Down Expand Up @@ -107,7 +112,7 @@ define(["Ti/_/declare", "Ti/_/UI/Widget", "Ti/_/dom", "Ti/_/css", "Ti/_/style",
},

value: {
set: function(value, oldValue) {
set: function(value) {
return this._constrainValue(value);
},
post: function(value, oldValue) {
Expand Down
74 changes: 37 additions & 37 deletions mobileweb/titanium/Ti/_/Gestures/SingleTap.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
define(["Ti/_/declare", "Ti/_/lang","Ti/_/Gestures/GestureRecognizer"], function(declare,lang,GestureRecognizer) {

// This is the amount of space the finger is allowed drift until the gesture is no longer considered a tap
var driftThreshold = 25;

return declare("Ti._.Gestures.SingleTap", GestureRecognizer, {

name: "singletap",

_touchStartLocation: null,

// This is the amount of space the finger is allowed drift until the gesture is no longer considered a tap
_driftThreshold: 25,


constructor: function(type) {
this._type = type;
},

withinThreshold: function(x, y) {
var start = this._touchStartLocation;
return start && Math.abs(start.x - x) < driftThreshold && Math.abs(start.y - y) < driftThreshold;
},

processTouchStartEvent: function(e, element){
if (e.touches.length == 1 && e.changedTouches.length == 1) {
var changed = e.changedTouches;
if (e.touches.length == 1 && changed.length == 1) {
this._touchStartLocation = {
x: e.changedTouches[0].clientX,
y: e.changedTouches[0].clientY
x: changed[0].clientX,
y: changed[0].clientY
}
this._driftedOutsideThreshold = false;
}
},

processTouchEndEvent: function(e, element){
if (e.touches.length == 0 && e.changedTouches.length == 1 && this._touchStartLocation) {
var x = e.changedTouches[0].clientX,
y = e.changedTouches[0].clientY;
if (Math.abs(this._touchStartLocation.x - x) < this._driftThreshold &&
Math.abs(this._touchStartLocation.y - y) < this._driftThreshold && !this._driftedOutsideThreshold) {
var changed = e.changedTouches,
x,
y;

if (e.touches.length == 0 && changed.length == 1) {
x = changed[0].clientX;
y = changed[0].clientY;

if (this.withinThreshold(x, y) && !this._driftedOutsideThreshold) {
this._touchStartLocation = null;
var source = this.getSourceNode(e,element);
// We don't reuse the same results object because the values are modified before the event is fired.
// If we reused the object, they would be modified twice, which is incorrect.
if (!element._isGestureBlocked(this.name)) {
element._handleTouchEvent("click", {
x: x,
y: y,
source: source
});
element._handleTouchEvent(this.name, {
x: x,
y: y,
source: source
});
}
element._isGestureBlocked(this.name) || element._handleTouchEvent(this._type, {
x: x,
y: y,
source: this.getSourceNode(e, element)
});
}
}
},

processTouchMoveEvent: function(e, element) {
if (Math.abs(this._touchStartLocation.x - e.changedTouches[0].clientX) > this._driftThreshold ||
Math.abs(this._touchStartLocation.y - e.changedTouches[0].clientY) > this._driftThreshold) {
this._driftedOutsideThreshold = true;
}
var changed = e.changedTouches;
this._driftedOutsideThreshold = changed.length == 1 && !this.withinThreshold(changed[0].clientX, changed[0].clientY);
},

processTouchCancelEvent: function(e, element){
this._touchStartLocation = null;
}
Expand Down
19 changes: 11 additions & 8 deletions mobileweb/titanium/Ti/_/Gestures/TouchEnd.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
define(["Ti/_/declare", "Ti/_/lang","Ti/_/Gestures/GestureRecognizer"], function(declare,lang,GestureRecognizer) {
define(["Ti/_/declare", "Ti/_/lang", "Ti/_/Gestures/GestureRecognizer"], function(declare,lang,GestureRecognizer) {

return declare("Ti._.Gestures.TouchEnd", GestureRecognizer, {

name: "touchend",

processTouchEndEvent: function(e, element){
if (!element._isGestureBlocked(this.name)) {
for (var i = 0; i < e.changedTouches.length; i++) {
element,element._handleTouchEvent(this.name,{
x: e.changedTouches[i].clientX,
y: e.changedTouches[i].clientY,
source: this.getSourceNode(e,element)
var changed = e.changedTouches,
i = 0,
l = changed.length;
for (; i < l; i++) {
element._handleTouchEvent(this.name, {
x: changed[i].clientX,
y: changed[i].clientY,
source: this.getSourceNode(e, element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should cache the source node so that it isn't recalculated each iteration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed!

});
}
}
Expand Down
13 changes: 8 additions & 5 deletions mobileweb/titanium/Ti/_/Gestures/TouchMove.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ define(["Ti/_/declare", "Ti/_/lang","Ti/_/Gestures/GestureRecognizer"], function

processTouchMoveEvent: function(e, element){
if (!element._isGestureBlocked(this.name)) {
for (var i = 0; i < e.changedTouches.length; i++) {
element,element._handleTouchEvent(this.name,{
x: e.changedTouches[i].clientX,
y: e.changedTouches[i].clientY,
source: this.getSourceNode(e,element)
var changed = e.changedTouches,
i = 0,
l = changed.length;
for (; i < l; i++) {
element._handleTouchEvent(this.name, {
x: changed[i].clientX,
y: changed[i].clientY,
source: this.getSourceNode(e, element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

});
}
}
Expand Down
19 changes: 11 additions & 8 deletions mobileweb/titanium/Ti/_/Gestures/TouchStart.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
define(["Ti/_/declare", "Ti/_/lang","Ti/_/Gestures/GestureRecognizer"], function(declare,lang,GestureRecognizer) {
define(["Ti/_/declare", "Ti/_/lang", "Ti/_/Gestures/GestureRecognizer"], function(declare,lang,GestureRecognizer) {

return declare("Ti._.Gestures.TouchStart", GestureRecognizer, {

name: "touchstart",

processTouchStartEvent: function(e, element){
if (!element._isGestureBlocked(this.name)) {
for (var i = 0; i < e.changedTouches.length; i++) {
element,element._handleTouchEvent(this.name,{
x: e.changedTouches[i].clientX,
y: e.changedTouches[i].clientY,
source: this.getSourceNode(e,element)
var changed = e.changedTouches,
i = 0,
l = changed.length;
for (; i < l; i++) {
element._handleTouchEvent(this.name, {
x: changed[i].clientX,
y: changed[i].clientY,
source: this.getSourceNode(e, element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

});
}
}
Expand Down
70 changes: 35 additions & 35 deletions mobileweb/titanium/Ti/_/UI/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ define(
"Ti/UI", "Ti/_/Promise", "Ti/_/string"],
function(browser, css, declare, dom, event, lang, style, Evented, UI, Promise, string) {

var unitize = dom.unitize,
var global = window,
unitize = dom.unitize,
computeSize = dom.computeSize,
on = require.on,
setStyle = style.set,
Expand Down Expand Up @@ -64,7 +65,7 @@ define(
// Handle click/touch/gestures
recognizers = this._gestureRecognizers = {},

useTouch = "ontouchstart" in window,
useTouch = "ontouchstart" in global,
bg = lang.hitch(this, "_doBackground");

function processTouchEvent(eventType, evt) {
Expand Down Expand Up @@ -95,7 +96,7 @@ define(

on(this.domNode, useTouch ? "touchstart" : "mousedown", function(evt){
var handles = [
on(window, useTouch ? "touchmove" : "mousemove", function(evt){
on(global, useTouch ? "touchmove" : "mousemove", function(evt){
if (!touchMoveBlocked) {
touchMoveBlocked = true;
(useTouch || self._touching) && processTouchEvent("TouchMoveEvent", evt);
Expand All @@ -104,12 +105,12 @@ define(
}, 30);
}
}),
on(window, useTouch ? "touchend" : "mouseup", function(evt){
on(global, useTouch ? "touchend" : "mouseup", function(evt){
self._touching = false;
processTouchEvent("TouchEndEvent", evt);
event.off(handles);
}),
useTouch && on(window, "touchcancel", function(evt){
useTouch && on(global, "touchcancel", function(evt){
processTouchEvent("TouchCancelEvent", evt);
event.off(handles);
})
Expand Down Expand Up @@ -177,7 +178,7 @@ define(
if (!(name in gestureRecognizers)) {
gestureRecognizers[name] = {
count: 0,
recognizer: new (require("Ti/_/Gestures/" + gestureMapping[name]))
recognizer: new (require("Ti/_/Gestures/" + gestureMapping[name]))(name)
};
}

Expand Down Expand Up @@ -296,12 +297,12 @@ define(
startLayout: function() {
this._batchUpdateInProgress = true;
},

finishLayout: function() {
this._batchUpdateInProgress = false;
UI._triggerLayout(this, true);
},

updateLayout: function(params) {
this.startLayout();
var i = 0,
Expand All @@ -311,22 +312,21 @@ define(
}
this.finishLayout();
},

convertPointToView: function(point, destinationView) {

// Make sure that both nodes are connected to the root
if (!this._isAttachedToActiveWin() || !destinationView._isAttachedToActiveWin()) {
return null;
}

if (!point || !is(point.x,"Number") || !is(point.y,"Number")) {
throw new Error("Invalid point");
}

if (!destinationView.domNode) {
throw new Error("Invalid destination view");
}

function getAbsolutePosition(node, point, additive) {
var x = point.x,
y = point.y,
Expand All @@ -340,19 +340,18 @@ define(

return {x: x, y: y};
}

// Find this node's location relative to the root
return getAbsolutePosition(destinationView,getAbsolutePosition(this,point,true),false);
return getAbsolutePosition(destinationView, getAbsolutePosition(this,point,true),false);
},

// This method returns the offset of the content relative to the parent's location.
// This is useful for controls like ScrollView that can move the children around relative to itself.
_getContentOffset: function(){
_getContentOffset: function() {
return {x: 0, y: 0};
},

_computeGradient: function() {

var backgroundGradient = this.backgroundGradient;
colors = backgroundGradient.colors,
type = backgroundGradient.type,
Expand Down Expand Up @@ -501,19 +500,20 @@ define(
}
}

cssVal += ")";

require.each(require.config.vendorPrefixes.css, lang.hitch(this,function(vendorPrefix) {
setStyle(this.domNode, "backgroundImage", vendorPrefix + cssVal);
setStyle(this.domNode, "backgroundImage", vendorPrefix + cssVal + ")");
}));
},

_preventDefaultTouchEvent: true,

_isGestureBlocked: function(gesture) {
for (var recognizer in this._gestureRecognizers) {
var blockedGestures = this._gestureRecognizers[recognizer].blocking;
for (var blockedGesture in blockedGestures) {
var recognizer,
blockedGestures,
blockedGesture;
for (recognizer in this._gestureRecognizers) {
blockedGestures = this._gestureRecognizers[recognizer].blocking;
for (blockedGesture in blockedGestures) {
if (gesture === blockedGestures[blockedGesture]) {
return true;
}
Expand All @@ -525,17 +525,17 @@ define(
_handleTouchEvent: function(type, e) {
if (this.enabled) {
// Normalize the location of the event.
if (is(e.x, "Number") && is(e.y, "Number") && e.source) {
var pt = UI._container.convertPointToView({
var pt, x, y;
if (is(e.x, "Number") && is(e.y, "Number")) {
pt = UI._container.convertPointToView({
x: e.x,
y: e.y
}, e.source);
e.x = pt ? pt.x : void 0;
e.y = pt ? pt.y : void 0;
} else {
e.x = void 0;
e.y = void 0;
}, e.source || this) || {};
x = pt.x;
y = pt.y;
}
e.x = x;
e.y = y;
this.fireEvent(type, e);
}
},
Expand Down Expand Up @@ -566,7 +566,7 @@ define(

_getBorderFromCSS: function() {
setTimeout(lang.hitch(this, function () {
var computedStyle = window.getComputedStyle(this.domNode),
var computedStyle = global.getComputedStyle(this.domNode),
left = parseInt(computedStyle["border-left-width"]),
right = parseInt(computedStyle["border-right-width"]),
top = parseInt(computedStyle["border-top-width"]),
Expand Down Expand Up @@ -711,7 +711,7 @@ define(
// Create the transition, must be set before setting the other properties
if (style.supports("transition", self.domNode)) {
setStyle(self.domNode, "transition", "all " + anim.duration + "ms " + curve + (anim.delay ? " " + anim.delay + "ms" : ""));
on.once(window, transitionEnd, function(e) {
on.once(global, transitionEnd, function(e) {
completeAnimation();
});
} else {
Expand Down