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({
41 this.element.src = this.options.openIcn;
43 document.fire('toggler:close', this);
47 this.element.src = this.options.closeIcn;
49 document.fire('toggler:open', this);
52 toggle: function(evt) {
53 if (evt && typeof evt.stop === "function") {
56 if (this.target.visible()) {
63 initialize: function(target, options) {
64 this.options = Object.extend({
65 openIcn: 'icons/bullet_arrow_right.png',
66 closeIcn: 'icons/bullet_arrow_down.png'
69 this.target = $(target).hide();
70 this.element = new Element("img").setStyle({ border: 'none', // in firefox, in image inside an anchor has a border
71 verticalAlign: "middle"});
72 this.element.observe('click', this.toggle.bindAsEventListener(this));
74 if (this.options.autoOpen) {
82 var Deck = Class.create({
83 initialize: function(elt, options) {
84 this.element = $(elt);
86 this.setIndex(parseInt(this.element.readAttribute("selectedindex") || 0, 10));
88 setIndex: function(idx) {
89 if (idx === this.index) {
93 var childs = this.element.childElements();
94 if (childs.length === 0) {
98 idx = Math.max(0, idx);
99 idx = Math.min(childs.length - 1, idx);
101 childs.each(function(item, i) {
110 getIndex: function() {
116 highlight: function(element, color, timeout) {
118 if (typeof timeout === "undefined") {
121 current = element.getStyle('backgroundColor');
122 Element.setStyle(element, {'backgroundColor': color});
123 Element.setStyle.delay(timeout, element, {'backgroundColor': current});
126 text: function(element, content) {
127 if (typeof content === "undefined") { // getter
128 if (element.nodeType === 8) {
130 } else if (element.nodeType === 3 || element.nodeType === 4) {
131 return element.nodeValue;
133 return $A(element.childNodes).inject("", function(acc, el) {
134 return acc + Element.text(el);
138 var node = document.createTextNode(content);
139 element.update().appendChild(node);
145 Ajax.TimedRequest = Class.create(Ajax.Request, {
150 // see http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
151 this.transport.onreadystatechange = Prototype.emptyFunction;
152 this.transport.abort();
153 Ajax.activeRequestCount--;
156 initialize: function($super, url, delay, options) {
162 options.onSuccess = options.onSuccess &&
163 options.onSuccess.wrap(function(proceed, transport, json) {
165 window.clearTimeout(this.timeout);
168 if (transport.getStatus() === 0) {
169 this.options.onFailure(transport, json);
171 proceed(transport, json);
175 options.onFailure = options.onFailure &&
176 options.onFailure.wrap(function(proceed, transport, json) {
178 window.clearTimeout(this.timeout);
181 proceed(transport, json);
184 $super(url, options);
187 request: function($super, url) {
188 this.timeout = function() {
189 if (this.options.onFailure) {
190 this.options.onFailure(null);
193 }.bind(this).delay(this.delay);
198 Ajax.Responders.register({
199 // needed for Ajax.TimedRequest.abort to work: see
200 // http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html
202 onComplete: function() {
203 Ajax.activeRequestCount--;
204 if (Ajax.activeRequestCount < 0) {
205 Ajax.activeRequestCount = 0;
210 // wrapper around Form.request that sets up the submit listener, stops the
211 // submit event, calls presubmit function, calls Form.request and calls a
212 // postsubmit function
213 Element.addMethods('form', {
214 ajaxize : function(form, options) {
217 options = Object.clone(options || {});
219 $(form).observe('submit', function(evt) {
220 evt.stop(); // cancel form submission
222 reqoptions = Object.clone(options);
223 delete(reqoptions.presubmit);
224 delete(reqoptions.postsubmit);
225 delete(reqoptions.delay);
227 if (Object.isFunction(options.presubmit)) {
228 if (options.presubmit(this) === false) {
233 var params = reqoptions.parameters, action = this.readAttribute('action') || '';
235 if (action.blank()) {
236 action = window.location.href;
238 reqoptions.parameters = this.serialize(true);
241 if (Object.isString(params)) {
242 params = params.toQueryParams();
244 Object.extend(reqoptions.parameters, params);
247 if (this.hasAttribute('method') && !reqoptions.method) {
248 reqoptions.method = this.method;
251 if (reqoptions.onFailure) {
252 reqoptions.onFailure = reqoptions.onFailure.wrap(function(proceed, transport, json) {
254 proceed(transport, json);
257 reqoptions.onFailure = function() {
262 if (reqoptions.onSuccess) {
263 reqoptions.onSuccess = reqoptions.onSuccess.wrap(function(proceed, transport, json) {
265 proceed(transport, json);
268 reqoptions.onSuccess = function() {
273 new Ajax.TimedRequest(action, options.delay || 20, reqoptions);
275 if (Object.isFunction(options.postsubmit)) {
276 options.postsubmit(this);
278 Form.getElements(form).each(function(elt) {
285 setfocus: function(form) {
289 error = form.down('.error');
291 tofocus = error.previous('input,textarea');
293 tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
296 if (error && (typeof tofocus.highlight === "function")) {
297 tofocus.highlight('#F08080');
303 checkEmptyElements: function(form, errorMessage) {
305 form.select('.required').each(function(elt) {
306 var id = elt.getAttribute('for'), control = $(id);
310 if (!control.check(function() {
311 return !this.value.strip().empty();
313 results.push(control);
320 Element.addMethods(['input', 'textarea'], {
321 check: function(control, callback, errorMessage) {
322 if (callback.call(control)) {
326 after: new Element("div", {className: 'error'}).update(errorMessage)
331 observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) {
332 if (eventName === "contentchange") {
333 proceed(element, 'keyup', function(evt) {
334 if (evt.keyCode === 13) {
337 handler.apply(null, arguments);
339 proceed(element, 'paste', handler);
340 return proceed(element, 'change', handler);
342 return proceed(element, eventName, handler);
345 timedobserve: function(element, callback, delay) {
346 var timeout = null, initialvalue = element.value;
348 if (typeof delay !== "number") {
351 delay = delay * 1000;
353 var canceltimer = function() {
355 clearTimeout(timeout);
359 var resettimer = function() {
361 timeout = setTimeout(triggercallback, delay);
363 var triggercallback = function() {
365 if (initialvalue !== element.value) {
366 initialvalue = element.value;
367 callback.call(element);
371 element.observe('blur', triggercallback).
372 observe('keyup', resettimer).
373 observe('paste', resettimer);
378 Element.addMethods('div', {
379 setMessage: function(div, message, status) {
382 div.setMessageStatus(status);
385 div.addMessage(message);
390 clearMessages: function(div) {
391 var node = div.firstChild, nextNode;
394 nextNode = node.nextSibling;
395 if (node.nodeType === 3 || node.tagName.toLowerCase() === 'br') {
396 div.removeChild(node);
404 addMessage: function(div, message) {
405 var node = (div.ownerDocument || document).createTextNode(message);
407 if ($A(div.childNodes).filter(function(node) {
408 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
410 div.insert(new Element('br'));
413 div.appendChild(node);
417 setMessageStatus: function(div, status) {
418 return div.removeClassName('error').
419 removeClassName('warn').
420 removeClassName('info').
421 removeClassName('success').
422 addClassName(status);