|
21 | 21 |
|
22 | 22 | vm.onConnectClick = function () {
|
23 | 23 |
|
24 |
| - window.addEventListener("message", getAccessToken, false); |
| 24 | + window.addEventListener("message", handleOAuthSuccess, false); |
25 | 25 | umbracoCmsIntegrationsCrmDynamicsResource.getAuthorizationUrl().then(function (response) {
|
26 | 26 | vm.authWindow = window.open(response,
|
27 | 27 | "Authorize", "width=900,height=700,modal=yes,alwaysRaised=yes");
|
| 28 | + |
| 29 | + // check in 7 seconds for authorization window closed |
| 30 | + setTimeout(() => { |
| 31 | + if (vm.authWindow && !vm.authWindow.closed) { |
| 32 | + toggleAuthCodeSection(true); |
| 33 | + } |
| 34 | + }, 7000); |
28 | 35 | });
|
29 | 36 | }
|
30 | 37 |
|
|
45 | 52 | });
|
46 | 53 | }
|
47 | 54 |
|
48 |
| - function getAccessToken(event) { |
| 55 | + vm.onAuthorize = function () { |
| 56 | + getAccessToken(vm.authCode); |
| 57 | + } |
| 58 | + |
| 59 | + function getAccessToken(code) { |
| 60 | + if (!code || code.length == 0) { |
| 61 | + notificationsService.warning("Dynamics Configuration", "Please add a valid authentication code."); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + umbracoCmsIntegrationsCrmDynamicsResource.getAccessToken(event.data.code).then(function (response) { |
| 66 | + if (response.startsWith("Error:")) { |
| 67 | + |
| 68 | + // if directive runs from property editor, the notifications should be hidden, because they will not be displayed properly behind the overlay window. |
| 69 | + // if directive runs from data type, the notifications are displayed |
| 70 | + if (typeof $scope.connected === "undefined") |
| 71 | + notificationsService.error("Dynamics Configuration", response); |
| 72 | + } else { |
| 73 | + vm.oauthConfig.isConnected = true; |
| 74 | + |
| 75 | + // if directive runs from property editor, the notifications should be hidden, because they will not be displayed properly behind the overlay window. |
| 76 | + // if directive runs from data type, the notifications are displayed |
| 77 | + if (typeof $scope.connected === "undefined") |
| 78 | + notificationsService.success("Dynamics Configuration", "OAuth connected."); |
| 79 | + |
| 80 | + umbracoCmsIntegrationsCrmDynamicsResource.getSystemUserFullName().then(function (response) { |
| 81 | + vm.oauthConfig.fullName = response; |
| 82 | + }); |
| 83 | + |
| 84 | + if (typeof $scope.connected === "function") |
| 85 | + $scope.connected(); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + function handleOAuthSuccess(event) { |
49 | 91 | if (event.data.type === "dynamics:oauth:success") {
|
50 |
| - umbracoCmsIntegrationsCrmDynamicsResource.getAccessToken(event.data.code).then(function (response) { |
51 |
| - if (response.startsWith("Error:")) { |
52 |
| - |
53 |
| - // if directive runs from property editor, the notifications should be hidden, because they will not be displayed properly behind the overlay window. |
54 |
| - // if directive runs from data type, the notifications are displayed |
55 |
| - if (typeof $scope.connected === "undefined") |
56 |
| - notificationsService.error("Dynamics Configuration", response); |
57 |
| - } else { |
58 |
| - vm.oauthConfig.isConnected = true; |
59 |
| - |
60 |
| - // if directive runs from property editor, the notifications should be hidden, because they will not be displayed properly behind the overlay window. |
61 |
| - // if directive runs from data type, the notifications are displayed |
62 |
| - if (typeof $scope.connected === "undefined") |
63 |
| - notificationsService.success("Dynamics Configuration", "OAuth connected."); |
64 |
| - |
65 |
| - umbracoCmsIntegrationsCrmDynamicsResource.getSystemUserFullName().then(function (response) { |
66 |
| - vm.oauthConfig.fullName = response; |
67 |
| - }); |
68 |
| - |
69 |
| - if (typeof $scope.connected === "function") |
70 |
| - $scope.connected(); |
71 |
| - } |
72 |
| - }); |
| 92 | + getAccessToken(event.data.code); |
73 | 93 | }
|
74 | 94 | }
|
| 95 | + |
| 96 | + function toggleAuthCodeSection(isVisible) { |
| 97 | + document.getElementById("authCode").style.display = isVisible ? "block" : "none"; |
| 98 | + } |
75 | 99 | }
|
76 | 100 |
|
77 | 101 | angular.module("umbraco")
|
|
0 commit comments