1 |
|
2 | var Tag = require('./tag'),
|
3 | wrappers = require('./wrappers');
|
4 |
|
5 | var Fieldgroup = module.exports = function Fieldgroup (options, elements) {
|
6 |
|
7 | this.options = options || {};
|
8 | this.elements = elements || [];
|
9 | this.options.theme = this.options.theme || {};
|
10 |
|
11 |
|
12 | if (this.options.label !== undefined && typeof this.options.label === 'string') {
|
13 | this.options.label = {
|
14 | label: this.options.label
|
15 | };
|
16 | }
|
17 |
|
18 |
|
19 | if (this.options.helpText !== undefined && typeof this.options.helpText === 'string') {
|
20 | this.options.helpText = {
|
21 | text: this.options.helpText
|
22 | }
|
23 | }
|
24 |
|
25 | };
|
26 |
|
27 | Fieldgroup.prototype.render = function(theme) {
|
28 |
|
29 | var label,
|
30 | small,
|
31 | content = '',
|
32 | fieldsContent = '',
|
33 | _theme = copy(theme);
|
34 |
|
35 |
|
36 | if (this.options.label) {
|
37 |
|
38 |
|
39 | var attributes = this.options.label.attributes || {};
|
40 |
|
41 |
|
42 | label = new Tag('label', attributes, this.options.label.label);
|
43 |
|
44 | }
|
45 |
|
46 |
|
47 | if (this.options.helpText) {
|
48 |
|
49 | attributes = this.options.helpText.attributes || { 'class': 'help-text' };
|
50 |
|
51 | small = new Tag((this.options.helpText.tag || 'small'), attributes, this.options.helpText.text);
|
52 |
|
53 | }
|
54 |
|
55 |
|
56 | _theme.field = this.options.theme.field || theme.fieldgroup.field;
|
57 |
|
58 |
|
59 | for (var i = 0; i < this.elements.length; i++) {
|
60 |
|
61 |
|
62 | fieldsContent += this.elements[i].render(_theme);
|
63 |
|
64 | }
|
65 |
|
66 |
|
67 | content = (this.options.theme.fields || theme.fieldgroup.fields)(fieldsContent, this.elements);
|
68 |
|
69 | return (this.options.theme.group || theme.fieldgroup.group)((label !== undefined ? label.render() : ''), content, (small !== undefined ? small.render() : ''), this);
|
70 |
|
71 | };
|
72 |
|
73 | var copy = function (obj) {
|
74 |
|
75 | var newObj = {};
|
76 |
|
77 | Object.keys(obj).forEach(function (key) {
|
78 | newObj[key] = obj[key];
|
79 | });
|
80 |
|
81 | return newObj;
|
82 |
|
83 | };
|