From 66654cb0cdbbadc67e491ba93402b9cd7d6b612b Mon Sep 17 00:00:00 2001 From: Quentin DECAUNES Date: Tue, 4 Jun 2019 12:23:28 +0200 Subject: [PATCH] Regroup presets functions to a preset.js file. --- index.html | 2 +- js/app.js | 2 +- js/methods.js | 118 ------------------------------------------------- js/preset.js | 120 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 121 insertions(+), 121 deletions(-) diff --git a/index.html b/index.html index 70fb84a..d618823 100644 --- a/index.html +++ b/index.html @@ -237,7 +237,7 @@
- + diff --git a/js/app.js b/js/app.js index b6de1b6..c918cfc 100644 --- a/js/app.js +++ b/js/app.js @@ -15,7 +15,7 @@ ready(function(){ recreateShortcut(); } handlePlatformSpecificDisplay(); - updatePresetList(); + preset_updateList(); checkForNewRelease(); document.isStarting = false; diff --git a/js/methods.js b/js/methods.js index 3783a0e..beede3c 100644 --- a/js/methods.js +++ b/js/methods.js @@ -439,124 +439,6 @@ function getCurrentSettings(keyType) { } } -/** - * Will save the current settings to a new preset. - */ -function saveToNewPreset() { - const settingsToBeSaved = getCurrentSettings("inputId"); - const currentPresets = require('electron-settings').get('presets') || {}; - var newPresetName = document.getElementById('new_preset_name').value; - - if (!newPresetName) { - notification('danger', 'You must provide a preset name.'); - return; - } - - if (typeof currentPresets[newPresetName] !== "undefined") { - newPresetName = findUnusedNewPresetName(newPresetName); - notification('warning', `This preset name already exist, your preset has been saved with the name "${newPresetName}".`); - } - - const newPresetList = Object.assign( - {}, - currentPresets, - { [newPresetName]: settingsToBeSaved } - ); - - require('electron-settings').set('presets', newPresetList); - appendLog(`saveToNewPreset(): Saved preset ${newPresetName}, ${JSON.stringify(newPresetList)}`); - updatePresetList(); - if (newPresetName === document.getElementById('new_preset_name').value) { - notification('success', `The preset ${newPresetName} has been saved.`); - } -} - -/** - * This recursive function will return an available preset name to be used to save a preset. - * - * @param {string} newPresetName The preset name to be edited. - * @param {number} suffix The preset name suffix - */ -function findUnusedNewPresetName(newPresetName, suffix = 1) { - const currentPresets = require('electron-settings').get('presets') || {}; - if (typeof currentPresets[`${newPresetName}${suffix}`] !== "undefined") { - suffix++; - return findUnusedNewPresetName(newPresetName, suffix); - } - return `${newPresetName}${suffix}`; -} - -/** - * This will update the preset tab based on saved presets. - */ -function updatePresetList() { - var presetTab = document.getElementById('presetTab'); - const currentPresets = require('electron-settings').get('presets') || {}; - - var content = ''; - content += ''; - presetTab.innerHTML = content; -} - -/** - * This will apply the preset you asked for. - * @param {string} presetName The preset name to be applied. - */ -function applyPreset(presetName) { - const presets = require('electron-settings').get(`presets.${presetName}`); - appendLog(`applyPreset(): preset ${presetName}: ${JSON.stringify(presets)}`); - var ret = require('electron-settings').set("latest_controller_tabs_settings", presets); - appendLog(`applyPreset(): saved preset: ${JSON.stringify(ret)}`); - loadLatestUsedSettings(); - applyRyzenSettings(); - toggleOptionDisplayBasedOnApplyCheckbox(); -} - -/** - * This will delete the preset you asked for. - * @param {string} presetName The preset name to be deleted. - */ -function presetDeletion(presetName) { - var presets = require('electron-settings').get(`presets`); - delete presets[presetName]; - require('electron-settings').set(`presets`, presets); - notification('success', `The preset ${presetName} has been deleted.`); - updatePresetList(); -} - /** * Will check for new release. */ diff --git a/js/preset.js b/js/preset.js index ca4bfa5..54834fa 100644 --- a/js/preset.js +++ b/js/preset.js @@ -27,6 +27,124 @@ function preset_import() { presetsToBeImported ); settings.set('presets', updatedPresets); - updatePresetList(); + preset_updateList(); modalTextArea.innerText = ''; } + +/** + * Will save the current settings to a new preset. + */ +function preset_createNewPreset() { + const settingsToBeSaved = getCurrentSettings("inputId"); + const currentPresets = require('electron-settings').get('presets') || {}; + var newPresetName = document.getElementById('new_preset_name').value; + + if (!newPresetName) { + notification('danger', 'You must provide a preset name.'); + return; + } + + if (typeof currentPresets[newPresetName] !== "undefined") { + newPresetName = preset_findUnusedPresetName(newPresetName); + notification('warning', `This preset name already exist, your preset has been saved with the name "${newPresetName}".`); + } + + const newPresetList = Object.assign( + {}, + currentPresets, + { [newPresetName]: settingsToBeSaved } + ); + + require('electron-settings').set('presets', newPresetList); + appendLog(`preset_createNewPreset(): Saved preset ${newPresetName}, ${JSON.stringify(newPresetList)}`); + preset_updateList(); + if (newPresetName === document.getElementById('new_preset_name').value) { + notification('success', `The preset ${newPresetName} has been saved.`); + } +} + +/** + * This recursive function will return an available preset name to be used to save a preset. + * + * @param {string} newPresetName The preset name to be edited. + * @param {number} suffix The preset name suffix + */ +function preset_findUnusedPresetName(newPresetName, suffix = 1) { + const currentPresets = require('electron-settings').get('presets') || {}; + if (typeof currentPresets[`${newPresetName}${suffix}`] !== "undefined") { + suffix++; + return preset_findUnusedPresetName(newPresetName, suffix); + } + return `${newPresetName}${suffix}`; +} + +/** + * This will update the preset tab based on saved presets. + */ +function preset_updateList() { + var presetTab = document.getElementById('presetTab'); + const currentPresets = require('electron-settings').get('presets') || {}; + + var content = ''; + content += ''; + presetTab.innerHTML = content; +} + +/** + * This will apply the preset you asked for. + * @param {string} presetName The preset name to be applied. + */ +function preset_apply(presetName) { + const presets = require('electron-settings').get(`presets.${presetName}`); + appendLog(`preset_apply(): preset ${presetName}: ${JSON.stringify(presets)}`); + var ret = require('electron-settings').set("latest_controller_tabs_settings", presets); + appendLog(`preset_apply(): saved preset: ${JSON.stringify(ret)}`); + loadLatestUsedSettings(); + applyRyzenSettings(); + toggleOptionDisplayBasedOnApplyCheckbox(); +} + +/** + * This will delete the preset you asked for. + * @param {string} presetName The preset name to be deleted. + */ +function preset_deletion(presetName) { + var presets = require('electron-settings').get(`presets`); + delete presets[presetName]; + require('electron-settings').set(`presets`, presets); + notification('success', `The preset ${presetName} has been deleted.`); + preset_updateList(); +}