-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
322 lines (306 loc) · 10.6 KB
/
utils.js
File metadata and controls
322 lines (306 loc) · 10.6 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
/**
* Group of utility functions for javascript
*
* @author jcaldwell
* @file utils.js
**/
var Utils = {
//add a class to an element
addClass : function(e, c){
if(!e.className.match(new RegExp("(\s*|^)"+c+"(\s*|$)"))){
e.className += e.className == '' ? c : ' '+c;
}
},
//remove a class from an element
rmClass : function(e, c){
e.className = e.className.replace(new RegExp("(\s*|^)"+c+"(\s*|$)", 'g'), '');
},
//A function for creating a DOM element and assigning attributes and content
createElement : function(tagName, content, attr){
var el = document.createElement(tagName);
if(el){
switch (typeof(content))
{
case 'object' :
if(content && content != null)
el.appendChild(content);
break;
case 'string' :
if(content.indexOf('<') > -1 || content.indexOf('>') > -1 || /&[a-zA-Z]+;/.test(content)){
el.innerHTML = content;
} else {
if(tagName == 'input'){
el.value = content;
} else {
$(el).append(content);
}
}
break;
case 'undefined':
//do nothing
break
default :
//We aren't sure what this is but we'll just set the innerHTML element and call it a day
typeof(el.innerHTML) != 'undefined' ? el.innerHTML = content : content;
}
//apply any provided attributes - these attributes should use the js name for this to work properly, for example className instead of class
for(var a in attr){
if(a == 'for') { a = 'htmlFor'; attr['htmlFor'] = attr['for']; } //simple fix to allow labels to have 'for' attribute
el[a] = attr[a];
}
}
return el;
},
//convience function for appending and storing
appendChild : function(parent, el){
parent.appendChild(el);
return el;
},
createTable : function(data, headers){
var table = u.createElement('table');
var rowWidth = 2;
if(typeof(headers) != 'object'){
//assume its just a number of cells per row
var temp = parseInt(headers);
rowWidth = isNaN(temp) ? rowWidth : temp;
} else {
//create the headers
rowWidth = headers.length
var thead = u.appendChild(table, u.createElement('thead'));
for(var i=0; i<rowWidth; i++){
thead.appendChild(u.createElement('th', headers[i], {className: 'table-header col-'+(i%2?'odd':'even')}));
}
}
var tbody = u.appendChild(table, u.createElement('tbody'));
for(var i=0; i<data.length; i+=rowWidth){
var tr = u.appendChild(tbody, u.createElement('tr', '', {className: 'table-row row-'+(i%(2*rowWidth)?'odd':'even')}));
for(var c=0; c<rowWidth && i+c < data.length; c++){
tr.appendChild(u.createElement('td', data[i+c], {className: 'table-cell cell-'+(c%2?'odd':'even')}));
}
}
return table;
},
/**
* Creates a list of checkbox options within a holder ul (UnorderedList) and returns it.
*
* **Parameters**:
* <ul>
* <li><em>options</em> - an array of options each of which should be in the form
* <code>
* {
* text: <labeltext>,
* value: <value>,
* checked:<true|false>,
* ch_attrs: {<attrs to be applied to the checkbox>},
* label_attrs: {<attrs to be applied to the label>}
* }
* </code>
* </li>
* <li><em>name</em> - the name to give the checkboxes input(s),
* if you would like them to each have their own distinct name, set name in each ch_attrs. <em>If the name parameter is set it will
* override the individual names<em>.
* </li>
* </ul>
*/
createCheckboxList : function(holder, options, name, radios, nostriping){
if(!options instanceof Array){
options = [options];
}
for(var i=0; i<options.length; i++){
var option = options[i];
if(typeof(option.ch_attrs) != 'object'){ option.ch_attrs = {}; };
option.ch_attrs.type = radios? 'radio' : 'checkbox';
option.ch_attrs.value = option.value;
var checked = typeof(option.checked)=='undefined' ? option.ch_attrs.checked||false : option.checked;
if(name){ option.ch_attrs.name = name; };
if(nostriping){
var item = u.appendChild(holder, u.createElement('li', '', {className:'checkbox-row clearfix'}));
} else {
var item = u.appendChild(holder, u.createElement('li', '', {className:'checkbox-row clearfix ' + (i%2?'odd':'even')}));
}
//var str = '<input type="' + option.ch_attrs.type + '" ' + (option.ch_attrs.checked ? 'checked ' : '') + 'cvalue="'+option.value + '"/>';
//$(item).append(str);
if(!radios){
var c = u.appendChild(item, u.createElement('input', '', option.ch_attrs));
c.cvalue = option.value;
c.checked = checked;
} else {
item.innerHTML = '<input type="radio" value="'+option.value+'" cvalue="'+option.value+'" name="'+name+'" ' + (checked?'checked':'') + '/>';
}
item.appendChild(u.createElement('label', option.text, option.label_attrs));
}
return holder;
},
//returns the 'real type' of an object instead of just 'Object'
realType : function(obj){
if(obj && obj.constructor){
return obj.constructor.name;
} else {
return typeof(obj);
}
},
//Inspect an object - returns a string
inspect : function(obj, index, tabs){
if(!index){
var index = '';
}else{
index += ' : ';
}
if(!tabs) var tabs = 0;
var rtype = u.realType(obj);
var mTabs = function(){
var str = '';
for(var i = 0; i < tabs; i++){
str += "\t";
}
return str;
};
if(typeof(obj) != 'object'){
return index + obj + ' <'+typeof(obj)+'>';
}
var ss = ' {', se = '}';
if (rtype == 'Array'){
ss = ' ['; se = ']';
}
var str = index + rtype + ss;
for(var sub in obj){
var ind = rtype =='Array' ? '' : sub;
str += "\n\t"+mTabs()+ u.inspect(obj[sub], ind, tabs+1)+',';
}
if(str.indexOf(',') > -1) str = str.substr(0, str.length-1);
str+= "\n"+mTabs()+se;
return str;
},
//inspect an object - returns an HTMLDivElement
inspectToElement : function(obj, index){
if(!index){
var index = '';
}else{
index += ' : ';
}
var rtype = u.realType(obj);
if(typeof(obj) != 'object'){
return u.createElement('div', index + obj + ' ('+typeof(obj)+')', {className : 'value'});
}
else if (rtype == 'Array'){
var el = u.createElement('div', index + rtype + ' [', {className : 'object-inspector'});
var children = u.createElement('ul', null, {className : 'child-list'});
el.appendChild(children);
for(var i in obj){
children.appendChild(u.createElement('li', u.inspectToElement(obj[i]), {className : 'child'}));
}
el.appendChild(document.createTextNode(']'));
return el;
}
else {
var el = u.createElement('div', index + rtype, {className : 'object-inspector'});
var children = u.createElement('ul', null, {className : 'child-list'});
el.appendChild(children);
for(var sub in obj){
children.appendChild(u.createElement('li', u.inspectToElement(obj[sub], sub), {className : 'child'}));
}
return el;
}
},
bind : function(o, f){
return function() { return f.apply(o, arguments); };
},
IEsafeDate : function(dateStr) {
//reformat the date string so IE doesn't choke
//from: 2010-04-30T09:30:00Z
//to: 04-03-2010 09:30:00 UTC
if (navigator.appName == 'Microsoft Internet Explorer') {
var monthday = dateStr.substring(6, 10);
var year = dateStr.substring(0, 4);
var time = dateStr.substring(11, 19);
var timezone = dateStr.substring(19);
if (timezone == 'Z') {
timezone = 'UTC';
}
return monthday + '-' + year + ' ' + time + ' ' + timezone;
} else {
return dateStr;
}
},
formatDate : function(dateStr) {
date = new Date(u.IEsafeDate(dateStr));
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
if (hour == 12) {
var ampm = 'PM';
} else if (hour >= 12) {
hour = hour - 12;
var ampm = 'PM';
} else if (hour == 0) {
hour = 12;
var ampm = 'AM';
} else {
var ampm = 'AM';
}
var minute = date.getMinutes();
if (minute < 10) {
minute = '0' + minute;
}
return month + '/' + day + '/' + year + ', ' + hour + ':' + minute + ' ' + ampm;
},
arrayRemove : function(arr, el){
for(var i = 0; i < arr.length; i++){
if(arr[i] == el){
arr.splice(i, 1);
break;
}
}
return arr;
},
createGoogleMapsLink : function(obj){
//link beginning
var base = 'http://maps.google.com/maps?q=';
//ensure we don't get "undefined's"
var street = obj.street ? obj.street : '';
var city = obj.city ? ' ' + obj.city : '';
var state = obj.state ? ' ' + obj.state : '';
var postal_code = obj.postal_code ? ' ' + obj.postal_code : '';
var county = obj.county ? ' ' + obj.county : '';
var loc = street + city + state + postal_code + county;
var loc = loc.replace(/ /g, '+');
//We can change this text / remove it if needed
var link = u.createElement('a', 'GoogleMaps', {href:base+loc, className:'google-maps-link location', target:'_BLANK'});
return link;
},
/**
* Does not modify passed array - creates a copy
*
* Potential point of slow-down
* Returns an array with only the elements that func returned true for
*/
filter : function(array, func){
var ret = new Array();
for(var i = 0; i < array.length; i++){
if(func(array[i])){
ret.push(array[i]);
}
}
return ret;
},
createParamString : function(obj){
var string = '';
for(var i in obj){
string += (string?'&':'') + encodeURI(i) + '=' + encodeURI(obj[i]);
}
return string;
},
restripe : function(list){
var l = list.children.length;
for(var i = 0; i<l; i++){
if(i%2){
$(list.children[i]).addClass('odd').removeClass('even');
} else {
$(list.children[i]).addClass('even').removeClass('odd');
}
}
}
};
//alias window.u and Utils for shortness
window.u = Utils;