Skip to content

Instantly share code, notes, and snippets.

View appden's full-sized avatar

Scott Kyle appden

View GitHub Profile
function $E(tag, props) {
if (typeof props == 'string')
props = { style : props };
if (typeof tag == 'string') {
var id = tag.match(/#([\w-]+)/);
var classes = tag.match(/(?:\.[\w-]+)+/);
tag = tag.replace(/[#.].*/, '');
props = props || {};
if (id) props.id = id[1];
if (classes) props['class'] = classes[0].replace(/\./g, ' ');
Native.implement([Element, Window, Document, Events], {
oneEvent : function(type, fn) {
return this.addEvent(type, function() {
this.removeEvent(type, arguments.callee);
return fn.apply(this, arguments);
});
}
});
/*
Element.Events.clickout = {
base : 'click',
condition : function(event){
event.stopPropagation(); // stop event bubbling to the body
return false; // never run handler when clicking on element
},
onAdd : function(fn){
this.getDocument().addEvent('click', fn);
},
onRemove : function(fn){
Class.Mutators.BindAll = function(self, bool) {
if (!bool) return self;
var init = self.initialize;
var exclude = arguments.callee.exclude;
self.initialize = function() {
for (var method in this) {
if (typeof this[method] != 'function' || exclude.contains(method))
continue;
Class.Initializers = {};
Class.Mutators.initialize = function(initialize){
return function(){
this._init_ = initialize;
for (var modifier in Class.Initializers) {
if (!this[modifier]) continue;
this[modifier] = Class.Initializers[modifier].call(this, this[modifier]);
// Auto-updating Date stuff
Date.pretty = new Class({
Implements: Options,
options: {
interval: 60000,
threshold: '24 hours'
},
// FUNCTION BINDING
Function.prototype.bind = function(bind, args){
var self = this.unbind();
if (args != null) args = $splat(args);
var fn = function(){
return self.apply(bind, args || arguments);
};
// inspired by jQuery watch: http://plugins.jquery.com/files/jquery-watch.js.txt
(function(){
var watch = Object.prototype.watch,
unwatch = Object.prototype.unwatch,
watched = [],
timer;
Object.watch = function(obj, prop, fn){
String.implement({
truncate: function(max, trail, separator){
var s = this.trim();
if (s.length <= max) return s;
separator = $pick(separator, ' ');
if (separator)
s = s.substr(0, max + 1).substr(0, s.lastIndexOf(separator));
else
s = s.substr(0, max);
Element.implement({
// truncates element text to fit its width
truncate: function(trail){
var text = this.get('text').trim();
if (!text) return;
trail = trail || '...';
var style = this.style;
var styles = { overflow: style.overflow, 'white-space': style.whiteSpace, padding: style.padding, margin: style.margin };