From 7c39cce563378f812414d0bc3b4193d6612c27d2 Mon Sep 17 00:00:00 2001 From: Ivan Efimov Date: Tue, 20 Sep 2022 01:23:03 -0500 Subject: [PATCH] Presets: favorite presets --- src/tabs/presets/FavoritePresets.js | 114 ++++++++++++++++++++++++++++ src/tabs/presets/presets.js | 26 ++++++- 2 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/tabs/presets/FavoritePresets.js diff --git a/src/tabs/presets/FavoritePresets.js b/src/tabs/presets/FavoritePresets.js new file mode 100644 index 00000000000..aed98e2c527 --- /dev/null +++ b/src/tabs/presets/FavoritePresets.js @@ -0,0 +1,114 @@ + +const s_maxFavoritePresetsCount = 50; +const s_favoritePresetsListConfigStorageName = "FavoritePresetsList"; + + +class FavoritePreset { + constructor(presetPath){ + this.presetPath = presetPath; + this.lastPickDate = Date.now(); + } +} + + +class FavoritePresetsData { + constructor() { + this._favoritePresetsList = []; + } + + _sort() { + this._favoritePresetsList.sort((a, b) => (a.lastPickDate > b.lastPickDate) ? 1 : -1); + } + + _purgeOldPresets() { + this._favoritePresetsList.splice(s_maxFavoritePresetsCount, this._favoritePresetsList.length); + } + + loadFromStorage() { + this._favoritePresetsList = []; + const obj = ConfigStorage.get(s_favoritePresetsListConfigStorageName); + + if (obj.FavoritePresetsList) { + this._favoritePresetsList = obj[s_favoritePresetsListConfigStorageName]; + } + } + + saveToStorage() { + ConfigStorage.set({s_favoritePresetsListConfigStorageName: this._favoritePresetsList}); + } + + presetPicked(presetPath) { + let preset = this.findPreset(presetPath); + + if (null === preset) { + preset = new FavoritePreset(presetPath); + this._favoritePresetsList.push(preset); + } + + preset.lastPickDate = Date.now(); + this._sort(); + this._purgeOldPresets(); + + return preset; + } + + findPresetIndex(presetPath) { + for (let i = 0; i < this._favoritePresetsList.length; i++) { + if (this._favoritePresetsList[i].presetPath === presetPath) { + return i; + } + } + + return -1; + } + + findPreset(presetPath) { + const index = this.findPresetIndex(presetPath); + + if (index >= 0) { + return this._favoritePresetsList[index]; + } + + return null; + } +} + + +class FavoritePresetsClass { + constructor() { + this._favoritePresetsData = new FavoritePresetsData(); + } + + presetPicked(preset) { + const favoritePreset = this._favoritePresetsData.presetPicked(preset.fullPath); + preset.lastPickDate = favoritePreset.lastPickDate; + } + + addLastPickDate(presets) { + for (let i = 0; i < presets.length; i++) { + let favoritePreset = this._favoritePresetsData.findPreset(presets[i].fullPath); + + if (favoritePreset) { + presets[i].lastPickDate = favoritePreset.lastPickDate; + } + } + } + + saveToStorage() { + this._favoritePresetsData.saveToStorage(); + } + + loadFromStorage() { + this._favoritePresetsData.loadFromStorage(); + } +} + + +let favoritePresets; // for export as singleton + +if (!favoritePresets) { + favoritePresets = new FavoritePresetsClass(); + favoritePresets.loadFromStorage(); +} + +export { favoritePresets }; diff --git a/src/tabs/presets/presets.js b/src/tabs/presets/presets.js index 21e967428a3..13f25f4de46 100644 --- a/src/tabs/presets/presets.js +++ b/src/tabs/presets/presets.js @@ -1,5 +1,7 @@ 'use strict'; +import { favoritePresets } from './FavoritePresets'; + const presets = { presetsRepo: null, cliEngine: null, @@ -82,6 +84,7 @@ presets.onSaveClick = function() { this.activateCli().then(() => { const cliCommandsArray = this.getPickedPresetsCli(); + this.markedPickedPresetsAsFavorites(); this.cliEngine.executeCommandsArray(cliCommandsArray).then(() => { const newCliErrorsCount = this.cliEngine.errorsCount; @@ -98,6 +101,14 @@ presets.onSaveClick = function() { }); }; +presets.markedPickedPresetsAsFavorites = function() { + for(const pickedPreset of this.pickedPresetList) { + favoritePresets.presetPicked(pickedPreset.preset); + } + + favoritePresets.saveToStorage(); +} + presets.disconnectCliMakeSure = function() { GUI.timeout_add('disconnect', function () { $('div.connect_controls a.connect').trigger( "click" ); @@ -307,6 +318,7 @@ presets.tryLoadPresets = function() { this.presetsRepo.loadIndex() .then(() => this.checkPresetSourceVersion()) .then(() => { + favoritePresets.addLastPickDate(this.presetsRepo.index.presets); this.prepareFilterFields(); this._divGlobalLoading.toggle(false); this._divMainContent.toggle(true); @@ -465,11 +477,23 @@ presets.getFitPresets = function(searchParams) { } } - result.sort((a, b) => (a.priority > b.priority) ? -1 : 1); + result.sort((a, b) => this.presetSearchPriorityComparer(a,b)); return result; }; +presets.presetSearchPriorityComparer = function(presetA, presetB) { + if (presetA.lastPickDate && presetB.lastPickDate) { + return (presetA.lastPickDate > presetB.lastPickDate) ? -1 : 1; + } + + if (presetA.lastPickDate || presetB.lastPickDate) { + return (presetA.lastPickDate) ? -1 : 1; + } + + return (presetA.priority > presetB.priority) ? -1 : 1; +} + presets.isPresetFitSearchStatuses = function(preset, searchParams) { return 0 === searchParams.status.length || searchParams.status.includes(preset.status); };