Skip to content

Instantly share code, notes, and snippets.

@astromac
astromac / binarytreesearch.js
Last active July 17, 2020 19:49
Binary Tree Search
/**
see https://codepen.io/chrisbfusion/pen/oNbyppO
sample tree visual
30
|
-------
| |
8 52
|
/* Returns the absolute page position
* of a given element.
*/
const absoluteElementPosition = node => {
if (isEmpty(node)) return null
let element = node
let left = 0
let top = 0
do {
@astromac
astromac / findParentElement.js
Last active March 4, 2020 14:00
Traverse through DOM
/* Locates the first parent element
* that has a given attribute.
*/
const findParentElement = (node, attribute) => {
if (isEmpty(node) || isEmpty(attribute)) return null;
let element = node;
/* eslint-disable no-cond-assign, no-empty */
while (
(element = element.parentElement) &&
@astromac
astromac / isEmpty.js
Last active April 20, 2021 14:15
isEmpty
/** Determines if passed value is empty or not.
* Works on multiple data types.
**/
const isEmpty = data => {
let count = 0;
if (typeof (data) === 'number' || typeof (data) === 'boolean') { return false; }
if (typeof (data) === 'undefined' || data === null) { return true; }
if (typeof (data.length) !== 'undefined') { return data.length === 0; }
Object.keys(data).forEach(() => { count += 1; });
return count === 0;
@astromac
astromac / cookies.js
Last active August 29, 2015 14:07
Get / Set Cookie
var setCookie = function (cookieName, cookieValue, expireDays) {
var date = new Date(),
expires = '';
date.setTime(date.getTime() + (expireDays * 24 * 60 * 60 * 1000));
expires = 'expires=' + date.toUTCString();
document.cookie = cookieName + '=' + cookieValue + '; ' + expires + '; ' + 'domain=' + document.location.hostname + '; path=/';
};
var getCookie = function(cookieName) {
@astromac
astromac / Bootstrap Template
Last active January 3, 2016 14:59
Starting HTML5 Bootstrap template
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
@astromac
astromac / Unhide Inputs
Last active January 3, 2016 03:39
Scrolls form inputs when they are hidden by a fixed header or footer
// Make sure focused elements aren't hidden by fixed positioned header or footer
function unhideInput(formelement) {
formelement.onfocus = function () {
var paddingheight = 30;
var headerheight = document.getElementsByTagName('header')[0].offsetHeight;
var footerheight = document.getElementsByTagName('footer')[document.getElementsByTagName('footer').length - 1].offsetHeight;
var viewportheight = window.innerHeight;
var elementpos = this.getBoundingClientRect();
if ((elementpos.top <= (headerheight + paddingheight)) || (elementpos.bottom >= (viewportheight - footerheight))) {
@astromac
astromac / Bootstrap Modal Video Player html
Created January 8, 2014 23:26
HTML5 video in a Bootstrap modal window.
<div class="modal-trigger">
<canvas></canvas>
<video preload="auto" controls="controls" poster="img/why-autologel-poster.png">
<source src="media/why-autologel.mp4" type='video/mp4'>
<source src="media/why-autologel.webm" type='video/webm'>
</video>
</div>
<!-- Modal Window -->
<div class="modal fade" id="modal-window" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
@astromac
astromac / Bootstrap Modal Video Player js
Created January 8, 2014 23:21
Plays selected video element in a Bootstrap modal window.
// Play very small videos in modal box
if ($(window).width() > 750) {
var allvideos = $('video');
// Hide controls for very small videos
for (var i = 0; i < allvideos.length; i++) {
if ($(allvideos[i]).width() < 470) {
$(allvideos[i]).removeAttr('controls');
// Insert poster image for IE 9
if ($('html').hasClass('IE-9')) {
@astromac
astromac / Remove no-js
Created January 8, 2014 23:00
Removes no-js class
// Remove no-js class
var tag = document.getElementsByTagName('html')[0];
if (tag.className.length > 0 && /no-js/i.test(tag.className)) {
tag.className = tag.className.replace(/\s?no-js\s?/i, '');
} else {
tag.className = '';
}