X-Git-Url: https://dev.renevier.net/?a=blobdiff_plain;f=public%2Fjs%2Futils.js;h=aba7b1d1f7aa6ebd91187612b4afba17af77bbd2;hb=40368091bccafc07d6a580c934b72e963a635b16;hp=f5817d491a2087f3198089e5dad1d0d77bc87a18;hpb=ce001229accb2aff799560eec402344f0dbb1762;p=syj.git diff --git a/public/js/utils.js b/public/js/utils.js index f5817d4..aba7b1d 100644 --- a/public/js/utils.js +++ b/public/js/utils.js @@ -10,22 +10,78 @@ 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) { + 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); + } elt.hide(); }); } }); +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); @@ -69,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; + } } }); @@ -115,12 +188,12 @@ Ajax.TimedRequest = Class.create(Ajax.Request, { }, request: function($super, url) { - this.timeout = (function() { + 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); } }); @@ -178,15 +251,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; @@ -240,7 +339,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); @@ -307,9 +406,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(); },