Cleaned basecode and added notifications management.

This commit is contained in:
Quentin Decaunes 2019-02-27 23:02:10 +01:00
parent 43fed585ab
commit db745b344d
3 changed files with 109 additions and 35 deletions

View File

@ -17,6 +17,8 @@
<li><a href="#">Logs</a></li>
</ul>
<div class="uk-container" id="notification-zone"></div>
<ul class="uk-switcher uk-margin">
@ -97,6 +99,7 @@
<li class="uk-container">Will be available soon!</li>
</ul>
<script type="text/javascript" src="./js/settings.js"></script>
<script type="text/javascript" src="./js/methods.js"></script>
<script type="text/javascript" src="./js/app.js"></script>
</body>
</html>

View File

@ -1,36 +1,12 @@
function ready(fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(function(){
var ranges = document.querySelectorAll('[repeat]');
for (const range in ranges) {
if (ranges.hasOwnProperty(range)) {
const element = ranges[range];
element.addEventListener('change', function(event) {
var repeater = document.getElementById(event.target.attributes.repeat.value);
repeater.value = event.target.value;
});
}
}
registerRepeaterForAllInput();
checkForAdminRights();
});
function getCurrentWorkingDirectory() {
return require('electron').remote.app.getAppPath();
}
function decimalToHexString(number) {
if (number < 0)
{
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toUpperCase();
}
/**
* Will create and handle ryzenadj.exe execution.
* @param {Event} e The event triggered.
*/
function applyRyzenSettings(e) {
const settings = {
"--stapm-limit=": document.getElementById('stapm_limit_w').value,
@ -39,7 +15,6 @@ function applyRyzenSettings(e) {
"--tctl-temp=": document.getElementById('temperature_limit_c').value,
"--vrmmax-current=": document.getElementById('vrm_current_m_a').value,
};
console.log(settings);
const child = require('child_process').execFile;
const executablePath = getCurrentWorkingDirectory() + "\\bin\\ryzenadj.exe";
@ -73,8 +48,13 @@ function applyRyzenSettings(e) {
}
child(executablePath, parameters, function(err, data) {
console.log(err)
console.log(data.toString());
var output = data.toString();
if (err) {
notification('danger', err + '<br/>' + output);
}
else if (output) {
notification('success', 'Ryzenadj output:<br/>' + output);
}
});
}

91
js/methods.js Normal file
View File

@ -0,0 +1,91 @@
/**
* Will create a nodes from an html string.
* @param {string} str An html string
*/
function parseHTML(str) {
var tmp = document.implementation.createHTMLDocument();
tmp.body.innerHTML = str;
return tmp.body.children;
};
/**
* Return the current working directory.
*/
function getCurrentWorkingDirectory() {
return require('electron').remote.app.getAppPath();
}
/**
* Conversion from int to hex.
* @param {int} number A number.
*/
function decimalToHexString(number) {
if (number < 0)
{
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toUpperCase();
}
/**
* Will execute the given callback once document is ready.
* @param {function} fn A callback to be executed.
*/
function ready(fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
/**
* Will make sure inputs are repeated when needed.
*
* Use the "repeat" html attribute to define where the current value must be repeated.
*/
function registerRepeaterForAllInput() {
var ranges = document.querySelectorAll('[repeat]');
for (const range in ranges) {
if (ranges.hasOwnProperty(range)) {
const element = ranges[range];
element.addEventListener('change', function(event) {
var repeater = document.getElementById(event.target.attributes.repeat.value);
repeater.value = event.target.value;
});
}
}
}
/**
* Check that the app is running with admin right.
*
* Will display a warning if not.
*/
function checkForAdminRights() {
var exec = require('child_process').exec;
exec('NET SESSION', function(err,so,se) {
if (se.length !== 0) {
notification('warning',
`Warning: you should launch this app as administrator,
ryzenadj.exe doesn't seems to work correctly without administrator rights.
`);
}
});
}
/**
* Will display a notification in ".notification-zone".
* @param {string} type "primary", "warning", "danger" or "success".
* @param {string} message The message to be displayed, new line will be replaced by <br/>.
*/
function notification(type, message) {
var element = parseHTML(`
<div class="uk-alert-${type}" uk-alert>
<a class="uk-alert-close" uk-close></a>
<p>${(''+message).replace(/(?:\r\n|\r|\n)/g, '<br/>')}</p>
</div>`);
var notifZone = document.getElementById('notification-zone');
notifZone.appendChild(element[0]);
}