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 if (typeof options !== "object") {
17 style = Object.extend({
24 imgsrc = (options.closeBtnSrc) || "icons/close.png";
25 btn = new Element("input", { type: "image", src: imgsrc, alt: "X"}).setStyle(style);
26 elt.insert({top: btn});
27 btn.observe("click", function(evt) {
29 if (typeof options.callback === "function") {
30 options.callback.call(elt);
37 var Toggler = Class.create({
38 initialize: function(target, options) {
39 options = Object.extend({}, options);
40 target = $(target).hide();
42 var openIcn = options.openIcn || 'icons/bullet_arrow_right.png',
43 closeIcn = options.closeIcn || 'icons/bullet_arrow_down.png';
45 this.element = new Element("img", { src: openIcn })
46 .setStyle({ border: 'none', // in firefox, in image inside an anchor has a border
47 verticalAlign: "middle"});
49 this.element.observe('click', function(evt) {
50 if (target.visible()) {
51 evt.target.src = openIcn;
54 evt.target.src = closeIcn;
60 if (options.initialShow) {
62 this.element.src = closeIcn;
67 var Deck = Class.create({
68 initialize: function(elt, options) {
69 this.element = $(elt);
71 this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10));
73 setIndex: function(idx) {
74 if (idx === this.index) {
78 var childs = this.element.childElements();
79 if (childs.length === 0) {
83 idx = Math.max(0, idx);
84 idx = Math.min(childs.length - 1, idx);
86 childs.each(function(item, i) {
95 getIndex: function() {
101 highlight: function(element, color, timeout) {
103 if (typeof timeout === "undefined") {
106 current = element.getStyle('backgroundColor');
107 Element.setStyle(element, {'backgroundColor': color});
108 Element.setStyle.delay(timeout, element, {'backgroundColor': current});
113 Ajax.TimedRequest = Class.create(Ajax.Request, {
118 // see http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
119 this.transport.onreadystatechange = Prototype.emptyFunction;
120 this.transport.abort();
121 Ajax.activeRequestCount--;
124 initialize: function($super, url, delay, options) {
130 options.onSuccess = options.onSuccess &&
131 options.onSuccess.wrap(function(proceed, transport, json) {
133 window.clearTimeout(this.timeout);
136 if (transport.getStatus() === 0) {
137 this.options.onFailure(transport, json);
139 proceed(transport, json);
143 options.onFailure = options.onFailure &&
144 options.onFailure.wrap(function(proceed, transport, json) {
146 window.clearTimeout(this.timeout);
149 proceed(transport, json);
152 $super(url, options);
155 request: function($super, url) {
156 this.timeout = function() {
157 if (this.options.onFailure) {
158 this.options.onFailure(null);
161 }.bind(this).delay(this.delay);
166 Ajax.Responders.register({
167 // needed for Ajax.TimedRequest.abort to work: see
168 // http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
170 onComplete: function() {
171 Ajax.activeRequestCount--;
172 if (Ajax.activeRequestCount < 0) {
173 Ajax.activeRequestCount = 0;
178 // wrapper around Form.request that sets up the submit listener, stops the
179 // submit event, calls presubmit function, calls Form.request and calls a
180 // postsubmit function
181 Element.addMethods('form', {
182 ajaxize : function(form, options) {
185 options = Object.clone(options || {});
187 $(form).observe('submit', function(evt) {
188 evt.stop(); // cancel form submission
190 reqoptions = Object.clone(options);
191 delete(reqoptions.presubmit);
192 delete(reqoptions.postsubmit);
193 delete(reqoptions.delay);
195 if (Object.isFunction(options.presubmit)) {
196 if (options.presubmit(this) === false) {
201 var params = reqoptions.parameters, action = this.readAttribute('action') || '';
203 if (action.blank()) {
204 action = window.location.href;
206 reqoptions.parameters = this.serialize(true);
209 if (Object.isString(params)) {
210 params = params.toQueryParams();
212 Object.extend(reqoptions.parameters, params);
215 if (this.hasAttribute('method') && !reqoptions.method) {
216 reqoptions.method = this.method;
219 if (reqoptions.onFailure) {
220 reqoptions.onFailure = reqoptions.onFailure.wrap(function(proceed, transport, json) {
222 proceed(transport, json);
225 reqoptions.onFailure = function() {
230 if (reqoptions.onSuccess) {
231 reqoptions.onSuccess = reqoptions.onSuccess.wrap(function(proceed, transport, json) {
233 proceed(transport, json);
236 reqoptions.onSuccess = function() {
241 new Ajax.TimedRequest(action, options.delay || 20, reqoptions);
243 if (Object.isFunction(options.postsubmit)) {
244 options.postsubmit(this);
246 Form.getElements(form).each(function(elt) {
253 setfocus: function(form) {
257 error = form.down('.error');
259 tofocus = error.previous('input,textarea');
261 tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
264 if (error && (typeof tofocus.highlight === "function")) {
265 tofocus.highlight('#F08080');
271 checkEmptyElements: function(form, errorMessage) {
273 form.select('.required').each(function(elt) {
274 var id = elt.getAttribute('for'), control = $(id);
278 if (!control.check(function() {
279 return !this.value.strip().empty();
281 results.push(control);
288 Element.addMethods(['input', 'textarea'], {
289 check: function(control, callback, errorMessage) {
290 if (callback.call(control)) {
294 after: new Element("div", {className: 'error'}).update(errorMessage)
299 observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) {
300 if (eventName === "contentchange") {
301 proceed(element, 'keyup', function(evt) {
302 if (evt.keyCode === 13) {
305 handler.apply(null, arguments);
307 proceed(element, 'paste', handler);
308 return proceed(element, 'change', handler);
310 return proceed(element, eventName, handler);
313 timedobserve: function(element, callback, delay) {
314 var timeout = null, initialvalue = element.value;
316 if (typeof delay !== "number") {
319 delay = delay * 1000;
321 var canceltimer = function() {
323 clearTimeout(timeout);
327 var resettimer = function() {
329 timeout = setTimeout(triggercallback, delay);
331 var triggercallback = function() {
333 if (initialvalue !== element.value) {
334 initialvalue = element.value;
335 callback.call(element);
339 element.observe('blur', triggercallback).
340 observe('keyup', resettimer).
341 observe('paste', resettimer);
346 Element.addMethods('div', {
347 setMessage: function(div, message, status) {
350 div.setMessageStatus(status);
353 div.addMessage(message);
358 clearMessages: function(div) {
359 var node = div.firstChild, nextNode;
362 nextNode = node.nextSibling;
363 if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') {
364 div.removeChild(node);
372 addMessage: function(div, message) {
373 var node = (div.ownerDocument || document).createTextNode(message);
375 if ($A(div.childNodes).filter(function(node) {
376 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
378 div.insert(new Element('br'));
381 div.appendChild(node);
385 setMessageStatus: function(div, status) {
386 return div.removeClassName('error').
387 removeClassName('warn').
388 removeClassName('info').
389 removeClassName('success').
390 addClassName(status);