// Generated by github.com/steida/coffee2closure 0.1.12
goog.provide('spark.components.Form');
goog.require('spark.core.View');
goog.require('spark.components.FieldFactory');
goog.require('spark.components.LabeledInput');
/**
Form component.
*/
/**
@constructor
@export
@param {Object=} options Class options.
@param {*=} data Class data
@extends {spark.core.View}
*/
spark.components.Form = function(options, data) {
var buttons, inputs;
if (options == null) {
options = {};
}
this.getCssClass(options, 'form');
spark.components.Form.superClass_.constructor.call(this, options, data);
this.inputs = [];
this.buttons = [];
this.inputsByName = {};
inputs = options.inputs || options['inputs'];
buttons = options.buttons || options['buttons'];
this.createContainers_();
if (inputs) {
inputs.forEach((function(_this) {
return function(options) {
return _this.createInput(options);
};
})(this));
}
if (buttons) {
buttons.forEach((function(_this) {
return function(options) {
return _this.createButton(options);
};
})(this));
}
this.setData(data);
}
goog.inherits(spark.components.Form, spark.core.View);
/**
Creates DOM element to hold input and button elements.
@private
*/
spark.components.Form.prototype.createContainers_ = function() {
this.inputsContainer = new spark.core.View({
cssClass: 'input-container'
});
this.buttonsContainer = new spark.core.View({
cssClass: 'buttons-container'
});
this.appendView(this.inputsContainer);
return this.appendView(this.buttonsContainer);
};
/**
Creates field component from passed option using FieldFactory.
FIXME: Creating LabeledInput should be handled in FieldFactory.
@export
@param {!Object} options Field options to create a field component.
*/
spark.components.Form.prototype.createInput = function(options) {
var allOptions, data, input, inputOptions, key, label, labelOptions, labeledInput, name, type, value;
name = options.name, label = options.label, type = options.type;
if (type === 'combobox') {
data = options.items;
}
if (label) {
labelOptions = {};
inputOptions = {};
for (key in options) {
value = options[key];
inputOptions[key] = value;
labelOptions[key] = value;
}
allOptions = {
labelOptions: labelOptions,
inputOptions: inputOptions
};
if (type === 'checkbox' || type === 'radio') {
allOptions.inputFirst = true;
}
labeledInput = new spark.components.LabeledInput(allOptions, data);
this.inputsContainer.appendView(labeledInput);
this.inputs.push(input = labeledInput.input);
} else {
input = spark.components.FieldFactory(options, data);
this.inputsContainer.appendView(input);
this.inputs.push(input);
}
if (name) {
return this.inputsByName[name] = input;
}
};
/**
Creates button with the options.
@export
@param {!Object} options Options to create a button component.
*/
spark.components.Form.prototype.createButton = function(options) {
var button;
button = new spark.components.Button(options);
this.buttons.push(button);
return this.buttonsContainer.appendView(button);
};
/**
Sets form data and update fields with the data.
Data should be an object and keys should match field names in this form.
Values should be string.
@export
*/
spark.components.Form.prototype.setData = function(data) {
var input, name, value;
if (this.inputsByName) {
for (name in data) {
value = data[name];
input = this.inputsByName[name];
if (input) {
input.setValue(value);
}
}
}
return spark.components.Form.superClass_.setData.apply(this, arguments);
};
/**
Returns form data from DOM elements.
This method may be useful for serializing the form data.
@export
*/
spark.components.Form.prototype.getData = function() {
var dataSet, input, _i, _len, _ref;
dataSet = {};
_ref = this.inputs;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
input = _ref[_i];
dataSet[input.getName()] = input.getValue();
}
return dataSet;
};
/**
Returns input container
@export
@return {spark.core.View}
*/
spark.components.Form.prototype.getInputsContainer = function() {
return this.inputsContainer;
};
/**
Returns buttons container
@export
@return {spark.core.View}
*/
spark.components.Form.prototype.getButtonsContainer = function() {
return this.buttonsContainer;
};
/**
Returns inputs
@export
@return {Array.<spark.components.Field>}
*/
spark.components.Form.prototype.getInputs = function() {
return this.inputs;
};
/**
Returns inputs
@export
@return {Array.<spark.components.Button>}
*/
spark.components.Form.prototype.getButtons = function() {
return this.buttons;
};
/**
Returns a field by name.
@export
@param {string} name Field name.
@return {Array.<spark.components.Field>|null}
*/
spark.components.Form.prototype.getInputByName = function(name) {
return this.inputsByName[name] || null;
};