Skip to content

Instantly share code, notes, and snippets.

@waaronking
waaronking / chartjs-3.5.1-custom-legend.js
Last active September 8, 2021 08:29
ChartJS 3.5.1 Custom Legend Plugin Example
/* This script requires the following elements in order to run:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js"></script>
<div id="legend-id"></div>
<canvas id="chart-id"></canvas>
*/
var data = {
labels: [
@waaronking
waaronking / index.vue
Created June 23, 2020 10:44
TipTap Align-Text Example
<!-- This file is mainly pseudo code with parts copied from a working project to illustrate how tiptap-aligntext.plugin.js can be implemented into your project -->
<template>
<div>
<editor-menu-bar :editor="editor" v-slot="{ commands }">
<div class="menubar">
<i class="fas fa-align-left" :class="{ 'is-active': editor.activeMarkAttrs.aligntext.align === 'left' }" @click="commands.aligntext({ align: 'left' })"></i>
<i class="fas fa-align-center" :class="{ 'is-active': editor.activeMarkAttrs.aligntext.align === 'center' }" @click="commands.aligntext({ align: 'center' })"></i>
<i class="fas fa-align-right" :class="{ 'is-active': editor.activeMarkAttrs.aligntext.align === 'right' }" @click="commands.aligntext({ align: 'right' })"></i>
<i class="fas fa-align-justify" :class="{ 'is-active': editor.activeMarkAttrs.aligntext.align === 'justify' }" @click="commands.aligntext({ align: 'justify' })"></i>
@waaronking
waaronking / removeKeys.js
Created August 31, 2017 07:39
Recursively removes a key deep in an object
function removeKeys(obj, key) {
for (const prop in obj) {
if (prop.toString() === key.toString()) {
delete obj[prop];
} else if (typeof obj[prop] === 'object') {
this.removeKeys(obj[prop], key);
}
}
}
@waaronking
waaronking / fibonacci.js
Created October 15, 2015 17:43 — forked from jshcrowthe/fibonacci.js
Fibonacci Sequence (The cool way)
var fibonacci = function(n) {
return Array.apply(null, Array(n))
.reduce(function(sequence, value, index) {
return sequence.concat((index < 2) ? index : sequence[index - 1] + sequence[index - 2]);
}, []);
};
@waaronking
waaronking / gist:a777377712aa7804d7de
Created June 23, 2015 22:39
Calculates times between two dates
/* TEMPORARY: Returns time string between two dates in days, hours, minutes, and seconds */
var timeBetweenTwoDates = function(date1, date2) {
var seconds = Math.round((date1 - date2)/1000); //seconds
return calculateTime(seconds);
};
/* TEMPORARY: Calculates time between two dates. TO BE REPLACED WITH BETTER i18n supported SOLUTION */
var calculateTime = function(seconds) {
var seconds = seconds || 0
, minutes = 0
@waaronking
waaronking / check_for_loaded_script.js
Created March 30, 2015 18:10
Check for loaded script before executing
var loaded = false;
while (loaded === false) {
for (i in $('script')) {
if ($('script')[i].src && $('script')[i].src.indexOf('name_of_script_here') > -1) {
loaded = true;
optInLiHPExperience('a');
break;
}
}
@waaronking
waaronking / timeBetweenDates.js
Last active August 29, 2015 14:08
Time Between Two Dates
/* Returns time string between two dates in days, hours, minutes, and seconds */
var timeBetweenTwoDates = function(date1, date2) {
var seconds = Math.round((todaysDate - updateDate)/1000); //seconds
return calculateTime(seconds);
};
var calculateTime = function(seconds) {
var seconds = seconds || 0
, minutes = 0
, hours = 0
@waaronking
waaronking / getURLParams
Created October 30, 2014 17:11
getURLParameters
//Works fairly well
/* Get URL parameters */
function getUrlParam(key) {
var params = [], pair;
var pairs = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
if (key) {
return decodeURIComponent((new RegExp('[?|&]' + key + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
} else {
@waaronking
waaronking / findOffsetTop
Created June 9, 2014 20:41
Find the offsetTop of any item by recursively looking at the parent element until parent offset is 0
/* Finds the total offset of an item inside a window */
var findOffsetTop = function(el) {
var offset = el.offsetTop;
if (el.parentNode.offsetTop !== 0) {
offset += findOffsetTop(el.parentNode);
}
return offset;
};