forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
348 lines (332 loc) · 10.9 KB
/
helpers.js
File metadata and controls
348 lines (332 loc) · 10.9 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
steal('jquerypp/view/ejs').then(function($){
/**
* @add jQuery.EJS.Helpers.prototype
*/
$.extend($.EJS.Helpers.prototype, {
/**
* Converts response to text.
*/
text: function( input, null_text ) {
if ( input == null || input === undefined ) return null_text || '';
if ( input instanceof Date ) return input.toDateString();
if ( input.toString ) return input.toString().replace(/\n/g, '<br />').replace(/''/g, "'");
return '';
},
// treyk 06/11/2009 - Pulled from old MVC.Date plugin for now. Will look for a suitable jQuery Date plugin
month_names: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
/**
* Creates a check box tag
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} options
* @param {Object} checked
*/
check_box_tag: function( name, value, options, checked ) {
options = options || {};
if(checked) options.checked = "checked";
return this.input_field_tag(name, value, 'checkbox', options);
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
*/
date_tag: function( name, value , html_options ) {
if(! (value instanceof Date)) value = new Date();
var years = [], months = [], days =[];
var year = value.getFullYear(), month = value.getMonth(), day = value.getDate();
for(var y = year - 15; y < year+15 ; y++) years.push({value: y, text: y});
for(var m = 0; m < 12; m++) months.push({value: (m), text: $View.Helpers.month_names[m]});
for(var d = 0; d < 31; d++) days.push({value: (d+1), text: (d+1)});
var year_select = this.select_tag(name+'[year]', year, years, {id: name+'[year]'} );
var month_select = this.select_tag(name+'[month]', month, months, {id: name+'[month]'});
var day_select = this.select_tag(name+'[day]', day, days, {id: name+'[day]'});
return year_select+month_select+day_select;
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
* @param {Object} interval - specified in minutes
*/
time_tag: function( name, value, html_options, interval ) {
var times = [];
if (interval == null || interval == 0)
interval = 60;
for(var h = 0; h < 24 ; h++)
for(var m = 0; m < 60; m+=interval)
{
var time = (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m;
times.push({ text: time, value: time });
}
return this.select_tag(name, value, times, html_options );
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
*/
file_tag: function( name, value, html_options ) {
return this.input_field_tag(name+'[file]', value , 'file', html_options);
},
/**
* @plugin view/helpers
* @param {Object} url_for_options
* @param {Object} html_options
*/
form_tag: function( url_for_options, html_options ) {
html_options = html_options || {};
if(html_options.multipart == true) {
html_options.method = 'post';
html_options.enctype = 'multipart/form-data';
}
html_options.action = url_for_options;
return this.start_tag_for('form', html_options);
},
/**
* @plugin view/helpers
*/
form_tag_end: function() { return this.tag_end('form'); },
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
*/
hidden_field_tag: function( name, value, html_options ) {
return this.input_field_tag(name, value, 'hidden', html_options);
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} inputType
* @param {Object} html_options
*/
input_field_tag: function( name, value , inputType, html_options ) {
html_options = html_options || {};
html_options.id = html_options.id || name;
html_options.value = value || '';
html_options.type = inputType || 'text';
html_options.name = name;
return this.single_tag_for('input', html_options);
},
/**
* @plugin view/helpers
* @param {Object} text
* @param {Object} html_options
*/
label_tag: function( text, html_options ) {
html_options = html_options || {};
return this.start_tag_for('label', html_options) + text + this.tag_end('label');
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} url
* @param {Object} html_options
*/
link_to: function( name, url, html_options ) {
if(!name) var name = 'null';
if(!html_options) var html_options = {};
this.set_confirm(html_options);
html_options.href=url;
return this.start_tag_for('a', html_options)+name+ this.tag_end('a');
},
/**
* @plugin view/helpers
* @param {Object} condition
* @param {Object} name
* @param {Object} url
* @param {Object} html_options
*/
link_to_if: function( condition, name, url, html_options ) {
return this.link_to_unless((!condition), name, url, html_options);
},
/**
* @plugin view/helpers
* @param {Object} condition
* @param {Object} name
* @param {Object} url
* @param {Object} html_options
*/
link_to_unless: function( condition, name, url, html_options ) {
if(condition) return name;
return this.link_to(name, url, html_options);
},
/**
* @plugin view/helpers
* @param {Object} html_options
*/
set_confirm: function( html_options ) {
if(html_options.confirm){
html_options.onclick = html_options.onclick || '';
html_options.onclick = html_options.onclick+
"; var ret_confirm = confirm(\""+html_options.confirm+"\"); if(!ret_confirm){ return false;} ";
html_options.confirm = null;
}
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} options
* @param {Object} html_options
* @param {Object} post
*/
submit_link_to: function( name, options, html_options, post ) {
if(!name) var name = 'null';
if(!html_options) html_options = {};
html_options.type = 'submit';
html_options.value = name;
this.set_confirm(html_options);
html_options.onclick=html_options.onclick+';window.location="'+options+'"; return false;';
return this.single_tag_for('input', html_options);
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
*/
password_field_tag: function( name, value, html_options ) { return this.input_field_tag(name, value, 'password', html_options); },
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} choices
* @param {Object} html_options
*/
select_tag: function( name, value, choices, html_options ) {
html_options = html_options || {};
html_options.id = html_options.id || name;
//html_options.value = value;
html_options.name = name;
var txt = '';
txt += this.start_tag_for('select', html_options);
for(var i = 0; i < choices.length; i++)
{
var choice = choices[i];
if(typeof choice == 'string') choice = {value: choice};
if(!choice.text) choice.text = choice.value;
if(!choice.value) choice.text = choice.text;
var optionOptions = {value: choice.value};
if(choice.value == value)
optionOptions.selected ='selected';
txt += this.start_tag_for('option', optionOptions )+choice.text+this.tag_end('option');
}
txt += this.tag_end('select');
return txt;
},
/**
* @plugin view/helpers
* @param {Object} tag
* @param {Object} html_options
*/
single_tag_for: function( tag, html_options ) { return this.tag(tag, html_options, '/>');},
/**
* @plugin view/helpers
* @param {Object} tag
* @param {Object} html_options
*/
start_tag_for: function( tag, html_options ) { return this.tag(tag, html_options); },
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} html_options
*/
submit_tag: function( name, html_options ) {
html_options = html_options || {};
html_options.type = html_options.type || 'submit';
html_options.value = name || 'Submit';
return this.single_tag_for('input', html_options);
},
/**
* @plugin view/helpers
* @param {Object} tag
* @param {Object} html_options
* @param {Object} end
*/
tag: function( tag, html_options, end ) {
end = end || '>';
var txt = ' ';
for(var attr in html_options) {
if(html_options.hasOwnProperty(attr)){
value = html_options[attr] != null ? html_options[attr].toString() : '';
if(attr == "Class" || attr == "klass") attr = "class";
if( value.indexOf("'") != -1 )
txt += attr+'=\"'+value+'\" ' ;
else
txt += attr+"='"+value+"' " ;
}
}
return '<'+tag+txt+end;
},
/**
* @plugin view/helpers
* @param {Object} tag
*/
tag_end: function( tag ) { return '</'+tag+'>'; },
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
*/
text_area_tag: function( name, value, html_options ) {
html_options = html_options || {};
html_options.id = html_options.id || name;
html_options.name = html_options.name || name;
value = value || '';
if(html_options.size) {
html_options.cols = html_options.size.split('x')[0];
html_options.rows = html_options.size.split('x')[1];
delete html_options.size;
}
html_options.cols = html_options.cols || 50;
html_options.rows = html_options.rows || 4;
return this.start_tag_for('textarea', html_options)+value+this.tag_end('textarea');
},
/**
* @plugin view/helpers
* @param {Object} name
* @param {Object} value
* @param {Object} html_options
*/
text_field_tag: function( name, value, html_options ) { return this.input_field_tag(name, value, 'text', html_options); },
/**
* @plugin view/helpers
* @param {Object} image_location
* @param {Object} options
*/
img_tag: function( image_location, options ) {
options = options || {};
options.src = steal.config().root.join("resources/images/"+image_location)+'';
return this.single_tag_for('img', options);
}
});
$.EJS.Helpers.prototype.text_tag = $.EJS.Helpers.prototype.text_area_tag;
// Private variables (in the (function($){})(jQuery) scope)
var data = {};
var name = 0;
$.EJS.Helpers.link_data = function(store){
var functionName = name++;
data[functionName] = store;
return "_data='"+functionName+"'";
};
$.EJS.Helpers.get_data = function(el){
if(!el) return null;
var dataAt = el.getAttribute('_data');
if(!dataAt) return null;
return data[parseInt(dataAt)];
};
$.EJS.Helpers.prototype.link_data = function(store){
return $.EJS.Helpers.link_data(store)
};
$.EJS.Helpers.prototype.get_data = function(el){
return $.EJS.Helpers.get_data(el)
};
});