Skip to content

Commit 7c19315

Browse files
author
Olivier Dufour
committed
handle popup width and height inside iframe
1 parent 0f0f3bd commit 7c19315

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

addon/button.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ function initButton(sfHost, inInspector) {
310310
inInspector,
311311
}, "*");
312312
}
313+
if (e.data.mouseX) {
314+
let boundingClientRect = popupEl.getBoundingClientRect();
315+
let evt = new CustomEvent("mousemove", {bubbles: true, cancelable: false});
316+
evt.clientX = e.data.mouseX + boundingClientRect.left;
317+
evt.clientY = e.data.mouseY + boundingClientRect.top;
318+
onResizeMove(evt);
319+
}
320+
if (e.data.trackMouseMove == false) {
321+
endResizeMove({skipMessage: true});
322+
}
313323
if (e.data.insextClosePopup) {
314324
closePopup();
315325
}"field-api-name";
@@ -362,21 +372,42 @@ function initButton(sfHost, inInspector) {
362372
popupHeight = resizeHeight - (resizeY - event.clientY);
363373
}
364374
let popupWidth = resizeWidth - (event.clientX - resizeX);
375+
let boundingClientRect = popupEl.getBoundingClientRect();
376+
if (popupHeight > window.innerHeight - boundingClientRect.top - 50) {
377+
popupHeight = window.innerHeight - boundingClientRect.top - 50;
378+
}
379+
if (popupWidth > window.innerWidth - 50) {
380+
popupWidth = window.innerWidth - 50;
381+
}
382+
if (popupHeight < 0) {
383+
popupHeight = 50;
384+
}
385+
if (popupWidth < 0) {
386+
popupWidth = 50;
387+
}
365388
localStorage.setItem("popupWidth", popupWidth);
366389
localStorage.setItem("popupHeight", popupHeight);
367390
popupWrapper.style.width = popupWidth + "px";
368391
popupWrapper.style.height = popupHeight + "px";
369392
}
370393
}
371-
function endResizeMove() {
394+
function endResizeMove(evt) {
372395
window.removeEventListener("mousemove", onResizeMove);
373396
window.removeEventListener("mouseup", endResizeMove);
397+
if (!evt.skipMessage) {
398+
popupEl.contentWindow.postMessage({
399+
trackMouseMove: false
400+
}, "*");
401+
}
374402
resizeX = 0;
375403
resizeY = 0;
376404
}
377405
popupWrapper.addEventListener("mousedown", () => {
378406
window.addEventListener("mousemove", onResizeMove);
379407
window.addEventListener("mouseup", endResizeMove);
408+
popupEl.contentWindow.postMessage({
409+
trackMouseMove: true
410+
}, "*");
380411
});
381412

382413

addon/popup.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ class App extends React.PureComponent {
330330
limitsHref: "limits.html?" + limitsArg
331331
});
332332
}
333+
onMouseMove(event) {
334+
parent.postMessage({mouseX: event.clientX, mouseY: event.clientY}, "*");
335+
}
336+
endResizeMove(evt) {
337+
window.removeEventListener("mousemove", this.onMouseMove);
338+
window.removeEventListener("mouseup", this.endResizeMove);
339+
if (!evt.skipMessage) {
340+
parent.postMessage({trackMouseMove: false}, "*");
341+
}
342+
}
333343
onContextUrlMessage(e) {
334344
if (e.source == parent && e.data.insextUpdateRecordId) {
335345
let {locationHref} = e.data;
@@ -345,6 +355,13 @@ class App extends React.PureComponent {
345355
return;
346356
} else if (e.data.whereFlowIsUsed) {
347357
this.whereFlowIsUsed(JSON.parse(e.data.whereFlowIsUsed));
358+
} else if (e.data.trackMouseMove != undefined) {
359+
if (e.data.trackMouseMove) {
360+
window.addEventListener("mousemove", this.onMouseMove);
361+
window.addEventListener("mouseup", this.endResizeMove);
362+
} else {
363+
this.endResizeMove({skipMessage: true});
364+
}
348365
} else if (e.data.showInvalidTokenBanner) {
349366
//TODO use model to store if displayed or not.
350367
const containerToShow = document.getElementById("invalidTokenBanner");

0 commit comments

Comments
 (0)