From 1de0f433144272946051bb0df886b8c43459beb8 Mon Sep 17 00:00:00 2001 From: arno Date: Wed, 28 Jul 2010 15:15:10 +0200 Subject: [PATCH] assemble all utils scripts in a utils.js file --- application/controllers/AccountController.php | 3 +- application/controllers/ContactController.php | 3 +- application/controllers/IdxController.php | 6 +- application/controllers/LoginController.php | 3 +- application/controllers/NewpwdController.php | 2 +- public/js/ajaxize.js | 58 ----- public/js/closebtn.js | 27 --- public/js/deck.js | 35 --- public/js/forms.js | 51 ---- public/js/highlight.js | 12 - public/js/message.js | 44 ---- public/js/utils.js | 222 ++++++++++++++++++ 12 files changed, 227 insertions(+), 239 deletions(-) delete mode 100644 public/js/ajaxize.js delete mode 100644 public/js/closebtn.js delete mode 100644 public/js/deck.js delete mode 100644 public/js/forms.js delete mode 100644 public/js/highlight.js delete mode 100644 public/js/message.js create mode 100644 public/js/utils.js diff --git a/application/controllers/AccountController.php b/application/controllers/AccountController.php index 00de24e..30daa5b 100644 --- a/application/controllers/AccountController.php +++ b/application/controllers/AccountController.php @@ -7,8 +7,7 @@ class AccountController extends Zend_Controller_Action public function init() { $this->view->headScript()->appendFile('js/prototype.js'); - $this->view->headScript()->appendFile('js/forms.js'); - $this->view->headScript()->appendFile('js/highlight.js'); + $this->view->headScript()->appendFile('js/utils.js'); $this->view->headScript()->appendFile('js/account.js'); $this->view->headLink()->appendStylesheet('css/generic.css'); $this->view->headLink()->appendStylesheet('css/account.css'); diff --git a/application/controllers/ContactController.php b/application/controllers/ContactController.php index 03dd4a6..9a7ade1 100644 --- a/application/controllers/ContactController.php +++ b/application/controllers/ContactController.php @@ -7,8 +7,7 @@ class ContactController extends Zend_Controller_Action public function init() { $this->view->headScript()->appendFile('js/prototype.js'); - $this->view->headScript()->appendFile('js/forms.js'); - $this->view->headScript()->appendFile('js/highlight.js'); + $this->view->headScript()->appendFile('js/utils.js'); $this->view->headScript()->appendFile('js/contact.js'); $this->view->headLink()->appendStylesheet('css/generic.css'); $this->view->headLink()->appendStylesheet('css/contact.css'); diff --git a/application/controllers/IdxController.php b/application/controllers/IdxController.php index 1e149fd..e606ebc 100644 --- a/application/controllers/IdxController.php +++ b/application/controllers/IdxController.php @@ -10,11 +10,7 @@ class IdxController extends Zend_Controller_Action $this->view->headScript()->appendFile('js/ModifiablePath.js'); $this->view->headScript()->appendFile('js/prototype.js'); $this->view->headScript()->appendFile('js/simplebox.js'); - $this->view->headScript()->appendFile('js/closebtn.js'); - $this->view->headScript()->appendFile('js/highlight.js'); - $this->view->headScript()->appendFile('js/deck.js'); - $this->view->headScript()->appendFile('js/ajaxize.js'); - $this->view->headScript()->appendFile('js/message.js'); + $this->view->headScript()->appendFile('js/utils.js'); $this->view->headScript()->appendFile('js/syj.js'); $this->view->headLink()->appendStylesheet('css/openlayers/style.css'); $this->view->headLink()->appendStylesheet('css/generic.css'); diff --git a/application/controllers/LoginController.php b/application/controllers/LoginController.php index 07bb40b..4863621 100644 --- a/application/controllers/LoginController.php +++ b/application/controllers/LoginController.php @@ -7,8 +7,7 @@ class LoginController extends Zend_Controller_Action public function init() { $this->view->headTitle($this->view->translate("login")); $this->view->headScript()->appendFile('js/prototype.js'); - $this->view->headScript()->appendFile('js/forms.js'); - $this->view->headScript()->appendFile('js/highlight.js'); + $this->view->headScript()->appendFile('js/utils.js'); $this->view->headScript()->appendFile('js/login.js'); $this->view->headLink()->appendStylesheet('css/generic.css'); $this->view->headLink()->appendStylesheet('css/login.css'); diff --git a/application/controllers/NewpwdController.php b/application/controllers/NewpwdController.php index 5facb89..93d7f9f 100644 --- a/application/controllers/NewpwdController.php +++ b/application/controllers/NewpwdController.php @@ -8,7 +8,7 @@ class NewpwdController extends Zend_Controller_Action public function init() { $this->view->headScript()->appendFile('js/prototype.js'); $this->view->headScript()->appendFile('js/newpwd.js'); - $this->view->headScript()->appendFile('js/highlight.js'); + $this->view->headScript()->appendFile('js/utils.js'); $this->view->headLink()->appendStylesheet('css/generic.css'); $this->view->headLink()->appendStylesheet('css/newpwd.css'); } diff --git a/public/js/ajaxize.js b/public/js/ajaxize.js deleted file mode 100644 index 6680110..0000000 --- a/public/js/ajaxize.js +++ /dev/null @@ -1,58 +0,0 @@ -/* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier, - and is published under the AGPL license. */ - -// 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 -Element.addMethods('form', {ajaxize : function(form, options) { - var reqoptions, timeout; - - options = Object.clone(options); - reqoptions = Object.clone(options); - timeout = null; - - function onSuccess(transport, json) { - if (timeout) { - window.clearTimeout(timeout); - timeout = null; - } - if (transport.getStatus() === 0) { - options.onFailure(transport, json); - } else { - options.onSuccess(transport, json); - } - } - - function onFailure(transport, json) { - if (timeout) { - window.clearTimeout(timeout); - timeout = null; - } - options.onFailure(transport, json); - } - - delete(reqoptions.presubmit); - delete(reqoptions.postsubmit); - - $(form).observe('submit', function(evt) { - var req; - - evt.stop(); // cancel form submission - if (Object.isFunction(options.presubmit)) { - if (options.presubmit(this) === false) { - return; - } - } - req = this.request(Object.extend(reqoptions, { - onSuccess: onSuccess, - onFailure: onFailure - })); - timeout = (function() { - options.onFailure(null); - req.abort(); - }).delay(options.timeout || 20); - if (Object.isFunction(options.postsubmit)) { - options.postsubmit(this); - } - }); -}}); diff --git a/public/js/closebtn.js b/public/js/closebtn.js deleted file mode 100644 index 84be90a..0000000 --- a/public/js/closebtn.js +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) 2010 Arnaud Renevier, Inc, published under the modified BSD - * license. */ - -var CloseBtn = Class.create({ - initialize: function(elt, options) { - var btn, imgsrc, style; - - elt = $(elt); - if (!elt) { - return; - } - - style = Object.extend({ - float: "right", - margin: "2px", - fontWeight: "bold", - padding: "0px" - }, typeof options === "object" ? options.style: {}); - - imgsrc = (options && 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) { - elt.hide(); - }); - } -}); diff --git a/public/js/deck.js b/public/js/deck.js deleted file mode 100644 index 73bc3d2..0000000 --- a/public/js/deck.js +++ /dev/null @@ -1,35 +0,0 @@ -/* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier, - and is published under the AGPL license. */ -var Deck = Class.create(); -Deck.prototype = { - initialize: function(elt, options) { - this.element = $(elt); - this.index = null; - this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10)); - }, - setIndex: function(idx) { - if (idx === this.index) { - return; - } - - var childs = this.element.childElements(); - if (childs.length === 0) { - this.index = -1; - return; - } - idx = Math.max(0, idx); - idx = Math.min(childs.length - 1, idx); - - childs.each(function(item, i) { - if (idx === i) { - item.show(); - } else { - item.hide(); - } - }); - this.index = idx; - }, - getIndex: function() { - return this.index; - } -}; diff --git a/public/js/forms.js b/public/js/forms.js deleted file mode 100644 index 738b056..0000000 --- a/public/js/forms.js +++ /dev/null @@ -1,51 +0,0 @@ -/* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier, - and is published under the AGPL license. */ -Element.addMethods(['input', 'textarea'], { - check: function(control, callback, errorMessage) { - if (callback.call(control)) { - return true; - } - control.insert({ - after: new Element("div", {className: 'error'}).update(errorMessage) - }); - return false; - } -}); - -Element.addMethods('form', { - focus: function(form) { - var tofocus, error; - - tofocus = null; - error = form.down('.error'); - if (error) { - tofocus = error.previous('input,textarea'); - } else { - tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])'); - } - if (tofocus) { - if (error && (typeof tofocus.highlight === "function")) { - tofocus.highlight('#F08080'); - } - tofocus.focus(); - tofocus.select(); - } - }, - - checkEmptyElements: function(form, errorMessage) { - var results = []; - form.select('.required').each(function(elt) { - var id = elt.getAttribute('for'), control = $(id); - if (!control) { - return; - } - if (!control.check(function() { - return !this.value.strip().empty(); - }, errorMessage)) { - results.push(control); - } - }); - return results; - } - -}); diff --git a/public/js/highlight.js b/public/js/highlight.js deleted file mode 100644 index 289cd2a..0000000 --- a/public/js/highlight.js +++ /dev/null @@ -1,12 +0,0 @@ -Element.addMethods({ - highlight: function(element, color, timeout) { - var current; - if (typeof timeout === "undefined") { - timeout = 0.3; - } - current = element.getStyle('backgroundColor'); - Element.setStyle(element, {'backgroundColor': color}); - Element.setStyle.delay(timeout, element, {'backgroundColor': current}); - return element; - } -}); diff --git a/public/js/message.js b/public/js/message.js deleted file mode 100644 index 795ae2b..0000000 --- a/public/js/message.js +++ /dev/null @@ -1,44 +0,0 @@ -/* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier, - and is published under the AGPL license. */ -Element.addMethods('div', { - setMessage: function(div, message, status) { - div.clearMessages(); - if (status) { - div.setMessageStatus(status); - } - if (message) { - div.addMessage(message); - } - return div; - }, - - clearMessages: function(div) { - var node = div.firstChild; - while (node) { - var nextNode = node.nextSibling; - if (node.nodeType == 3 || node.tagName.toLowerCase() == 'br') { - div.removeChild(node); - } - node = nextNode; - } - - return div; - }, - - addMessage: function(div, message) { - var node = (div.ownerDocument || document).createTextNode(message); - if (!div.empty()) { - div.insert(new Element('br')); - } - div.appendChild(node); - return div.show(); - }, - - setMessageStatus: function(div, status) { - return div.removeClassName('error'). - removeClassName('warn'). - removeClassName('info'). - removeClassName('success'). - addClassName(status); - } -}); diff --git a/public/js/utils.js b/public/js/utils.js new file mode 100644 index 0000000..d8696a0 --- /dev/null +++ b/public/js/utils.js @@ -0,0 +1,222 @@ +/* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier, + and is published under the AGPL license. */ + +var CloseBtn = Class.create({ + initialize: function(elt, options) { + var btn, imgsrc, style; + + elt = $(elt); + if (!elt) { + return; + } + + style = Object.extend({ + float: "right", + margin: "2px", + fontWeight: "bold", + padding: "0px" + }, typeof options === "object" ? options.style: {}); + + imgsrc = (options && 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) { + elt.hide(); + }); + } +}); + +var Deck = Class.create({ + initialize: function(elt, options) { + this.element = $(elt); + this.index = null; + this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10)); + }, + setIndex: function(idx) { + if (idx === this.index) { + return; + } + + var childs = this.element.childElements(); + if (childs.length === 0) { + this.index = -1; + return; + } + idx = Math.max(0, idx); + idx = Math.min(childs.length - 1, idx); + + childs.each(function(item, i) { + if (idx === i) { + item.show(); + } else { + item.hide(); + } + }); + this.index = idx; + }, + getIndex: function() { + return this.index; + } +}); + +Element.addMethods({ + highlight: function(element, color, timeout) { + var current; + if (typeof timeout === "undefined") { + timeout = 0.3; + } + current = element.getStyle('backgroundColor'); + Element.setStyle(element, {'backgroundColor': color}); + Element.setStyle.delay(timeout, element, {'backgroundColor': current}); + return element; + } +}); + +// 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 +Element.addMethods('form', { + ajaxize : function(form, options) { + var reqoptions, timeout; + + options = Object.clone(options); + reqoptions = Object.clone(options); + timeout = null; + + function onSuccess(transport, json) { + if (timeout) { + window.clearTimeout(timeout); + timeout = null; + } + if (transport.getStatus() === 0) { + options.onFailure(transport, json); + } else { + options.onSuccess(transport, json); + } + } + + function onFailure(transport, json) { + if (timeout) { + window.clearTimeout(timeout); + timeout = null; + } + options.onFailure(transport, json); + } + + delete(reqoptions.presubmit); + delete(reqoptions.postsubmit); + + $(form).observe('submit', function(evt) { + var req; + + evt.stop(); // cancel form submission + if (Object.isFunction(options.presubmit)) { + if (options.presubmit(this) === false) { + return; + } + } + req = this.request(Object.extend(reqoptions, { + onSuccess: onSuccess, + onFailure: onFailure + })); + timeout = (function() { + options.onFailure(null); + req.abort(); + }).delay(options.timeout || 20); + if (Object.isFunction(options.postsubmit)) { + options.postsubmit(this); + } + }); + }, + + focus: function(form) { + var tofocus, error; + + tofocus = null; + error = form.down('.error'); + if (error) { + tofocus = error.previous('input,textarea'); + } else { + tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])'); + } + if (tofocus) { + if (error && (typeof tofocus.highlight === "function")) { + tofocus.highlight('#F08080'); + } + tofocus.focus(); + tofocus.select(); + } + }, + + checkEmptyElements: function(form, errorMessage) { + var results = []; + form.select('.required').each(function(elt) { + var id = elt.getAttribute('for'), control = $(id); + if (!control) { + return; + } + if (!control.check(function() { + return !this.value.strip().empty(); + }, errorMessage)) { + results.push(control); + } + }); + return results; + } +}); + +Element.addMethods(['input', 'textarea'], { + check: function(control, callback, errorMessage) { + if (callback.call(control)) { + return true; + } + control.insert({ + after: new Element("div", {className: 'error'}).update(errorMessage) + }); + return false; + } +}); + +Element.addMethods('div', { + setMessage: function(div, message, status) { + div.clearMessages(); + if (status) { + div.setMessageStatus(status); + } + if (message) { + div.addMessage(message); + } + return div; + }, + + clearMessages: function(div) { + var node = div.firstChild, nextNode; + + while (node) { + nextNode = node.nextSibling; + if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') { + div.removeChild(node); + } + node = nextNode; + } + + return div; + }, + + addMessage: function(div, message) { + var node = (div.ownerDocument || document).createTextNode(message); + if (!div.empty()) { + div.insert(new Element('br')); + } + div.appendChild(node); + return div.show(); + }, + + setMessageStatus: function(div, status) { + return div.removeClassName('error'). + removeClassName('warn'). + removeClassName('info'). + removeClassName('success'). + addClassName(status); + } +}); -- 2.39.2