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 7c39cce
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
114 changes: 114 additions & 0 deletions src/tabs/presets/FavoritePresets.js
Original file line number Diff line number Diff line change
@@ -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 };
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.markedPickedPresetsAsFavorites();
this.cliEngine.executeCommandsArray(cliCommandsArray).then(() => {
const newCliErrorsCount = this.cliEngine.errorsCount;

Expand All @@ -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" );
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 (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);
};
Expand Down

0 comments on commit 7c39cce

Please sign in to comment.