Skip to content

Instantly share code, notes, and snippets.

@postpostscript
postpostscript / jquery.ajax-load.js
Last active June 14, 2016 19:51
ajax-load html attribute
$(function() {
$('[ajax-load]').each(function() {
var $this = $(this);
$this.load($this.attr('ajax-load'), function() {
if ($this.attr('ajax-onloaded')) {
eval($this.attr('ajax-onloaded'));
}
});
});
});
@postpostscript
postpostscript / gist:5f968939ffbe72bbf7b0c94084ba47b4
Created June 16, 2016 17:03
Encoding data into an order of a fixed number of array indexes
function getOptions(len) {
return Array(len).join().split(",").map(function(c, i) {
return i;
});
}
function getStep(i, len) {
return factorial(len) / len * i;
}
function to(order) {
<?php
function replace_template_strings($template, $data = array(), $beginning_regex = '\{\{', $end_regex = '\}\}') {
return preg_replace_callback('/' . $beginning_regex . '(\w+)' . $end_regex . '/', function($match) use ($data) {
return empty($data[$match[1]]) ? "" : $data[$match[1]];
}, $template);
}
<?php
function make_select($name, $options, $selected = "", $additional_attrs = "") {
$res = "<select";
if ($name) {
$res .= " name='$name'";
}
if ($additional_attrs) {
$res .= " $additional_attrs";
}
<?php
function export_variables_to_js($vars = array(), $parent = "window") {
$js_vars = array();
foreach ($vars as $key => $val) {
$key = json_encode($key);
$val = json_encode($val);
if ($parent) {
$js_vars[] = "$parent[$key] = $val";
} else {
@postpostscript
postpostscript / replify
Last active November 7, 2024 02:47
replify - Create a REPL for any command
#!/bin/sh
command="${*}"
printf "Initialized REPL for `%s`\n" "$command"
printf "%s> " "$command"
read -r input
while [ "$input" != "" ];
do
eval "$command $input"
printf "%s> " "$command"
import json, os.path
def json_load(filepath):
return json.load(open(filepath))
def valid_filenames(filenames):
return map(os.path.realpath, filter(os.path.exists, filenames))
def merge(*dicts):
res = {}
var scrollLock = {
scrollTop: 0,
lock: function(scrollTop) {
if (typeof scrollTop == "number") {
this.scrollTop = scrollTop;
}
$(window).on("scroll", this.onScroll);
},
unlock: function() {
$(window).off("scroll", this.onScroll);
<?php
function convert_spreadsheet_rows_to_sql($table_name, $row_mappings = array(), $rows = array(), $start_row = 2) {
$mappings = array();
$fields = array();
foreach ($row_mappings as $ind => $field) {
if ($field) {
$mappings[] = $ind;
$fields[] = $field;
}
}
$(function() {
$.fn.toggleFullscreen = function() {
var elem = this[0];
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();