Skip to content

Instantly share code, notes, and snippets.

@foofoodog
Last active August 29, 2015 14:15
Show Gist options
  • Save foofoodog/93f85f0403e7dca42b32 to your computer and use it in GitHub Desktop.
Save foofoodog/93f85f0403e7dca42b32 to your computer and use it in GitHub Desktop.
Intercept KO click bindings and add confirmation where desired in OctoPrint
Index: src/octoprint/static/js/app/confirm.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/octoprint/static/js/app/confirm.js (revision )
+++ src/octoprint/static/js/app/confirm.js (revision )
@@ -0,0 +1,36 @@
+(function() {
+ var originalInit = ko.bindingHandlers.click.init;
+ ko.bindingHandlers.click = {
+ conditions: [
+ {locator: "event.target.id=='printer_connect'", test: "this.isOperational()"}, // disconnect when connected
+ {locator: "event.target.classList.contains('icon-trash')", test: "true"}, // trash icon always
+ {locator: "event.target.id=='job_cancel'", test: "true"}, // job cancel always
+ ],
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
+ var wrappedValueAccessor = function() {
+ return function(data, event) {
+ ko.bindingHandlers.click.preOnClick.call(viewModel, data, event, function() {valueAccessor().call(viewModel, data, event);});
+ };
+ };
+ originalInit(element, wrappedValueAccessor, allBindingsAccessor, viewModel);
+ },
+ preOnClick: function(data, event, handler) {
+ // See if we can find a rule
+ var condition = ko.bindingHandlers.click.conditions.filter(function(x) {
+ return eval(x.locator);
+ });
+ // If we found a rule see if it is in effect, if not just proceed
+ var test = condition[0] ? condition[0].test : false;
+ if (!eval(test)) {
+ handler();
+ return;
+ }
+ $("#confirmation_dialog .confirmation_dialog_message").text(gettext(event.target.innerText || event.target.title) + "?");
+ $("#confirmation_dialog .confirmation_dialog_acknowledge").unbind("click");
+ $("#confirmation_dialog .confirmation_dialog_acknowledge").click(function(e) {e.preventDefault(); $("#confirmation_dialog").modal("hide"); handler(); });
+ $("#confirmation_dialog").modal("show");
+ }
+ };
+})();
+
+
Index: src/octoprint/templates/javascripts.jinja2
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/octoprint/templates/javascripts.jinja2 (revision 6cdd8de248da6da2f9c96906417bc4a2b2f953a7)
+++ src/octoprint/templates/javascripts.jinja2 (revision )
@@ -59,6 +59,7 @@
<script type="text/javascript" src="{{ url_for('static', filename='js/app/helpers.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/app/main.js') }}"></script>
+<script type="text/javascript" src="{{ url_for('static', filename='js/app/confirm.js') }}"></script>
<!-- /Include OctoPrint files -->
<!-- Include i18n language files -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment