/** * Will fill the export preset modal textarea with the preset data. */ function preset_export() { const modalTextArea = document.getElementById('modal-export-preset-textarea'); const settings = require('electron-settings'); var presets = settings.get('presets'); presets = JSON.stringify(presets); modalTextArea.innerHTML = btoa(unescape(encodeURIComponent(presets))); } /** * Will import the preset from the export preset modal textarea. */ function preset_import() { const modalTextArea = document.getElementById('modal-import-preset-textarea'); const settings = require('electron-settings'); var currentPresets = settings.get('presets'); var presetsToBeImported; try { presetsToBeImported = decodeURIComponent(escape(atob(modalTextArea.value))); presetsToBeImported = JSON.parse(presetsToBeImported); } catch (e) { notification('danger', 'Unable to import presets, malformed data.'); appendLog(`preset_import() ${e}`); return; } var updatedPresets = Object.assign( {}, currentPresets, presetsToBeImported ); settings.set('presets', updatedPresets); 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}`; } /** * Will save the preset to be enabled on AC plugged out. */ function preset_enableAutoApplyOnAcStatusChange(statusName, presetName) { const settings = require('electron-settings'); const status = { "update-ac-plugged-in": `will be applied on AC plugged in.`, "update-ac-plugged-out": `will be applied on AC plugged out.`, }; if (typeof status[statusName] === "undefined") { let message = `Error while updating auto apply on AC status change.`; notification('danger', message); console.log(`preset_enableAutoApplyOnAcStatusChange(statusName:"${statusName}", presetName:"${presetName}")`); Sentry.captureException(new Error(message)); return; } settings.set(`auto-apply.${statusName}`, presetName); if (presetName) { notification('primary', `Preset "${presetName}" ${status[statusName]}`); } else { notification('primary', `No preset ${status[statusName]}`); } } /** * 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 += /*html*/`
Name | Apply on | Action | |
---|---|---|---|
${presetName} ${valueSummary} | |||
Disable auto apply |