From fe6b93150be2e579f3a822fb7912d64e999b4783 Mon Sep 17 00:00:00 2001
From: Andrey Antukh <niwi@niwi.nz>
Date: Sat, 2 Jan 2016 14:36:19 +0200
Subject: [PATCH] Add missing event for select object matched by selrect.

---
 frontend/uxbox/data/workspace.cljs | 35 ++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/frontend/uxbox/data/workspace.cljs b/frontend/uxbox/data/workspace.cljs
index d12bfb8ed..772587275 100644
--- a/frontend/uxbox/data/workspace.cljs
+++ b/frontend/uxbox/data/workspace.cljs
@@ -84,6 +84,41 @@
           (update-in state [:workspace :selected] disj id)
           (update-in state [:workspace :selected] conj id))))))
 
+(defn contained-in-selrect?
+  [shape selrect]
+  (let [sx1 (:x selrect)
+        sx2 (+ sx1 (:width selrect))
+        sy1 (:y selrect)
+        sy2 (+ sy1 (:height selrect))
+        rx1 (:x shape)
+        rx2 (+ rx1 (:width shape))
+        ry1 (:y shape)
+        ry2 (+ ry1 (:height shape))]
+    (and (neg? (- (:y selrect) (:y shape)))
+         (neg? (- (:x selrect) (:x shape)))
+         (pos? (- (+ (:y selrect)
+                     (:height selrect))
+                  (+ (:y shape)
+                     (:height shape))))
+         (pos? (- (+ (:x selrect)
+                     (:width selrect))
+                  (+ (:x shape)
+                     (:width shape)))))))
+
+(defn select-shapes
+  "Select shapes that matches the select rect."
+  [selrect]
+  (reify
+    rs/UpdateEvent
+    (-apply-update [_ state]
+      (println "select-shapes" selrect)
+      (let [pid (get-in state [:workspace :page])
+            shapes (->> (vals (:shapes-by-id state))
+                        (filter #(= (:page %) pid))
+                        (filter #(contained-in-selrect? % selrect))
+                        (map :id))]
+        (assoc-in state [:workspace :selected] (into #{} shapes))))))
+
 (defn deselect-all
   "Mark a shape selected for drawing in the canvas."
   []