Skip to content

Commit

Permalink
Fixed for OctoPrint 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ieatacid committed Dec 6, 2020
1 parent 9cb5814 commit 95a5315
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 65 deletions.
134 changes: 70 additions & 64 deletions octoprint_GcodeEditor/static/js/GcodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $(function() {
self.printerState = parameters[2];
self.settings = parameters[3];

const BSEL = '#files div.btn-group.action-buttons';
var _loadingFile = false;
var _firstRun = true;
var _selectedFilePath;
Expand All @@ -31,26 +32,32 @@ $(function() {

OctoPrint.files.upload("local", file, { filename: _selectedFilePath + fName });

$("#gcode_edit_dialog").modal("hide");
$('#gcode_edit_dialog').modal('hide');
});

self.canSaveGcode = ko.pureComputed(function() {
return !(self.printerState.isPrinting() && self.printerState.filename() === self.destinationFilename());
});

self.saveGcodeButtonTooltip = ko.pureComputed(function() {
if (!self.canSaveGcode()) {
if(!self.canSaveGcode()) {
return gettext("Cannot edit gcode of file that is currently printing");
} else {
return gettext("Save gcode");
}
});

function dbg(...args) {
console.info(`%c GCodeEditor `,
'color: white; font-weight: bold; background: #21cf7a',
...args);
}

// Modified from M33-Fio https://github.com/donovan6000/M33-Fio/blob/master/octoprint_m33fio/static/js/m33fio.js#L3970
function showGcodeEditor(url, name, header, onloadCallback, delay) {
var str = getGcodePathAndName(getRootFilePath(), url);

if (str.split("/").length > 2) {
if(str.split("/").length > 2) {
_selectedFilePath = str.substring(1, str.lastIndexOf("/")) + "/";
} else {
_selectedFilePath = "";
Expand All @@ -71,7 +78,7 @@ $(function() {
"Cache-Control": "no-cache, no-store, must-revalidate"
},
beforeSend: function() {
$('#loading_modal').modal("show");
$('#loading_modal').modal('show');
}
// Done
}).done(function(data) {
Expand All @@ -83,117 +90,116 @@ $(function() {

self.gcodeTextArea(data);

$("#gcode_edit_dialog").modal("show");
$('#gcode_edit_dialog').modal('show');
});
}

$('body').on('shown', '#gcode_edit_dialog', function(e) {
$('#loading_modal').modal("hide");
$('#loading_modal').modal('hide');
});

function removeEditButtons() {
$("#files div.gcode_files div.entry .action-buttons div.btn-mini.editGcode").remove();
$('div.editGcode').remove();
}

function disableEditButton(name, reason) {
var select = _.sprintf(gettext("#files div.gcode_files div.entry .title:contains('%(filename)s')"), { filename: name });
var select = _.sprintf(gettext('div.gcode_files .title:contains("%(filename)s")'), { filename: name });
var gc = $(select).parent().children('div.btn-group.action-buttons').children('.editGcode');

if ($(select).parent().children().eq(4).children().eq(2).hasClass('editGcode')) {
$(select).parent().children().eq(4).children().eq(2).addClass('disabled');
if(gc) {
gc.addClass('disabled');

if (reason.length > 0) {
$(select).parent().children().eq(4).children().eq(2).prop('title', reason);
if(reason.length > 0) {
gc.prop('title', reason);
}
}
}

function enableEditButton(name) {
var select = _.sprintf(gettext("#files div.gcode_files div.entry .title:contains('%(filename)s')"), { filename: name });
var select = _.sprintf(gettext('#files div.gcode_files .title:contains("%(filename)s")'), { filename: name });
var gc = $(select).parent().children('div.btn-group.action-buttons').children('.editGcode');

if ($(select).parent().children().eq(4).children().eq(2).hasClass('editGcode')) {
$(select).parent().children().eq(4).children().eq(2).removeClass('disabled');
$(select).parent().children().eq(4).children().eq(2).prop('title', 'Edit');
if(gc) {
gc.removeClass('disabled');
gc.prop('title', 'Edit');
}
}

function setEditButton(title_text, enabled = true) {
var div = document.createElement("div");
var i = document.createElement("i");
div.classList = "btn btn-mini editGcode";
i.classList = "fas fa-edit";
div.appendChild(i);
div.title = encodeQuotes(gettext(title_text));
enabled || div.classList.add("disabled");
return div;
}

// Modified from M33-Fio https://github.com/donovan6000/M33-Fio/blob/master/octoprint_m33fio/static/js/m33fio.js#L5026
// Add edit buttons to G-code
function addEditButtonsToGcode() {
// Go through all file entries
$("#files div.gcode_files div.entry .action-buttons").each(function() {
$(BSEL).each(function() {

// Check if file is G-code
if ($(this).children().children("i.icon-print, i.fa.fa-print").length) {
var url = $(this).children().eq(0).attr("href");
var size = _bytesFromSize($(this).parent().children().eq(2).text());
if($(this).children().children('i.fas.fa-print').length) {
var url = $(this).children('a.btn.btn-mini').attr('href');
var size = _bytesFromSize($(this).parent().children('div.size').text());

// Add edit button
if (!$(this).children().eq(0).hasClass("disabled")) {
if (size > self.maxGcodeSize() || (OctoPrint.coreui.browser.mobile && size > self.maxGcodeSizeMobile())) {
$(this).children("a.btn-mini").after("\
<div class=\"btn btn-mini editGcode disabled\" title=\"" + encodeQuotes(gettext("File size too large")) + "\">\
<i class=\"icon-pencil\"></i>\
</div>\
");
if(!$(this).children('a.btn.btn-mini').hasClass('disabled')) {
if(size > self.maxGcodeSize() || (OctoPrint.coreui.browser.mobile && size > self.maxGcodeSizeMobile())) {
$(this).children('a.btn.btn-mini').after(setEditButton("File size too large", false));
}
else if (url.indexOf("/files/local/") === -1) {
$(this).children("a.btn-mini").after("\
<div class=\"btn btn-mini editGcode disabled\" title=\"" + encodeQuotes(gettext("Not local file")) + "\">\
<i class=\"icon-pencil\"></i>\
</div>\
");
else if(url.indexOf("/files/local/") === -1) {
$(this).children('a.btn.btn-mini').after(setEditButton("Not local file", false));
}
else if (self.printerState.isPrinting() && self.printerState.filename() === $(this).children().parent().parent().children().eq(0).text()) {
$(this).children("a.btn-mini").after("\
<div class=\"btn btn-mini editGcode disabled\" title=\"" + encodeQuotes(gettext("File is currently printing")) + "\">\
<i class=\"icon-pencil\"></i>\
</div>\
");
else if(self.printerState.isPrinting() && self.printerState.filename() === $(this).parent().children('div.title').text()) {
$(this).children('a.btn.btn-mini').after(setEditButton("File is currently printing", false));
}
else {
$(this).children("a.btn-mini").after("\
<div class=\"btn btn-mini editGcode\" title=\"" + encodeQuotes(gettext("Edit")) + "\">\
<i class=\"icon-pencil\"></i>\
</div>\
");
$(this).children('a.btn.btn-mini').after(setEditButton("Edit"));
}
}
}
else {
console.warn('FAILURE');
}
});

// Check if user isn't logged in
if (!self.loginState.loggedIn()) {
if(!self.loginState.loggedIn()) {
// Disable edit buttons
$("#files div.gcode_files div.entry .action-buttons div.btn-mini.editGcode").addClass("disabled");
$('div.editGcode').addClass('disabled');
}

// Edit button click event
$("#files div.gcode_files div.entry .action-buttons div.btn-mini.editGcode").click(function() {

$('div.editGcode').click(function() {
var button = $(this);

// Blur self
button.blur();

// Check if button is not disabled
if (!button.hasClass("disabled")) {
if(!button.hasClass('disabled')) {

// Check if not already loading file
if (!_loadingFile) {
if(!_loadingFile) {

// Set loading file
_loadingFile = true;

// Enable other edit buttons
// $("#files div.gcode_files div.entry .action-buttons div.btn-mini.editGcode").removeClass("disabled");
// $('#files div.gcode_files .action-buttons div.btn-mini.editGcode').removeClass('disabled');

// Set icon to spinning animation
button.addClass("disabled").children("i").removeClass("icon-pencil").addClass("icon-spinner icon-spin");
button.addClass('disabled').children('i').removeClass('fas fa-edit').addClass('icon-spinner icon-spin');

setTimeout(function() {

var furl = button.parent().children("a.btn-mini").attr("href");
var fname = button.parent().parent().children("div.title").eq(0).html();
var furl = button.parent().children('a.btn-mini').attr('href');
var fname = button.parent().parent().children('div.title').html();
var fheader = _.sprintf(gettext("Editing %(fileName)s"),
{
fileName: htmlEncode(typeof self.files.currentPath === "undefined" ||
Expand All @@ -207,7 +213,7 @@ $(function() {
_loadingFile = false;

// Restore edit icon and enable button
button.removeClass("disabled").children("i").removeClass("icon-spinner icon-spin").addClass("icon-pencil");
button.removeClass('disabled').children('i').removeClass('icon-spinner icon-spin').addClass('fas fa-edit');
}, 0);
}, 0);
}, 200);
Expand All @@ -229,15 +235,15 @@ $(function() {
var entry = self.files.listHelper.allItems[0];

// Check if OctoPrint version doesn't use upload folders
if (entry && !entry.hasOwnProperty("parent")) {
if(entry && !entry.hasOwnProperty("parent")) {

// Construct root file path
var root = {
children: {}
};

// Go throguh all entries
for (var index in self.files.listHelper.allItems)
for(var index in self.files.listHelper.allItems)

// Add entry to root's children
root.children[index] = self.files.listHelper.allItems[index];
Expand All @@ -260,21 +266,21 @@ $(function() {
function getGcodePathAndName(entry, gcodeUrl) {

// Check if entry is a folder
if (entry && entry.hasOwnProperty("children"))
if(entry && entry.hasOwnProperty("children"))

// Go through each entry in the folder
for (var child in entry.children) {
for(var child in entry.children) {

// Check if current child is the specified G-code file
var value = getGcodePathAndName(entry.children[child], gcodeUrl);
if (typeof value !== "undefined")
if(typeof value !== "undefined")

// Return upload date
return value;
}

// Otherwise check if entry is the specified G-code file
else if (entry && entry.hasOwnProperty("name") && entry.refs && entry.refs.hasOwnProperty("download") && entry["refs"]["download"] === gcodeUrl)
else if(entry && entry.hasOwnProperty("name") && entry.refs && entry.refs.hasOwnProperty("download") && entry["refs"]["download"] === gcodeUrl)

// Return path and name
return (typeof self.files.currentPath !== "undefined" ? "/" : "") + (entry.hasOwnProperty("path") ? entry["path"] : entry["name"]);
Expand Down Expand Up @@ -318,10 +324,10 @@ $(function() {

// Modified from M33-Fio https://github.com/donovan6000/M33-Fio/blob/master/octoprint_m33fio/static/js/m33fio.js#L18516
// Go through all view models
for (var viewModel in payload) {
for(var viewModel in payload) {

// Otherwise check if view model is files view model
if (payload[viewModel].constructor.name === "FilesViewModel" || payload[viewModel].constructor.name === "GcodeFilesViewModel") {
if(payload[viewModel].constructor.name === "FilesViewModel" || payload[viewModel].constructor.name === "GcodeFilesViewModel") {

// Set files
self.files = payload[viewModel];
Expand All @@ -342,7 +348,7 @@ $(function() {
}

self.onUserLoggedIn = function() {
if (!_firstRun) {
if(!_firstRun) {
removeEditButtons();
addEditButtonsToGcode();
}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-GcodeEditor"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.2.9"
plugin_version = "0.2.10"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 95a5315

Please sign in to comment.