X-Git-Url: https://dev.renevier.net/?a=blobdiff_plain;f=public%2Fjs%2Futils.js;h=588a85b590e2af6303225f47b478b7c62ce1837f;hb=17ac0916d7f99be6c7568f0100e5bd628b52124d;hp=b1dd910ccaee06c62f78b0528ddc3716e5670354;hpb=21102dd850917ee1973c29864e2268b45b710c44;p=syj.git diff --git a/public/js/utils.js b/public/js/utils.js index b1dd910..588a85b 100644 --- a/public/js/utils.js +++ b/public/js/utils.js @@ -21,6 +21,10 @@ var CloseBtn = Class.create({ btn = new Element("input", { type: "image", src: imgsrc, alt: "X"}).setStyle(style); elt.insert({top: btn}); btn.observe("click", function(evt) { + evt.stop(); + if (typeof options.callback === "function") { + options.callback.call(elt); + } elt.hide(); }); } @@ -85,8 +89,12 @@ Ajax.TimedRequest = Class.create(Ajax.Request, { initialize: function($super, url, delay, options) { this.delay = delay; + if (!options) { + options = {}; + } - options.onSuccess = options.onSuccess.wrap(function(proceed, transport, json) { + options.onSuccess = options.onSuccess && + options.onSuccess.wrap(function(proceed, transport, json) { if (this.timeout) { window.clearTimeout(this.timeout); this.timeout = null; @@ -98,7 +106,8 @@ Ajax.TimedRequest = Class.create(Ajax.Request, { } }).bind(this); - options.onFailure = options.onFailure.wrap(function(proceed, transport, json) { + options.onFailure = options.onFailure && + options.onFailure.wrap(function(proceed, transport, json) { if (this.timeout) { window.clearTimeout(this.timeout); this.timeout = null; @@ -110,10 +119,12 @@ Ajax.TimedRequest = Class.create(Ajax.Request, { }, request: function($super, url) { - this.timeout = (function() { - this.options.onFailure(null); + this.timeout = function() { + if (this.options.onFailure) { + this.options.onFailure(null); + } this.abort(); - }).bind(this).delay(this.delay); + }.bind(this).delay(this.delay); $super(url); } }); @@ -179,7 +190,7 @@ Element.addMethods('form', { }); }, - focus: function(form) { + setfocus: function(form) { var tofocus, error; tofocus = null; @@ -223,6 +234,52 @@ Element.addMethods(['input', 'textarea'], { after: new Element("div", {className: 'error'}).update(errorMessage) }); return false; + }, + + observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) { + if (eventName === "contentchange") { + proceed(element, 'keyup', function(evt) { + if (evt.keyCode === 13) { + return; + } + handler.apply(null, arguments); + }); + proceed(element, 'paste', handler); + return proceed(element, 'change', handler); + } + return proceed(element, eventName, handler); + }), + + timedobserve: function(element, callback, delay) { + var timeout = null, initialvalue = element.value; + + if (typeof delay !== "number") { + delay = 0.5; + } + delay = delay * 1000; + + var canceltimer = function() { + if (timeout) { + clearTimeout(timeout); + timeout = null; + } + }; + var resettimer = function() { + canceltimer(); + timeout = setTimeout(triggercallback, delay); + }; + var triggercallback = function() { + canceltimer(); + if (initialvalue !== element.value) { + initialvalue = element.value; + callback.call(element); + } + }; + + element.observe('blur', triggercallback). + observe('keyup', resettimer). + observe('paste', resettimer); + return element; } });