Skip to content

Commit

Permalink
Presets: favorite presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Efimov committed Sep 22, 2022
1 parent e0692c1 commit 6266f29
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
99 changes: 99 additions & 0 deletions src/tabs/presets/FavoritePresets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

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));
}

_purgeOldPresets() {
this._favoritePresetsList.splice(s_maxFavoritePresetsCount + 1, this._favoritePresetsList.length);
}

loadFromStorage() {
this._favoritePresetsList = [];
const obj = ConfigStorage.get(s_favoritePresetsListConfigStorageName);

if (obj[s_favoritePresetsListConfigStorageName]) {
this._favoritePresetsList = obj[s_favoritePresetsListConfigStorageName];
}
}

saveToStorage() {
const obj = {};
obj[s_favoritePresetsListConfigStorageName] = this._favoritePresetsList;
ConfigStorage.set(obj);
}

add(presetPath) {
let preset = this.findPreset(presetPath);

if (!preset) {
preset = new FavoritePreset(presetPath);
this._favoritePresetsList.push(preset);
}

preset.lastPickDate = Date.now();
this._sort();
this._purgeOldPresets();

return preset;
}

findPreset(presetPath) {
return this._favoritePresetsList.find((preset) => preset.presetPath === presetPath);
}
}


class FavoritePresetsClass {
constructor() {
this._favoritePresetsData = new FavoritePresetsData();
}

add(preset) {
const favoritePreset = this._favoritePresetsData.add(preset.fullPath);
preset.lastPickDate = favoritePreset.lastPickDate;
}

addLastPickDate(presets) {
for (let preset of presets) {
let favoritePreset = this._favoritePresetsData.findPreset(preset.fullPath);

if (favoritePreset) {
preset.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 };
26 changes: 25 additions & 1 deletion src/tabs/presets/presets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import { favoritePresets } from './FavoritePresets';

const presets = {
presetsRepo: null,
cliEngine: null,
Expand Down Expand Up @@ -82,6 +84,7 @@ presets.onSaveClick = function() {

this.activateCli().then(() => {
const cliCommandsArray = this.getPickedPresetsCli();
this.markPickedPresetsAsFavorites();
this.cliEngine.executeCommandsArray(cliCommandsArray).then(() => {
const newCliErrorsCount = this.cliEngine.errorsCount;

Expand All @@ -98,6 +101,14 @@ presets.onSaveClick = function() {
});
};

presets.markPickedPresetsAsFavorites = function() {
for(const pickedPreset of this.pickedPresetList) {
favoritePresets.add(pickedPreset.preset);
}

favoritePresets.saveToStorage();
};

presets.disconnectCliMakeSure = function() {
GUI.timeout_add('disconnect', function () {
$('div.connect_controls a.connect').trigger( "click" );
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 presetB.lastPickDate - presetA.lastPickDate;
}

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);
};
Expand Down

0 comments on commit 6266f29

Please sign in to comment.