1 |
|
2 | var Tag = require('./tag');
|
3 |
|
4 | var Fieldset = module.exports = function Fieldset (options, elements) {
|
5 |
|
6 | this.options = options || {};
|
7 |
|
8 | this.attributes = this.options.attributes || {};
|
9 |
|
10 | this.options.theme = this.options.theme || {};
|
11 |
|
12 | this.elements = elements || [];
|
13 |
|
14 |
|
15 | if (this.options.legend !== undefined && typeof this.options.legend === 'string') {
|
16 | this.options.legend = {
|
17 | label: this.options.legend,
|
18 | attributes: {}
|
19 | };
|
20 | }
|
21 |
|
22 | if (this.options.legend) {
|
23 | this.elements.unshift(new Tag('legend', this.options.legend.attributes, this.options.legend.label));
|
24 | }
|
25 |
|
26 | };
|
27 |
|
28 | Fieldset.prototype.render = function(theme) {
|
29 |
|
30 | var content = '',
|
31 | tag;
|
32 |
|
33 | for (var i = 0; i < this.elements.length; i++) {
|
34 | content += this.elements[i].render(theme);
|
35 | }
|
36 |
|
37 | tag = new Tag('fieldset', this.attributes, content);
|
38 |
|
39 | return (this.options.theme.fieldset || theme.fieldset)(tag.render(), this);
|
40 |
|
41 | };
|
42 |
|
43 | Fieldset.prototype.add = function(elObject) {
|
44 |
|
45 | this.elements.push(elObject);
|
46 |
|
47 | return elObject;
|
48 |
|
49 | };
|
50 |
|
51 | Fieldset.prototype.attribute = function (key, value) {
|
52 |
|
53 | if (value === undefined) {
|
54 | return this.attributes[key];
|
55 | }
|
56 |
|
57 | this.attributes[key] = value;
|
58 |
|
59 |
|
60 | return this;
|
61 |
|
62 | }; |
\ | No newline at end of file |