Skip to content

Instantly share code, notes, and snippets.

View gmac's full-sized avatar

Greg MacWilliam gmac

View GitHub Profile
@gmac
gmac / Nixable Controller
Created February 13, 2013 22:55
Click-off dismissal behavior for a component that should close upon any mouse click, touch, or resize.
require("nixable", [
"jquery",
"underscore"
], function( $, _ ) {
return {
nixable: function( closeTest, closeCall, context ) {
var self = this;
// Clear any outstanding nix call:
@gmac
gmac / csv-parser
Last active December 17, 2015 13:59
CSV Parser
function parseCSV(csv, delimiter) {
delimiter = (delimiter || ",");
var pattern = new RegExp("(\\"+ delimiter +"|\\r?\\n|\\r|^)"+"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|"+"([^\"\\"+ delimiter +"\\r\\n]*))", "gi");
var quote = new RegExp("\"\"", "g");
var data = [[]];
var matches = null;
var val, d;
while (matches = pattern.exec(csv)) {
@gmac
gmac / simple-ajax
Last active December 17, 2015 15:10
API: ajax('url', callbackFunction?, postData?); --> returns: a handle object with an ".abort();" method for canceling the request.
var ajax = (function( root ) {
function getRequest() {
if (root.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
else if (root.XMLHttpRequest) return new XMLHttpRequest();
else return null;
}
return function(url, callback, post) {
post = post || "";
@gmac
gmac / expression-parser
Last active December 17, 2015 17:28
Simple lexical analysis for a JavaScript expression. Written as a prototype for pre-parsing JavaScript bindings (Epoxy.js). Will alphabetically sort the order of object property lists while removing all non-string whitespace. Example expression: 'text: all("a", "x", "c"), css:{fontWeight:"bold", color:["#f00, \\"#fee", "#00f"]}, html:firstName'
function parseBindings(bindings, sorted) {
var attrs = [];
var token = '';
bindings += ',';
for (var i = 0, len = bindings.length; i < len; i++) {
var c = bindings.charAt(i);
// Comma: chunk off tokens at list delimiter.
if (c == ',') {
@gmac
gmac / assign.sh
Created March 4, 2015 23:37
Assign files to all students
# Copy assignment into each student's folder
# Run this from the day's folder
# example:
# `~/Dev/wdi/homework/week_01/d_01`
# `assign`
assign(){
# path to assignment. defaults to assignment folder.
assignment=${1:-"ASSIGNMENT_FILES/"}
# get list of directories
@gmac
gmac / tree.json
Last active September 28, 2015 22:23
[
{
"id": "a",
"parent": null
},
{
"id": "b",
"parent": "a"
},
{
{
"android_icon": "https://cdn0.vox-cdn.com/dev/uploads/chorus_asset/file/7045788/sandbox-www-data-ip-10-61-185-107_/sandbox_VergeFlavicon192.0.png",
"cache_version": "201",
"color_accent": "#e5127d",
"color_body": "#424242",
"color_body_bg": "#ffffff",
"color_body_link_underline": "currentColor",
"color_border_nav": "transparent",
"color_border_thick": "#EFEFF0",
"color_breaker_bg": "#EFEFF0",
import Initializer from 'presto/initializer';
var $loaded = $.Deferred();
Initializer.registerComponent('site/font_loader', function(element, data) {
const STORAGE_NS = 'chorus-fonts-'+ data.version;
const FONTS_LOADED = ' fonts-loaded';
const SUCCESS = function() {};
var FontFaceObserver = require('./scripts/vendor/fontfaceobserver');
@gmac
gmac / theme_editor.js
Created March 16, 2017 19:48
Notes for theme editor talk
// THEME
// Get plain variable text:
theme.getVar('color_breaker_fg')
// Get rendered variable value:
theme.getVarValue('color_breaker_fg')
// Compile mapped field dependencies:
theme.getDependencyGraph()
{
"domain": "vox.com",
"cells": [
{
"slug": "UnisonNavListCell",
"bucket": "vox.com",
"data": {
"presto_component_data": "{\"trending_pinned\":false,\"communities_pinned\":false,\"nav_items\":[{\"title\":\"Politics\",\"url\":\"/policy-and-politics\",\"pinned\":false,\"all_link\":{\"title\":\"\",\"url\":\"\"},\"sub_items\":[],\"group_id\":30770},{\"title\":\"Explainers\",\"url\":\"/explainers\",\"pinned\":true,\"sub_items\":[],\"group_id\":24365},{\"title\":\"World\",\"url\":\"/world\",\"pinned\":false,\"all_link\":{\"title\":\"\",\"url\":\"\"},\"sub_items\":[],\"group_id\":30778},{\"title\":\"Culture\",\"url\":\"/culture\",\"pinned\":false,\"all_link\":{\"title\":\"\",\"url\":\"\"},\"sub_items\":[],\"group_id\":27608},{\"title\":\"Science & Health\",\"url\":\"/science-and-health\",\"pinned\":false,\"all_link\":{\"title\":\"\",\"url\":\"\"},\"sub_items\":[],\"group_id\":30772},{\"title\":\"Identities\",\"url\":\"/identities\",\"pinned\":false,\"all_link\":{\"title\":\"\",\"url\":\"\"},\"sub_items\":[],\"group_