From b2aa0b80843e6e58f737e2937c45351d653255fa Mon Sep 17 00:00:00 2001
From: Sam Wilkins <samwilkins333@gmail.com>
Date: Mon, 27 Jan 2020 04:18:01 -0500
Subject: pivot field box and proper AND/OR behavior of facets

---
 src/client/views/collections/CollectionSubView.tsx | 29 ++++++++++++++++------
 1 file changed, 22 insertions(+), 7 deletions(-)

(limited to 'src/client/views/collections/CollectionSubView.tsx')

diff --git a/src/client/views/collections/CollectionSubView.tsx b/src/client/views/collections/CollectionSubView.tsx
index db3fde797..b96b86929 100644
--- a/src/client/views/collections/CollectionSubView.tsx
+++ b/src/client/views/collections/CollectionSubView.tsx
@@ -109,15 +109,30 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
             const viewSpecScript = Cast(this.props.Document.viewSpecScript, ScriptField);
             const viewedDocs = viewSpecScript ? docs.filter(d => viewSpecScript.script.run({ doc: d }, console.log).result) : docs;
             const docFilters = Cast(this.props.Document._docFilter, listSpec("string"), []);
+            const clusters: { [key: string]: { [value: string]: string } } = {};
+            for (let i = 0; i < docFilters.length; i += 3) {
+                const [key, value, modifiers] = docFilters.slice(i, i + 3);
+                const cluster = clusters[key];
+                if (!cluster) {
+                    const child: { [value: string]: string } = {};
+                    child[value] = modifiers;
+                    clusters[key] = child;
+                } else {
+                    cluster[value] = modifiers;
+                }
+            }
             const filteredDocs = docFilters.length ? viewedDocs.filter(d => {
-                let result = false;
-                for (let i = 0; i < docFilters.length; i += 3) {
-                    const key = docFilters[i];
-                    const value = docFilters[i + 1];
-                    const modifiers = docFilters[i + 2];
-                    result = result || ((modifiers === "x") !== Doc.matchFieldValue(d, key, value));
+                for (const key of Object.keys(clusters)) {
+                    const cluster = clusters[key];
+                    const satisfiesFacet = Object.keys(cluster).some(inner => {
+                        const modifier = cluster[inner];
+                        return (modifier === "x") !== Doc.matchFieldValue(d, key, inner);
+                    });
+                    if (!satisfiesFacet) {
+                        return false;
+                    }
                 }
-                return result;
+                return true;
             }) : viewedDocs;
             return filteredDocs;
         }
-- 
cgit v1.2.3-70-g09d2

