Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jamigibbs
Last active December 9, 2019 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamigibbs/272cb77c3d0fbdd168faa251ad09e1bf to your computer and use it in GitHub Desktop.
Save jamigibbs/272cb77c3d0fbdd168faa251ad09e1bf to your computer and use it in GitHub Desktop.
LWC Utils Collection
import checkPermission from '@salesforce/apex/PermissionService.checkPermission'; // https://github.com/tsalb/lwc-utils
const hasPermission = async (apiName) => {
const response = await checkPermission({ apiName: apiName });
return response;
}
// Straight from component library playground
const fetchFakeDataHelper = async ({ amountOfRecords }) => {
const recordMetadata = {
name: 'name',
email: 'email',
website: 'url',
amount: 'currency',
phone: 'phoneNumber',
closeAt: 'dateInFuture',
};
const response = await fetch('https://data-faker.herokuapp.com/collection', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
amountOfRecords,
recordMetadata,
}),
});
return response.json();
}
/**
* Returns a dom elements position in px relative to the viewport.
*
* @param el; DOM element
* @returns {object}
*/
const getOffset = (el) => {
const rect = el.getBoundingClientRect();
return {
left: rect.left + window.scrollX,
top: rect.top + window.scrollY
};
}
const scrollToElement = (pageElement, topSubtract = 0) => {
var positionX = 0,
positionY = 0;
while (pageElement != null) {
positionX += pageElement.offsetLeft;
positionY += pageElement.offsetTop;
pageElement = pageElement.offsetParent;
if (pageElement) {
window.scrollTo({
top: positionY - topSubtract,
left: positionX,
behavior: 'smooth'
});
}
}
}
const openNewWindow = function (url) {
const win = window.open(url, '_blank', 'location=yes,height=800,width=1200,scrollbars=yes,status=yes');
win.focus();
}
export {
getOffset,
scrollToElement,
openNewWindow,
// Apex Utils
hasPermission,
// Supports prototyping
// ONLY USE THIS FOR SANDBOX ENVIRONMENTS BY WHITELISTING CSP
// Trusted Site Name: salesforce_heroku_data_faker
// Trusted Site URL: https://data-faker.herokuapp.com
fetchFakeDataHelper
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment