Explorar o código

refactor(js): minor refactoring of `touch_handler.js`

- Mark a method as `static`
- use `Math.sqrt` instead of `Math.pow(…, 0.5)`
- Use `Math.sign` instead of a condition on the sign
- Inline some used-once variables
- Reduce the scope of some variables
Julien Voisin hai 1 ano
pai
achega
fccca0ce1e
Modificáronse 1 ficheiros con 13 adicións e 19 borrados
  1. 13 19
      internal/ui/static/js/touch_handler.js

+ 13 - 19
internal/ui/static/js/touch_handler.js

@@ -26,7 +26,7 @@ class TouchHandler {
         return 0;
     }
 
-    findElement(element) {
+    static findElement(element) {
         if (element.classList.contains("entry-swipe")) {
             return element;
         }
@@ -42,7 +42,7 @@ class TouchHandler {
         this.reset();
         this.touch.start.x = event.touches[0].clientX;
         this.touch.start.y = event.touches[0].clientY;
-        this.touch.element = this.findElement(event.touches[0].target);
+        this.touch.element = TouchHandler.findElement(event.touches[0].target);
         this.touch.element.style.transitionDuration = "0s";
     }
 
@@ -60,11 +60,7 @@ class TouchHandler {
         if (absDistance > 0) {
             this.touch.moved = true;
 
-            let tx = absDistance > 75 ? Math.pow(absDistance - 75, 0.5) + 75 : absDistance;
-
-            if (distance < 0) {
-                tx = -tx;
-            }
+            const tx = (absDistance > 75 ? Math.sqrt(absDistance - 75) + 75 : absDistance) * Math.sign(distance);
 
             this.touch.element.style.transform = "translateX(" + tx + "px)";
 
@@ -78,9 +74,7 @@ class TouchHandler {
         }
 
         if (this.touch.element !== null) {
-            const absDistance = Math.abs(this.calculateDistance());
-
-            if (absDistance > 75) {
+            if (Math.abs(this.calculateDistance()) > 75) {
                 toggleEntryStatus(this.touch.element);
             }
 
@@ -118,15 +112,15 @@ class TouchHandler {
             return;
         }
 
-        const distance = this.calculateDistance();
-        const absDistance = Math.abs(distance);
-        const now = Date.now();
-
-        if (now - this.touch.time <= 1000 && absDistance > 75) {
-            if (distance > 0) {
-                goToPage("previous");
-            } else {
-                goToPage("next");
+        if (Date.now() - this.touch.time <= 1000) {
+            const distance = this.calculateDistance();
+            const absDistance = Math.abs(distance);
+            if (absDistance > 75) {
+                if (distance > 0) {
+                    goToPage("previous");
+                } else {
+                    goToPage("next");
+                }
             }
         }