-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcanvas-static.js
230 lines (169 loc) · 5.08 KB
/
canvas-static.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//local javascript VF Page.
/*
The MIT License (MIT)
Copyright (c) 2016 SeedCode
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var cnv = (function() {
"use strict";
var config;
//Get the initial window height so this works correctly on mobile.
var initialWindowHeight = window.innerHeight;
//Public methods
return {
subscribe: subscribe,
init: init,
resize: resize,
publish: publish,
};
//publish to canvas
function publish(eventName, payload) {
Sfdc.canvas.controller.publish(
{
name : eventName,
payload : payload,
}
);
}
//Use the cnv.publish(<eventname>,<payload>) to call these events from your canvas app.
function subscribe() {
Sfdc.canvas.controller.subscribe(
[
//this allows us to publish a resize event from within dayback
{
"name": "cnvstart.resize",
"onData": function(e) {
resize(e);
}
},
{
"name": "cnvstart.navigate",
"onData": navigate,
},
//#############################################################
//#############################################################
//#############################################################
//add your event subscription events here separated by commas
//Use a unique name with the dbk prefix
//follow the syntax from the above events
//#############################################################
] //end array of subscriptions
);
}
//!!You shouldn't need to edit below this line!!
function init(initialConfig) {
//Assign config to global var
config = initialConfig;
//Resize our view so the iframes are sized correctly
resize();
//Add resize event listener
window.addEventListener("resize", function() {
resize();
});
}
//function for resizing canvas in vf page.
//Called after load and resize from vf page.
function resize(e) {
var height, target;
//If we are on mobile web we don't want to run
//resize as there is a salesforce bug currently
if (isMobileWeb()) {
//Mobile web site
height = getMobileHeight();
}
else if (isMobileOne()) {
//Mobile app
height = getMobileHeight();
}
else if (isLightningDesktop()) {
//Lightning Desktop
height = window.innerHeight;
}
else {
//Desktop
height = getHeight();
}
target = {
"canvas": "cnvstart"
};
Sfdc.canvas.controller.resize({
"height": height + "px"
}, target);
//Returns the height we would like to set the iframe height to
function getHeight() {
var headerElement, footerElement, bodyElement, bodyOffset, height;
try {
headerElement = document.querySelector(".bPageHeader");
footerElement = document.querySelector(".bPageFooter");
bodyElement = document.querySelector(".bodyDiv");
bodyOffset = bodyElement.offsetHeight - bodyElement.clientHeight;
height = window.innerHeight - headerElement.offsetHeight - footerElement.offsetHeight - bodyOffset;
}
catch(error) {
height = window.innerHeight - 132;
}
return height;
}
//Returns the height we would like to set the iframe height to on mobile devices
function getMobileHeight() {
var height = initialWindowHeight;
return height;
}
}
//Functions to determine what platform we are running on
function isClassicDesktop() {
var theme = config.theme;
return theme === 'Theme1' || theme === 'Theme2' || theme === 'Theme3';
}
function isLightningDesktop() {
var theme = config.theme;
return theme === 'Theme4d';
}
function isMobileOne() {
var theme = config.theme;
return theme === 'Theme4t';
}
//Check if we are on the mobile lightning sites
function isMobileWeb() {
var url = window.location.href;
var matchString = "Host=web";
return url.indexOf(matchString) > -1;
}
function navigate (e) {
var newTab = e.new;
var url = e.url;
var id = e.id;
var view = e.view;
if(!url && id) {
url="/"+id;
}
if( ( isLightningDesktop() || isMobileOne() ) && id ){
if(view){
sforce.one.navigateToSObject(id,view);
}
else {
sforce.one.navigateToSObject(id);
}
}
else if (newTab && url) {
window.open(url);
}
else if(url) {
window.location.assign(url);
}
}
}());