]> dev.renevier.net Git - syj.git/blobdiff - public/js/utils.js
do not use a reserved js keyword
[syj.git] / public / js / utils.js
index d59a112736ce89d77e79b00c78b9de699c3f7e4c..376fbe85c358a72283e7e1b80e69d1265284fd1e 100644 (file)
@@ -10,14 +10,18 @@ var CloseBtn = Class.create({
             return;
         }
 
+        if (typeof options !== "object") {
+            options = {};
+        }
+
         style = Object.extend({
-            float: "right",
+            '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) {
@@ -30,6 +34,51 @@ var CloseBtn = Class.create({
     }
 });
 
+var Toggler = Class.create({
+    options: {},
+
+    close: function() {
+        this.element.src = this.options.openIcn;
+        this.target.hide();
+        document.fire('toggler:close', this);
+    },
+
+    open: function() {
+        this.element.src = this.options.closeIcn;
+        this.target.show();
+        document.fire('toggler:open', this);
+    },
+
+    toggle: function(evt) {
+        if (evt && typeof evt.stop === "function") {
+            evt.stop();
+        }
+        if (this.target.visible()) {
+            this.close();
+        } else {
+            this.open();
+        }
+    },
+
+    initialize: function(target, options) {
+        this.options = Object.extend({
+                openIcn: 'icons/bullet_arrow_right.png',
+                closeIcn: 'icons/bullet_arrow_down.png'
+            }, options);
+
+        this.target = $(target).hide();
+        this.element = new Element("img").setStyle({ border: 'none',  // in firefox, in image inside an anchor has a border
+                                                    verticalAlign: "middle"});
+        this.element.observe('click', this.toggle.bindAsEventListener(this));
+
+        if (this.options.autoOpen) {
+            this.open();
+        } else {
+            this.close();
+        }
+    }
+});
+
 var Deck = Class.create({
     initialize: function(elt, options) {
         this.element = $(elt);
@@ -73,6 +122,23 @@ Element.addMethods({
         Element.setStyle(element, {'backgroundColor': color});
         Element.setStyle.delay(timeout, element, {'backgroundColor': current});
         return element;
+    },
+    text: function(element, content) {
+        if (typeof content === "undefined") { // getter
+            if (element.nodeType === 8) {
+                return "";
+            } else if (element.nodeType === 3 || element.nodeType === 4)  {
+                return element.nodeValue;
+            } else {
+                return $A(element.childNodes).inject("", function(acc, el) {
+                    return acc + Element.text(el);
+                 });
+            }
+        } else { // setter
+            var node = document.createTextNode(content);
+            element.update().appendChild(node);
+            return element;
+        }
     }
 });
 
@@ -182,11 +248,37 @@ 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();
+            });
         });
     },