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) {
25 if (typeof options.callback === "function") {
26 options.callback.call(elt);
33 var Deck = Class.create({
34 initialize: function(elt, options) {
35 this.element = $(elt);
37 this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10));
39 setIndex: function(idx) {
40 if (idx === this.index) {
44 var childs = this.element.childElements();
45 if (childs.length === 0) {
49 idx = Math.max(0, idx);
50 idx = Math.min(childs.length - 1, idx);
52 childs.each(function(item, i) {
61 getIndex: function() {
67 highlight: function(element, color, timeout) {
69 if (typeof timeout === "undefined") {
72 current = element.getStyle('backgroundColor');
73 Element.setStyle(element, {'backgroundColor': color});
74 Element.setStyle.delay(timeout, element, {'backgroundColor': current});
79 Ajax.TimedRequest = Class.create(Ajax.Request, {
84 // see http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
85 this.transport.onreadystatechange = Prototype.emptyFunction;
86 this.transport.abort();
87 Ajax.activeRequestCount--;
90 initialize: function($super, url, delay, options) {
96 options.onSuccess = options.onSuccess &&
97 options.onSuccess.wrap(function(proceed, transport, json) {
99 window.clearTimeout(this.timeout);
102 if (transport.getStatus() === 0) {
103 this.options.onFailure(transport, json);
105 proceed(transport, json);
109 options.onFailure = options.onFailure &&
110 options.onFailure.wrap(function(proceed, transport, json) {
112 window.clearTimeout(this.timeout);
115 proceed(transport, json);
118 $super(url, options);
121 request: function($super, url) {
122 this.timeout = (function() {
123 if (this.options.onFailure) {
124 this.options.onFailure(null);
127 }).bind(this).delay(this.delay);
132 Ajax.Responders.register({
133 // needed for Ajax.TimedRequest.abort to work: see
134 // http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
136 onComplete: function() {
137 Ajax.activeRequestCount--;
138 if (Ajax.activeRequestCount < 0) {
139 Ajax.activeRequestCount = 0;
144 // wrapper around Form.request that sets up the submit listener, stops the
145 // submit event, calls presubmit function, calls Form.request and calls a
146 // postsubmit function
147 Element.addMethods('form', {
148 ajaxize : function(form, options) {
151 options = Object.clone(options || {});
153 $(form).observe('submit', function(evt) {
154 evt.stop(); // cancel form submission
156 reqoptions = Object.clone(options);
157 delete(reqoptions.presubmit);
158 delete(reqoptions.postsubmit);
159 delete(reqoptions.delay);
161 if (Object.isFunction(options.presubmit)) {
162 if (options.presubmit(this) === false) {
167 var params = reqoptions.parameters, action = this.readAttribute('action') || '';
169 if (action.blank()) {
170 action = window.location.href;
172 reqoptions.parameters = this.serialize(true);
175 if (Object.isString(params)) {
176 params = params.toQueryParams();
178 Object.extend(reqoptions.parameters, params);
181 if (this.hasAttribute('method') && !reqoptions.method) {
182 reqoptions.method = this.method;
185 new Ajax.TimedRequest(action, options.delay || 20, reqoptions);
187 if (Object.isFunction(options.postsubmit)) {
188 options.postsubmit(this);
193 focus: function(form) {
197 error = form.down('.error');
199 tofocus = error.previous('input,textarea');
201 tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
204 if (error && (typeof tofocus.highlight === "function")) {
205 tofocus.highlight('#F08080');
211 checkEmptyElements: function(form, errorMessage) {
213 form.select('.required').each(function(elt) {
214 var id = elt.getAttribute('for'), control = $(id);
218 if (!control.check(function() {
219 return !this.value.strip().empty();
221 results.push(control);
228 Element.addMethods(['input', 'textarea'], {
229 check: function(control, callback, errorMessage) {
230 if (callback.call(control)) {
234 after: new Element("div", {className: 'error'}).update(errorMessage)
239 observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) {
240 if (eventName === "contentchange") {
241 proceed(element, 'keyup', function(evt) {
242 if (evt.keyCode === 13) {
245 handler.apply(null, arguments);
247 proceed(element, 'paste', handler);
248 return proceed(element, 'change', handler);
250 return proceed(element, eventName, handler);
253 timedobserve: function(element, callback, delay) {
254 var timeout = null, initialvalue = element.value;
256 if (typeof delay !== "number") {
259 delay = delay * 1000;
261 var canceltimer = function() {
263 clearTimeout(timeout);
267 var resettimer = function() {
269 timeout = setTimeout(triggercallback, delay);
271 var triggercallback = function() {
273 if (initialvalue !== element.value) {
274 initialvalue = element.value;
275 callback.call(element);
279 element.observe('blur', triggercallback).
280 observe('keyup', resettimer).
281 observe('paste', resettimer);
286 Element.addMethods('div', {
287 setMessage: function(div, message, status) {
290 div.setMessageStatus(status);
293 div.addMessage(message);
298 clearMessages: function(div) {
299 var node = div.firstChild, nextNode;
302 nextNode = node.nextSibling;
303 if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') {
304 div.removeChild(node);
312 addMessage: function(div, message) {
313 var node = (div.ownerDocument || document).createTextNode(message);
315 div.insert(new Element('br'));
317 div.appendChild(node);
321 setMessageStatus: function(div, status) {
322 return div.removeClassName('error').
323 removeClassName('warn').
324 removeClassName('info').
325 removeClassName('success').
326 addClassName(status);