public
Description: JavaScript that spies on your forms and warns people if they try to leave a form with unsaved changes.
Homepage: http://indielabs.com/entries/the-informant/
Clone URL: git://github.com/ihearithurts/informant.git
informant / informant.min.js
100644 1 lines (1 sloc) 0.886 kb
1
Element.addMethods({hasSubmitButton:function(a){var b=$(a).identify();return $$("#"+b+' input[type="submit"]',"#"+b+' input[type="image"]',"#"+b+" button").length>0}});var Informant={message:"All unsaved changes will be lost.",skipClass:"uninformed",delay:1000,initialize:function(){this.forms=$$("form:not([class~="+this.skipClass+"])").reject(function(a){return !a.hasSubmitButton()}.bind(this));if(this.forms.length===0){return}this.forms.each(function(a){a.before=a.serialize();a.unsaved=function(){return this.before!=this.serialize()};Event.observe(a,"submit",function(b){a.before=a.serialize()})});window.onbeforeunload=function(){if(this.unsaved()){return this.message}}.bind(this)},unsaved:function(){return this.forms.find(function(a){return a.unsaved()})!==undefined}};document.observe("dom:loaded",function(){setTimeout(function(){Informant.initialize()},Informant.delay)});