OpenBuilds-CONTROL/app/js/main.js

364 wiersze
11 KiB
JavaScript

2018-06-21 21:04:41 +00:00
var gcode;
2020-03-30 15:26:11 +00:00
var loadedFileName = "";
2018-08-24 19:00:03 +00:00
var editor;
2018-08-28 15:09:20 +00:00
var isJogWidget = false;
2018-06-21 21:04:41 +00:00
2020-03-30 15:26:11 +00:00
function setWindowTitle(status) {
var string = "OpenBuilds CONTROL"
if (status) {
string += " v" + status.driver.version
2020-09-01 16:16:16 +00:00
} else if (laststatus) {
2020-03-30 15:26:11 +00:00
string += " v" + laststatus.driver.version
}
if (loadedFileName.length > 0) {
string += " / " + loadedFileName
}
if (!nostatusyet && laststatus.comms.interfaces.activePort) {
string += " / connected to " + laststatus.comms.interfaces.activePort
}
$('#windowtitle').html(string)
2020-09-01 13:25:40 +00:00
document.title = string
2020-03-30 15:26:11 +00:00
}
2019-01-14 18:09:53 +00:00
function getChangelog() {
$("#changelog").empty()
var template2 = `<ul>`
2019-05-06 17:10:44 +00:00
$.get("https://raw.githubusercontent.com/OpenBuilds/OpenBuilds-CONTROL/master/CHANGELOG.txt?date=" + new Date().getTime(), function(data) {
2019-01-14 18:09:53 +00:00
var lines = data.split('\n');
2020-04-20 20:58:03 +00:00
if (lines.length < 12) {
2019-01-14 18:09:53 +00:00
var count = lines.length - 1
} else {
2020-04-20 20:58:03 +00:00
var count = 12
2019-01-14 18:09:53 +00:00
}
for (var line = 0; line < count - 1; line++) {
template2 += '<li>' + lines[line] + '</li>'
}
template2 += `</ul>`
$("#changelog").html(template2);
2020-04-19 16:23:18 +00:00
// Update Dialog
var template3 = `<h6>Changelog:</h6> <hr> <ul>`
for (var line = 0; line < 5; line++) {
2020-03-06 15:59:58 +00:00
template3 += '<li>' + lines[line] + '</li>'
}
template3 += `</ul>`
$("#changelogupdate").html(template3);
2019-01-14 18:09:53 +00:00
});
}
2018-06-21 20:02:40 +00:00
$(document).ready(function() {
2019-10-18 18:55:41 +00:00
initDiagnostics(); // run second time to ensure checkboxes are ticked
2018-08-28 15:09:20 +00:00
if (!isJogWidget) {
init3D();
}
2018-06-22 19:49:51 +00:00
2018-08-24 19:00:03 +00:00
if (typeof ace !== 'undefined') {
editor = ace.edit("editor");
editor.$blockScrolling = Infinity;
editor.session.setMode("ace/mode/cncpro");
editor.setTheme('ace/theme/sqlserver')
// editor.setOption('printMarginColumn', 0)
editor.setAutoScrollEditorIntoView(true);
editor.session.setValue('; No GCODE yet - please Load a GCODE file from the Open GCODE button'); // from samplefile.js
editor.setShowPrintMargin(false);
2018-08-28 15:09:20 +00:00
editor.getSession().on('change', function() {
2018-09-06 17:50:01 +00:00
// parseGcodeInWebWorker(editor.getValue())
2018-08-28 15:09:20 +00:00
});
2018-08-24 19:00:03 +00:00
}
2018-09-06 21:08:06 +00:00
function setposition(e) {
var bodyOffsets = document.body.getBoundingClientRect();
tempX = e.pageX //- bodyOffsets.left;
tempY = e.pageY;
// console.log(tempX);
var offset = $("#editorContextMenu").offset();
$("#editorContextMenu").css({
display: 'block',
left: e.pageX,
top: e.pageY
});
}
2019-04-22 14:52:10 +00:00
if (editor) {
editor.container.addEventListener("contextmenu", function(e) {
setposition(e);
e.preventDefault();
2020-04-20 20:58:03 +00:00
$('.linenumber').html((editor.getSelectionRange().start.row + 1));
2019-04-22 14:52:10 +00:00
// alert('success! - rightclicked line ' + (editor.getSelectionRange().start.row + 1));
}, false);
}
2018-06-22 19:49:51 +00:00
2018-06-21 21:04:41 +00:00
var fileOpen = document.getElementById('file');
2018-08-24 19:00:03 +00:00
if (fileOpen) {
fileOpen.addEventListener('change', readFile, false);
}
2018-08-08 20:46:11 +00:00
2018-08-29 08:11:14 +00:00
$.get("/gcode").done(function(data) {
2019-04-18 17:25:23 +00:00
// console.log(data.length)
if (data.length > 2) {
editor.session.setValue(data);
2018-09-06 17:50:01 +00:00
parseGcodeInWebWorker(data)
$('#controlTab').click()
2019-10-18 18:55:41 +00:00
if (!webgl) {
$('#gcodeviewertab').click();
} else {
$('#gcodeeditortab').click()
}
2018-08-29 08:11:14 +00:00
}
2018-08-29 08:11:14 +00:00
});
2019-01-14 18:09:53 +00:00
getChangelog()
2018-09-18 14:34:11 +00:00
setTimeout(function() {
$('#splash').fadeOut(500);
}, 100)
2018-08-29 08:11:14 +00:00
2020-03-30 15:26:11 +00:00
setInterval(function() {
setWindowTitle();
}, 1000)
2020-04-17 21:07:39 +00:00
const element = new Image();
Object.defineProperty(element, 'id', {
get: function() {
/* Call callback function here */
socket.emit("maximize", true)
console.log('%cOpenBuilds CONTROL Devtools', 'font-weight: bold; font-size: 20px;color: rgb(50,80,188); text-shadow: 1px 1px 0 rgb(0,00,39)');
console.log('%c', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%cGeneral: Check for any errors, messages as requested by our support team', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%c', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%cConsole Commands:', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%cAccess the last received feedback data (positions', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%coffsets, probes, comms, queues, etc )', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%claststatus', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%cAccess the Grbl Settings on the controller', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%cgrblParams', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c; Clears the console screen', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%cconsole.clear()', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c; Print a log entry/message to the Serial Log', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%cprintLog("string")', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%cAccess the running/last ran gcode via API', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%c$.get("/gcode", function(data) { //do something with gcode data });', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c; Send a job, ideal for macros, jobs. Can display a message when complete.', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%c; Set isJob to store for access via GET /gcode if needed', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%csocket.emit("runJob", {', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c data: gcode-commands,', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c isJob: false,', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c completedMsg: "message displayed upon completion ",', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c});', 'font-weight: regular; font-size: 12px;color: black; ');
console.log('%c; Send the GCODE string to the controller, ideal for single commands', 'font-weight: bold; font-size: 12px;color: black; ');
console.log('%csendGcode("gcode-string")', 'font-weight: regular; font-size: 12px;color: black; ');
2020-03-30 15:26:11 +00:00
2018-08-29 08:11:14 +00:00
2020-04-17 21:07:39 +00:00
}
});
console.log('%c', element);
2018-06-21 21:04:41 +00:00
});
function readFile(evt) {
console.group("New FileOpen Event:");
console.log(evt);
console.groupEnd();
// Close the menu
$("#drop1").dropdown("toggle");
// Files
var files = evt.target.files || evt.dataTransfer.files;
for (var i = 0; i < files.length; i++) {
loadFile(files[i]);
}
2018-09-18 14:09:11 +00:00
document.getElementById('file').value = '';
2018-06-21 21:04:41 +00:00
}
// load file
function loadFile(f) {
// Filereader
if (f) {
var r = new FileReader();
// if (f.name.match(/.gcode$/i)) {
r.readAsText(f);
r.onload = function(event) {
2018-06-22 19:49:51 +00:00
editor.session.setValue(this.result);
2020-03-30 15:26:11 +00:00
loadedFileName = f.name;
setWindowTitle()
2019-10-18 18:55:41 +00:00
if (webgl) {
2019-10-16 13:57:56 +00:00
printLog('<span class="fg-red">[ GCODE Parser ]</span><span class="fg-green"> GCODE File Loaded, please wait while we render a preview... </span>');
} else {
printLog('<span class="fg-red">[ GCODE Parser ]</span><span class="fg-green"> GCODE File Loaded </span>');
}
2019-09-03 18:36:09 +00:00
parseGcodeInWebWorker(this.result)
2018-06-21 21:04:41 +00:00
};
// }
}
2018-08-08 20:46:11 +00:00
}
function versionCompare(v1, v2, options) {
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');
function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}
if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}
if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}
if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}
for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}
if (v1parts[i] == v2parts[i]) {
continue;
} else if (v1parts[i] > v2parts[i]) {
return 1;
} else {
return -1;
}
}
if (v1parts.length != v2parts.length) {
return -1;
}
return 0;
2018-08-28 15:22:12 +00:00
}
var webgl = (function() {
2019-10-18 18:55:41 +00:00
if (disable3Dviewer) {
2018-08-28 15:22:12 +00:00
return false;
2019-10-18 18:55:41 +00:00
} else {
// console.log("Testing WebGL")
try {
return !!window.WebGLRenderingContext && !!document.createElement('canvas').getContext('experimental-webgl');
} catch (e) {
return false;
}
2018-08-28 15:22:12 +00:00
}
2019-10-18 18:55:41 +00:00
2019-05-01 18:44:44 +00:00
})();
function saveGcode() {
var blob = new Blob([editor.getValue()], {
type: "plain/text"
});
invokeSaveAsDialog(blob, 'edited-gcode.gcode');
}
function invokeSaveAsDialog(file, fileName) {
if (!file) {
throw 'Blob object is required.';
}
if (!file.type) {
file.type = 'text/plain';
}
var fileExtension = file.type.split('/')[1];
if (fileName && fileName.indexOf('.') !== -1) {
var splitted = fileName.split('.');
fileName = splitted[0];
fileExtension = splitted[1];
}
var fileFullName = (fileName || (Math.round(Math.random() * 9999999999) + 888888888)) + '.' + fileExtension;
if (typeof navigator.msSaveOrOpenBlob !== 'undefined') {
return navigator.msSaveOrOpenBlob(file, fileFullName);
} else if (typeof navigator.msSaveBlob !== 'undefined') {
return navigator.msSaveBlob(file, fileFullName);
}
var hyperlink = document.createElement('a');
hyperlink.href = URL.createObjectURL(file);
// hyperlink.target = '_blank';
hyperlink.download = fileFullName;
if (!!navigator.mozGetUserMedia) {
hyperlink.onclick = function() {
(document.body || document.documentElement).removeChild(hyperlink);
};
(document.body || document.documentElement).appendChild(hyperlink);
}
var evt = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
hyperlink.dispatchEvent(evt);
if (!navigator.mozGetUserMedia) {
URL.revokeObjectURL(hyperlink.href);
}
2019-10-16 13:57:56 +00:00
}
2019-10-18 18:55:41 +00:00
// function setViewerDisableUI() {
// if (localStorage.getItem('viewerDisable')) {
// if (JSON.parse(localStorage.getItem('viewerDisable')) == true) {
// $('#viewerdisabled').removeClass("checked");
// } else {
// $('#viewerdisabled').addClass("checked");
// }
// } else {
// $('#viewerdisabled').removeClass("checked");
// return false;
// }
// return (!JSON.parse(localStorage.getItem('viewerDisable')))
// }
//
// function viewerdisable() {
// console.log("viewerdisable")
// if (localStorage.getItem('viewerDisable')) {
// if (JSON.parse(localStorage.getItem('viewerDisable')) == true) {
// console.log("viewerdisable disabled")
// localStorage.setItem('viewerDisable', false);
// location.reload();
// } else {
// console.log("viewerdisable enabled")
// localStorage.setItem('viewerDisable', true);
// location.reload();
// }
// } else {
// console.log("viewerdisable defaulted")
// localStorage.setItem('viewerDisable', false);
// location.reload();
// }
// setViewerDisableUI()
// }