2019-07-05 15:06:10 +07:00
|
|
|
/**
|
|
|
|
* Will fill the export preset modal textarea with the preset data.
|
|
|
|
*/
|
2019-06-04 16:37:03 +07:00
|
|
|
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);
|
2019-08-14 22:46:53 +07:00
|
|
|
modalTextArea.innerHTML = btoa(unescape(encodeURIComponent(presets)));
|
2019-06-04 16:37:03 +07:00
|
|
|
}
|
|
|
|
|
2019-07-05 15:06:10 +07:00
|
|
|
/**
|
|
|
|
* Will import the preset from the export preset modal textarea.
|
|
|
|
*/
|
2019-06-04 16:37:03 +07:00
|
|
|
function preset_import() {
|
|
|
|
const modalTextArea = document.getElementById('modal-import-preset-textarea');
|
|
|
|
const settings = require('electron-settings');
|
|
|
|
var currentPresets = settings.get('presets');
|
2019-07-30 21:45:59 +07:00
|
|
|
var presetsToBeImported;
|
2019-08-14 22:46:53 +07:00
|
|
|
|
2019-06-04 16:37:03 +07:00
|
|
|
try {
|
2019-08-14 22:46:53 +07:00
|
|
|
presetsToBeImported = decodeURIComponent(escape(atob(modalTextArea.value)));
|
2019-06-04 16:37:03 +07:00
|
|
|
presetsToBeImported = JSON.parse(presetsToBeImported);
|
|
|
|
} catch (e) {
|
|
|
|
notification('danger', 'Unable to import presets, malformed data.');
|
2019-07-25 18:51:32 +07:00
|
|
|
appendLog(`preset_import() ${e}`);
|
2019-06-04 16:37:03 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var updatedPresets = Object.assign(
|
|
|
|
{},
|
|
|
|
currentPresets,
|
|
|
|
presetsToBeImported
|
|
|
|
);
|
|
|
|
settings.set('presets', updatedPresets);
|
2019-06-04 17:23:28 +07:00
|
|
|
preset_updateList();
|
2019-06-04 16:37:03 +07:00
|
|
|
modalTextArea.innerText = '';
|
|
|
|
}
|
2019-06-04 17:23:28 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}`;
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:50:02 +07:00
|
|
|
/**
|
|
|
|
* 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]}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 17:23:28 +07:00
|
|
|
/**
|
|
|
|
* 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 = '';
|
2019-08-17 22:50:02 +07:00
|
|
|
content += /*html*/`
|
|
|
|
<table class="uk-table uk-table-striped uk-table-hover uk-table-middle">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th colspan="2">Apply on</th>
|
|
|
|
<th>Action</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
`;
|
2019-06-04 17:23:28 +07:00
|
|
|
|
|
|
|
if (Object.keys(currentPresets).length === 0) {
|
2019-08-17 22:50:02 +07:00
|
|
|
content += /*html*/`<li>No preset has been created yet, import them or use the "Save to preset" button on Controller tab to create one.</li>`;
|
2019-06-04 17:23:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const presetName in currentPresets) {
|
|
|
|
if (currentPresets.hasOwnProperty(presetName)) {
|
|
|
|
const preset = currentPresets[presetName];
|
|
|
|
|
|
|
|
let valueSummary = [];
|
|
|
|
for (const key in preset) {
|
|
|
|
if (preset.hasOwnProperty(key) && key.indexOf('_range') !== -1 && key.indexOf('apply_') != 0) {
|
|
|
|
const value = preset[key];
|
|
|
|
valueSummary.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valueSummary.join(', ');
|
|
|
|
|
2019-08-17 22:50:02 +07:00
|
|
|
content += /*html*/`
|
|
|
|
<tr class="uk-margin">
|
|
|
|
<td>
|
|
|
|
<span class="uk-text-lead">${presetName}</span>
|
|
|
|
<i class="uk-text-small">${valueSummary}</i>
|
|
|
|
</td>
|
|
|
|
<td class="uk-table-expand">
|
|
|
|
<label style="cursor: pointer;">
|
|
|
|
<input
|
|
|
|
value="${presetName}"
|
|
|
|
class="uk-radio onAcStatusChange"
|
|
|
|
type="radio"
|
|
|
|
name="update-ac-plugged-in"
|
|
|
|
${presetName === require('electron-settings').get('auto-apply.update-ac-plugged-in') ? 'checked' : ''}
|
|
|
|
/>
|
|
|
|
AC plugged in
|
|
|
|
</label>
|
|
|
|
</td>
|
|
|
|
<td class="uk-table-expand">
|
|
|
|
<label style="cursor: pointer;">
|
|
|
|
<input
|
|
|
|
value="${presetName}"
|
|
|
|
class="uk-radio onAcStatusChange"
|
|
|
|
type="radio"
|
|
|
|
name="update-ac-plugged-out"
|
|
|
|
${presetName === require('electron-settings').get('auto-apply.update-ac-plugged-out') ? 'checked' : ''}
|
|
|
|
/>
|
|
|
|
AC plugged out
|
|
|
|
</label>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<button class="uk-button uk-margin uk-button-danger" type="button" onClick="preset_deletion('${presetName}')">
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
<button class="uk-button uk-button-primary" type="button" onClick="preset_apply('${presetName}')">
|
|
|
|
Apply
|
|
|
|
</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2019-06-04 17:23:28 +07:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 22:50:02 +07:00
|
|
|
content += /*html*/`
|
|
|
|
<tr>
|
|
|
|
<td><span class="uk-align-right">Disable auto apply</span></td>
|
|
|
|
<td>
|
|
|
|
<label style="cursor: pointer;">
|
|
|
|
<input
|
|
|
|
value=""
|
|
|
|
class="uk-radio onAcStatusChange"
|
|
|
|
type="radio"
|
|
|
|
name="update-ac-plugged-in"
|
|
|
|
${!require('electron-settings').get('auto-apply.update-ac-plugged-in') ? 'checked' : ''}
|
|
|
|
/>
|
|
|
|
On charging
|
|
|
|
</label>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<label style="cursor: pointer;">
|
|
|
|
<input
|
|
|
|
value=""
|
|
|
|
class="uk-radio onAcStatusChange"
|
|
|
|
type="radio"
|
|
|
|
name="update-ac-plugged-out"
|
|
|
|
${!require('electron-settings').get('auto-apply.update-ac-plugged-out') ? 'checked' : ''}
|
|
|
|
/>
|
|
|
|
On discharging
|
|
|
|
</label>
|
|
|
|
</td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tbody>
|
|
|
|
</table>
|
|
|
|
`;
|
2019-06-04 17:23:28 +07:00
|
|
|
presetTab.innerHTML = content;
|
2019-08-17 22:50:02 +07:00
|
|
|
var acStatusChangeRadios = document.querySelectorAll('.onAcStatusChange');
|
|
|
|
|
|
|
|
Array.from(acStatusChangeRadios).forEach(radio => {
|
|
|
|
radio.addEventListener('click', function(event) {
|
|
|
|
preset_enableAutoApplyOnAcStatusChange(this.name, this.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-04 17:23:28 +07:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:48:58 +07:00
|
|
|
/**
|
|
|
|
* Will check if the preset exists.
|
|
|
|
*
|
|
|
|
* @param {String} name The preset name to look for.
|
|
|
|
*/
|
|
|
|
function preset_isExist(name) {
|
|
|
|
const preset = require('electron-settings').get(`presets`)[name];
|
|
|
|
return !!preset;
|
|
|
|
}
|
|
|
|
|
2019-06-04 17:23:28 +07:00
|
|
|
/**
|
|
|
|
* This will apply the preset you asked for.
|
|
|
|
* @param {string} presetName The preset name to be applied.
|
|
|
|
*/
|
|
|
|
function preset_apply(presetName) {
|
2019-08-17 22:48:58 +07:00
|
|
|
if (!preset_isExist(presetName)) {
|
|
|
|
notification('danger', `Unable to apply unexisting preset "${presetName}".`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const preset = require('electron-settings').get(`presets`)[presetName];
|
|
|
|
appendLog(`preset_apply(): preset ${presetName}: ${JSON.stringify(preset)}`);
|
|
|
|
|
|
|
|
var ret = require('electron-settings').set("latest_controller_tabs_settings", preset);
|
2019-06-04 17:23:28 +07:00
|
|
|
appendLog(`preset_apply(): saved preset: ${JSON.stringify(ret)}`);
|
2019-08-17 22:48:58 +07:00
|
|
|
|
2019-06-04 17:23:28 +07:00
|
|
|
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();
|
|
|
|
}
|