Skip to content

Commit cdf2198

Browse files
author
OpenShift Bot
authored
Merge pull request #1675 from spadgett/detect-blocked-confirm-on-exit-dialog
Merged by openshift-bot
2 parents 9e2599c + 7ee435a commit cdf2198

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

app/scripts/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ angular.extend(window.OPENSHIFT_CONSTANTS, {
7878
// removes the option from the route creation form.
7979
DISABLE_WILDCARD_ROUTES: true,
8080

81+
// true indicates that the web console should not show confirmation prompts
82+
// when users navigate away from a page without saving.
83+
DISABLE_CONFIRM_ON_EXIT: false,
84+
8185
// This blacklist hides certain kinds from the "Other Resources" page because they are unpersisted, disallowed for most end users, or not supported by openshift but exist in kubernetes
8286
AVAILABLE_KINDS_BLACKLIST: [],
8387

app/scripts/directives/confirmOnExit.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
// http://stackoverflow.com/questions/14852802/detect-unsaved-changes-and-alert-user-using-angularjs
44
angular.module("openshiftConsole")
5-
.directive("confirmOnExit", function() {
5+
.directive("confirmOnExit", function(Logger) {
66
return {
77
scope: {
88
dirty: '=',
99
message: '='
1010
},
1111
link: function($scope) {
12+
// If the feature is disabled or the user has asked the browser to
13+
// block dialogs, don't try to prompt.
14+
if (_.get(window, 'OPENSHIFT_CONSTANTS.DISABLE_CONFIRM_ON_EXIT') ||
15+
_.get(window, 'OPENSHIFT_CONSTANTS.CONFIRM_DIALOG_BLOCKED')) {
16+
return;
17+
}
18+
1219
var getMessage = function() {
1320
return $scope.message || "You have unsaved changes. Leave this page anyway?";
1421
};
@@ -27,7 +34,27 @@ angular.module("openshiftConsole")
2734
return;
2835
}
2936

30-
if(!confirm(getMessage())) {
37+
var start = new Date().getTime();
38+
39+
// Use a native confirm dialog to block code execution since we're in
40+
// the location change listener.
41+
var okToExit = confirm(getMessage());
42+
if (okToExit) {
43+
return;
44+
}
45+
46+
// Workaround "Prevent this page from creating additional dialogs"
47+
//
48+
// If the response took less than 50ms, assume the confirm dialog was
49+
// blocked by the browser. There's no API to detect that the user
50+
// has told the browser to block these dialogs, and the user can't
51+
// navigate away otherwise.
52+
var end = new Date().getTime();
53+
if ((end - start) < 50) {
54+
// Remember that the user is blocking dialogs. This is a per session settings.
55+
_.set(window, 'OPENSHIFT_CONSTANTS.CONFIRM_DIALOG_BLOCKED', true);
56+
Logger.warn("Confirm on exit prompt appears to have been blocked by the browser.");
57+
} else {
3158
event.preventDefault();
3259
}
3360
});

dist/scripts/scripts.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ DEFAULT_HPA_CPU_TARGET_PERCENT:80,
690690
DISABLE_OVERVIEW_METRICS:!1,
691691
DISABLE_CUSTOM_METRICS:!1,
692692
DISABLE_WILDCARD_ROUTES:!0,
693+
DISABLE_CONFIRM_ON_EXIT:!1,
693694
AVAILABLE_KINDS_BLACKLIST:[],
694695
ENABLE_TECH_PREVIEW_FEATURE:{
695696
service_catalog_landing_page:!1,
@@ -13304,28 +13305,36 @@ e.findReferenceValueForEntries(b.entries, b.valueFromSelectorOptions);
1330413305
} ]
1330513306
};
1330613307
} ]);
13307-
}(), angular.module("openshiftConsole").directive("confirmOnExit", function() {
13308+
}(), angular.module("openshiftConsole").directive("confirmOnExit", [ "Logger", function(a) {
1330813309
return {
1330913310
scope:{
1331013311
dirty:"=",
1331113312
message:"="
1331213313
},
13313-
link:function(a) {
13314-
var b = function() {
13315-
return a.message || "You have unsaved changes. Leave this page anyway?";
13316-
}, c = function() {
13317-
if (a.dirty) return b();
13314+
link:function(b) {
13315+
if (!_.get(window, "OPENSHIFT_CONSTANTS.DISABLE_CONFIRM_ON_EXIT") && !_.get(window, "OPENSHIFT_CONSTANTS.CONFIRM_DIALOG_BLOCKED")) {
13316+
var c = function() {
13317+
return b.message || "You have unsaved changes. Leave this page anyway?";
13318+
}, d = function() {
13319+
if (b.dirty) return c();
1331813320
};
13319-
$(window).on("beforeunload", c);
13320-
var d = a.$on("$locationChangeStart", function(c) {
13321-
a.dirty && (confirm(b()) || c.preventDefault());
13321+
$(window).on("beforeunload", d);
13322+
var e = b.$on("$locationChangeStart", function(d) {
13323+
if (b.dirty) {
13324+
var e = new Date().getTime(), f = confirm(c());
13325+
if (!f) {
13326+
var g = new Date().getTime();
13327+
g - e < 50 ? (_.set(window, "OPENSHIFT_CONSTANTS.CONFIRM_DIALOG_BLOCKED", !0), a.warn("Confirm on exit prompt appears to have been blocked by the browser.")) :d.preventDefault();
13328+
}
13329+
}
1332213330
});
13323-
a.$on("$destroy", function() {
13324-
$(window).off("beforeunload", c), d && d();
13331+
b.$on("$destroy", function() {
13332+
$(window).off("beforeunload", d), e && e();
1332513333
});
1332613334
}
13335+
}
1332713336
};
13328-
}), angular.module("openshiftConsole").filter("duration", function() {
13337+
} ]), angular.module("openshiftConsole").filter("duration", function() {
1332913338
return function(a, b, c, d) {
1333013339
function e(a, b, d) {
1333113340
if (0 !== a) return 1 === a ? void (c ? h.push(b) :h.push("1 " + b)) :void h.push(a + " " + d);

0 commit comments

Comments
 (0)