-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.buildin.templates.js
69 lines (65 loc) · 1.91 KB
/
jquery.buildin.templates.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
/* htmlbuilder/JsonML templates
* -- uses htmlbuilder and JsonML's syntax to generate arrays that can
* be converted into commonly used HTML structures
* -- Purpose:
* <table><tbody><tr><td>This is so much work!</td></tr></tbody></table>
* -- original examples include: table
*
* Usage:
* builder.templates.table(
* {<options>}, //<==options are optional
* [<th 1 contents>, <th 2 contents>],
* [<td 1 contents>, <td 2 contents>]
* )
*
* Example:
* var table = builder.templates.table(['header1','header2'], ['item1', 'item2'])
*
* This will give you a buildable array:
* ['table', {},
* ['tbody', {},
* ['tr', {},
* ['th', {}, 'header1'],
* ['th', {}, 'header2']
* ],
* ['tr', {},
* ['td', {}, 'item1'],
* ['td', {}, 'item2']
* ]
* ]
* ]
*
* Which you can build and insert!
* document.body.appendChild(builder.build(table))
*/
;if(typeof(window.builder)=='undefined'){window.builder={}}
builder.templatesVersion = "0.03";
builder.templates=(function(){
function TableTemplate(options) {
var rowCount, val, _currentRow, colIndex,
_optsPresent = !((options instanceof Array) && (typeof(options)!=='string')),
opts = _optsPresent ? options : {},
argCountRowStart = _optsPresent ? 1 : 0, //skips first argument if _optsPresent
useTHs = (opts.th || true),
_contents = ["tbody", {}];
for(rowCount = argCountRowStart; rowCount<arguments.length; rowCount++) {
val=arguments[rowCount];
_currentRow = ["tr", {}];
for(colIndex=0; colIndex<val.length; colIndex++) {
_currentRow.push([
(useTHs && rowCount==argCountRowStart) ? "th" : "td",
{},
val[colIndex]
]);
}
_contents.push(_currentRow);
}
return [
"table", {},
_contents
];
}
return {
table: TableTemplate
}
})();