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 Ajax.TimedRequest = Class.create(Ajax.Request, {
80 // see http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
81 this.transport.onreadystatechange = Prototype.emptyFunction;
82 this.transport.abort();
83 Ajax.activeRequestCount--;
86 initialize: function($super, url, delay, options) {
89 options.onSuccess = options.onSuccess.wrap(function(proceed, transport, json) {
91 window.clearTimeout(this.timeout);
94 if (transport.getStatus() === 0) {
95 this.options.onFailure(transport, json);
97 proceed(transport, json);
101 options.onFailure = options.onFailure.wrap(function(proceed, transport, json) {
103 window.clearTimeout(this.timeout);
106 proceed(transport, json);
109 $super(url, options);
112 request: function($super, url) {
113 this.timeout = (function() {
114 this.options.onFailure(null);
116 }).bind(this).delay(this.delay);
121 Ajax.Responders.register({
122 // needed for Ajax.TimedRequest.abort to work: see
123 // http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
125 onComplete: function() {
126 Ajax.activeRequestCount--;
127 if (Ajax.activeRequestCount < 0) {
128 Ajax.activeRequestCount = 0;
133 // wrapper around Form.request that sets up the submit listener, stops the
134 // submit event, calls presubmit function, calls Form.request and calls a
135 // postsubmit function
136 Element.addMethods('form', {
137 ajaxize : function(form, options) {
140 options = Object.clone(options || {});
142 $(form).observe('submit', function(evt) {
143 evt.stop(); // cancel form submission
145 reqoptions = Object.clone(options);
146 delete(reqoptions.presubmit);
147 delete(reqoptions.postsubmit);
148 delete(reqoptions.delay);
150 if (Object.isFunction(options.presubmit)) {
151 if (options.presubmit(this) === false) {
156 var params = reqoptions.parameters, action = this.readAttribute('action') || '';
158 if (action.blank()) {
159 action = window.location.href;
161 reqoptions.parameters = this.serialize(true);
164 if (Object.isString(params)) {
165 params = params.toQueryParams();
167 Object.extend(reqoptions.parameters, params);
170 if (this.hasAttribute('method') && !reqoptions.method) {
171 reqoptions.method = this.method;
174 new Ajax.TimedRequest(action, options.delay || 20, reqoptions);
176 if (Object.isFunction(options.postsubmit)) {
177 options.postsubmit(this);
182 focus: function(form) {
186 error = form.down('.error');
188 tofocus = error.previous('input,textarea');
190 tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
193 if (error && (typeof tofocus.highlight === "function")) {
194 tofocus.highlight('#F08080');
200 checkEmptyElements: function(form, errorMessage) {
202 form.select('.required').each(function(elt) {
203 var id = elt.getAttribute('for'), control = $(id);
207 if (!control.check(function() {
208 return !this.value.strip().empty();
210 results.push(control);
217 Element.addMethods(['input', 'textarea'], {
218 check: function(control, callback, errorMessage) {
219 if (callback.call(control)) {
223 after: new Element("div", {className: 'error'}).update(errorMessage)
229 Element.addMethods('div', {
230 setMessage: function(div, message, status) {
233 div.setMessageStatus(status);
236 div.addMessage(message);
241 clearMessages: function(div) {
242 var node = div.firstChild, nextNode;
245 nextNode = node.nextSibling;
246 if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') {
247 div.removeChild(node);
255 addMessage: function(div, message) {
256 var node = (div.ownerDocument || document).createTextNode(message);
258 div.insert(new Element('br'));
260 div.appendChild(node);
264 setMessageStatus: function(div, status) {
265 return div.removeClassName('error').
266 removeClassName('warn').
267 removeClassName('info').
268 removeClassName('success').
269 addClassName(status);