|
1 |
| -function urlInspectionToolController(notificationsService, umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource) { |
| 1 | +function urlInspectionToolController(editorState, notificationsService, umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource) { |
2 | 2 | var vm = this;
|
3 | 3 |
|
4 | 4 | vm.loading = false;
|
5 |
| - vm.isConnected = false; |
| 5 | + vm.showResults = false; |
| 6 | + vm.oauthConfiguration = {}; |
| 7 | + vm.inspectionResult = {}; |
6 | 8 |
|
7 |
| - umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource.getAuthorizationUrl().then(function(response) { |
8 |
| - vm.authorizationUrl = response; |
| 9 | + // get available cultures for current node |
| 10 | + vm.currentCultures = editorState.current.urls.map(p => p.culture); |
| 11 | + |
| 12 | + // build default url inspection object |
| 13 | + vm.siteUrl = location.href.slice(0, location.href.indexOf("umbraco") - 1); |
| 14 | + vm.urlInspection = { |
| 15 | + inspectionUrl: `${vm.siteUrl}${editorState.current.urls.find(p => p.culture === vm.currentCultures[0]).text}`, |
| 16 | + siteUrl: vm.siteUrl, |
| 17 | + languageCode: vm.currentCultures[0], |
| 18 | + enabled: false |
| 19 | + }; |
| 20 | + |
| 21 | + // get oauth configuration |
| 22 | + umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource.getOAuthConfiguration().then(function(response) { |
| 23 | + vm.oauthConfiguration = response; |
9 | 24 | });
|
10 | 25 |
|
11 | 26 | vm.onConnectClick = function() {
|
12 |
| - vm.authorizationWindow = window.open(vm.authorizationUrl, |
| 27 | + vm.authorizationWindow = window.open(vm.oauthConfiguration.authorizationUrl, |
13 | 28 | "GoogleSearchConsole_Authorize",
|
14 | 29 | "width=900,height=700,modal=yes,alwaysRaised=yes");
|
15 | 30 | }
|
|
18 | 33 | revokeToken();
|
19 | 34 | }
|
20 | 35 |
|
| 36 | + vm.onInspect = function () { |
| 37 | + |
| 38 | + vm.loading = true; |
| 39 | + |
| 40 | + umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource.inspect(vm.urlInspection.inspectionUrl, vm.urlInspection.siteUrl, vm.urlInspection.languageCode) |
| 41 | + .then(function (response) { |
| 42 | + |
| 43 | + vm.loading = false; |
| 44 | + |
| 45 | + if (response.error !== undefined && response.error !== null) { |
| 46 | + |
| 47 | + notificationsService.warning(response.error.status, response.error.message); |
| 48 | + |
| 49 | + // if token expired -> refresh access token |
| 50 | + if (response.error.code === "401") { |
| 51 | + vm.isConnected = false; |
| 52 | + |
| 53 | + // refresh access token |
| 54 | + notificationsService.warning("Google Search Console Authorization", "Refreshing access token."); |
| 55 | + |
| 56 | + umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource.refreshAccessToken().then( |
| 57 | + function(response) { |
| 58 | + if (response.length !== "error") { |
| 59 | + vm.isConnected = true; |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + } else { |
| 64 | + vm.showResults = true; |
| 65 | + vm.inspectionResult = response.inspectionResult; |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + vm.onEdit = function() { |
| 71 | + vm.urlInspection.enabled = true; |
| 72 | + } |
| 73 | + |
| 74 | + vm.onChangeLanguageCode = function() { |
| 75 | + vm.urlInspection.inspectionUrl = |
| 76 | + `${vm.siteUrl}${editorState.current.urls.find(p => p.culture === vm.urlInspection.languageCode).text}`; |
| 77 | + } |
| 78 | + |
21 | 79 | // authorization listener
|
22 | 80 | window.addEventListener("message", function (event) {
|
23 | 81 | if (event.data.type === "google:oauth:success") {
|
24 | 82 |
|
25 | 83 | var codeParam = "?code=";
|
| 84 | + var scopeParam = "&scope="; |
26 | 85 |
|
27 | 86 | vm.authorizationWindow.close();
|
28 | 87 |
|
29 |
| - var code = event.data.url.slice(event.data.url.indexOf(codeParam) + codeParam.length); |
| 88 | + var code = event.data.url.slice(event.data.url.indexOf(codeParam) + codeParam.length, event.data.url.indexOf(scopeParam)); |
30 | 89 |
|
31 | 90 | umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource.getAccessToken(code).then(function (response) {
|
32 | 91 | if (response !== "error") {
|
33 |
| - vm.isConnected = true; |
34 |
| - notificationsService.success("Google Search Console Authentication", "Access Approved"); |
| 92 | + vm.oauthConfiguration.isConnected = true; |
| 93 | + notificationsService.success("Google Search Console Authorization", "Access Approved"); |
35 | 94 | } else {
|
36 |
| - notificationsService.error("Google Search Console Authentication", "Access Denied"); |
| 95 | + notificationsService.error("Google Search Console Authorization", "Access Denied"); |
37 | 96 | }
|
38 | 97 | });
|
39 | 98 | } else if (event.data.type === "google:oauth:denied") {
|
40 |
| - vm.showError("Google Search Console Authentication", "Access Denied"); |
41 |
| - |
42 |
| - vm.authWindow.close(); |
| 99 | + notificationsService.error("Google Search Console Authorization", "Access Denied"); |
| 100 | + vm.oauthConfiguration.isConnected = false; |
| 101 | + vm.authorizationWindow.close(); |
43 | 102 | }
|
44 | 103 |
|
45 | 104 | }, false);
|
46 | 105 |
|
47 | 106 | function revokeToken() {
|
48 | 107 | umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource.revokeToken().then(function () {
|
49 |
| - vm.isConnected = false; |
| 108 | + vm.oauthConfiguration.isConnected = false; |
| 109 | + vm.showResults = false; |
50 | 110 | });
|
51 | 111 | }
|
52 | 112 | }
|
|
0 commit comments