Skip to content

Instantly share code, notes, and snippets.

@buster95
Last active February 5, 2025 18:48
Show Gist options
  • Save buster95/c4f9893f024a7b865ea3e017514564ea to your computer and use it in GitHub Desktop.
Save buster95/c4f9893f024a7b865ea3e017514564ea to your computer and use it in GitHub Desktop.
javascript functions
function checkboxLogic(comp, val, isChecked) {
var panel = comp.findParentByType('panel');
var addPrimaryCta = panel.getComponent('addPrimaryCta');
var primaryCtaRedirect = panel.getComponent('primaryCtaRedirect');
var primaryCtaText = panel.getComponent('primaryCtaText');
var primaryCtaLink = panel.getComponent('primaryCtaLink');
var primaryCtaAriaLabel = panel.getComponent('primaryCtaAriaLabel');
var primaryCtaDesign = panel.getComponent('primaryCtaDesign');
if (isChecked) {
primaryCtaRedirect.show();
primaryCtaRedirect.setWidth(300);
primaryCtaRedirect.allowBlank = false;
primaryCtaText.show();
primaryCtaText.setWidth(300);
primaryCtaText.allowBlank = false;
primaryCtaLink.show();
primaryCtaLink.setWidth(300);
primaryCtaLink.allowBlank = false;
primaryCtaAriaLabel.show();
primaryCtaAriaLabel.setWidth(300);
primaryCtaAriaLabel.allowBlank = false;
primaryCtaDesign.show();
primaryCtaDesign.setWidth(300);
primaryCtaDesign.allowBlank = false;
} else {
primaryCtaRedirect.hide();
primaryCtaRedirect.allowBlank = true;
primaryCtaText.hide();
primaryCtaText.allowBlank = true;
primaryCtaLink.hide();
primaryCtaLink.allowBlank = true;
primaryCtaAriaLabel.hide();
primaryCtaAriaLabel.allowBlank = true;
primaryCtaDesign.hide();
primaryCtaDesign.allowBlank = true;
}
}
function checkboxLogic2(comp, val, isChecked) {
var panel = comp.findParentByType('panel');
var priceLbl = panel.getComponent('priceLbl');
var financeLbl = panel.getComponent('financeLbl');
var leaseLbl = panel.getComponent('leaseLbl');
if (isChecked) {
priceLbl.show();
priceLbl.setWidth(300);
priceLbl.allowBlank = false;
financeLbl.show();
financeLbl.setWidth(300);
financeLbl.allowBlank = false;
leaseLbl.show();
leaseLbl.setWidth(300);
leaseLbl.allowBlank = false;
} else {
priceLbl.hide();
priceLbl.allowBlank = true;
financeLbl.hide();
financeLbl.allowBlank = true;
leaseLbl.hide();
leaseLbl.allowBlank = true;
}
}
function getRandomArbitrary(min, max) {
return Math.random() * ((++max) - min) + min;
}
function formatDate(date) {
const day = String(date.getDate()).padStart(2, '0');
const month = date.toLocaleString('default', { month: 'short' });
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
const generateWorkSchedule = (year) => {
const schedule = [];
const daysInYear = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 366 : 365;
const startDate = new Date(year, 0, 1);
let week = [];
for (let i = 0; i < daysInYear; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
const dayOfWeek = currentDate.getDay();
if (dayOfWeek === 0 || dayOfWeek === 6) continue; // Skip Saturdays and Sundays
const entryHour = getRandomArbitrary(7, 8); // Random hour between 7 and 8
const entryMinute = entryHour === 7 ? getRandomArbitrary(30, 60) : getRandomArbitrary(0, 20);
const exitHour = getRandomArbitrary(17, 18); // Random hour between 17 and 18
const exitMinute = getRandomArbitrary(2, 60);
const entryTime = new Date(currentDate);
entryTime.setHours(entryHour, entryMinute);
const exitTime = new Date(currentDate);
exitTime.setHours(exitHour, exitMinute);
week.push({
date: formatDate(currentDate),
entry: entryTime.toTimeString().split(' ')[0],
exit: exitTime.toTimeString().split(' ')[0]
});
if (dayOfWeek === 5) { // End of the week (Friday)
schedule.push(week);
week = [];
}
}
if (week.length > 0) {
schedule.push(week); // Push the last week if it has any days
}
return schedule;
};
generateWorkSchedule(2024);
var mongoObjectId = function () {
var timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function () {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment