From 5f8c43cc2ca270f7c383f90f951b74a0647bb0ef Mon Sep 17 00:00:00 2001 From: opdx0 <235612605+opdx0@users.noreply.github.com> Date: Sat, 31 Oct 2026 18:29:52 -0700 Subject: [PATCH] Add quick launcher (Ctrl/Cmd+K) for jumping to apps Adds a keyboard-driven overlay that searches the user's apps by title, tag and URL, shows recently opened apps first, and offers the configured web search provider as a fallback. A floating button opens it on touch devices. Items come from a new GET /search/items endpoint behind the allowed middleware, scoped to the current user and to roles when roles mode is on. The launcher is hidden on auth pages and on public-front dashboards until the visitor is logged in. --- app/Http/Controllers/SearchController.php | 64 ++++ app/Http/Middleware/CheckAllowed.php | 12 +- app/Item.php | 9 + app/User.php | 15 + lang/en/app.php | 6 + readme.md | 3 + resources/assets/js/index.js | 1 + resources/assets/js/quicklauncher.js | 304 ++++++++++++++++++ resources/assets/sass/_app.scss | 107 +++++- resources/views/item.blade.php | 6 +- resources/views/layouts/app.blade.php | 1 + .../views/partials/quicklauncher.blade.php | 26 ++ routes/web.php | 1 + tests/Feature/SearchItemsTest.php | 194 +++++++++++ 14 files changed, 733 insertions(+), 16 deletions(-) create mode 100644 resources/assets/js/quicklauncher.js create mode 100644 resources/views/partials/quicklauncher.blade.php create mode 100644 tests/Feature/SearchItemsTest.php diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 44b76de5b..85a0e6037 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -2,7 +2,9 @@ namespace App\Http\Controllers; +use App\Item; use App\Search; +use App\Setting; use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; @@ -12,6 +14,13 @@ class SearchController extends Controller { + public function __construct() + { + parent::__construct(); + // The item list exposes the user's apps, so it gets the dashboard gate + $this->middleware('allowed')->only('items'); + } + /** * @return Application|RedirectResponse|Redirector|mixed|void */ @@ -44,6 +53,61 @@ public function index(Request $request) abort(404, 'Provider type not supported'); } + /** + * All apps the current user can see, pinned or not, for the quick launcher + */ + public function items(Request $request): JsonResponse + { + $query = Item::where('type', 0)->with('parents')->orderBy('order', 'asc'); + + if (config('app.auth_roles_enable')) { + $roles = explode( + config('app.auth_roles_delimiter'), + $request->header(config('app.auth_roles_header')) ?? '' + ); + $query->whereIn('role', $roles); + } + + $items = $query->get()->map(function (Item $app) { + return [ + 'id' => $app->id, + 'title' => $app->title, + 'url' => $app->link, + 'colour' => $app->colour, + 'icon' => $app->icon_url, + 'tags' => $app->parents->where('id', '!=', 0)->pluck('title')->values(), + ]; + })->values(); + + // Web search fallback, unless the provider is the tile filter or search is off + $fallback = null; + if ((bool) Setting::fetch('homepage_search')) { + $provider = Setting::fetch('search_provider'); + $details = $provider && $provider !== 'tiles' && $provider !== 'none' + ? Search::providerDetails($provider) + : false; + if ($details) { + $fallback = ['provider' => $provider, 'name' => $details->name, 'url' => url('search')]; + } + } + + return response()->json([ + 'items' => $items, + 'fallback' => $fallback, + 'target' => $this->linkTarget(), + ]); + } + + /** + * The "Link opens in" setting as an anchor target + */ + private function linkTarget(): string + { + $target = Setting::fetch('window_target'); + + return in_array($target, [null, '', 'current'], true) ? '_self' : $target; + } + /** * Get autocomplete suggestions for a search query * diff --git a/app/Http/Middleware/CheckAllowed.php b/app/Http/Middleware/CheckAllowed.php index d243820ee..1bfc20438 100644 --- a/app/Http/Middleware/CheckAllowed.php +++ b/app/Http/Middleware/CheckAllowed.php @@ -37,19 +37,11 @@ public function handle(Request $request, Closure $next): Response } } - // Continue with passwordless user - if (empty($current_user->password)) { + // Continue with a passwordless user, or one the visitor is logged in as + if ($current_user->isOpenToVisitor()) { return $next($request); } - // Check if user is logged in as $current_user - if (Auth::check()) { - $loggedin_user = Auth::user(); - if ($loggedin_user->id === $current_user->getId()) { - return $next($request); - } - } - // Redirect to login Auth::authenticate(); return redirect()->route('user.select'); diff --git a/app/Item.php b/app/Item.php index e0d96cae0..d90bb84a0 100644 --- a/app/Item.php +++ b/app/Item.php @@ -37,6 +37,7 @@ * @property-read \Illuminate\Database\Eloquent\Collection|Item[] $children * @property-read int|null $children_count * @property-read string $droppable + * @property-read string $icon_url * @property-read \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\UrlGenerator|mixed|string $link * @property-read string $link_icon * @property-read string $link_target @@ -230,6 +231,14 @@ public function getDroppableAttribute(): string } } + /** The tile icon URL, or the default Heimdall icon when there is none */ + public function getIconUrlAttribute(): string + { + return $this->icon + ? asset('/storage/'.str_replace('supportedapps', 'icons', $this->icon)) + : asset('/img/heimdall-icon-small.png'); + } + public function getLinkTargetAttribute(): string { $target = Setting::fetch('window_target'); diff --git a/app/User.php b/app/User.php index 75322bad9..c09646097 100644 --- a/app/User.php +++ b/app/User.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Auth; // @codingStandardsIgnoreStart /** @@ -76,6 +77,20 @@ public function getId() return $this->id; } + /** + * Whether the current visitor may use this user's private pages: the user + * has no password, or the visitor is logged in as them. The "allowed" + * middleware and the views that must match it both use this. + */ + public function isOpenToVisitor(): bool + { + if (empty($this->password)) { + return true; + } + + return Auth::check() && Auth::user()->id === $this->getId(); + } + /** * Get the items for the user. */ diff --git a/lang/en/app.php b/lang/en/app.php index bb507cbcd..0e44a2e36 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -119,4 +119,10 @@ 'role' => 'Authentication role', 'unauthorized_for_form' => 'You are not authorized to view this form.', 'disabled_feature' => 'This feature is disabled.', + 'quicklauncher.title' => 'Quick launcher', + 'quicklauncher.open' => 'Open quick launcher', + 'quicklauncher.placeholder' => 'Search apps…', + 'quicklauncher.loading' => 'Loading…', + 'quicklauncher.error' => 'Could not load your apps', + 'quicklauncher.search_web' => 'Search :provider for “:query”', ); \ No newline at end of file diff --git a/readme.md b/readme.md index 652c05e89..3dba1330a 100644 --- a/readme.md +++ b/readme.md @@ -55,6 +55,9 @@ There is also a multi-arch Docker which supports x86-64, armhf and arm64, instru ## Updating To update your instance, simply clone this repository or download the zip/tar file with the new version and copy it over the old installation. +## Quick launcher +Press `Cmd+K` (`Ctrl+K` on Windows and Linux) on any page to open a quick launcher that finds any of your apps, pinned or not. Use the arrow keys and `Enter` to open one, or `Esc` to close it. Apps you open most recently are listed first (this history is kept in your browser, so it isn't shared between devices). If a default search provider is set for the homepage search, the launcher offers a web search for anything it can't find. On touch devices a search button in the bottom left corner opens it. + ## Search Providers v2.3.0 added the ability for users to customise the search options. diff --git a/resources/assets/js/index.js b/resources/assets/js/index.js index 2500fd62e..7b5a2d87f 100644 --- a/resources/assets/js/index.js +++ b/resources/assets/js/index.js @@ -4,6 +4,7 @@ import "./globals"; import "./jquery-ui.min"; import "./app"; import "./keyBindings"; +import "./quicklauncher"; import "./itemExport"; import "./itemImport"; import "./liveStatRefresh"; diff --git a/resources/assets/js/quicklauncher.js b/resources/assets/js/quicklauncher.js new file mode 100644 index 000000000..0b57db10d --- /dev/null +++ b/resources/assets/js/quicklauncher.js @@ -0,0 +1,304 @@ +// Quick launcher: Cmd/Ctrl+K (or the button on touch devices) opens an overlay +// that finds any app, pinned or not and on any page, and can fall back to a web +// search. The markup and strings come from resources/views/partials/quicklauncher. +const root = document.getElementById("quicklauncher"); + +if (root) { + const base = (document.querySelector("base") || {}).href || "/"; + const input = root.querySelector(".quicklauncher-input"); + const list = root.querySelector(".quicklauncher-list"); + const button = document.getElementById("quicklauncher-button"); + const { user, loading, error, noResults, searchWeb } = root.dataset; + const searchIcon = document.getElementById("quicklauncher-search-icon"); + + // Cmd on Apple platforms, where Ctrl+K is the system "kill line" shortcut + const isApple = /Mac|iPhone|iPad/.test(navigator.platform); + + // Per Heimdall user, as users can share a browser + const RECENT_KEY = `heimdall.quicklauncher.recent.${user}`; + const RECENT_MAX = 8; + const RESULTS_MAX = 8; + + let data = null; // { items, fallback, target } once /search/items has loaded + let status = "idle"; // idle | loading | loaded | failed (failed retries on open) + let opener = null; // what had focus before the launcher opened + let results = []; + let selected = 0; + + const isOpen = () => root.classList.contains("open"); + + const el = (tag, className, text) => { + const node = document.createElement(tag); + if (className) node.className = className; + if (text !== undefined) node.textContent = text; + return node; + }; + + // localStorage can be missing or throw (private windows, blocked storage), + // and the launcher must still work without it. + const loadRecent = () => { + try { + const ids = JSON.parse(localStorage.getItem(RECENT_KEY)); + return Array.isArray(ids) ? ids : []; + } catch (e) { + return []; + } + }; + + const saveRecent = (id) => { + try { + const ids = [id, ...loadRecent().filter((x) => x !== id)]; + localStorage.setItem( + RECENT_KEY, + JSON.stringify(ids.slice(0, RECENT_MAX)) + ); + } catch (e) { + // Nothing to do + } + }; + + const score = (item, q) => { + const title = item.title.toLowerCase(); + if (title === q) return 100; + if (title.startsWith(q)) return 80; + if (title.split(/[\s._-]+/).some((word) => word.startsWith(q))) return 60; + if (title.includes(q)) return 40; + if (item.tags.some((tag) => tag.toLowerCase().includes(q))) return 20; + if ((item.url || "").toLowerCase().includes(q)) return 10; + return 0; + }; + + const search = (q) => { + const query = q.trim().toLowerCase(); + const items = data ? data.items : []; + if (!query) { + // Recently opened first (apps since deleted are skipped), then the rest + // in dashboard order. + const recent = loadRecent() + .map((id) => items.find((item) => item.id === id)) + .filter(Boolean); + return recent.concat(items.filter((item) => !recent.includes(item))); + } + return items + .map((item) => ({ item, s: score(item, query) })) + .filter((r) => r.s > 0) + .sort((a, b) => b.s - a.s) + .map((r) => r.item) + .slice(0, RESULTS_MAX); + }; + + const close = (restoreFocus = true) => { + root.classList.remove("open"); + input.setAttribute("aria-expanded", "false"); + input.blur(); + if (restoreFocus && opener) opener.focus(); + opener = null; + }; + + // Follow a link the way a tile does, so the link-target setting behaves the + // same (including reusing a named window). + const go = (url, target) => { + close(false); + const link = el("a"); + link.href = url; + link.target = target; + link.rel = "noopener noreferrer"; + // Attached while clicking: some browsers ignore clicks on detached links. + document.body.appendChild(link); + link.click(); + link.remove(); + }; + + const activate = () => { + const row = results[selected]; + if (!row) return; + if (row.type === "app") { + saveRecent(row.item.id); + go(row.item.url, data.target); + } else { + const { fallback } = data; + const q = encodeURIComponent(input.value.trim()); + go( + `${fallback.url}?provider=${encodeURIComponent( + fallback.provider + )}&q=${q}`, + data.target + ); + } + }; + + const select = (index) => { + selected = index; + list.querySelectorAll(".quicklauncher-row").forEach((row, i) => { + row.classList.toggle("active", i === index); + row.setAttribute("aria-selected", i === index); + if (i === index) input.setAttribute("aria-activedescendant", row.id); + }); + }; + + const appRow = (item) => { + const icon = el("span", "quicklauncher-icon"); + icon.style.backgroundColor = item.colour || ""; + const img = el("img"); + img.src = item.icon; + img.alt = ""; + icon.appendChild(img); + return [ + icon, + el("span", "quicklauncher-title", item.title), + ...(item.tags.length + ? [el("span", "quicklauncher-tags", item.tags.join(", "))] + : []), + ]; + }; + + const webRow = (fallback, q) => { + const icon = el("span", "quicklauncher-icon"); + icon.appendChild(searchIcon.content.cloneNode(true)); + // Function replacers: the query is user input and may contain "$" patterns. + const text = searchWeb + .replace(":provider", () => fallback.name) + .replace(":query", () => q); + return [icon, el("span", "quicklauncher-title", text)]; + }; + + const render = () => { + const q = input.value.trim(); + results = search(q).map((item) => ({ type: "app", item })); + if (q && data && data.fallback) results.push({ type: "web" }); + selected = Math.min(selected, Math.max(results.length - 1, 0)); + list.textContent = ""; + input.removeAttribute("aria-activedescendant"); + + if (!results.length) { + const messages = { loaded: noResults, failed: error }; + list.appendChild( + el("div", "quicklauncher-empty", messages[status] || loading) + ); + return; + } + + results.forEach((row, i) => { + const li = el("div", "quicklauncher-row"); + li.id = `quicklauncher-option-${i}`; + li.setAttribute("role", "option"); + (row.type === "app" + ? appRow(row.item) + : webRow(data.fallback, q) + ).forEach((node) => li.appendChild(node)); + li.dataset.index = i; + list.appendChild(li); + }); + select(selected); + }; + + const load = () => { + status = "loading"; + fetch(`${base}search/items`, { + headers: { Accept: "application/json" }, + credentials: "same-origin", + }) + .then((response) => { + // A login redirect ends up as an HTML page, which is not usable. + const type = response.headers.get("content-type") || ""; + if (!response.ok || !type.includes("json")) throw new Error(); + return response.json(); + }) + .then((json) => { + data = json; + status = "loaded"; + }) + .catch(() => { + status = "failed"; + }) + .then(() => { + if (isOpen()) render(); + }); + }; + + const open = () => { + // Reordering mode: tiles are being dragged, so stay out of the way + if (document.getElementById("app").classList.contains("header")) return; + if (status === "idle" || status === "failed") load(); + opener = document.activeElement; + input.value = ""; + selected = 0; + root.classList.add("open"); + input.setAttribute("aria-expanded", "true"); + render(); + input.focus(); + }; + + root.addEventListener("mousedown", (e) => { + if (e.target === root) close(); + }); + + input.addEventListener("input", () => { + selected = 0; + render(); + }); + + // One pair of listeners for all rows + const rowIndex = (e) => { + const row = e.target.closest(".quicklauncher-row"); + return row ? Number(row.dataset.index) : -1; + }; + + // Scrolling the list with the arrow keys moves rows under a resting pointer, + // and some browsers report that as a mouse move. Only follow a real move. + let pointer = { x: -1, y: -1 }; + list.addEventListener("mousemove", (e) => { + if (e.clientX === pointer.x && e.clientY === pointer.y) return; + pointer = { x: e.clientX, y: e.clientY }; + const i = rowIndex(e); + if (i >= 0 && i !== selected) select(i); + }); + + list.addEventListener("click", (e) => { + const i = rowIndex(e); + if (i < 0) return; + selected = i; + activate(); + }); + + button.appendChild(searchIcon.content.cloneNode(true)); + button.addEventListener("click", () => open()); + + // Capture phase, so keys typed into the overlay never reach the page-level + // handlers (keyBindings.js steals focus on "/", app.js hides things on Esc). + document.addEventListener( + "keydown", + (e) => { + if ( + (isApple ? e.metaKey : e.ctrlKey) && + !e.altKey && + (e.key || "").toLowerCase() === "k" + ) { + e.preventDefault(); + e.stopPropagation(); + if (isOpen()) close(); + else open(); + return; + } + if (!isOpen()) return; + e.stopPropagation(); + if (e.key === "Tab") { + // The input is the only control, so keep focus from leaving the dialog + e.preventDefault(); + } else if (e.key === "Escape") { + e.preventDefault(); + close(); + } else if (e.key === "ArrowDown" || e.key === "ArrowUp") { + e.preventDefault(); + const n = results.length; + if (!n) return; + select((selected + (e.key === "ArrowDown" ? 1 : n - 1)) % n); + list.querySelector(".active").scrollIntoView({ block: "nearest" }); + } else if (e.key === "Enter" && !e.isComposing) { + e.preventDefault(); + activate(); + } + }, + true + ); +} diff --git a/resources/assets/sass/_app.scss b/resources/assets/sass/_app.scss index 7e9d4ae52..6f05f0010 100644 --- a/resources/assets/sass/_app.scss +++ b/resources/assets/sass/_app.scss @@ -1263,4 +1263,109 @@ select:-webkit-autofill:focus { color: $app-red; } } -} \ No newline at end of file +} + +// Quick launcher (Cmd/Ctrl+K) +.quicklauncher { + display: none; + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; + z-index: 2000; + background: rgba(0, 0, 0, 0.45); + padding-top: 14vh; + &.open { + display: block; + } + .quicklauncher-box { + width: 90%; + max-width: 600px; + margin: 0 auto; + background: white; + border-radius: 10px; + overflow: hidden; + box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35); + } + .quicklauncher-input { + display: block; + width: 100%; + box-sizing: border-box; + height: 56px; + padding: 0 20px; + font-size: 20px; + border: 0; + outline: none; + color: $app-text; + } + .quicklauncher-list { + max-height: 50vh; + overflow-y: auto; + border-top: 1px solid #f0f0f0; + } + .quicklauncher-row { + display: flex; + align-items: center; + gap: 12px; + padding: 10px 20px; + cursor: pointer; + color: $app-text; + &.active { + background: #eef1f6; + } + } + .quicklauncher-icon { + flex: none; + display: flex; + align-items: center; + justify-content: center; + width: 32px; + height: 32px; + border-radius: 6px; + img { + max-width: 22px; + max-height: 22px; + } + svg { + width: 18px; + height: 18px; + } + } + .quicklauncher-title { + flex: 1; + font-size: 16px; + } + .quicklauncher-tags { + font-size: 12px; + color: #8a8d99; + } + .quicklauncher-empty { + padding: 16px 20px; + color: #8a8d99; + } +} + +// Opens the quick launcher where there is no keyboard shortcut (touch devices) +.quicklauncher-button { + display: none; + position: fixed; + bottom: 0; + left: 0; + width: 50px; + height: 50px; + padding: 0; + border: 1px solid transparent; + background: rgba(0, 0, 0, 0.8); + color: white; + cursor: pointer; + align-items: center; + justify-content: center; + @media (hover: none) { + display: flex; + } + // Reorder mode has its own controls + #app.header & { + display: none; + } +} diff --git a/resources/views/item.blade.php b/resources/views/item.blade.php index df4c7285f..25d80b9de 100644 --- a/resources/views/item.blade.php +++ b/resources/views/item.blade.php @@ -1,11 +1,7 @@
- @if($app->icon) - - @else - - @endif +
{{ $app->title }}
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index f53f47055..b82a125d3 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -119,6 +119,7 @@
+ @include('partials.quicklauncher')
@vite('resources/assets/js/index.js') diff --git a/resources/views/partials/quicklauncher.blade.php b/resources/views/partials/quicklauncher.blade.php new file mode 100644 index 000000000..f3e931aef --- /dev/null +++ b/resources/views/partials/quicklauncher.blade.php @@ -0,0 +1,26 @@ +{{-- Quick launcher overlay (Cmd/Ctrl+K), driven by resources/assets/js/quicklauncher.js. + The strings are handed to the script as data attributes so they are translated. --}} +{{-- Only for visitors /search/items is open to: it is guarded by the "allowed" + middleware, which asks User::isOpenToVisitor(), minus the public_front + exemption that applies to the dashboard but not here. So a public dashboard + shows nothing until the visitor has logged in. Not on the login pages. --}} +@if(!Route::is('login', 'register', 'password.*') && $current_user->isOpenToVisitor()) +{{-- The bundled Font Awesome has no search icon; the script copies this one into the button and the web search row. --}} + + + +@endif diff --git a/routes/web.php b/routes/web.php index 24836df25..61ced77da 100644 --- a/routes/web.php +++ b/routes/web.php @@ -75,6 +75,7 @@ Route::get('get_stats/{id}', [ItemController::class,'getStats'])->name('get_stats'); Route::get('/search', [SearchController::class,'index'])->name('search'); +Route::get('/search/items', [SearchController::class,'items'])->name('search.items'); Route::get('/search/autocomplete', [SearchController::class,'autocomplete'])->name('search.autocomplete'); Route::get('view/{name_view}', function ($name_view) { diff --git a/tests/Feature/SearchItemsTest.php b/tests/Feature/SearchItemsTest.php new file mode 100644 index 000000000..64cdc4b31 --- /dev/null +++ b/tests/Feature/SearchItemsTest.php @@ -0,0 +1,194 @@ +create($attributes); + + ItemTag::factory()->create([ + 'item_id' => $item->id, + 'tag_id' => 0, + ]); + + return $item; + } + + private function itemTitles($response): array + { + return array_column($response->json('items'), 'title'); + } + + public function test_lists_pinned_and_unpinned_apps(): void + { + $this->seed(); + $this->addItem(['title' => 'Pinned app', 'pinned' => 1]); + $this->addItem(['title' => 'Unpinned app', 'pinned' => 0]); + + $response = $this->getJson('/search/items'); + + $response->assertOk(); + $this->assertEqualsCanonicalizing(['Pinned app', 'Unpinned app'], $this->itemTitles($response)); + } + + public function test_leaves_out_tags(): void + { + $this->seed(); + $this->addItem(['title' => 'An app']); + Item::factory()->create(['title' => 'A tag', 'type' => 1]); + + $this->assertSame(['An app'], $this->itemTitles($this->getJson('/search/items'))); + } + + public function test_includes_tag_titles_but_not_the_home_dashboard(): void + { + $this->seed(); + $tag = Item::factory()->create(['title' => 'Media', 'type' => 1]); + $item = $this->addItem(['title' => 'An app']); + ItemTag::factory()->create(['item_id' => $item->id, 'tag_id' => $tag->id]); + + $this->assertSame(['Media'], $this->getJson('/search/items')->json('items.0.tags')); + } + + public function test_leaves_out_other_users_apps(): void + { + $this->seed(); + $other = User::factory()->create(); + $this->addItem(['title' => 'Mine']); + $this->addItem(['title' => 'Theirs', 'user_id' => $other->id]); + + $this->assertSame(['Mine'], $this->itemTitles($this->getJson('/search/items'))); + } + + public function test_requires_login_for_a_password_protected_user(): void + { + $this->seed(); + $this->addItem(['title' => 'Secret']); + $user = User::find(1); + $user->update(['password' => bcrypt('secret')]); + // The session user was cached while booting, before the password was set + $this->withSession(['current_user' => $user->fresh()]); + + $this->get('/search/items')->assertRedirect(route('login')); + $this->getJson('/search/items')->assertUnauthorized(); + } + + public function test_roles_mode_only_lists_apps_for_the_callers_roles(): void + { + $this->seed(); + config([ + 'app.auth_roles_enable' => true, + 'app.auth_roles_header' => 'X-Roles', + 'app.auth_roles_delimiter' => ',', + ]); + $this->addItem(['title' => 'For admins', 'role' => 'admin']); + $this->addItem(['title' => 'For family', 'role' => 'family']); + + $response = $this->getJson('/search/items', ['X-Roles' => 'family,guest']); + + $this->assertSame(['For family'], $this->itemTitles($response)); + } + + public function test_launcher_is_on_the_dashboard_but_not_the_login_page(): void + { + $this->seed(); + + $this->get('/')->assertSee('id="quicklauncher"', false); + $this->get('/login')->assertOk()->assertDontSee('id="quicklauncher"', false); + } + + public function test_public_dashboard_hides_the_launcher_until_login(): void + { + $this->seed(); + $user = User::find(1); + $user->forceFill(['password' => bcrypt('secret'), 'public_front' => 1])->save(); + $this->withSession(['current_user' => $user->fresh()]); + + $this->get('/')->assertOk()->assertDontSee('id="quicklauncher"', false); + + $this->actingAs($user)->get('/')->assertSee('id="quicklauncher"', false); + } + + public function test_roles_mode_lists_nothing_without_a_roles_header(): void + { + $this->seed(); + config([ + 'app.auth_roles_enable' => true, + 'app.auth_roles_header' => 'X-Roles', + 'app.auth_roles_delimiter' => ',', + ]); + $this->addItem(['title' => 'For admins', 'role' => 'admin']); + + $this->assertSame([], $this->itemTitles($this->getJson('/search/items'))); + } + + public function test_icon_urls_match_the_tiles(): void + { + $this->seed(); + $this->addItem(['title' => 'With icon', 'icon' => 'supportedapps/radarr.svg']); + $this->addItem(['title' => 'Without icon', 'icon' => null]); + + $icons = array_column($this->getJson('/search/items')->json('items'), 'icon', 'title'); + + $this->assertSame(asset('/storage/icons/radarr.svg'), $icons['With icon']); + $this->assertSame(asset('/img/heimdall-icon-small.png'), $icons['Without icon']); + } + + public function test_link_target_other_than_current_is_passed_through(): void + { + $this->seed(); + Setting::where('key', 'window_target')->update(['value' => '_blank']); + + $this->assertSame('_blank', $this->getJson('/search/items')->json('target')); + } + + public function test_link_target_current_maps_to_self(): void + { + $this->seed(); + Setting::where('key', 'window_target')->update(['value' => 'current']); + + $this->assertSame('_self', $this->getJson('/search/items')->json('target')); + } + + public function test_offers_the_homepage_search_provider_as_web_fallback(): void + { + $this->seed(); + Setting::where('key', 'homepage_search')->update(['value' => 1]); + Setting::where('key', 'search_provider')->update(['value' => 'google']); + + $fallback = $this->getJson('/search/items')->json('fallback'); + + $this->assertSame('google', $fallback['provider']); + $this->assertSame(url('search'), $fallback['url']); + } + + public function test_has_no_web_search_fallback_when_the_provider_is_the_tile_filter(): void + { + $this->seed(); + Setting::where('key', 'homepage_search')->update(['value' => 1]); + Setting::where('key', 'search_provider')->update(['value' => 'tiles']); + + $this->assertNull($this->getJson('/search/items')->json('fallback')); + } + + public function test_has_no_web_search_fallback_when_homepage_search_is_off(): void + { + $this->seed(); + + $this->assertNull($this->getJson('/search/items')->json('fallback')); + } +}