Skip to content

Instantly share code, notes, and snippets.

View paul-bjorkstrand's full-sized avatar

paul-bjorkstrand

View GitHub Profile
@paul-bjorkstrand
paul-bjorkstrand / jQuery.selectors.js
Last active January 2, 2016 10:58
This function allows a developer to set attributes on an HTML element using selector-esque syntax, and return a selector-syntax of the current jQuery object. This is by no means production ready/complete. Hopefully someone finds this as useful as I do.
/*
Copyright 2014 Paul Bjorkstrand
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@paul-bjorkstrand
paul-bjorkstrand / jQuery.custom-ready.js
Created January 18, 2014 17:36
Register, handle, and trigger custom ready events in jQuery. This facility simplifies the handling of ready events aside from the global document ready. This could help avoid different pieces of javascript contending for $.holdReady, and allows applications wait until only what they need is complete.
(function($) {
"use strict";
var readys = {};
function newReady(name) {
Object.defineProperty(
$,
name, {
enumerable: false,
configurable: false,
@paul-bjorkstrand
paul-bjorkstrand / jQuery.global-ajax-2.js
Last active January 3, 2016 17:09
Global ajax management
/*
* Requires a minimal change to the $.ajax calls, adding:
* globalAjax: true
* globalAjax: {
* name: "" // optional
* usePostData: true // optional
* }
* As long as the globalAjax object is "truthy", it will store the response,
* at bare minimum using options.type + "|" + options.url as the key
*/
// Function.prototype.promisify
(function() {
"use strict";
if (Function.prototype.promisify) {
console.error("Function.prototype.promisify() already exists");
return;
}
var promiseFactory = function(context, f) {
(function() {
if (!Function.prototype.callFn) {
Function.prototype.callFn = function(scope/*, args */) {
var args = Array.prototype.slice.call(arguments, 1);
return this.applyFn(scope, args);
};
}
if (!Function.prototype.applyFn) {
Function.prototype.applyFn = function(scope, args) {
@paul-bjorkstrand
paul-bjorkstrand / show-hide-while.js
Created April 1, 2014 16:33
Show (or hide) an element until a function/promise is complete, then hide (or show) that element after the function/promise is complete.
(function($) {
$.fn.showWhile = function(fn) {
var obj = this.show();
var result = fn();
if (typeof result.then !== "function") {
result = $.Deferred.resolve(fn).promise();
}
@paul-bjorkstrand
paul-bjorkstrand / jQuery.center.js
Created May 6, 2014 13:34
Centers any element on a page
(function($, window) {
var $w = $(window);
$.fn.center = function() {
this.css({
position: "absolute",
left: Math.max(0, ($w.width() - this.outerWidth()) / 2 + $(window).scrollTop()) + "px",
top: Math.max(0, ($w.height() - this.outerHeight()) / 2 + $(window).scrollTop()) + "px"
});
};
@paul-bjorkstrand
paul-bjorkstrand / Math.min-max.js
Created May 6, 2014 13:36
Simple min-max, bounding a value between a minimum and maximum
if (!Math.minMax) {
Math.minMax = function(min, max, val) {
return Math.max(min, Math.min(max, val));
};
}
@paul-bjorkstrand
paul-bjorkstrand / jQuery.parents-selector.js
Created August 28, 2014 21:11
jQuery.parents-selector.js
(function($) {
$.expr[":"].parents = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).parents(arg).length > 0;
};
});
})(jQuery);
window.addEventListener('storage', function(e) {
if (e.storageArea === localStorage) {
var key = e.key;
if (data) {
$(document).trigger('storage.' + e.key, data);
}
}
});
$(document).on("storage.test", function(e, data) {