Skip to content

Instantly share code, notes, and snippets.

@datchley
datchley / chrome-console-debug.js
Created April 16, 2014 16:53
Styled, timestamped console log messages for chrome's console API.
function _debug() {
var tm = "%c[" + (new Date()).toTimeString().split(/\s+/)[0] + "]",
pre = "%c debug ",
args = [].slice.call(arguments, 0),
// styles for: timestamp, prefix, message
styles = ["color: #4682b4", "color: #fff; background-color: #cd5c5c;", "color: #2f4f4f"],
msg = tm + pre + "%c " + args.shift();
console.log.apply(console, [msg].concat(styles, args));
}
@datchley
datchley / template-parser.php
Last active September 26, 2019 16:49
Quick and Dirty Template parsing in PHP
<?php
// the template
$template = "<h1>{TITLE}</h1>";
// add any template placeholder key/values here
$map = array(
'TITLE' => "ExtJS Sucks"
);
// Quick and dirty template parser, replace occurences of '{KEY}' with
@datchley
datchley / function-extensions.js
Last active August 29, 2015 13:58
Function.prototype extensions, including a bind polyfill and a Function.delay (setTimeout equiv) and Function.repeat (setInterval equiv).
var has_bind_native = Function.prototype.bind ? true : false;
/**
* An ES5 compaitible polyfill for Function.prototype.bind(thisArg, arg1, arg2, ...). Allows you to
* rebind a function to a different this context. This is already available in browsers implementing
* the ES5 standards.
*
* @param {Object} _context - the new object to bind this to in the function
* @returns {Function} - the newly bound function
*/
@datchley
datchley / serialize-form-json.js
Last active August 29, 2015 13:58
Dead simple function to serialize a form as a JSON object, using '.' notation in the field 'name' attribute and only serializing fields with a class of 'serialize'.
window.serializeFormJSON = function(f) {
var json = {},
$f = f.jquery ? f : $(f);
$('input.serialize, select.serialize, textarea.serialize', $f).each(function(index, node) {
var keypath = $(this).attr('name').split('.'),
key = keypath.pop(),
item = json;
for (var i=0, l=keypath.length; i < l; i++) {
@datchley
datchley / component-data.js
Last active August 29, 2015 13:57
A Simple Flight.js Component and Mixin (using standalone flight.js)
// Simple Data Component to handle authorization calls
var Auth = flight.component(function() {
this.defaultAttrs({
user: 'default'
});
this.authenticate = function(ev, data) {
console.log("[debug] calling authenticate");
$.ajax({
method: 'POST',
@datchley
datchley / data-utils.js
Last active December 25, 2015 12:59
The ability to do a deep copy of objects and arrays in javascript, handling Dates, RegEx and others correctly; along with a deep equals for comparing javascript objects. Both functions handle native types as well as objects and arrays; and the deep equals function should treat arrays with the same values; but not in the same order as equal as we…
if (!Array.isArray) {
Array.isArray = function(a) { return Object.prototype.toString.call(a) == '[object Array]'; }
}
if (!Object.isObject) {
Object.isObject = function(a) { return Object.prototype.toString.call(a) == '[object Object]'; }
}
// Clone an object or native type in javascript
// Exceptions (doesn't handle these types):
@datchley
datchley / iframe-overlay.js
Created October 2, 2013 13:33
For each iframe on a page, build an overlay on top of it that captures click events and sends them to the document in the iframe. This is to allow mobile scrolling in webkit, since it seems touching the iframe when swiping won't bubble up the touch event to the main page and hence won't scroll the page. This only works if you control the content…
$(document).ready(function() {
"use strict";
var ident = 0; // generate unique element id
// Find all iframe DFP ads
$('iframe').each(function() {
var width = $(this).width(),
height = $(this).height(),
@datchley
datchley / set-iframe-size-onload.js
Created October 2, 2013 13:06
An attempt to set each iframe's width/height on the page to the width/height of it's contained document.
$('document').ready(function() {
$('iframe').load(function() {
'use strict';
var self = this;
setTimeout(function() {
var iframe = self,
w = $(iframe).width(), h = $(iframe).height(),
docw = iframe.contentWindow.document.width,
doch = iframe.contentWindow.document.height;
@datchley
datchley / mixins.js
Last active December 23, 2015 11:01
Inspired by functional mixins in frameworks like Twitter's Flight, I thought I'd work up a version that handled both functional and object based mixins, and with variable arguments instead of having to pass in an array of mixins to add.
var mixin = function(target /*, mixins */) {
var args = [].slice.call(arguments, 1);
target.mixins = target.hasOwnProperty('mixins') ? target.mixins : [];
args.forEach(function(mixin) {
if (target.mixins.indexOf(mixin) === -1) {
if (typeof mixin === 'function') {
// Functional mixin using 'call'
mixin.call(target);
}
@datchley
datchley / vimrc
Created August 28, 2013 18:23 — forked from heygarrett/vimrc
" Set colorscheme to solarized
colorscheme solarized
" Change the Solarized background to dark or light depending upon the time of
" day (5 refers to 5AM and 17 to 5PM). Change the background only if it is not
" already set to the value we want.
function! SetSolarizedBackground()
if strftime("%H") >= 5 && strftime("%H") < 17
if &background != 'light'
set background=light