From 20520100f259bc174533fcd959c65550e2f7f074 Mon Sep 17 00:00:00 2001
From: marv <rotrobmin@gmail.com>
Date: Thu, 8 Feb 2024 04:00:35 -0800
Subject: Search Page Profile Switching (#648)

* Add profile select dropdown

* Remove right align on profile switcher in search

* Fix API access

* Don't cache optionsFull

---------

Co-authored-by: Darius Jahandarie <djahandarie@gmail.com>
---
 ext/js/display/search-display-controller.js | 55 +++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

(limited to 'ext/js')

diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js
index 49c69520..ed2061e2 100644
--- a/ext/js/display/search-display-controller.js
+++ b/ext/js/display/search-display-controller.js
@@ -115,6 +115,10 @@ export class SearchDisplayController {
         if (displayOptions !== null) {
             this._onDisplayOptionsUpdated({options: displayOptions});
         }
+
+        const {profiles, profileCurrent} = await this._display.application.api.optionsGetFull();
+
+        this._updateProfileSelect(profiles, profileCurrent);
     }
 
     /**
@@ -313,6 +317,33 @@ export class SearchDisplayController {
         this._updateClipboardMonitorEnabled();
     }
 
+    /**
+     * @param {Event} event
+     */
+    async _onProfileSelectChange(event) {
+        const node = /** @type {HTMLInputElement} */ (event.currentTarget);
+        const value = parseInt(node.value, 10);
+        const optionsFull = await this._display.application.api.optionsGetFull();
+        if (typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= optionsFull.profiles.length) {
+            this._setPrimaryProfileIndex(value);
+        }
+    }
+
+    /**
+     * @param {number} value
+     */
+    async _setPrimaryProfileIndex(value) {
+        /** @type {import('settings-modifications').ScopedModificationSet} */
+        const modification = {
+            action: 'set',
+            path: 'profileCurrent',
+            value,
+            scope: 'global',
+            optionsContext: null
+        };
+        await this._display.application.api.modifySettings([modification], 'search');
+    }
+
     /**
      * @param {boolean} enabled
      */
@@ -546,4 +577,28 @@ export class SearchDisplayController {
         if (element instanceof HTMLElement && element.isContentEditable) { return true; }
         return false;
     }
+
+    /**
+     * @param {import('settings').Profile[]} profiles
+     * @param {number} profileCurrent
+     */
+    _updateProfileSelect(profiles, profileCurrent) {
+        /** @type {HTMLSelectElement} */
+        const select = querySelectorNotNull(document, '#profile-select');
+        /** @type {HTMLElement} */
+        const optionGroup = querySelectorNotNull(document, '#profile-select-option-group');
+        const fragment = document.createDocumentFragment();
+        for (let i = 0, ii = profiles.length; i < ii; ++i) {
+            const {name} = profiles[i];
+            const option = document.createElement('option');
+            option.textContent = name;
+            option.value = `${i}`;
+            fragment.appendChild(option);
+        }
+        optionGroup.textContent = '';
+        optionGroup.appendChild(fragment);
+        select.value = `${profileCurrent}`;
+
+        select.addEventListener('change', this._onProfileSelectChange.bind(this), false);
+    }
 }
-- 
cgit v1.2.3