1 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
2 and is published under the AGPL license. */
4 var CloseBtn = Class.create({
5 initialize: function(elt, options) {
6 var btn, imgsrc, style;
13 style = Object.extend({
18 }, typeof options === "object" ? options.style: {});
20 imgsrc = (options && options.closeBtnSrc) || "icons/close.png";
21 btn = new Element("input", { type: "image", src: imgsrc, alt: "X"}).setStyle(style);
22 elt.insert({top: btn});
23 btn.observe("click", function(evt) {
29 var Deck = Class.create({
30 initialize: function(elt, options) {
31 this.element = $(elt);
33 this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10));
35 setIndex: function(idx) {
36 if (idx === this.index) {
40 var childs = this.element.childElements();
41 if (childs.length === 0) {
45 idx = Math.max(0, idx);
46 idx = Math.min(childs.length - 1, idx);
48 childs.each(function(item, i) {
57 getIndex: function() {
63 highlight: function(element, color, timeout) {
65 if (typeof timeout === "undefined") {
68 current = element.getStyle('backgroundColor');
69 Element.setStyle(element, {'backgroundColor': color});
70 Element.setStyle.delay(timeout, element, {'backgroundColor': current});
75 // wrapper around Form.request that sets up the submit listener, stops the
76 // submit event, calls presubmit function, calls Form.request and calls a
77 // postsubmit function
78 Element.addMethods('form', {
79 ajaxize : function(form, options) {
80 var reqoptions, timeout;
82 options = Object.clone(options);
83 reqoptions = Object.clone(options);
86 function onSuccess(transport, json) {
88 window.clearTimeout(timeout);
91 if (transport.getStatus() === 0) {
92 options.onFailure(transport, json);
94 options.onSuccess(transport, json);
98 function onFailure(transport, json) {
100 window.clearTimeout(timeout);
103 options.onFailure(transport, json);
106 delete(reqoptions.presubmit);
107 delete(reqoptions.postsubmit);
109 $(form).observe('submit', function(evt) {
112 evt.stop(); // cancel form submission
113 if (Object.isFunction(options.presubmit)) {
114 if (options.presubmit(this) === false) {
118 req = this.request(Object.extend(reqoptions, {
119 onSuccess: onSuccess,
122 timeout = (function() {
123 options.onFailure(null);
125 }).delay(options.timeout || 20);
126 if (Object.isFunction(options.postsubmit)) {
127 options.postsubmit(this);
132 focus: function(form) {
136 error = form.down('.error');
138 tofocus = error.previous('input,textarea');
140 tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
143 if (error && (typeof tofocus.highlight === "function")) {
144 tofocus.highlight('#F08080');
151 checkEmptyElements: function(form, errorMessage) {
153 form.select('.required').each(function(elt) {
154 var id = elt.getAttribute('for'), control = $(id);
158 if (!control.check(function() {
159 return !this.value.strip().empty();
161 results.push(control);
168 Element.addMethods(['input', 'textarea'], {
169 check: function(control, callback, errorMessage) {
170 if (callback.call(control)) {
174 after: new Element("div", {className: 'error'}).update(errorMessage)
180 Element.addMethods('div', {
181 setMessage: function(div, message, status) {
184 div.setMessageStatus(status);
187 div.addMessage(message);
192 clearMessages: function(div) {
193 var node = div.firstChild, nextNode;
196 nextNode = node.nextSibling;
197 if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') {
198 div.removeChild(node);
206 addMessage: function(div, message) {
207 var node = (div.ownerDocument || document).createTextNode(message);
209 div.insert(new Element('br'));
211 div.appendChild(node);
215 setMessageStatus: function(div, status) {
216 return div.removeClassName('error').
217 removeClassName('warn').
218 removeClassName('info').
219 removeClassName('success').
220 addClassName(status);