SyncedTabsDeckStore.sys.mjs (1645B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { EventEmitter } from "resource:///modules/syncedtabs/EventEmitter.sys.mjs"; 6 7 /** 8 * SyncedTabsDeckStore 9 * 10 * This store keeps track of the deck view state, including the panels and which 11 * one is selected. The view listens for change events on the store, which are 12 * triggered whenever the state changes. If it's a small change, the state 13 * will have `isUpdatable` set to true so the view can skip rerendering the whole 14 * DOM. 15 */ 16 export function SyncedTabsDeckStore() { 17 EventEmitter.call(this); 18 this._panels = []; 19 } 20 21 Object.assign(SyncedTabsDeckStore.prototype, EventEmitter.prototype, { 22 _change(isUpdatable = false) { 23 let panels = this._panels.map(panel => { 24 return { id: panel, selected: panel === this._selectedPanel }; 25 }); 26 this.emit("change", { panels, isUpdatable }); 27 }, 28 29 /** 30 * Sets the selected panelId and triggers a change event. 31 * 32 * @param {string} panelId - ID of the panel to select. 33 */ 34 selectPanel(panelId) { 35 if (!this._panels.includes(panelId) || this._selectedPanel === panelId) { 36 return; 37 } 38 this._selectedPanel = panelId; 39 this._change(true); 40 }, 41 42 /** 43 * Update the set of panels in the deck and trigger a change event. 44 * 45 * @param {Array} panels - an array of IDs for each panel in the deck. 46 */ 47 setPanels(panels) { 48 if (panels === this._panels) { 49 return; 50 } 51 this._panels = panels || []; 52 this._change(); 53 }, 54 });