Skip to content

Instantly share code, notes, and snippets.

@aradnom
aradnom / round-to.js
Created February 20, 2014 00:47
Round number to nearest <n> (5, 10, 50, etc.)
// Round value to nearest <n>
// multiplier allows values to be multiplied and stay rounded to nearest <n>
// (i.e. 1732 * 2 => nearest 50)
function RoundTo ( value, to, multiplier ) {
multiplier = multiplier || 1;
return Math.round( ( parseInt( value ) * multiplier ) / to ) * to;
}
@aradnom
aradnom / ellipsize.js
Created February 21, 2014 23:44
Ellipsize text in a container with elements. Note that the base function only looks at text, so it's possible to split an inline element. Example of use included.
// Example of ellipsizing
$( function () {
var ellipsis_length = 10;
// Ellipsize text
var text_elements = $('.promotion__content__text *');
// Flip through content elements looking for ellipsis boundary
var boundary = null;
text_elements.each( function() {
@aradnom
aradnom / wp-clear-all-crons.php
Created March 28, 2014 16:33
Clear all crons in WordPress. This is a nuclear option, so use with care.
// Pull all crons from options
$crons = get_option('cron');
// Kill each one. Filter by timestamp or hook_name if desired.
foreach ($crons as $timestamp => $hook) {
foreach ($hook as $hook_name => $details) {
wp_unschedule_event( $timestamp, $hook_name );
}
}
@aradnom
aradnom / grunticon-svg-inliner.js
Last active August 29, 2015 14:03
Grunticon SVG inliner - automatically replace grunticon-produced SVG backgrounds with inline SVGs. Framework-agnostic.
(function () {
var waiting = setInterval( function () {
var nodes = document.querySelectorAll('.inline-svg[class^="icon-"], .inline-svg[class*=" icon-"]');
for ( var i = 0; i < nodes.length; i++ ) {
// Pull the background in (this should always be a background image)
var background = document.defaultView.getComputedStyle(nodes[i])['background-image'];
// Check to see if an element has a background - if so, background has loaded
if (background && background !== 'none') {
if ( waiting ) clearInterval( waiting );
@aradnom
aradnom / jscrollpane-mouseup-fix.js
Created July 11, 2014 23:31
jScrollPane is a neato custom scrollbar plugin, but it won't unbind drag mouseup events correctly within a Google Maps container (it binds these events to html and the unbind never fires). The following resolves this.
$('.jscrollpane__container').jScrollPane().bind( 'mouseup.jsp', function () {
$('html').unbind('dragstart.jsp selectstart.jsp mousemove.jsp mouseup.jsp mouseleave.jsp');
// This will remove the active class from ALL windows, so keep that in mind
// if that isn't the desired behavior.
$('.jspActive').removeClass( 'jspActive' );
});
@aradnom
aradnom / youtube-embed-existing-iframe.js
Created July 29, 2014 21:28
YouTube API example wrapping existing iframe elements.
/* Youtube */
window.onYouTubeIframeAPIReady = function () {
window.youtube_players = [];
$('.video_container.youtube').each( function () {
// Only set the play functionality up if the video has a thumbnail
if ( $(this).find('.video_thumbnail').length ) {
var thumbnail = $(this).find('.video_thumbnail'),
play = $(this).find('.video_play'),
player = new YT.Player( $(this).children('iframe').attr('id') );
window.youtube_players.push( player );
@aradnom
aradnom / vimeo-embed-existing-iframe.js
Created July 29, 2014 21:29
Simple Vimeo embed example wrapping existing video iframe.
/* Vimeo */
window.vimeo_players = [];
$('.video_container.vimeo').each( function () {
$f($(this).find('iframe')[0]).addEvent( 'ready', (function ( player_id ) {
if ( ! $(this).hasClass('no_thumbnail') ) {
var thumbnail = $(this).find('.video_thumbnail'),
play = $(this).find('.video_play'),
player = $f( player_id );
window.vimeo_players.push( player );
var thumbnail_height = thumbnail.find('img').height();
@aradnom
aradnom / array-intersect.php
Created February 4, 2015 21:00
Performs a true array intersection (array_uintersect will keep items in a1 even if they are not present in a2, a3, etc.)
$combined = array_filter($array1, function ($a) use ($array2) {
$found = array_filter($array2, function ($b) use ($a) {
return ((int) $a->ID) === ((int) $b->ID);
});
return !empty($found);
});
@aradnom
aradnom / camelcase-to-display.js
Created March 2, 2015 19:02
Given a camelcased string, return the formatted (display version) of the string, i.e. returnThisString --> Return This String
return ('' + str).match( /[A-Z]?[a-z]+/g )
.map( function ( v ) { return v.substr( 0, 1 ).toUpperCase() + v.substr( 1, v.length ); } )
.join( ' ' );
@aradnom
aradnom / repeatDoneEvent.js
Created March 30, 2015 17:19
Simple directive for firing an event when ng-repeat is finished populating.
'use strict';
/**
* Simple directive for emitting an event when repeat is finished
*/
App.directive( 'repeatDoneEvent', function ( $window ) {
return function( scope, element, attrs ) {
if ( scope.$last ) {
// At this point, ng-repeat is done populating - but we're not finished
// yet because $compile still has to compile any tags in the repeat