Skip to content

Instantly share code, notes, and snippets.

View barneycarroll's full-sized avatar
🛠️
Working on Mithril type stuff

Barney Carroll barneycarroll

🛠️
Working on Mithril type stuff
View GitHub Profile
@barneycarroll
barneycarroll / keepPasswordsHidden.css
Last active December 20, 2015 16:49
Stop Chrome popping up 'Show' buttons by your passwords.
#saved-passwords-list .list-inline-button {
display: none
}
@barneycarroll
barneycarroll / README.md
Last active August 29, 2022 12:02
Lock and unlock a page's scroll position.

jquery.scrollLock.js

Useful for when a blocking user experience is needed (in my case, didn't want people unwittingly loosing their place by scrolling while a modal required their attention): $.scrollLock() locks the body in place, preventing scroll until it is unlocked.

// Locks the page if it's currently unlocked
$.scrollLock();

// ...or vice versa
@barneycarroll
barneycarroll / jquery.whichclick.js
Last active May 8, 2018 08:42
Internet Explorer's click event's `which` property won't indicate which mouse button was clicked. To get around this, create distinct `leftclick`, `rightclick` and `middleclick` events which reliably prevent default behaviour. Also creates the `anyclick` event for convenience, which is essentially equivalent to `click` with `event.which` polyfil…
void function whichClickClosure( $ ){
var events = {
1 : 'leftclick',
2 : 'middleclick',
3 : 'rightclick'
},
// List of interruption events for symbolic linking between custom and native events
interrupts = [
'preventDefault',
'stopPropagation',
@barneycarroll
barneycarroll / jquery.lt2d.js
Created October 28, 2013 17:31
jQuery's `:hidden` selector only grabs elements if both their height and width are 0. My interpretation often desires it to apply to either height or width of 0 (including padding and border, natch). This does that. `:lt2d` stands for 'less than two-dimensional', since what this really does is returns elements that have one or less dimension in …
$.expr[':'].lt2d = function isLessThan2D( element ){
var dimensions = element.getBoundingClientRects();
return !dimensions.height || !dimensions.width;
}
@barneycarroll
barneycarroll / imagePromise.js
Created November 18, 2013 14:56
Quirk-proof promise for image loading using jQuery's Deferred and events interfaces. imagePromise( url ).then( function( img ){ /*...*/ } )
// Create a Promise for loading an image!
// Basically taking out the useful, tricky, and heavily refined bit from
// http://desandro.github.io/imagesloaded/
function imagePromise( src ){
var deferred = $.Deferred();
var img = new Image();
function resolve(){
// Resolution callbacks receive the image, which you can then inject into the DOM
// to avoid triggering an extra HTTP request in IE
@barneycarroll
barneycarroll / jquery.JSONML.js
Last active December 28, 2015 18:49
Convert JSONML to jQuery. http://www.jsonml.org/syntax/
$.JSONML = function jQueryJSONML( JSONML ){
var $output;
var fragment;
JSONML = $.extend( true, [], JSONML );
if ( $.isArray( JSONML[ 0 ] ) ){
fragment = true;
$output = $( '<x>' );
@barneycarroll
barneycarroll / jquery.tap.js
Created December 5, 2013 16:10
Utility to allow arbitrary code execution in the middle of a jQuery chain
// Utility to allow arbitrary code execution in the middle of a jQuery chain
$.fn.tap = function tap( fn ){
fn.call( this );
return this;
};
@barneycarroll
barneycarroll / lazyTimeout.js
Last active December 31, 2015 01:39
setTimeout override that uses the `window` `focus` event to defer execution of timers that weren't fired when the window was out of focus.
void function lazyTimeouts(){
// window inherits setTimeout from the DOMWindow constructor,
// which is innacessible in some browsers. In IE, window will
// often optimise lookup by assigning its inherited properties
// directly to itself. Below is the only reliable method of getting
// a firm reference to the native timer methods.
// http://www.adequatelygood.com/Replacing-setTimeout-Globally.html
window.setTimeout = window.setTimeout;
window.clearTimeout = window.clearTimeout;
@barneycarroll
barneycarroll / dateInput.js
Created December 20, 2013 15:28
A little function for converting between Javascript Date objects, date input values, and date input display strings. Only reliably works in Chrome & Opera desktop, whose date input displays happen to use the same format as the Date object's toLocaleString: Safari uses the same ISO standard for date attributes, values and display values.
var dateInput = ( function inputDateClosure(){
function getType( x ){
return Object.prototype.toString.call( x );
}
function isDate( x ){
return getType( x ) === '[object Date]';
}
function isInput( x ){
@barneycarroll
barneycarroll / jquery.toPromise.js
Created December 20, 2013 18:22
e.g: var promise = $( 'button' ).toPromise( 'on', 'click' ); …and then: promise.done( function( e ){ e.preventDefault(); } );
$.fn.toPromise = function toPromise( method, event ){
var deferred = $.Deferred();
this[ method ]( event, function resolvePromise(){
deferred.resolve.apply( this, $.makeArray( arguments ) );
} );
return deferred.promise();
};