]> _ Git - odl.git/commitdiff
Wait #5040 @5.5
authorStephen Cameron <stephen@cubedesigners.com>
Thu, 20 Jan 2022 19:33:53 +0000 (20:33 +0100)
committerStephen Cameron <stephen@cubedesigners.com>
Thu, 20 Jan 2022 19:33:53 +0000 (20:33 +0100)
app/Jobs/SearchIndex.php
resources/js/search.js
resources/views/components/search.blade.php
resources/views/layouts/app.blade.php

index c243d80ed7b5cebe035df571096dc6b64f0f8914..64007dc5e5e731cf8f3db1490c72f1ca263987b6 100644 (file)
@@ -41,7 +41,8 @@ class SearchIndex extends Index
             $doc = new Document();
             $doc->setType($data->get('type'));
             $doc->setId($data->get('id'));
-            $doc->setUrl('asset_' . $data->get('id'));
+            $doc->setUrl($this->relativeURL($asset->getFirstMediaUrl($asset->file_upload)));
+            $doc->setThumb($this->relativeURL($asset->getThumbURL()));
             $doc->setTitle($data->get('title'));
             $doc->setKeywords($data->get('keywords'));
             $this->addDocument($doc);
@@ -78,12 +79,18 @@ class SearchIndex extends Index
         foreach ($pdfs as $pdf) {
             $document = new PDF($pdf->getFirstMediaInField('file_upload')->getPath());
             $document->setId($pdf->id);
-            $document->setUrl('asset_' . $document->getId());
+            $document->setUrl($this->relativeURL($pdf->getFirstMediaUrl($pdf->file_upload)));
             $document->setTitle($pdf->title);
             $document->setKeywords($pdf->keywords);
-            $document->setThumb($pdf->getThumbURL());
+            $document->setThumb($this->relativeURL($pdf->getThumbURL()));
             $document->setType('pdf');
             $this->addDocument($document);
         }
     }
+
+    public function relativeURL($URL) {
+        // Get the relative URL by taking from /storage/ onwards
+        // We assume that assets are always going to be in the storage folder
+        return '.' . strstr($URL, '/storage/');
+    }
 }
index ea0659e6a081c48596e4036dd580be565ba232dc..a881dba3b0e9ebdbb2c94e5799e179b611addb5d 100644 (file)
@@ -16,11 +16,38 @@ export default () => ({
         },
     },
     query: '', // The search query
+    medialibrary: '', // Holds URL of the media library that is passed in from the component
 
     async init() {
-        this.miniSearch = new MiniSearch(this.setup);
-        await loadScript('../js/search.index.js');
-        this.miniSearch.addAllAsync(minisearchodl);
+        // We need the URL from the HTML because it gets properly updated when exporting the final version
+        await loadScript(this.$el.dataset.searchindex);
+
+        // Delay miniSearch initialisation so there's less chance of animations stuttering during page loads
+        setTimeout(() => {
+            this.miniSearch = new MiniSearch(this.setup);
+            this.miniSearch.addAllAsync(minisearchodl);
+        }, 4000);
+
+        this.medialibrary = this.$el.dataset.medialibrary;
+    },
+
+    makeResultLink(result, classes) {
+        // Result links have differing attributes, so we need this function to be able to generate the HTML correctly
+        // This can't be done directly in the x-for template loop
+        let URL = result.url;
+        let attributes = '';
+
+        // Media items get displayed on the media library page (simpler this way so extra JSON object isn't needed)
+        if (result.type === 'video' || result.type === 'audio') {
+            URL = `${this.medialibrary}?player=${result.id}`;
+        }
+
+        // PDFs can be opened directly
+        if (result.type === 'pdf') {
+            attributes = '@click.prevent="openPDF($el.attributes.href.value); closeSearch();"';
+        }
+
+        return `<a href="${URL}" class="${classes}" ${attributes}>${result.title}</a>`;
     },
 
     get results() {
index a7f670da5498b196e792f09d289374e400dcea49..bffa838550e5d5d33c97e667de039c74f68ec5a9 100644 (file)
@@ -3,6 +3,9 @@
 <div class="overlay search-overlay
             bg-white text-black
             z-20"
+     {{-- Pass URLs in from HTML so JS will always have the correct version regardless of environment --}}
+     data-searchindex="{{ asset('js/search.index.js') }}"
+     data-medialibrary="{{ \App\Http\Controllers\FrontController::getLinkData('medialibrary')['url'] }}"
      {{-- See js/search.js --}}
      x-data="search()"
      x-show="searchOpen"
@@ -40,7 +43,7 @@
                 placeholder="Recherche"
                 @keyup.enter="$el.blur()" {{-- Remove focus on enter in order to hide virtual keyboard --}}
                 x-model="query"
-                class="appearance-none outline-none
+                class="appearance-none outline-none bg-white
                        mt-8 w-full
                        font-semibold text-6xl uppercase
                        animate"
@@ -56,7 +59,7 @@
     </div>
 
     {{-- Search Results --}}
-    <div class="flex-1 max-h-full overflow-y-auto"
+    <div class="flex-1 max-h-full w-full overflow-y-auto"
          x-show="searchOpen"
          x-transition:enter.opacity.duration.500ms.delay.1500ms
          x-transition:leave.opacity.delay.50ms>
@@ -66,7 +69,7 @@
             <div class="h-full max-h-full overflow-y-auto">
                 <template x-for="result in results" :key="result.id">
                     <div class="py-8 border-b border-black border-opacity-20">
-                        <a :href="result.url" x-text="result.title" class="text-2xl text-blue"></a>
+                        <div x-html="makeResultLink(result, 'text-2xl text-blue')"></div>
                         <p x-text="result.text" class="mt-2 line-clamp-1"></p>
                     </div>
                 </template>
index 51035008bbfa1401f4cbed20b570a46822c52711..be46663d51149de541e22b5d4dd28b9bf4186a62 100644 (file)
@@ -43,6 +43,8 @@
                 },
 
                 openSearch() {
+                    // Search can be opened from the menu, so make sure menu is closed first
+                    // to prevent interference since the menu always has a higher z-index
                     this.menuOpen = false;
                     this.searchOpen = true;
                     this.$nextTick(() => {
                 openPDF(PDF_URL, updateQuerystring = true) {
                     PDF_URL = PDF_URL.replace('./storage', '../../../storage');
 
+                    // Extra level needed when on non-exported version
+                    if (location.href.includes('/front/') && !PDF_URL.includes('http')) {
+                        PDF_URL = '../' + PDF_URL;
+                    }
+
                     if (updateQuerystring) {
                         const location = new URL(window.location.href);
                         location.searchParams.set('pdf', PDF_URL);
         </ul>
     </div>
 
-    {{-- Search Overlay --}}
-    <x-search/>
-
     {{-- PDF Viewer Overlay --}}
     <div class="overlay pdf-overlay
                 bg-white text-black
     {{-- Media Player --}}
     <x-media-library.player/>
 
+    {{-- Search Overlay (placed last so it sits above other modals, like the PDF viewer, with the same z-index) --}}
+    <x-search/>
+
 @endsection