Created
February 9, 2010 21:55
-
-
Save ryanflorence/299717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HashEvents = new Class({ | |
Implements: [Options, Events], | |
options: { | |
/* | |
onChange: $empty, | |
*/ | |
delimiter: '&', | |
poll: 200 | |
}, | |
initialize: function(options){ | |
this.setOptions(options); | |
this.last = false; | |
this.bound = { | |
focus: this.listen.bind(this), | |
blur: this.ignore.bind(this) | |
}; | |
}, | |
attach: function(){ | |
window.addEvents({ | |
focus: this.bound.focus, | |
blur: this.bound.blur | |
}); | |
return this; | |
}, | |
detach: function(){ | |
window.removeEvent('focus', this.bound.focus); | |
window.removeEvent('blur', this.bound.blur); | |
return this; | |
}, | |
listen: function(){ | |
$clear(this.periodical); | |
this.periodical = this.check.periodical(this.options.poll, this); | |
return this; | |
}, | |
ignore: function(){ | |
$clear(this.periodical); | |
return this; | |
}, | |
check: function(){ | |
var hash = window.location.hash; | |
if(this.checkHash(hash)){ | |
this.fireEvent('change'); | |
this.last = hash; | |
hash.substr(1).split(this.options.delimiter).each(function(hashPair){ | |
var splitPair = this.splitPair(hashPair); | |
var args = this.splitArgs(splitPair[1]); | |
this.fireEvent(splitPair[0], args); | |
}.bind(this)); | |
} | |
return this; | |
}, | |
checkHash: function(hash){ | |
return (hash != '' && hash != this.last); | |
}, | |
splitPair: function(hashKeyValue){ | |
return hashKeyValue.split('='); | |
}, | |
splitArgs: function(args){ | |
return (args) ? args.split(',') : false; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var manager; | |
window.addEvent('domready',function(){ | |
var log = $('log'); | |
manager = new HashEvents({ | |
poll: 100 | |
}); | |
manager.addEvents({ | |
here: function(arg){ | |
$('here').highlight(); | |
console.log(arg); | |
}, | |
there: function(one, two, three){ | |
$('there').highlight(); | |
console.log(one); | |
console.log(two); | |
console.log(three); | |
}, | |
every: function(){ | |
$('every').highlight(); | |
}, | |
where: function(){ | |
$('where').highlight(); | |
} | |
}).attach().listen(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment