//
// Save page handler... Handles CTRL+S keystroke press
//
// Usage example :
//
// var n = new SaveHandler();
// n.addSaveHandler(function() {
// alert("Before save");
// });
//
// n.addSaveHandler(getElementById("submit_button_id"));
//
// n.addSaveHandler($("#submit_button_id")); // Multiplie object also pass via function call
//
// @author : Eugene (http://eugene.esy.es/works/programmer/js/jquery-save-handler)
//
function SaveHandler()
{
if ( ! jQuery ) throw new Error("jQuery required for this class");
var callptr = 1;
var domobject = 2;
var jqueryobj = 3;
var handlers = [];
// Exports addSaveHandler function
this.addSaveHandler = function(handler)
{
switch ( typeof(handler))
{
case "function" : handlers.push({ type: callptr, item: handler }); break;
case "object" : {
// DOM Object
if ( handler.hasOwnProperty('tagName')) {
handlers.push({ type: domobject, item: handler });
break;
}
// jQuery object
if (( handler instanceof jQuery ) && ( !! handler.has("*").length )) {
handlers.push({ type: jqueryobj, item: handler });
break;
}
}
default : console.error("Unsupported type of handler - supported only callback, dom object or jQuery object");
}
}
// Connect to Window add base handler
$(window).bind('keydown', function(event) {
if ( event.ctrlKey || event.metaKey ) {
switch ( String.fromCharCode(event.which).toLowerCase())
{
case 's' : { // CTRL+S
for ( var i=0 ; i<handlers.length ; i++ ) {
switch ( handlers[i].type ) {
case callptr : $i.item.call(this);
case domobject : $(handlers[i].item).click();
case jqueryobj : handlers[i].item.click();
}
}
event.preventDefault();
return false;
}
}
}
return true;
});
}