高章伟
2022-03-18 4bfe21c4b5ddc089ae5a95f4b10f6cff148b690d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<apex:page showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false" controller="WaveLabsAppController">
 
    <link rel="stylesheet" href="//www.jumpstartwave.com/lib/ladda.min.css" type="text/css"/>
    <style>
        BODY, .noSidebarCell, .bodyDiv .outerNoSidebar, .outerNoSidebar { padding: 0px !important; }
        BODY { margin: 0px; font-size: 14px; }        
        
        #header {
            width: 100%;
            height: 10px;
            background: white;
            color: white;
        }
        #banner {
            width: calc(100% - 300px);
            height: 300px;
            padding: 20px 0px 0px 300px;
            font-family: Arial, Helvetica, sans-serif;
            background-image: url("https://www.jumpstartwave.com/home/assets/img/wavelabs_install_bg.jpg");
            background-repeat: no-repeat;
            background-color: #e5e5e5;
        }
        #btnCreateSettings {
            background-color: white;
            height: 46px;
            padding: 5px 15px;
            border: 1px solid #2ec2ba;
            border-radius: 6px;
            font-size: 14px;
            font-weight: bold;
            color: #2ec2ba;
            cursor: pointer;
        }
        #btnCreateSettings:disabled {
            cursor: initial;
        }        
        #divBtn {
            color: red;
            font-weight: bold;
        }
        #otherNotes {
            color: #5C7A99;
            font-size: 12px;
        }
    </style>
    
    <script src="//www.jumpstartwave.com/lib/spin.min.js"></script>    
    <script src="//www.jumpstartwave.com/lib/ladda.min.js"></script>
    
    <script>
        var org = "{!org}";
        var instance = org.split(".")[0]; if(instance == "gs0") instance = "gus";
        var vfPrefix = location.href.indexOf("wavelabs.") > -1 ? "wavelabs" : "c";
        var looksLikeSDO = location.href.indexOf(org) > -1;
    
        var fullVFPath = 'https://' + vfPrefix + '.' + instance + '.visual.force.com';
        if(looksLikeSDO) {
            fullVFPath = 'https://' + org; // SDOs have VF on same domain as org
        }
    
        var customDomain = false;
        if(org.indexOf('.my.salesforce.com') > -1) {
            fullVFPath = org;
            customDomain = true;
        }
    
        function createSiteSettingsAPEX() {
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.WaveLabsAppController.createRemoteSiteSettings}',
                instance,
                function(result, event) {
                    if (event.status) { 
                        setTimeout( function() { 
                            var l = Ladda.create( document.querySelector( '.ladda-button' ) );                            
                            l.stop(); 
                            var elBtn = document.getElementById("btnCreateSettings");
                            elBtn.disabled = true;
                            elBtn.innerHTML = "Done! You can start using any of the tools now.";                            
                        }, 1000 );                        
                    } else {
                        var elDivBtn = document.getElementById("divBtn");
                        elDivBtn.innerHTML = "Error creating Remote Site Settings: " + event.message;
                    }
                }, 
                {escape: true}
            );            
        }
 
        // Create the VF Remote Site first using this AJAX call (e.g. https://wavelabs.[host].visual.force.com)
        // Then use the controller to create the instance site (e.g. https://[host].salesforce.com)
        // Why? Javascript can't create the latter, and Controller can't create the former (good times)
        function createSiteSettings() {
            var l = Ladda.create( document.querySelector( '.ladda-button' ) );
            l.start();            
            
            var binding = new XMLHttpRequest();
            var request =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
                    '<env:Header>' +
                        '<urn:SessionHeader xmlns:urn="http://soap.sforce.com/2006/04/metadata">' +
                            '<urn:sessionId>{!$Api.Session_ID}</urn:sessionId>' +
                        '</urn:SessionHeader>' +
                    '</env:Header>' +
                    '<env:Body>' +
                        '<createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">' +
                            '<metadata xsi:type="RemoteSiteSetting">' +
                                '<fullName>WaveLabsApp_OrgVF_URL</fullName>' +
                                '<description></description>' +
                                '<disableProtocolSecurity>false</disableProtocolSecurity>' +
                                '<isActive>true</isActive>' +
                                '<url>' + fullVFPath + '</url>' +
                            '</metadata>' +
                        '</createMetadata>' +
                    '</env:Body>' +
                '</env:Envelope>';
            binding.open('POST', fullVFPath + '/services/Soap/m/31.0');
            binding.setRequestHeader('SOAPAction','""');
            binding.setRequestHeader('Content-Type', 'text/xml');
            binding.onreadystatechange =
                function() {
                    if(this.readyState==4) {
                        var parser = new DOMParser();
                        var doc  = parser.parseFromString(this.response, 'application/xml');
                        var errors = doc.getElementsByTagName('errors');
                        var messageText = '';
                        for(var errorIdx = 0; errorIdx < errors.length; errorIdx++)
                            messageText += errors.item(errorIdx).getElementsByTagName('message').item(0).innerHTML + '\n';
                        if(messageText.length > 0 && messageText.indexOf("already exists") == -1) {
                            var elDivBtn = document.getElementById("divBtn");
                            elDivBtn.innerHTML = "Error creating Remote Site Settings: " + messageText;
                        } else {
                            if(!customDomain && !looksLikeSDO & messageText.length == 0) {
                                createSiteSettingsAPEX();
                            } else {
                                l.stop();
                                var elBtn = document.getElementById("btnCreateSettings");
                                elBtn.disabled = true;
                                elBtn.innerHTML = "Done! You can start using any of the tools now.";                                
                            }
                        }
                    }
                }
            binding.send(request);
        }    
    </script>
    
    <div id="header"></div>
    
    <div id="banner">
        <h2>Installation Successful</h2>
        
        Thank you for installing the Wave Labs App.
        
        <br/><br/>
        There's one last step needed: there are API calls to Salesforce endpoints, 
        so we need to create a couple Remote Site Settings. Just click the button below and 
        the settings will be created for you.
        
        <br/><br/>
        <div id="divBtn">
            <button id="btnCreateSettings" class="ladda-button" data-spinner-color="#2ec2ba" data-style="zoom-in" onclick="createSiteSettings()">Create Remote Site Settings</button>        
        </div>
        
        <br/><br/>
        <div id="otherNotes">
            Other notes:<br/>
            - Ensure the Wave Public API permission is enabled<br/>
            - Ensure there is a public folder called 'Shared Documents' (this is where dashboard template images are created)
        </div>
    </div>
    
</apex:page>