Skip to content

Instantly share code, notes, and snippets.

View timmywil's full-sized avatar
🚀
Working on @spokestack

Timmy Willison timmywil

🚀
Working on @spokestack
View GitHub Profile
// get xml file & publish
km.core = {
url: 'someDir/someFile',
fetch: $.get(url, function(xml) {
$.publish('xmlFetch', [xml]);
})
};
// subscribe to xml file and do stuff
km.widget.edVolGraph = {
@timmywil
timmywil / Field_Selections.js
Created January 25, 2011 01:08
Get/Set field selections
/**
* Gets the current cursor position in a textfield
* to determine where to delete/update a character.
* @param {Element} field The raw field to get the cursor pos from.
* @return {Number} The index for the position of the cursor.
*/
function getCursorPosition( field ) {
if ( field != null ) {
// IE
if ( document.selection ) {
@timmywil
timmywil / jquery.keyframe.js
Created May 28, 2011 20:13
Oldschool Keyframes in jQuery
(function( $ ) {
$.fn.keyframe = function(){
if (arguments.length == 0) {
return this;
}
var self = this,
args = Array.prototype.slice.call( arguments ),
// List of keyframes
@timmywil
timmywil / page_management.js
Created June 20, 2011 21:46
On a medium-sized site, you may want your javascript all in the same file, but only process certain functions based on element presence.
/**
* Internal page management
* Add selector-dependent js to the array
* or call page to execute all js relevant to the current page
* @param {String} selector A jQuery selector
* @param {Function} fn A function to execute if sel is present on the page
*/
var pages = [];
var page = function( selector, fn ) {
@timmywil
timmywil / jquery.hasattr.js
Created July 10, 2011 17:55 — forked from rwaldron/jquery.hasattr.js
A commonly requested feature that is simply not worth adding to jQuery core. Usage: http://jsfiddle.net/rwaldron/kSmP2/
/*!
* jQuery.fn.hasAttr()
*
* Copyright 2011, Rick Waldron
* Licensed under MIT license.
*
*/
(function( jQuery ) {
jQuery.fn.hasAttr = function( name ) {
for ( var i = 0, l = this.length; i < l; i++ ) {
@timmywil
timmywil / step.js
Created July 13, 2011 05:25
Callback execution after return false
if ( gotoEnd || t >= options.duration + this.startTime ) {
this.now = this.end;
this.pos = this.state = 1;
this.update();
options.animatedProperties[ this.prop ] = true;
for ( p in options.animatedProperties ) {
if ( options.animatedProperties[ p ] !== true ) {
done = false;
@timmywil
timmywil / toggle-animate.js
Created September 14, 2011 19:37
Toggle animations with jQuery 1.7
$('#open-menu').click(function() {
// No more .stop( true, true )
// jQuery will remember state automatically
$someMenu.stop().slideToggle(1000);
});
@timmywil
timmywil / ie_method_call.js
Created October 24, 2011 14:57
IE calls methods simply when accessed. This is not always an issue, but can be problematic. Below is a failing test case using XML. This issue warrants the need for other ways to check for the presence of methods.
var xml = $.parseXML('<crazy><element>asdf</element><div>asdf</div></crazy>'),
counter = 0;
console.log( xml, $(xml).find('*') );
$(xml).find('*').andSelf().each(function( i, elem ) {
if ( elem.getElementsByTagName ) {
counter++;
}
});
@timmywil
timmywil / module_defer.js
Created October 27, 2011 14:11
Modules with deferred dependencies
// This could be compatible with jQuery's Deferred implementation,
// futures.js (slightly different syntax) or any one of a number
// of other implementations
define(['lib/Deferred'], function( Deferred ){
var defer = new Deferred();
require(['lib/templates/?index.html','lib/data/?stats'],
function( template, data ){
defer.resolve({ template: template, data:data });
}
);
@timmywil
timmywil / avaya_draggable.js
Created October 28, 2011 13:41
A small draggable implementation for a lightbox
/**
* @license avaya.js
* Load avaya webchat lightbox with iframe
* Copyright (c) 2011 EPB
*/
define([ 'jquery', 'jquery-plugins/minLight' ],
function( $ ) {
"use strict";