From 3aaf71fdea914af1a7f2b2fb97bb6ae44132fcc4 Mon Sep 17 00:00:00 2001
From: Debanshu Kundu <debanshu.kundu@joshtechnologygroup.com>
Date: Fri, 20 Jan 2017 23:11:13 +0530
Subject:  #4257 Added functionality to create previews for post links using
 open graph data from those links. (#4890)

---
 webapp/utils/commons.jsx | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 webapp/utils/commons.jsx

(limited to 'webapp/utils/commons.jsx')

diff --git a/webapp/utils/commons.jsx b/webapp/utils/commons.jsx
new file mode 100644
index 000000000..1888869dc
--- /dev/null
+++ b/webapp/utils/commons.jsx
@@ -0,0 +1,36 @@
+export function getDistanceBW2Points(point1, point2, xAttr = 'x', yAttr = 'y') {
+    return Math.sqrt(Math.pow(point1[xAttr] - point2[xAttr], 2) + Math.pow(point1[yAttr] - point2[yAttr], 2));
+}
+
+/**
+  * Funtion to return nearest point of given pivot point.
+  * It return two points one nearest and other nearest but having both coorditanes smaller than the given point's coordinates.
+  */
+export function getNearestPoint(pivotPoint, points, xAttr = 'x', yAttr = 'y') {
+    var nearestPoint = {};
+    var nearestPointLte = {};  // Nearest point smaller than or equal to point
+    for (const point of points) {
+        if (typeof nearestPoint[xAttr] === 'undefined' || typeof nearestPoint[yAttr] === 'undefined') {
+            nearestPoint = point;
+        } else if (getDistanceBW2Points(point, pivotPoint, xAttr, yAttr) < getDistanceBW2Points(nearestPoint, pivotPoint, xAttr, yAttr)) {
+        // Check for bestImage
+            nearestPoint = point;
+        }
+
+        if (typeof nearestPointLte[xAttr] === 'undefined' || typeof nearestPointLte[yAttr] === 'undefined') {
+            if (point[xAttr] <= pivotPoint[xAttr] && point[yAttr] <= pivotPoint[yAttr]) {
+                nearestPointLte = point;
+            }
+        } else if (
+        // Check for bestImageLte
+            getDistanceBW2Points(point, pivotPoint, xAttr, yAttr) < getDistanceBW2Points(nearestPointLte, pivotPoint, xAttr, yAttr) &&
+            point[xAttr] <= pivotPoint[xAttr] && point[yAttr] <= pivotPoint[yAttr]
+        ) {
+            nearestPointLte = point;
+        }
+    }
+    return {
+        nearestPoint,
+        nearestPointLte
+    };
+}
-- 
cgit v1.2.3-1-g7c22

