]> dev.renevier.net Git - syj.git/blobdiff - public/js/utils.js
search a place with nominatim
[syj.git] / public / js / utils.js
index b1dd910ccaee06c62f78b0528ddc3716e5670354..dfc1a3d76952e960e706653b6a94dfdfa9c616d4 100644 (file)
@@ -10,22 +10,60 @@ var CloseBtn = Class.create({
             return;
         }
 
+        if (typeof options !== "object") {
+            options = {};
+        }
+
         style = Object.extend({
             float: "right",
             margin: "2px",
             fontWeight: "bold",
             padding: "0px"
-        }, typeof options === "object" ? options.style: {});
+        }, options.style);
 
-        imgsrc = (options && options.closeBtnSrc) || "icons/close.png";
+        imgsrc = (options.closeBtnSrc) || "icons/close.png";
         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();
         });
     }
 });
 
+var Toggler = Class.create({
+    initialize: function(target, options) {
+        options = Object.extend({}, options);
+        target = $(target).hide();
+
+        var openIcn = options.openIcn || 'icons/bullet_arrow_right.png',
+            closeIcn = options.closeIcn || 'icons/bullet_arrow_down.png';
+
+        this.element = new Element("img", { src: openIcn })
+                              .setStyle({ border: 'none',  // in firefox, in image inside an anchor has a border
+                                        verticalAlign: "middle"});
+
+        this.element.observe('click', function(evt) {
+            if (target.visible()) {
+                evt.target.src = openIcn;
+                target.hide();
+            } else {
+                evt.target.src = closeIcn;
+                target.show();
+            }
+            evt.stop();
+        });
+
+        if (options.initialShow) {
+            target.show();
+            this.element.src = closeIcn;
+        }
+    }
+});
+
 var Deck = Class.create({
     initialize: function(elt, options) {
         this.element = $(elt);
@@ -85,8 +123,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 +140,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 +153,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);
     }
 });
@@ -171,15 +216,41 @@ Element.addMethods('form', {
                 reqoptions.method = this.method;
             }
 
+            if (reqoptions.onFailure) {
+                reqoptions.onFailure = reqoptions.onFailure.wrap(function(proceed, transport, json) {
+                    form.enable();
+                    proceed(transport, json);
+                });
+            } else {
+                reqoptions.onFailure = function() {
+                    form.enable();
+                };
+            }
+
+            if (reqoptions.onSuccess) {
+                reqoptions.onSuccess = reqoptions.onSuccess.wrap(function(proceed, transport, json) {
+                    form.enable();
+                    proceed(transport, json);
+                });
+            } else {
+                reqoptions.onSuccess = function() {
+                    form.enable();
+                };
+            }
+
             new Ajax.TimedRequest(action, options.delay || 20, reqoptions);
 
             if (Object.isFunction(options.postsubmit)) {
                 options.postsubmit(this);
             }
+            Form.getElements(form).each(function(elt) {
+                elt.blur();
+                elt.disable();
+            });
         });
     },
 
-    focus: function(form) {
+    setfocus: function(form) {
         var tofocus, error;
 
         tofocus = null;
@@ -223,6 +294,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;
     }
 });
 
@@ -254,9 +371,13 @@ Element.addMethods('div', {
 
     addMessage: function(div, message) {
         var node = (div.ownerDocument || document).createTextNode(message);
-        if (!div.empty()) {
+
+        if ($A(div.childNodes).filter(function(node) {
+                return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
+             }).length) {
             div.insert(new Element('br'));
         }
+
         div.appendChild(node);
         return div.show();
     },