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 (evt.detail === 0) { // it's not a real click, possibly a submit event
32 if (typeof options.callback === "function") {
33 options.callback.call(elt);
40 var Toggler = Class.create({
44 this.element.src = this.options.openIcn;
46 document.fire('toggler:close', this);
50 this.element.src = this.options.closeIcn;
52 document.fire('toggler:open', this);
55 toggle: function(evt) {
56 if (evt && typeof evt.stop === "function") {
59 if (this.target.visible()) {
66 initialize: function(target, options) {
67 this.options = Object.extend({
68 openIcn: 'icons/bullet_arrow_right.png',
69 closeIcn: 'icons/bullet_arrow_down.png'
72 this.target = $(target).hide();
73 this.element = new Element("img").setStyle({ border: 'none', // in firefox, in image inside an anchor has a border
74 verticalAlign: "middle"});
75 this.element.observe('click', this.toggle.bindAsEventListener(this));
77 if (this.options.autoOpen) {
85 var Deck = Class.create({
86 initialize: function(elt, options) {
87 this.element = $(elt);
89 this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10));
91 setIndex: function(idx) {
92 if (idx === this.index) {
96 var childs = this.element.childElements();
97 if (childs.length === 0) {
101 idx = Math.max(0, idx);
102 idx = Math.min(childs.length - 1, idx);
104 childs.each(function(item, i) {
113 getIndex: function() {
119 highlight: function(element, color, timeout) {
121 if (typeof timeout === "undefined") {
124 current = element.getStyle('backgroundColor');
125 Element.setStyle(element, {'backgroundColor': color});
126 Element.setStyle.delay(timeout, element, {'backgroundColor': current});
129 text: function(element, content) {
130 if (typeof content === "undefined") { // getter
131 if (element.nodeType === 8) {
133 } else if (element.nodeType === 3 || element.nodeType === 4) {
134 return element.nodeValue;
136 return $A(element.childNodes).inject("", function(acc, el) {
137 return acc + Element.text(el);
141 var node = document.createTextNode(content);
142 element.update().appendChild(node);
148 Ajax.TimedRequest = Class.create(Ajax.Request, {
153 // see http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
154 this.transport.onreadystatechange = Prototype.emptyFunction;
155 this.transport.abort();
156 Ajax.activeRequestCount--;
159 initialize: function($super, url, delay, options) {
165 options.onSuccess = options.onSuccess &&
166 options.onSuccess.wrap(function(proceed, transport, json) {
168 window.clearTimeout(this.timeout);
171 if (transport.getStatus() === 0) {
172 this.options.onFailure(transport, json);
174 proceed(transport, json);
178 options.onFailure = options.onFailure &&
179 options.onFailure.wrap(function(proceed, transport, json) {
181 window.clearTimeout(this.timeout);
184 proceed(transport, json);
187 $super(url, options);
190 request: function($super, url) {
191 this.timeout = function() {
192 if (this.options.onFailure) {
193 this.options.onFailure(null);
196 }.bind(this).delay(this.delay);
201 Ajax.Responders.register({
202 // needed for Ajax.TimedRequest.abort to work: see
203 // http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
205 onComplete: function() {
206 Ajax.activeRequestCount--;
207 if (Ajax.activeRequestCount < 0) {
208 Ajax.activeRequestCount = 0;
213 // wrapper around Form.request that sets up the submit listener, stops the
214 // submit event, calls presubmit function, calls Form.request and calls a
215 // postsubmit function
216 Element.addMethods('form', {
217 ajaxize : function(form, options) {
220 options = Object.clone(options || {});
222 $(form).observe('submit', function(evt) {
223 evt.stop(); // cancel form submission
225 reqoptions = Object.clone(options);
226 delete(reqoptions.presubmit);
227 delete(reqoptions.postsubmit);
228 delete(reqoptions.delay);
230 if (Object.isFunction(options.presubmit)) {
231 if (options.presubmit(this) === false) {
236 var params = reqoptions.parameters, action = this.readAttribute('action') || '';
238 if (action.blank()) {
239 action = window.location.href;
241 reqoptions.parameters = this.serialize(true);
244 if (Object.isString(params)) {
245 params = params.toQueryParams();
247 Object.extend(reqoptions.parameters, params);
250 if (this.hasAttribute('method') && !reqoptions.method) {
251 reqoptions.method = this.method;
254 if (reqoptions.onFailure) {
255 reqoptions.onFailure = reqoptions.onFailure.wrap(function(proceed, transport, json) {
257 proceed(transport, json);
260 reqoptions.onFailure = function() {
265 if (reqoptions.onSuccess) {
266 reqoptions.onSuccess = reqoptions.onSuccess.wrap(function(proceed, transport, json) {
268 proceed(transport, json);
271 reqoptions.onSuccess = function() {
276 new Ajax.TimedRequest(action, options.delay || 20, reqoptions);
278 if (Object.isFunction(options.postsubmit)) {
279 options.postsubmit(this);
281 Form.getElements(form).each(function(elt) {
288 setfocus: function(form) {
292 error = form.down('.error');
294 tofocus = error.previous('input,textarea');
296 tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
299 if (error && (typeof tofocus.highlight === "function")) {
300 tofocus.highlight('#F08080');
306 checkEmptyElements: function(form, errorMessage) {
308 form.select('.required').each(function(elt) {
309 var id = elt.getAttribute('for'), control = $(id);
313 if (!control.check(function() {
314 return !this.value.strip().empty();
316 results.push(control);
323 Element.addMethods(['input', 'textarea'], {
324 check: function(control, callback, errorMessage) {
325 if (callback.call(control)) {
329 after: new Element("div", {className: 'error'}).update(errorMessage)
334 observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) {
335 if (eventName === "contentchange") {
336 proceed(element, 'keyup', function(evt) {
337 if (evt.keyCode === 13) {
340 handler.apply(null, arguments);
342 proceed(element, 'paste', handler.defer.bind(handler));
343 return proceed(element, 'change', handler);
345 return proceed(element, eventName, handler);
348 timedobserve: function(element, callback, delay) {
349 var timeout = null, initialvalue = element.value;
351 if (typeof delay !== "number") {
354 delay = delay * 1000;
356 var canceltimer = function() {
358 clearTimeout(timeout);
362 var resettimer = function() {
364 timeout = setTimeout(triggercallback, delay);
366 var triggercallback = function() {
368 if (initialvalue !== element.value) {
369 initialvalue = element.value;
370 callback.call(element);
374 element.observe('blur', triggercallback).
375 observe('keyup', resettimer).
376 observe('paste', resettimer);
381 Element.addMethods('div', {
382 setMessage: function(div, message, status) {
385 div.setMessageStatus(status);
388 div.addMessage(message);
393 clearMessages: function(div) {
394 var node = div.firstChild, nextNode;
397 nextNode = node.nextSibling;
398 if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br' || node.textContent || node.innerText) {
399 div.removeChild(node);
407 addMessage: function(div, message) {
408 var node = (div.ownerDocument || document).createTextNode(message);
410 if ($A(div.childNodes).filter(function(node) {
411 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br' || node.textContent || node.innerText);
413 div.insert(new Element('br'));
416 div.appendChild(node);
420 setMessageStatus: function(div, status) {
421 return div.removeClassName('error').
422 removeClassName('warn').
423 removeClassName('info').
424 removeClassName('success').
425 addClassName(status);