-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcreate.js
137 lines (134 loc) · 5.74 KB
/
mcreate.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
/**
* @file Create a library for quickly, easily and crossbrowser-ly creating dom elements from data
* @author jec006
*/
//create a wrapper object to contain all our functions and namespace them
window.mCreate = {
/**
* Create an element from data
* @param tagName - the type of tag to create, e.g. div
* @param content - the content of the created tag, can be a string (including html) or another element
* @param attr - attributes to apply to the element - needs to be the js version i.e className not class
*/
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 {
//just put it as the inner html since we can't really figure out what it is
el.innerHTML = 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;
},
/**
* Append an element as the last child of a parent element and return el
* @param parent - the element to append el as a child of
* @param el - the element to be appended. Must be a DOMElement
* @return el after its appended to parent
*/
appendChild : function(parent, el){
parent.appendChild(el);
return el;
},
/**
* Creates a table from arrays of data and headers
* @param data - the rows for the table. An array of arrays, each subarray represents a row, each element a cell
* @param headers - the headers (if any) for the table, or a string/int of the number of columns
* TODO: Make headers not required and use array size of first row
*/
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'));
var r = thead.appendChild(u.createElement('tr'));
for(var i=0; i<rowWidth; i++){
r.appendChild(u.createElement('th', headers[i], {className: 'table-header col-'+(i%2?'odd':'even')+(!i?' first':'')}));
}
}
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.
* @param holder - DOMElement - the ul to append the items to. Must already exist
* @param options - object - each item to be attached. Should be in the form:
* {
* 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>}
* }
* @param name - string - the name for the form item represented by all these checkboxes
* @param radios - boolean - use radio buttons
* @param nostriping - boolean - don't add even/odd classes
*/
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;
}
};