1 |
|
2 | var utils = require('./utils'),
|
3 | humanize = utils.humanize;
|
4 |
|
5 | module.exports = FormForResource;
|
6 |
|
7 | function FormForResource(res, formParams, path, hs) {
|
8 | this.__helpers = hs;
|
9 | this.__formParams = formParams;
|
10 | this.__app = hs.controller.app;
|
11 | this.__resourceName = res&& res.constructor && res.constructor.modelName || false;
|
12 | this.__res = res;
|
13 | this.__path = path || this.__resourceName;
|
14 |
|
15 | this.__complexNames = (this.__app.set('view options') || {}).complexNames;
|
16 | if (typeof this.__complexNames === 'undefined') {
|
17 | this.__complexNames = true;
|
18 | }
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | FormForResource.prototype.__makeId = function makeId(name, params) {
|
29 | if (params && params.id) {
|
30 | return params.id;
|
31 | }
|
32 | return this.__complexNames && this.__path ? ((this.__path + '_' + name).replace(/[\[\]_]+/g, '_').replace(/_$/, '')) : name;
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | FormForResource.prototype.__makeName = function makeName(name, params) {
|
43 | if (params && params.name) {
|
44 | return params.name;
|
45 | }
|
46 | return this.__complexNames && this.__path ? (this.__path + name.replace(/^([^\[]+)/, '[$1]')) : name;
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | FormForResource.prototype.begin = function begin() {
|
55 | return this.__helpers.formTagBegin(this.__formParams || {});
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | FormForResource.prototype.end = function end() {
|
64 | return this.__helpers.formTagEnd();
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | FormForResource.prototype.fieldsFor = function fieldsFor(name) {
|
74 | return new FormForResource(this.__res[name], this.__formParams, this.__path + '[' + name + ']', this.__helpers);
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | FormForResource.prototype.input = function (name, params) {
|
92 | params = params || {};
|
93 | if (params.type === undefined) {
|
94 | params.type = 'text';
|
95 | }
|
96 | if (params.value === undefined && params.type.toLowerCase() !== 'password') {
|
97 | params.value = typeof this.__res[name] !== 'undefined' ? this.__res[name] : '';
|
98 | }
|
99 | return this.__helpers.inputTag({
|
100 | name: this.__makeName(name, params),
|
101 | id: this.__makeId(name, params)
|
102 | }, params);
|
103 | };
|
104 |
|
105 | FormForResource.prototype.checkbox = function checkbox(name, params) {
|
106 | params = params || {};
|
107 | if (params.value === undefined) {
|
108 | params.value = this.__res[name] || 1;
|
109 | }
|
110 | if (params.checked === undefined) {
|
111 | if(this.__res[name]) {
|
112 | params.checked = 'checked';
|
113 | }
|
114 | } else if (params.checked === false) {
|
115 | delete params.checked;
|
116 | }
|
117 | return this.__helpers.inputTag({
|
118 | name: this.__makeName(name, params),
|
119 | id: this.__makeId(name, params),
|
120 | type: 'checkbox'
|
121 | }, params);
|
122 | };
|
123 |
|
124 | FormForResource.prototype.file = function file(name, params) {
|
125 | return this.__helpers.inputTag({
|
126 | name: this.__makeName(name, params),
|
127 | id: this.__makeId(name, params),
|
128 | type: 'file'
|
129 | }, params);
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | FormForResource.prototype.label = function label(name, caption, params) {
|
148 | if (typeof caption !== 'string') {
|
149 | if (!params) {
|
150 | params = caption;
|
151 | }
|
152 | var description = '';
|
153 | var model = this.__res.constructor.modelName;
|
154 | var ctl = this.__helpers.controller;
|
155 | var shortPath = 'models.' + model + '.fields.' + name;
|
156 | var long = ctl.t(shortPath + '.label', '');
|
157 |
|
158 | if (long) {
|
159 | caption = long;
|
160 | } else {
|
161 | caption = ctl.t(shortPath, humanize(name));
|
162 | }
|
163 |
|
164 | description = ctl.t(shortPath + '.description', description);
|
165 | if (description) {
|
166 | caption += this.__helpers.icon('info-sign', {
|
167 | rel: 'popover',
|
168 | title: 'Help',
|
169 | 'data-content': description
|
170 | });
|
171 | }
|
172 | }
|
173 | return this.__helpers.labelTag(
|
174 | caption,
|
175 | {for: this.__makeId(name, params) },
|
176 | params);
|
177 | };
|
178 |
|
179 | FormForResource.prototype.submit = function submit(name, params) {
|
180 | return this.__helpers.tag('button', name || 'Commit', {type: 'submit'}, params);
|
181 | };
|
182 |
|
183 | FormForResource.prototype.button = function button(name, params) {
|
184 | return this.__helpers.tag('button', name, params);
|
185 | };
|
186 |
|
187 | FormForResource.prototype.textarea = function textarea(name, params) {
|
188 | var value = params && 'value' in params ? params.value : this.__res[name] || '';
|
189 | return this.__helpers.textareaTag(this.__helpers.sanitize(value), {name: this.__makeName(name, params), id: this.__makeId(name, params)}, params);
|
190 | };
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | FormForResource.prototype.select = function select(name, options, params) {
|
213 | options = options || [];
|
214 | params = params || {};
|
215 |
|
216 |
|
217 | var optionFieldname = params.fieldname || 'value';
|
218 | delete params.fieldname;
|
219 |
|
220 |
|
221 | var optionFieldvalue = params.fieldvalue || optionFieldname;
|
222 | delete params.fieldvalue;
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | if (params.size === undefined) {
|
228 | params.size = 1;
|
229 | } else {
|
230 | if (params.size > 1) {
|
231 | if (params.multiple === undefined || params.multiple === true) {
|
232 | params.multiple = 'multiple';
|
233 | } else {
|
234 | delete params.multiple;
|
235 | }
|
236 | }
|
237 | }
|
238 |
|
239 |
|
240 | if (params.select === undefined) {
|
241 | params.select = this.__res[name] || '';
|
242 | }
|
243 | var optionSelected = params.select;
|
244 | delete params.select;
|
245 |
|
246 |
|
247 | var innerOptions = '';
|
248 |
|
249 |
|
250 | if (params.blank !== undefined) {
|
251 | innerOptions += this.__helpers.optionTag(this.__helpers.sanitize(params.blank), {value: ''});
|
252 | }
|
253 |
|
254 | for (var optionsNr in options) {
|
255 | var option = options[optionsNr];
|
256 | var optionParameters = {};
|
257 |
|
258 |
|
259 | if (option[optionFieldvalue] != option[optionFieldname]) {
|
260 | optionParameters.value = option[optionFieldvalue];
|
261 | }
|
262 |
|
263 | var actualValue, displayValue;
|
264 | if (typeof option === 'object') {
|
265 | actualValue = optionFieldname in option ? option[optionFieldvalue] : option;
|
266 | displayValue = optionFieldname in option ? option[optionFieldname] : option;
|
267 | } else {
|
268 | displayValue = actualValue = option + '';
|
269 |
|
270 | }
|
271 |
|
272 | if (activeValue(actualValue, optionSelected, options.matcher)) {
|
273 | optionParameters.selected = 'selected';
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 | innerOptions += this.__helpers.optionTag(this.__helpers.sanitize(displayValue), optionParameters);
|
279 | }
|
280 |
|
281 | return this.__helpers.selectTag(innerOptions, {name: this.__makeName(name, params), id: this.__makeId(name, params)}, params);
|
282 | };
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | function activeValue(value, selectvalue, matcher) {
|
293 | var returnBool = false;
|
294 |
|
295 |
|
296 | if (Object.prototype.toString.call(selectvalue) === '[object Array]') {
|
297 |
|
298 | for (var selectvalueNr in selectvalue) {
|
299 |
|
300 | if (matcher) {
|
301 | if (matcher(value, selectvalue[selectvalueNr])) {
|
302 | returnBool = true;
|
303 | }
|
304 | } else if (String(value) == String(selectvalue[selectvalueNr])) {
|
305 | returnBool = true;
|
306 | continue;
|
307 | }
|
308 | }
|
309 | } else {
|
310 |
|
311 |
|
312 | if (String(value) == String(selectvalue)) {
|
313 | returnBool = true;
|
314 | }
|
315 | }
|
316 | return returnBool;
|
317 | }
|