diff --git a/docs/plugins/viewmodels.rst b/docs/plugins/viewmodels.rst index 91759647f..9c9180b04 100644 --- a/docs/plugins/viewmodels.rst +++ b/docs/plugins/viewmodels.rst @@ -225,6 +225,12 @@ onUserPermissionsChanged(user) will be provided as only parameter. Note that this may also be triggered for not logged in guests if the guest group is modified. In this case ``user`` will be undefined. +onBeforePrintStart(callback) + Called before a print is started either by clicking the "Print" button in the state panel or the select & print icon + in the file list. The callback to actually proceed with starting the print is provided as the only parameter. By returning + ``false`` from this, plugins may prevent a print from actually starting, optionally starting it at a later date by + calling ``callback`` themselves. This can be used for example to implement an additional confirmation dialog. + onTabChange(next, current) Called before the main tab view switches to a new tab, so `before` the new tab becomes visible. Called with the next (changed to) and current (still visible) tab's hash (e.g. ``#control``). Note that ``current`` might be undefined diff --git a/src/octoprint/static/js/app/viewmodels/files.js b/src/octoprint/static/js/app/viewmodels/files.js index d22a3cb19..59e53eb92 100644 --- a/src/octoprint/static/js/app/viewmodels/files.js +++ b/src/octoprint/static/js/app/viewmodels/files.js @@ -9,6 +9,8 @@ $(function () { self.printerProfiles = parameters[4]; self.access = parameters[5]; + self.allViewModels = undefined; + self.filesListVisible = ko.observable(true); self.showInternalFilename = ko.observable(true); @@ -629,13 +631,32 @@ $(function () { return; } + var proceed = function (p) { + var prevented = false; + var callback = function () { + OctoPrint.files.select(data.origin, data.path, p); + }; + + if (p) { + callViewModels(self.allViewModels, "onBeforePrintStart", function ( + method + ) { + prevented = prevented || method(callback) === false; + }); + } + + if (!prevented) { + callback(); + } + }; + if ( printAfterLoad && self.listHelper.isSelected(data) && self.enablePrint(data) ) { // file was already selected, just start the print job - OctoPrint.job.start(); + self.printerState.print(); } else { // select file, start print job (if requested and within dimensions) var withinPrintDimensions = self.evaluatePrintDimensions(data, true); @@ -650,12 +671,12 @@ $(function () { cancel: gettext("No"), proceed: gettext("Yes"), onproceed: function () { - OctoPrint.files.select(data.origin, data.path, print); + proceed(print); }, nofade: true }); } else { - OctoPrint.files.select(data.origin, data.path, print); + proceed(print); } } }; @@ -1718,6 +1739,9 @@ $(function () { self.settingsViewModel.settings.appearance.showInternalFilename() ); }; + self.onAllBound = function (allViewModels) { + self.allViewModels = allViewModels; + }; } OCTOPRINT_VIEWMODELS.push({ diff --git a/src/octoprint/static/js/app/viewmodels/printerstate.js b/src/octoprint/static/js/app/viewmodels/printerstate.js index 799b5d4cb..9276bde16 100644 --- a/src/octoprint/static/js/app/viewmodels/printerstate.js +++ b/src/octoprint/static/js/app/viewmodels/printerstate.js @@ -6,6 +6,8 @@ $(function () { self.settings = parameters[1]; self.access = parameters[2]; + self.allViewModels = undefined; + self.stateString = ko.observable(undefined); self.errorString = ko.observable(undefined); self.hasErrorString = ko.pureComputed(function () { @@ -433,8 +435,25 @@ $(function () { } }); } else { + var proceed = function (p) { + var prevented = false; + var callback = function () { + OctoPrint.job.start(); + }; + + callViewModels(self.allViewModels, "onBeforePrintStart", function ( + method + ) { + prevented = prevented || method(callback) === false; + }); + + if (!prevented) { + callback(); + } + }; + if (!self.settings.feature_printStartConfirmation()) { - OctoPrint.job.start(); + proceed(); } else { showConfirmationDialog({ message: gettext( @@ -443,9 +462,7 @@ $(function () { question: gettext("Do you want to start the print job now?"), cancel: gettext("No"), proceed: gettext("Yes"), - onproceed: function () { - OctoPrint.job.start(); - }, + onproceed: proceed, nofade: true }); } @@ -479,6 +496,10 @@ $(function () { }); } }; + + self.onAllBound = function (allViewModels) { + self.allViewModels = allViewModels; + }; } OCTOPRINT_VIEWMODELS.push({