-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from bosonprotocol/click-close-modal
feat: allow to hide the modal + finalize js script
- Loading branch information
Showing
7 changed files
with
215 additions
and
116 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html PUBLIC> | ||
<html> | ||
<head> | ||
<title>Widget Integration Example</title> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="./styles.css"> | ||
<script async type="text/javascript" src="./scripts/boson-widgets.js"></script> | ||
</head> | ||
<body> | ||
<div> | ||
<button type="button" id="boson-finance" class="bosonButton" data-seller-id="25">Show Finance</button> | ||
</div> | ||
<div> | ||
<button type="button" id="boson-redeem" class="bosonButton">Show Redeem</button> | ||
</div> | ||
<!-- <div> | ||
<button type="button" id="boson-redeem" class="bosonButton" data-exchange-id="80">Show Redeem (ExchangeId:80)</button> | ||
</div> --> | ||
<div> | ||
<button type="button" id="boson-hide-modal" class="bosonButton">Hide</button> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const constants = { | ||
scriptName: "boson-widgets.js", // the name of the current file | ||
css: { | ||
modalClass: "bosonModal", | ||
loadingClass: "bosonLoading" | ||
}, | ||
loadingDurationMSec: 2000, | ||
iFrameId: "bosonModal", | ||
showRedeemId: "boson-redeem", | ||
showFinanceId: "boson-finance", | ||
exchangeIdTag: "data-exchange-id", | ||
sellerIdTag: "data-seller-id", | ||
hideModalId: "boson-hide-modal", | ||
hideModalMessage: "boson-close-iframe", | ||
financeUrl: (widgetsHost, sellerId) => | ||
`${widgetsHost}/#/finance?sellerId=${sellerId}`, | ||
redeemUrl: (widgetsHost, exchangeId) => | ||
`${widgetsHost}/#/redeem?exchangeId=${exchangeId}` | ||
}; | ||
var scripts = document.getElementsByTagName("script"); | ||
var widgetsHost = null; | ||
if (scripts) { | ||
for (var i = 0; i < scripts.length; i++) { | ||
if ( | ||
scripts[i].attributes["src"].value.endsWith(`/${constants.scriptName}`) | ||
) { | ||
widgetsHost = scripts[i].attributes["src"].value.replace( | ||
`/${constants.scriptName}`, | ||
"" | ||
); | ||
break; | ||
} | ||
} | ||
} else { | ||
console.error("Unable to find <scripts> tag"); | ||
} | ||
const injectCSS = (css) => { | ||
let el = document.createElement("style"); | ||
el.type = "text/css"; | ||
el.innerText = css; | ||
document.head.appendChild(el); | ||
return el; | ||
}; | ||
injectCSS( | ||
`.${constants.css.modalClass} { position: fixed; z-index: 1; width: 100%; left: 0; height: 100%; top: 0; border-style: none; }` + | ||
`.${constants.css.loadingClass} { position: fixed; z-index: 1; width: 100%; left: 0; height: 100%; top: 0; border-style: none; opacity: 50%; background: black; cursor: wait; }` | ||
); | ||
const showLoading = (delayMsec) => { | ||
var loading = document.createElement("div"); | ||
loading.className = constants.css.loadingClass; | ||
document.body.appendChild(loading); | ||
setTimeout(() => { | ||
loading.remove(); | ||
}, delayMsec); | ||
}; | ||
const createIFrame = (src) => { | ||
var bosonModal = document.createElement("iframe"); | ||
bosonModal.id = constants.iFrameId; | ||
bosonModal.src = src; | ||
bosonModal.className = constants.css.modalClass; | ||
document.body.appendChild(bosonModal); | ||
}; | ||
const hideIFrame = () => { | ||
let el = document.getElementById(constants.iFrameId); | ||
if (el) { | ||
el.remove(); | ||
} | ||
}; | ||
const showFinanceId = document.getElementById(constants.showFinanceId); | ||
if (showFinanceId) { | ||
showFinanceId.onclick = function () { | ||
showLoading(constants.loadingDurationMSec); | ||
hideIFrame(); | ||
var sellerId = showFinanceId.attributes[constants.sellerIdTag]?.value; | ||
createIFrame(constants.financeUrl(widgetsHost, sellerId)); | ||
}; | ||
} | ||
const showRedeemId = document.getElementById(constants.showRedeemId); | ||
if (showRedeemId) { | ||
showRedeemId.onclick = function () { | ||
showLoading(constants.loadingDurationMSec); | ||
hideIFrame(); | ||
var exchangeId = showRedeemId.attributes[constants.exchangeIdTag]?.value; | ||
createIFrame(constants.redeemUrl(widgetsHost, exchangeId)); | ||
}; | ||
} | ||
const hideModalId = document.getElementById(constants.hideModalId); | ||
if (hideModalId) { | ||
hideModalId.onclick = function () { | ||
hideIFrame(); | ||
}; | ||
} | ||
window.addEventListener("message", (event) => { | ||
if (event.data === constants.hideModalMessage) { | ||
hideIFrame(); | ||
} | ||
}); |
Oops, something went wrong.