Skip to main content

doubly linked list

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

Horizontal legend (d3.js)

Dec 17, 2024shaiRos

0 likes • 2 views

const rectSize = 15; // Size of the color box
const spacing = 5; // Space between legend items
let legend = canvas.append('g').attr('class','chart-legend')
.attr('transform', `translate(${0}, ${height + 3})`);
// Sample data for the legend
const legendData = [
{ label: 'low', color: '#1f77b4' },
{ label: 'medium', color: '#ff7f0e' },
{ label: 'high', color: '#2ca02c' },
{ label: 'very high', color: '#2ca02c' }
];
legend.selectAll('g')
.data(legendData)
.join('g')
.attr('class', 'legend-item')
.each(function (d, i) {
const legendItem = d3.select(this);
console.log(d.label)
console.log(d.label?.length)
let labelWidth = (d.label.length * 5) + 7 + 15
var bbox = legendItem.node().getBBox()
var width = bbox.width
console.log(width)
// Add rectangle for color
legendItem.append('rect')
.attr('width', rectSize)
.attr('height', rectSize)
.attr('fill', d.color);
// Add label
legendItem.append('text')
.attr('x', rectSize + spacing) // Position text to the right of the rectangle
.attr('y', rectSize / 2) // Align text vertically with the rectangle
.attr('dy', '.35em') // Adjust vertical alignment
.attr('text-anchor', 'start') // Align text to the start
.text(d.label)
.attr('fill', 'black'); // Optional: Text color
// legendItem.attr('transform', `translate(${i * (rectSize + spacing + labelWidth)}, 0)`) // Position each item horizontally
});
const legendItems_spacing = 12
var legendLength = 0
legend.selectAll('.legend-item').each(function(d,i) {
d3.select(this).attr('transform',function() {
var bbox = d3.select(this).node().getBBox()
var width = bbox.width
// if (width == 0) width = (d.text.length * 5) +15 // not in dom yet
let startingPosition = 0
if (i > 0) {
startingPosition = legendLength
}
legendLength += width + legendItems_spacing
// return `translate(${xx - (width + 30)},0$)`
return `translate(${startingPosition},0)`
})
})

drag and drop uploader

Nov 18, 2022AustinLeath

0 likes • 0 views

const dragAndDropDiv = editor.container;
dragAndDropDiv.addEventListener('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
dragAndDropDiv.addEventListener("drop",function(e){
// Prevent default behavior (Prevent file from being opened)
e.stopPropagation();
e.preventDefault();
const files = e.dataTransfer.items; // Array of all files
console.assert(files.length >= 1);
if (files[0].kind === 'file') {
var file = e.dataTransfer.items[0].getAsFile();
const fileSize = file.size;
const fileName = file.name;
const fileMimeType = file.type;
reader = new FileReader();
reader.onloadend = function(){
editor.setValue(reader.result);
}
reader.readAsText(file);
}else{
//Maybe handle if text is dropped
console.log(files[0].kind);
}
});

Canvas File Downloader

Nov 18, 2022AustinLeath

0 likes • 4 views

results =
{
"11345240":"Media Comment-80e47004-1719-493a-af69-a43b2652b41c",
"11345282":"zoom_3_22_21.mp4",
"11345303":"Recording-2.m4a",
"11345348":"February 12%2C 2021.mp4",
"11345826":"Lecture_Rousso_Chapter_06-2.pptx",
"11345933":"Themes_InClassAssigment.pptx",
"11346193":"Agenda – Week of March23.pptx",
"11346318":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
"11346319":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
"11346322":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
"11346323":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
"11346381":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
"11346449":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
"11346450":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
"11346451":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
"11346453":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
"11346454":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
"11346495":"Steinel chromatic ornamentation étude rhythm changes - Aug 23 2020 - 12-19 PM.pdf",
"11346532":"Media Comment-136242b4-7f2c-4d5d-8aa4-091f07e797cf",
"11346581":"2019 Trust Based Relationship Intervention Manual.pdf",
"11346584":"TBRI ppt.pptx",
"11346593":"Trauma informed care.ppt"
}
function NewTab(testing) {
window.open(testing, "_blank");
}
for(test in results) {
var testing = 'https://unt.instructure.com/files/' + test + '/download';
NewTab(testing);
//window.open = 'https://unt.instructure.com/files/' + test + '/download';
}

Heroku Production Build

Mar 11, 2021C S

1 like • 1 view

if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}

Express Login Endpoint

Mar 11, 2021C S

0 likes • 4 views

router.post("/", async (req, res) => {
const { email, password } = req.body;
try {
if (!email) return res.status(400).json({ msg: "An email is required" });
if (!password)
return res.status(400).json({ msg: "A password is required" });
const user = await User.findOne({ email }).select("_id password");
if (!user) return res.status(400).json({ msg: "Invalid credentials" });
const match = await bcrypt.compare(password, user.password);
if (!match) return res.status(400).json({ msg: "Invalid credentials" });
const accessToken = genAccessToken({ id: user._id });
const refreshToken = genRefreshToken({ id: user._id });
res.cookie("token", refreshToken, {
expires: new Date(Date.now() + 604800),
httpOnly: true,
});
res.json({ accessToken });
} catch (err) {
console.log(err.message);
res.status(500).json({ msg: "Error logging in user" });
}
});

levenshtein distance

Nov 19, 2022CodeCatch

0 likes • 1 view

const levenshteinDistance = (s, t) => {
if (!s.length) return t.length;
if (!t.length) return s.length;
const arr = [];
for (let i = 0; i <= t.length; i++) {
arr[i] = [i];
for (let j = 1; j <= s.length; j++) {
arr[i][j] =
i === 0
? j
: Math.min(
arr[i - 1][j] + 1,
arr[i][j - 1] + 1,
arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1)
);
}
}
return arr[t.length][s.length];
};
levenshteinDistance('duck', 'dark'); // 2