Skip to content

Instantly share code, notes, and snippets.

View m3g4p0p's full-sized avatar
💭
waits until idle

m3g4p0p

💭
waits until idle
View GitHub Profile
@m3g4p0p
m3g4p0p / visible.js
Created June 12, 2016 18:26
jQuery extension to reduce the set of matched elements to those being (vertically) in the viewport
$.fn.visible = function() {
'use strict';
var $window = $(window),
windowScrollTop = $window.scrollTop(),
windowHeight = $window.height();
return this.filter(function() {
var $this = $(this),
thisOffsetTop = $this.offset().top,
@m3g4p0p
m3g4p0p / json2sql.php
Last active June 20, 2016 22:52
A simple script to iterate over an associative array (from a JSON file), create an SQL schema and populate it with the data
<?php
$idMap = [];
function json2sql($current, $parent, $json) {
global $idMap;
$table = "";
$insert = "";
$children = "";
@m3g4p0p
m3g4p0p / autocomplete.js
Last active June 21, 2016 19:23
A JS autocomplete module
var Autocomplete = function(element, suggestions) {
'use strict';
/**
* Variables
*/
var container = document.createElement('div');
var list = document.createElement('ul');
var listItems = [];
var current = [];
@m3g4p0p
m3g4p0p / jquery-viewport.js
Last active July 21, 2016 22:59
jQuery plugin to reduce the set of elements to those being within the viewport
/**
* Plugin to reduce a set of elements to those
* visible in the viewport
*
* @param {Object}
* @param {Function}
* @param {Function}
* @return {jQuery}
*/
(function($) {
$.fn.observe = function(callback, options) {
options = options || {
attributes: true,
childList: true,
characterData: true
};
$(this).each(function() {
var observer = new MutationObserver(callback.bind(this));
observer.observe(this, options);
(function($) {
$.fn.watch = function(callback, once) {
var options = {
childList: true,
subtree: true
};
var _callOnNode = function() {
@m3g4p0p
m3g4p0p / logdash.js
Last active July 22, 2016 17:23
Allows logging messages to the console.
window._ = function() {console.log(...arguments)};
@m3g4p0p
m3g4p0p / state_archivist.php
Last active August 19, 2016 07:37
Store and restore the states of objects
<?php
/**
* Optional convenience class. Objects extending the
* StateHandler class can register with a StateArchivist
* instance; then calling remember() or restore() on the
* archivist will be propagated to all registered objects.
*/
class StateArchivist {
/**
@m3g4p0p
m3g4p0p / dom-helpers.js
Last active August 26, 2016 15:51
Some ideas for simply DOM helper functions
/**
* Get an array (!) of elements in a given context
*/
const find = (selector, context) =>
Array.from((context || document).querySelectorAll(selector));
/**
* Create a new element and optionally initialize it
* with some content and attributes
*/
@m3g4p0p
m3g4p0p / array_access.php
Created September 5, 2016 20:50
Access a specific element of a nested array
<?php
/**
* Return the reference to an element/subarray
* of an array, specified by a key array
*
* @param array &$array Array to access by reference
* @param mixed $keys Array of the keys
* @return mixed Reference to the match
*/
function &array_access(&$array, $keys) {