1 include(bbq.gui.GUIWidget); 2 include(bbq.gui.form.FormField); 3 include(bbq.gui.button.GUIButton); 4 include(bbq.gui.form.TextField); 5 6 bbq.gui.form.Form = new Class.create(bbq.gui.GUIWidget, /** @lends bbq.gui.form.Form.prototype */ { 7 _submitButton: null, 8 9 /** 10 * This is a collection of form fields with names and values. Calling Form#getValues returns 11 * a key/value Object of values. 12 * 13 * @example 14 * <pre><code class="language-javascript"> 15 * // create two fields 16 * var field1 = new bbq.gui.form.TextField({name: "foo"}); 17 * var field2 = new bbq.gui.form.TextField({name: "bar"}); 18 * 19 * // add them to our form 20 * var form = new bbq.gui.form.Form(); 21 * form.appendChild(field1); 22 * form.appendChild(field2); 23 * 24 * // set the values on the fields 25 * field1.setValue("hello"); 26 * field2.setValue("world"); 27 * 28 * // returns {foo: "hello", bar: "world"} 29 * form.getValues(); 30 * </code></pre> 31 * @constructs 32 * @extends bbq.gui.GUIWidget 33 * @param {Object} options 34 */ 35 initialize: function($super, options) { 36 $super(options); 37 38 this.setRootNode("form"); 39 this.addClass("Form"); 40 }, 41 42 /** 43 * @throws {Error} The error has two fields {String} error for language translations and {bbq.gui.form.FormField} field which is the field which caused the error. 44 * @returns {Object} Key/value parings of form elements previously added to this form. 45 */ 46 getValues: function() { 47 var output = {}; 48 49 this._walkForValues(this.getRootNode(), output); 50 51 return output; 52 }, 53 54 _walkForValues: function(node, values) { 55 var children = $A(node.childNodes); 56 57 children.each(function(child) { 58 if(child.owner && child.owner() instanceof bbq.gui.form.FormField) { 59 var formField = child.owner(); 60 61 if(formField.getName()) { 62 values[formField.getName()] = formField.getValue(); 63 } 64 } 65 66 this._walkForValues(child, values); 67 }.bind(this)); 68 }, 69 70 /** 71 * @inheritDoc 72 */ 73 render: function() { 74 this._walkForSubmitButtons(this.getRootNode()); 75 this._walkForTextFields(this.getRootNode()); 76 }, 77 78 _walkForSubmitButtons: function(node) { 79 var children = $A(node.childNodes); 80 81 children.each(function(child) { 82 if(child.owner && child.owner() instanceof bbq.gui.button.GUIButton) { 83 this._submitButton = child.owner(); 84 } 85 86 this._walkForSubmitButtons(child); 87 }.bind(this)); 88 }, 89 90 _walkForTextFields: function(node) { 91 var children = $A(node.childNodes); 92 93 children.each(function(child) { 94 if(child.owner && child.owner() instanceof bbq.gui.form.TextField) { 95 var field = child.owner(); 96 97 Event.observe(field.getRootNode(), "keypress", this._shouldSubmit.bindAsEventListener(this)); 98 } 99 100 this._walkForTextFields(child); 101 }.bind(this)); 102 }, 103 104 _shouldSubmit: function(event) { 105 if (event.keyCode == Event.KEY_RETURN) { 106 Event.stop(event); 107 108 if(this._submitButton) { 109 this._submitButton.buttonClicked(); 110 } 111 } 112 } 113 }); 114