]> dev.renevier.net Git - syj.git/blobdiff - public/js/utils.js
better mobile support
[syj.git] / public / js / utils.js
index dfc1a3d76952e960e706653b6a94dfdfa9c616d4..8936797b79db1080f973fde7d49764eeaa44a1be 100644 (file)
@@ -1,4 +1,4 @@
-/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
+/*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
     and is published under the AGPL license. */
 
 var CloseBtn = Class.create({
@@ -15,7 +15,7 @@ var CloseBtn = Class.create({
         }
 
         style = Object.extend({
-            float: "right",
+            'float': "right",
             margin: "2px",
             fontWeight: "bold",
             padding: "0px"
@@ -26,6 +26,9 @@ var CloseBtn = Class.create({
         elt.insert({top: btn});
         btn.observe("click", function(evt) {
             evt.stop();
+            if (evt.detail === 0) { // it's not a real click, possibly a submit event
+                return;
+            }
             if (typeof options.callback === "function") {
                 options.callback.call(elt);
             }
@@ -35,31 +38,46 @@ var CloseBtn = Class.create({
 });
 
 var Toggler = Class.create({
-    initialize: function(target, options) {
-        options = Object.extend({}, options);
-        target = $(target).hide();
+    options: {},
 
-        var openIcn = options.openIcn || 'icons/bullet_arrow_right.png',
-            closeIcn = options.closeIcn || 'icons/bullet_arrow_down.png';
+    close: function() {
+        this.element.src = this.options.openIcn;
+        this.target.hide();
+        document.fire('toggler:close', this);
+    },
 
-        this.element = new Element("img", { src: openIcn })
-                              .setStyle({ border: 'none',  // in firefox, in image inside an anchor has a border
-                                        verticalAlign: "middle"});
+    open: function() {
+        this.element.src = this.options.closeIcn;
+        this.target.show();
+        document.fire('toggler:open', this);
+    },
 
-        this.element.observe('click', function(evt) {
-            if (target.visible()) {
-                evt.target.src = openIcn;
-                target.hide();
-            } else {
-                evt.target.src = closeIcn;
-                target.show();
-            }
+    toggle: function(evt) {
+        if (evt && typeof evt.stop === "function") {
             evt.stop();
-        });
+        }
+        if (this.target.visible()) {
+            this.close();
+        } else {
+            this.open();
+        }
+    },
 
-        if (options.initialShow) {
-            target.show();
-            this.element.src = closeIcn;
+    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();
         }
     }
 });
@@ -107,6 +125,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;
+        }
     }
 });
 
@@ -177,7 +212,8 @@ Ajax.Responders.register({
 
 // wrapper around Form.request that sets up the submit listener, stops the
 // submit event, calls presubmit function, calls Form.request and calls a
-// postsubmit function
+// postsubmit function. If form has some visible and activated file inputs,
+// execute presubmit, but do not send the file with ajax.
 Element.addMethods('form', {
     ajaxize : function(form, options) {
         var reqoptions;
@@ -185,7 +221,6 @@ Element.addMethods('form', {
         options = Object.clone(options || {});
 
         $(form).observe('submit', function(evt) {
-            evt.stop(); // cancel form submission
 
             reqoptions = Object.clone(options);
             delete(reqoptions.presubmit);
@@ -194,10 +229,30 @@ Element.addMethods('form', {
 
             if (Object.isFunction(options.presubmit)) {
                 if (options.presubmit(this) === false) {
+                    evt.stop(); // cancel form submission
                     return;
                 }
             }
 
+            // get list of input file not disabled, and not hidden
+            if (this.getInputs('file').find(function(elt) {
+                if (elt.disabled) {
+                    return false;
+                }
+                while (elt && $(elt).identify() !== this.identify()) {
+                    if (!elt.visible()) {
+                        return false;
+                    }
+                    elt = elt.parentNode;
+                }
+                return true;
+             }.bind(this))) {
+                // form has some file inputs. Do not manage on our own.
+                return;
+            }
+
+            evt.stop(); // cancel form submission
+
             var params = reqoptions.parameters, action = this.readAttribute('action') || '';
 
             if (action.blank()) {
@@ -304,7 +359,7 @@ Element.addMethods(['input', 'textarea'], {
                 }
                 handler.apply(null, arguments);
             });
-            proceed(element, 'paste', handler);
+            proceed(element, 'paste', handler.defer.bind(handler));
             return proceed(element, 'change', handler);
         }
         return proceed(element, eventName, handler);
@@ -360,7 +415,7 @@ Element.addMethods('div', {
 
         while (node) {
             nextNode = node.nextSibling;
-            if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') {
+            if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br' || node.textContent || node.innerText) {
                 div.removeChild(node);
             }
                 node = nextNode;
@@ -373,7 +428,7 @@ Element.addMethods('div', {
         var node = (div.ownerDocument || document).createTextNode(message);
 
         if ($A(div.childNodes).filter(function(node) {
-                return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
+                return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br' || node.textContent || node.innerText);
              }).length) {
             div.insert(new Element('br'));
         }
@@ -383,10 +438,16 @@ Element.addMethods('div', {
     },
 
     setMessageStatus: function(div, status) {
-        return div.removeClassName('error').
-                removeClassName('warn').
-                removeClassName('info').
-                removeClassName('success').
-                addClassName(status);
+        $A(["error", "warn", "info", "success", "optional"]).each(function(clname) {
+            div.removeClassName(clname);
+        });
+        if (typeof status === "string") {
+            div.addClassName(status);
+        } else {
+            $A(status).each(function(clname) {
+                div.addClassName(clname);
+            });
+        }
+        return div;
     }
 });