forked from thespacedojo/simple-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
253 lines (231 loc) · 7.56 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
processClass = function(optionsHash) {
if (!optionsHash) {
return
}
if (optionsHash['class']) {
html_class = " " + optionsHash['class']
} else {
html_class = ""
}
return html_class
}
processRequired = function(optionHash) {
if (optionHash['required']) {
return " required"
} else {
return ""
}
}
processId = function(optionsHash) {
if (!optionsHash) {
return
}
if (optionsHash['id']) {
return " id='" + optionsHash['id'] + "'";
} else {
return ""
}
}
processPlaceHolder = function(optionsHash) {
if (optionsHash['placeholder']) {
placeholder = " placeholder='" + optionsHash['placeholder'] + "' "
} else {
placeholder = ""
}
return placeholder
}
processLabel = function(optionsHash, field) {
if (_.isString(optionsHash['label'])) {
label_words = optionsHash['label']
} else {
label_words = _.humanize(field)
}
return label_words
}
buildLabel = function(optionsHash, field) {
if (optionsHash['label'] === false) {
return ''
} else {
label_words = processLabel(optionsHash, field)
return "<label for='"+ field +"'>" + label_words + "</label>"
}
}
buildHintBlock = function(optionsHash) {
if (optionsHash['hint']) {
hintBlock = "<span class='help-block'>" + optionsHash['hint'] + "</span>";
} else {
hintBlock = "";
}
return hintBlock;
}
buildBeforeAddon = function(optionsHash) {
addon = ""
if (optionsHash['before'] || optionsHash['after']) {
addon = "<div class='input-group'>"
if (optionsHash['before']) {
addon = addon + "<span class='input-group-addon'>" + optionsHash['before'] + "</span>"
}
}
return addon
}
buildAfterAddon = function(optionsHash) {
addon = ""
if (optionsHash['before'] || optionsHash['after']) {
if (optionsHash['after']) {
addon = "<span class='input-group-addon'>" + optionsHash['after'] + "</span>"
}
addon = addon + "</div>"
}
return addon
}
processForBelongsTo = function(field, object) {
name = object.constructor.name
if (!window[name]) {
return false
}
isAssociation = _.contains(_.pluck(window[name].belongs_to, 'name'), field)
if (isAssociation) {
associations = window[_.classify(field)].all()
var array = [];
_.each(associations, function(association) {
array.push({value: association._id, name: association.name})
})
return array
} else {
return false
}
}
processForHaBTM = function(field, object) {
name = object.constructor.name
if (!window[name]) {
return false
}
isAssociation = _.contains(_.pluck(window[name].has_and_belongs_to_many, 'name'), field)
if (isAssociation) {
associations = window[_.classify(_.singularize(field))].all()
var array = [];
_.each(associations, function(association) {
array.push({value: association._id, name: association.name})
})
return array
} else {
return false
}
}
buildAssociationCheckboxes = function(field, object, checkboxes, options) {
return false
builtCheckboxes = _.map(checkboxes, function(checkbox) {
html_class = processClass(options.hash)
checked = _.contains(object[_.singularize(field) + '_ids'], checkbox.value) === true ? ' checked' : '';
label = processLabel(options.hash, checkbox.name)
html = "<label for='"+ checkbox.name +"'><input id='"+ checkbox.name +"' name='" + checkbox.name + "' type='hidden' value='false'><input name='" + checkbox.name + "' class='"+ html_class +"' type='checkbox' value='" + checkbox.value + "' " + checked + ">" + label + "</label>";
return html;
});
return new Spacebars.SafeString(builtCheckboxes.join(' '));
}
/*----- HELPERS ------*/
UI.registerHelper('text_field', function(field, options){
var _this = this;
if (!field) {
return;
}
value = _this[field] || ""
html_class = processClass(options.hash)
type = options.hash['type'] || "text"
placeholder = processPlaceHolder(options.hash)
required = processRequired(options.hash)
html = "<input type='"+ type +"' id='" + field + "' name='"+ field +"' value='"+ value +"' class='form-control"+ html_class +"'"+ placeholder + required + " >"
label = buildLabel(options.hash, field)
hint = buildHintBlock(options.hash)
beforeAddon = buildBeforeAddon(options.hash)
afterAddon = buildAfterAddon(options.hash)
return new Spacebars.SafeString(label + beforeAddon + html + afterAddon + hint);
});
UI.registerHelper('text_area', function(field, options){
var _this = this;
if (!field) {
return;
}
value = _this[field] || ""
html_class = processClass(options.hash)
if (options.hash['rows']) {
rows = "rows='"+ options.hash['rows'] +"' "
} else {
rows = ""
}
required = processRequired(options.hash)
html = "<textarea id='" + field + "' "+ rows +"name='"+ field +"' class='form-control"+ html_class +"'" + required + ">"+ value +"</textarea>"
label = buildLabel(options.hash, field)
hint = buildHintBlock(options.hash)
return new Spacebars.SafeString(label + html + hint);
});
UI.registerHelper('select_box', function(field, options) {
_this = this;
optionsValues = undefined
if (!field) {
return;
}
associationOptions = processForBelongsTo(field, _this)
html_class = processClass(options.hash)
if (associationOptions) {
optionsValues = associationOptions
dbField = field + "_id"
} else {
dbField = field
if (options.hash.optionValues && options.hash.optionValues.length > 0) {
optionsValues = options.hash.optionValues
} else {
optionsValues = _this["" + field + "Options"]();
}
}
required = processRequired(options.hash)
html_options = [];
_.each(optionsValues, function(option) {
name = option.name || _.humanize(option)
value = option.value || option
selected = _this[field] === value ? ' selected' : '';
return html_options.push("<option value='" + value + "'" + selected + ">" + name + "</option>");
});
html = "<select class='form-control" + html_class + "' name='" + dbField + "'" + required + ">" + (html_options.join('')) + "</select>"
label = buildLabel(options.hash, dbField)
hint = buildHintBlock(options.hash)
return new Spacebars.SafeString(label + html + hint);
});
UI.registerHelper('check_box', function(field, options) {
var capitalizedField, checked;
if (!field) {
return;
}
associationOptions = null//processForHaBTM(field, this)
if (associationOptions) {
return buildAssociationCheckboxes(field, this, associationOptions, options)
} else {
html_class = processClass(options.hash)
checked = this[field] === 'true' ? ' checked' : '';
label = processLabel(options.hash, field)
required = processRequired(options.hash)
html = "<label for='"+ field +"'><input id='"+ field +"' name='" + field + "' type='hidden' value='false'><input name='" + field + "' class='"+ html_class +"' type='checkbox' value='true' " + checked + required + ">" + label + "</label>";
hint = buildHintBlock(options.hash)
return new Spacebars.SafeString(html + hint);
}
});
UI.registerHelper('submit_button', function(text, options){
var _this = this;
if (!text && !options) {
options = {};
}
if (text && text.hash) {
options = text;
text = undefined;
}
klass = _this.constructor.name;
value = text || "Submit " + klass;
html_class = processClass(options.hash);
html_id = processId(options.hash);
if (options.hash && options.hash['button']) {
html = "<button type='submit' class='btn btn-default"+ html_class + "'"+ html_id +">" + value + "</button>";
} else {
html = "<input type='submit' value='"+ value +"' class='btn btn-default"+ html_class + "'"+ html_id +">";
}
return new Spacebars.SafeString(html);
});