Merge lp:~abreu-alexandre/ubuntu-html5-theme/fix-touch-detection-on-desktop-and-tabs into lp:ubuntu-html5-theme

Proposed by Alexandre Abreu
Status: Merged
Approved by: Alexandre Abreu
Approved revision: 130
Merged at revision: 138
Proposed branch: lp:~abreu-alexandre/ubuntu-html5-theme/fix-touch-detection-on-desktop-and-tabs
Merge into: lp:ubuntu-html5-theme
Diff against target: 173 lines (+60/-21)
2 files modified
0.1/ambiance/js/core.js (+18/-6)
0.1/ambiance/js/tabs.js (+42/-15)
To merge this branch: bzr merge lp:~abreu-alexandre/ubuntu-html5-theme/fix-touch-detection-on-desktop-and-tabs
Reviewer Review Type Date Requested Status
Alexandre Abreu Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+206212@code.launchpad.net

Commit message

Fix touch detection on UbuntuTouch & Desktop: we handle both. Touch detection does not work reliably, we should embrace the dual world.

Description of the change

Fix touch detection on UbuntuTouch & Desktop: we handle both. Touch detection does not work reliably, we should embrace the dual world.

To post a comment you must log in.
130. By Alexandre Abreu

untabify

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alexandre Abreu (abreu-alexandre) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '0.1/ambiance/js/core.js'
2--- 0.1/ambiance/js/core.js 2014-01-27 18:42:31 +0000
3+++ 0.1/ambiance/js/core.js 2014-02-13 15:55:07 +0000
4@@ -103,10 +103,10 @@
5 var U = this;
6 U.isTouch = "ontouchstart" in window;
7 U.touchEvents = {
8- touchStart: U.isTouch ? 'touchstart' : 'mousedown',
9- touchMove: U.isTouch ? 'touchmove' : 'mousemove',
10- touchEnd: U.isTouch ? 'touchend' : 'mouseup',
11- touchLeave: U.isTouch ? null : 'mouseleave' //we manually detect leave on touch devices, so null event here
12+ touchStart: ['touchstart','mousedown'],
13+ touchMove: ['touchmove','mousemove'],
14+ touchEnd: ['touchend','mouseup'],
15+ touchLeave: ['mouseleave'],
16 };
17 };
18
19@@ -187,6 +187,20 @@
20 get isTouch() {
21 return self.isTouch;
22 },
23+ registerTouchEvent: function(eventId,
24+ element,
25+ callback) {
26+ if ( ! eventId
27+ || typeof(eventId) !== 'object'
28+ || eventId.length === 0) {
29+ console.log('Invalid or empty eventId for registerTouchEvent: ' + eventId.toString() + ' ' + typeof(eventId));
30+ return;
31+ }
32+
33+ for (var i = 0; i < eventId.length; ++i) {
34+ element.addEventListener(eventId[i], callback);
35+ }
36+ },
37 touchEvents: {
38 get touchStart() {
39 return self.touchEvents.touchStart;
40@@ -202,8 +216,6 @@
41 },
42 },
43 translateTouchEvent: function(event) {
44- if (self.isTouch)
45- return event;
46 var touch = __createTouchObject(event);
47 var translatedTouchEvent = event;
48 translatedTouchEvent.changedTouches = [touch];
49
50=== modified file '0.1/ambiance/js/tabs.js'
51--- 0.1/ambiance/js/tabs.js 2014-01-30 19:56:22 +0000
52+++ 0.1/ambiance/js/tabs.js 2014-02-13 15:55:07 +0000
53@@ -75,6 +75,7 @@
54 if (tabs == null || touchInfoDelegate == null) {
55 return;
56 }
57+ this._touchDown = false;
58 this._tabs = tabs;
59
60 this._infos = {
61@@ -84,16 +85,21 @@
62 this._touchInfoDelegate = touchInfoDelegate;
63
64 var touchEvents = touchInfoDelegate.touchEvents;
65- this._tabs.addEventListener(touchEvents.touchStart,
66- this.__onTouchStart.bind(this));
67- this._tabs.addEventListener(touchEvents.touchMove, this.__onTouchMove.bind(this));
68- this._tabs.addEventListener(touchEvents.touchEnd, this.__onTouchEnd.bind(this));
69-
70- // we only have leave events on desktop, we manually calcuate
71- // leave on touch as its not supported in webkit
72- if (touchEvents.touchLeave) {
73- this._tabs.addEventListener(touchEvents.touchLeave, this.__onTouchLeave.bind(this));
74- }
75+
76+ touchInfoDelegate.registerTouchEvent(
77+ touchEvents.touchStart,
78+ this._tabs,
79+ this.__onTouchStart.bind(this));
80+
81+ touchInfoDelegate.registerTouchEvent(
82+ touchEvents.touchMove,
83+ this._tabs,
84+ this.__onTouchMove.bind(this));
85+
86+ touchInfoDelegate.registerTouchEvent(
87+ touchEvents.touchEnd,
88+ this._tabs,
89+ this.__onTouchEnd.bind(this));
90
91 // initialize default page
92 this.__setupInitialTabVisibility();
93@@ -156,6 +162,7 @@
94 if (targetPage)
95 targetPage.style.display=value;
96 };
97+ updateDisplayStyle(tab, 'block');
98 [].slice.
99 call(this._tabs.querySelectorAll('[data-role="tabitem"]:not(:first-child)')).
100 forEach(function(element) {
101@@ -203,8 +210,10 @@
102 * @private
103 */
104 __onTouchStart: function (e) {
105- console.log('touchstart');
106 if (!this._tabs) return;
107+
108+ e.preventDefault();
109+
110 this.__clearInternalState();
111
112 if (state === STATES.basic) {
113@@ -219,12 +228,20 @@
114 startTimestamp = _e.timeStamp;
115
116 this._tabs.style['-webkit-transition-duration'] = 0;
117+
118+ this._touchDown = true;
119 },
120
121 /**
122 * @private
123 */
124 __onTouchMove: function (e) {
125+ if (!this._touchDown) {
126+ return;
127+ }
128+
129+ e.preventDefault();
130+
131 var _e = this._touchInfoDelegate.translateTouchEvent(e);
132 deltaX = _e.touches[0].pageX - pageX;
133 deltaY = _e.touches[0].pageY - pageY;
134@@ -242,11 +259,17 @@
135
136 offsetX = (deltaX / resistance) + this.__getScroll();
137
138- var maxLeftReached = this._tabs.querySelector('li:first-child').getBoundingClientRect().left >= 0 &&
139+ var firstTab = this._tabs.querySelector('li:first-child');
140+ var maxRightScrollReached =
141+ firstTab.getBoundingClientRect().left >= 0 &&
142 deltaX > 0;
143- var maxRightReached = this._tabs.querySelector('li:last-child').getBoundingClientRect().right <= this.__getHeaderWidth() &&
144+
145+ var lastTab = this._tabs.querySelector('li:last-child');
146+ var maxLeftScrollReached =
147+ lastTab.getBoundingClientRect().right <= this.__getHeaderWidth() &&
148 deltaX < 0;
149- if (this.__getTabHeadersWidth() > this.__getHeaderWidth() && !maxLeftReached && !maxRightReached) {
150+
151+ if (!maxRightScrollReached && !maxLeftScrollReached) {
152 this._tabs.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)';
153 }
154 },
155@@ -257,13 +280,17 @@
156 __onTouchEnd: function (e) {
157 if (!this._tabs || isScrolling) return;
158
159+ e.preventDefault();
160+ this._touchDown = false;
161+
162 var _e = this._touchInfoDelegate.translateTouchEvent(e);
163
164 var MIN_JITTER_THRESHOLD = 20;
165 if (state === STATES.transitioning_to_navigation) {
166 state = STATES.navigating;
167 }
168- else if (state === STATES.navigating && Math.abs((_e.changedTouches[0].pageX - pageX)) < MIN_JITTER_THRESHOLD) {
169+ else if (state === STATES.navigating &&
170+ Math.abs((_e.changedTouches[0].pageX - pageX)) < MIN_JITTER_THRESHOLD) {
171 this.__onClicked(_e);
172 // Timer should have been cancelled, back to basic
173 state = STATES.basic;

Subscribers

People subscribed via source and target branches