From 95a53154883321a70edfef149099db514a20de71 Mon Sep 17 00:00:00 2001 From: ieatacid Date: Sat, 5 Dec 2020 20:05:38 -0500 Subject: [PATCH] Fixed for OctoPrint 1.5.0 --- .../static/js/GcodeEditor.js | 134 +++++++++--------- setup.py | 2 +- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/octoprint_GcodeEditor/static/js/GcodeEditor.js b/octoprint_GcodeEditor/static/js/GcodeEditor.js index 02caf1f..a1f1b2e 100755 --- a/octoprint_GcodeEditor/static/js/GcodeEditor.js +++ b/octoprint_GcodeEditor/static/js/GcodeEditor.js @@ -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; @@ -31,7 +32,7 @@ $(function() { OctoPrint.files.upload("local", file, { filename: _selectedFilePath + fName }); - $("#gcode_edit_dialog").modal("hide"); + $('#gcode_edit_dialog').modal('hide'); }); self.canSaveGcode = ko.pureComputed(function() { @@ -39,18 +40,24 @@ $(function() { }); 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 = ""; @@ -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) { @@ -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("\ -
\ - \ -
\ - "); + 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("\ -
\ - \ -
\ - "); + 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("\ -
\ - \ -
\ - "); + 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("\ -
\ - \ -
\ - "); + $(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" || @@ -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); @@ -229,7 +235,7 @@ $(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 = { @@ -237,7 +243,7 @@ $(function() { }; // 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]; @@ -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"]); @@ -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]; @@ -342,7 +348,7 @@ $(function() { } self.onUserLoggedIn = function() { - if (!_firstRun) { + if(!_firstRun) { removeEditButtons(); addEditButtonsToGcode(); } diff --git a/setup.py b/setup.py index 08e99af..2321abd 100755 --- a/setup.py +++ b/setup.py @@ -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