Created
June 9, 2010 19:01
-
-
Save RStankov/431990 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
(function(){ | |
function isForm(element) { | |
return element.nodeName.toUpperCase() == 'FORM'; | |
} | |
function isInput(element) { | |
var name = element.nodeName.toUpperCase() | |
return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'; | |
} | |
var EMULATED_RULES = { | |
submit: isForm, | |
change: isInput, | |
focus: isInput, | |
blur: isInput | |
}, | |
EMULATED_EVENTS = (function(){ | |
var element = document.createElement("div"); | |
// credits to Juriy Zaytsev ( http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/ ) | |
function isEventSupported(eventName){ | |
eventName = "on" + eventName; | |
if (eventName in element){ | |
return true; | |
} | |
element.setAttribute(eventName, "return;"); | |
return Object.isFunction(element[eventName]); | |
} | |
var events = ['change', 'submit', 'focus', 'blur'].reject(isEventSupported); | |
element = null; | |
return events; | |
})(); | |
if (EMULATED_EVENTS.length > 0){ | |
(function(){ | |
var emulate = { | |
submit: function(){ | |
function emulateSubmit(e){ | |
if (this.fire('emulated:submit').returnValue === false){ | |
e.preventDefault() | |
} | |
} | |
document.on('focus', 'form', function(e, element){ | |
if (element.retrieve('emulated:submit')){ | |
element.store('emulated:submit', element.on('submit', emulateSubmit)); | |
} | |
}); | |
document.on('blur', 'form', function(e, element){ | |
var observer = element.retrieve('emulated:submit'); | |
if (observer){ | |
observer.stop(); | |
} | |
}); | |
}, | |
change: function(){ | |
var key = 'emulated:change:current_value'; | |
document.on('focus', 'input[type=text],input[type=file],select,textarea', function(e, element){ | |
element.store(key, element.getValue()); | |
}); | |
document.on('blur', 'input[type=text],input[type=file],select,textarea', function(e, element){ | |
if (element.retrieve(key) != element.getValue()){ | |
element.fire('emulated:change'); | |
} | |
element.getStorage().unset(key); | |
}); | |
}, | |
focus: function(){ | |
function focusHandler(e){ | |
e.findElement().fire("emulated:focus"); | |
} | |
if (document.addEventListener){ | |
document.addEventListener("focus", focusHandler, true); | |
} else { | |
document.observe("focusin", focusHandler); | |
} | |
}, | |
blur: function(){ | |
function blurHandler(e){ | |
e.findElement().fire("emulated:blur"); | |
} | |
if (document.addEventListener){ | |
document.addEventListener("blur", blurHandler, true); | |
} else { | |
document.observe("focusout", blurHandler); | |
} | |
} | |
}; | |
EMULATED_EVENTS.each(function(eventName){ | |
Event.register('emulated' + eventName, emulate[eventName]); | |
}); | |
})(); | |
Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(function(initialize, element, eventName, selector, callback) { | |
element = $(element); | |
if (element && selector && EMULATED_EVENTS.indexOf(eventName) > -1 && EMULATED_RULES[eventName](element)){ | |
eventName = 'emulated:' + eventName; | |
} | |
initialize(element, eventName, selector, callback); | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment