2007-01-18 22:24:27 +00:00
|
|
|
var Form = {
|
|
|
|
reset: function(form) {
|
|
|
|
$(form).reset();
|
|
|
|
return form;
|
|
|
|
},
|
|
|
|
|
|
|
|
serializeElements: function(elements, getHash) {
|
|
|
|
var data = elements.inject({}, function(result, element) {
|
|
|
|
if (!element.disabled && element.name) {
|
|
|
|
var key = element.name, value = $(element).getValue();
|
2007-03-04 21:54:09 +00:00
|
|
|
if (value != null) {
|
|
|
|
if (key in result) {
|
2007-01-18 22:24:27 +00:00
|
|
|
if (result[key].constructor != Array) result[key] = [result[key]];
|
|
|
|
result[key].push(value);
|
|
|
|
}
|
|
|
|
else result[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
return getHash ? data : Hash.toQueryString(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.Methods = {
|
|
|
|
serialize: function(form, getHash) {
|
|
|
|
return Form.serializeElements(Form.getElements(form), getHash);
|
|
|
|
},
|
|
|
|
|
|
|
|
getElements: function(form) {
|
|
|
|
return $A($(form).getElementsByTagName('*')).inject([],
|
|
|
|
function(elements, child) {
|
|
|
|
if (Form.Element.Serializers[child.tagName.toLowerCase()])
|
|
|
|
elements.push(Element.extend(child));
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
getInputs: function(form, typeName, name) {
|
|
|
|
form = $(form);
|
|
|
|
var inputs = form.getElementsByTagName('input');
|
|
|
|
|
|
|
|
if (!typeName && !name) return $A(inputs).map(Element.extend);
|
|
|
|
|
|
|
|
for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) {
|
|
|
|
var input = inputs[i];
|
|
|
|
if ((typeName && input.type != typeName) || (name && input.name != name))
|
|
|
|
continue;
|
|
|
|
matchingInputs.push(Element.extend(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchingInputs;
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function(form) {
|
|
|
|
form = $(form);
|
2007-03-27 17:53:52 +00:00
|
|
|
Form.getElements(form).invoke('disable');
|
2007-01-18 22:24:27 +00:00
|
|
|
return form;
|
|
|
|
},
|
|
|
|
|
|
|
|
enable: function(form) {
|
|
|
|
form = $(form);
|
2007-03-27 17:53:52 +00:00
|
|
|
Form.getElements(form).invoke('enable');
|
2007-01-18 22:24:27 +00:00
|
|
|
return form;
|
|
|
|
},
|
|
|
|
|
|
|
|
findFirstElement: function(form) {
|
|
|
|
return $(form).getElements().find(function(element) {
|
|
|
|
return element.type != 'hidden' && !element.disabled &&
|
|
|
|
['input', 'select', 'textarea'].include(element.tagName.toLowerCase());
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
focusFirstElement: function(form) {
|
|
|
|
form = $(form);
|
|
|
|
form.findFirstElement().activate();
|
|
|
|
return form;
|
2007-01-28 07:30:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
request: function(form, options) {
|
|
|
|
form = $(form), options = Object.clone(options || {});
|
|
|
|
|
|
|
|
var params = options.parameters;
|
|
|
|
options.parameters = form.serialize(true);
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
if (typeof params == 'string') params = params.toQueryParams();
|
|
|
|
Object.extend(options.parameters, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (form.hasAttribute('method') && !options.method)
|
|
|
|
options.method = form.method;
|
|
|
|
|
|
|
|
return new Ajax.Request(form.action, options);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.extend(Form, Form.Methods);
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
Form.Element = {
|
|
|
|
focus: function(element) {
|
|
|
|
$(element).focus();
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
|
|
|
|
select: function(element) {
|
|
|
|
$(element).select();
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Form.Element.Methods = {
|
|
|
|
serialize: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
if (!element.disabled && element.name) {
|
|
|
|
var value = element.getValue();
|
|
|
|
if (value != undefined) {
|
|
|
|
var pair = {};
|
|
|
|
pair[element.name] = value;
|
|
|
|
return Hash.toQueryString(pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
|
|
|
|
getValue: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
var method = element.tagName.toLowerCase();
|
|
|
|
return Form.Element.Serializers[method](element);
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function(element) {
|
|
|
|
$(element).value = '';
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
|
|
|
|
present: function(element) {
|
|
|
|
return $(element).value != '';
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: function(element) {
|
|
|
|
element = $(element);
|
2007-01-18 23:07:02 +00:00
|
|
|
try {
|
|
|
|
element.focus();
|
|
|
|
if (element.select && (element.tagName.toLowerCase() != 'input' ||
|
|
|
|
!['button', 'reset', 'submit'].include(element.type)))
|
|
|
|
element.select();
|
|
|
|
} catch (e) {}
|
2007-01-18 22:24:27 +00:00
|
|
|
return element;
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function(element) {
|
|
|
|
element = $(element);
|
2007-03-05 16:03:11 +00:00
|
|
|
element.blur();
|
2007-01-18 22:24:27 +00:00
|
|
|
element.disabled = true;
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
|
|
|
|
enable: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
element.disabled = false;
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.extend(Form.Element, Form.Element.Methods);
|
2007-01-27 19:45:34 +00:00
|
|
|
Object.extend(Element.Methods.ByTag, {
|
|
|
|
"FORM": Object.clone(Form.Methods),
|
|
|
|
"INPUT": Object.clone(Form.Element.Methods),
|
|
|
|
"SELECT": Object.clone(Form.Element.Methods),
|
|
|
|
"TEXTAREA": Object.clone(Form.Element.Methods)
|
|
|
|
});
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
var Field = Form.Element;
|
|
|
|
var $F = Form.Element.getValue;
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
Form.Element.Serializers = {
|
|
|
|
input: function(element) {
|
|
|
|
switch (element.type.toLowerCase()) {
|
|
|
|
case 'checkbox':
|
|
|
|
case 'radio':
|
|
|
|
return Form.Element.Serializers.inputSelector(element);
|
|
|
|
default:
|
|
|
|
return Form.Element.Serializers.textarea(element);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
inputSelector: function(element) {
|
|
|
|
return element.checked ? element.value : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
textarea: function(element) {
|
|
|
|
return element.value;
|
|
|
|
},
|
|
|
|
|
|
|
|
select: function(element) {
|
|
|
|
return this[element.type == 'select-one' ?
|
|
|
|
'selectOne' : 'selectMany'](element);
|
|
|
|
},
|
|
|
|
|
|
|
|
selectOne: function(element) {
|
|
|
|
var index = element.selectedIndex;
|
|
|
|
return index >= 0 ? this.optionValue(element.options[index]) : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
selectMany: function(element) {
|
|
|
|
var values, length = element.length;
|
|
|
|
if (!length) return null;
|
|
|
|
|
|
|
|
for (var i = 0, values = []; i < length; i++) {
|
|
|
|
var opt = element.options[i];
|
|
|
|
if (opt.selected) values.push(this.optionValue(opt));
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
},
|
|
|
|
|
|
|
|
optionValue: function(opt) {
|
|
|
|
// extend element because hasAttribute may not be native
|
|
|
|
return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
Abstract.TimedObserver = function() {}
|
|
|
|
Abstract.TimedObserver.prototype = {
|
|
|
|
initialize: function(element, frequency, callback) {
|
|
|
|
this.frequency = frequency;
|
|
|
|
this.element = $(element);
|
|
|
|
this.callback = callback;
|
|
|
|
|
|
|
|
this.lastValue = this.getValue();
|
|
|
|
this.registerCallback();
|
|
|
|
},
|
|
|
|
|
|
|
|
registerCallback: function() {
|
|
|
|
setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
|
|
|
|
},
|
|
|
|
|
|
|
|
onTimerEvent: function() {
|
|
|
|
var value = this.getValue();
|
|
|
|
var changed = ('string' == typeof this.lastValue && 'string' == typeof value
|
|
|
|
? this.lastValue != value : String(this.lastValue) != String(value));
|
|
|
|
if (changed) {
|
|
|
|
this.callback(this.element, value);
|
|
|
|
this.lastValue = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Form.Element.Observer = Class.create();
|
|
|
|
Form.Element.Observer.prototype = Object.extend(new Abstract.TimedObserver(), {
|
|
|
|
getValue: function() {
|
|
|
|
return Form.Element.getValue(this.element);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Form.Observer = Class.create();
|
|
|
|
Form.Observer.prototype = Object.extend(new Abstract.TimedObserver(), {
|
|
|
|
getValue: function() {
|
|
|
|
return Form.serialize(this.element);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
Abstract.EventObserver = function() {}
|
|
|
|
Abstract.EventObserver.prototype = {
|
|
|
|
initialize: function(element, callback) {
|
|
|
|
this.element = $(element);
|
|
|
|
this.callback = callback;
|
|
|
|
|
|
|
|
this.lastValue = this.getValue();
|
|
|
|
if (this.element.tagName.toLowerCase() == 'form')
|
|
|
|
this.registerFormCallbacks();
|
|
|
|
else
|
|
|
|
this.registerCallback(this.element);
|
|
|
|
},
|
|
|
|
|
|
|
|
onElementEvent: function() {
|
|
|
|
var value = this.getValue();
|
|
|
|
if (this.lastValue != value) {
|
|
|
|
this.callback(this.element, value);
|
|
|
|
this.lastValue = value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
registerFormCallbacks: function() {
|
|
|
|
Form.getElements(this.element).each(this.registerCallback.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
registerCallback: function(element) {
|
|
|
|
if (element.type) {
|
|
|
|
switch (element.type.toLowerCase()) {
|
|
|
|
case 'checkbox':
|
|
|
|
case 'radio':
|
|
|
|
Event.observe(element, 'click', this.onElementEvent.bind(this));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Event.observe(element, 'change', this.onElementEvent.bind(this));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Form.Element.EventObserver = Class.create();
|
|
|
|
Form.Element.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), {
|
|
|
|
getValue: function() {
|
|
|
|
return Form.Element.getValue(this.element);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Form.EventObserver = Class.create();
|
|
|
|
Form.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), {
|
|
|
|
getValue: function() {
|
|
|
|
return Form.serialize(this.element);
|
|
|
|
}
|
|
|
|
});
|