Browse Source

Only use touchend when really available and on mobile #345

Tobias Reich 8 years ago
parent
commit
aca4fcf400
6 changed files with 38 additions and 30 deletions
  1. 1 1
      src/scripts/header.js
  2. 1 2
      src/scripts/init.js
  3. 9 0
      src/scripts/lychee.js
  4. 1 1
      src/scripts/sidebar.js
  5. 20 20
      src/scripts/swipe.js
  6. 6 6
      src/scripts/view/main.js

+ 1 - 1
src/scripts/header.js

@@ -19,7 +19,7 @@ header.dom = function(selector) {
 header.bind = function() {
 
 	// Event Name
-	var eventName = ('ontouchend' in document.documentElement) ? 'touchend' : 'click';
+	var eventName = lychee.getEventName();
 
 	/* Buttons */
 	header.dom('#title').on(eventName, function(e) {

+ 1 - 2
src/scripts/init.js

@@ -6,8 +6,7 @@
 $(document).ready(function() {
 
 	/* Event Name */
-	var touchendSupport	= (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement),
-		eventName		= (touchendSupport===true ? 'touchend' : 'click');
+	var eventName = lychee.getEventName();
 
 	/* Set API error handler */
 	api.onError = lychee.error;

+ 9 - 0
src/scripts/lychee.js

@@ -384,6 +384,15 @@ lychee.loadDropbox = function(callback) {
 
 }
 
+lychee.getEventName = function() {
+
+	let touchendSupport	= (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement),
+		eventName		= (touchendSupport===true ? 'touchend' : 'click');
+
+	return eventName;
+
+}
+
 lychee.removeHTML = function(html = '') {
 
 	if (html==='') return html;

+ 1 - 1
src/scripts/sidebar.js

@@ -30,7 +30,7 @@ sidebar.bind = function() {
 	// event handlers should be removed before binding a new one.
 
 	// Event Name
-	let eventName = ('ontouchend' in document.documentElement) ? 'touchend' : 'click'
+	let eventName = lychee.getEventName();
 
 	sidebar
 		.dom('#edit_title')

+ 20 - 20
src/scripts/swipe.js

@@ -5,48 +5,48 @@
 
 swipe = {
 
-	obj:		null,
-	tolerance:	150,
-	offset:		0
+	obj       : null,
+	tolerance : 150,
+	offset    : 0
 
 }
 
 swipe.start = function(obj, tolerance) {
 
-	if (obj)		swipe.obj = obj;
-	if (tolerance)	swipe.tolerance = tolerance;
+	if (obj)       swipe.obj       = obj
+	if (tolerance) swipe.tolerance = tolerance
 
-	return true;
+	return true
 
 }
 
 swipe.move = function(e) {
 
-	if (swipe.obj===null) return false;
+	if (swipe.obj===null) return false
 
-	swipe.offset = -1 * e.x;
+	swipe.offset = -1 * e.x
 
 	swipe.obj.css({
-		WebkitTransform:	'translateX(' + swipe.offset + 'px)',
-		MozTransform:		'translateX(' + swipe.offset + 'px)',
-		transform:			'translateX(' + swipe.offset + 'px)'
-	});
+		WebkitTransform : 'translateX(' + swipe.offset + 'px)',
+		MozTransform    : 'translateX(' + swipe.offset + 'px)',
+		transform       : 'translateX(' + swipe.offset + 'px)'
+	})
 
 }
 
 swipe.stop = function(e, left, right) {
 
-	if (e.x<=-swipe.tolerance)		left(true);
-	else if (e.x>=swipe.tolerance)	right(true);
+	if (e.x<=-swipe.tolerance)     left(true)
+	else if (e.x>=swipe.tolerance) right(true)
 	else if (swipe.obj!==null) {
 		swipe.obj.css({
-			WebkitTransform:	'translateX(0px)',
-			MozTransform:		'translateX(0px)',
-			transform:			'translateX(0px)'
-		});
+			WebkitTransform : 'translateX(0px)',
+			MozTransform    : 'translateX(0px)',
+			transform       : 'translateX(0px)'
+		})
 	}
 
-	swipe.obj		= null;
-	swipe.offset	= 0;
+	swipe.obj    = null
+	swipe.offset = 0
 
 }

+ 6 - 6
src/scripts/view/main.js

@@ -9,17 +9,17 @@ var lychee		= { content: $('#content') },
 
 $(document).ready(function() {
 
-	/* Event Name */
-	if ('ontouchend' in document.documentElement)	eventName = 'touchend';
-	else											eventName = 'click';
+	// Event Name
+	var touchendSupport	= (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement),
+		eventName		= (touchendSupport===true ? 'touchend' : 'click');
 
-	/* Set API error handler */
+	// Set API error handler
 	api.onError = error;
 
-	/* Infobox */
+	// Infobox
 	header.dom('#button_info').on(eventName, sidebar.toggle);
 
-	/* Direct Link */
+	// Direct Link
 	header.dom('#button_direct').on(eventName, function() {
 
 		var link = $('#imageview #image').css('background-image').replace(/"/g,'').replace(/url\(|\)$/ig, '');