2018-08-25 17:55:48 +03:00
|
|
|
// OctoPrint Klipper Plugin
|
|
|
|
//
|
|
|
|
// Copyright (C) 2018 Martin Muehlhaeuser <github@mmone.de>
|
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
function KlipperMacroDialogViewModel(parameters) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.parameters = ko.observableArray();
|
|
|
|
self.interpolatedCmd;
|
|
|
|
self.macro;
|
|
|
|
self.macroName = ko.observable();
|
|
|
|
|
|
|
|
var paramObjRegex = /{(.*?)}/g;
|
2018-08-25 22:16:03 +03:00
|
|
|
var keyValueRegex = /(\w*)\s*:\s*([\w\s°"|]*)/g;
|
2018-08-25 17:55:48 +03:00
|
|
|
|
|
|
|
self.process = function(macro) {
|
|
|
|
self.macro = macro.macro();
|
|
|
|
self.macroName(macro.name());
|
|
|
|
|
|
|
|
var matches = self.macro.match(paramObjRegex);
|
|
|
|
var params = [];
|
|
|
|
|
|
|
|
for (var i=0; i < matches.length; i++) {
|
|
|
|
var obj = {};
|
|
|
|
var res = keyValueRegex.exec(matches[i]);
|
|
|
|
|
|
|
|
while (res != null) {
|
|
|
|
if("options" == res[1]) {
|
2018-08-25 22:16:03 +03:00
|
|
|
obj["options"] = res[2].trim().split("|");
|
2018-08-25 17:55:48 +03:00
|
|
|
} else {
|
2018-08-25 22:16:03 +03:00
|
|
|
obj[res[1]] = res[2].trim();
|
2018-08-25 17:55:48 +03:00
|
|
|
}
|
|
|
|
res = keyValueRegex.exec(matches[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!("label" in obj)) {
|
|
|
|
obj["label"] = "Input " + (i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!("unit" in obj)) {
|
|
|
|
obj["unit"] = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if("default" in obj) {
|
|
|
|
obj["value"] = obj["default"];
|
|
|
|
}
|
|
|
|
|
|
|
|
params.push(obj);
|
|
|
|
}
|
|
|
|
self.parameters(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.executeMacro = function() {
|
|
|
|
var i=-1;
|
|
|
|
|
|
|
|
function replaceParams(match) {
|
|
|
|
i++;
|
|
|
|
return self.parameters()[i]["value"];
|
|
|
|
}
|
|
|
|
|
|
|
|
expanded = self.macro.replace(paramObjRegex, replaceParams)
|
2019-02-12 19:03:36 +03:00
|
|
|
// expanded = "[\"" + expanded + "\"]"
|
|
|
|
// expanded = expanded.replace(/(?:\r\n|\r|\n)/g, "\",\"");
|
|
|
|
// Split using RegEx - cleaner
|
|
|
|
expanded = expanded.split(/\r\n|\r|\n/);
|
2019-02-12 03:34:59 +03:00
|
|
|
|
2019-02-12 16:18:39 +03:00
|
|
|
OctoPrint.control.sendGcode(expanded);
|
2018-08-25 17:55:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OCTOPRINT_VIEWMODELS.push({
|
|
|
|
construct: KlipperMacroDialogViewModel,
|
|
|
|
dependencies: [],
|
|
|
|
elements: ["#klipper_macro_dialog"]
|
|
|
|
});
|
|
|
|
});
|