-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
120 lines (101 loc) · 3.68 KB
/
demo.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
function renderStrings(stringsTable){
var stringkeys = Object.keys(stringsTable);
for(var i=0 ; i<stringkeys.length;i++){
$("#"+stringkeys[i]).text(stringsTable[stringkeys[i]]);
}
}
function renderWidget(name) {
var widgetSettings = settings[name];
if (widgetSettings.renderto) {
var $container = $("#" + widgetSettings.renderto);
switch (name) {
case "clock":
$container.text(new Date().format( widgetSettings.format));
break;
case "logo":
var img = new Image();
img.src = widgetSettings.src;
$container.append(img);
break;
default:
//widget not defined
$container.text("Missing widget " + name);
break;
}
}
}
function renderContainer($targetElem, tagInfoString) {
//if target is UL, OL need intermediate LI
//parse tagInfo
var tagInfo = {};
var parser = /([a-zA-Z][a-zA-Z0-9\-_]*)?(?:#([a-zA-Z](?:[a-zA-Z0-9\-_]|\\\\[^\s])*))?((?:\.(?:(?:[a-zA-Z]|\\\\[^\s])(?:[a-zA-Z0-9\-_]|\\\\[^\s])*))*)/;
if (tagInfoString) {
var matches = parser.exec(tagInfoString);
if (typeof matches[1] != "undefined") {
tagInfo.tagName = matches[1];
}
if (typeof matches[2] != "undefined") {
tagInfo.id = matches[2];
}
if (typeof matches[3] != "undefined") {
tagInfo.classes = matches[3].match(/(?:\.((?:[a-zA-Z]|\\\\[^\s])(?:[a-zA-Z0-9\-_]|\\\\[^\s])*))/g);
}
}
var $container;
if (!tagInfo.tagName) {
if ($targetElem[0].tagName == "UL" || $targetElem[0].tagName == "OL") {
$container = $(document.createElement("li"));
} else {
switch ($targetElem[0].tagName.toLowerCase()) {
case "header":
case "footer":
case "section":
case "main":
$container = $(document.createElement("section"));
break;
case "span":
$container = $(document.createElement("span"));
break;
default:
$container = $(document.createElement("div"));
break;
}
}
} else {
$container = $(document.createElement(tagInfo.tagName));
}
if (tagInfo.id) {
$container.attr("id", tagInfo.id);
}
if (tagInfo.classes) {
for (var i = 0; i < tagInfo.classes.length; i++) {
$container.addClass(tagInfo.classes[i].substring(1));
}
}
$targetElem.append($container);
console.log("new item", tagInfoString, tagInfo, $container.html());
//if target is header, use section (html5)
//if target is footer, use section (html5)
//if target is section, use section (html5)
//if target is div, use div (pre-html5)
//if tagret is span, use span (depreciated)
return $container;
}
function renderObject(items, $targetElem) {
$targetElem.empty();
for (var i = 0; i < items.length; i++) {
if (typeof (items[i]) == "string") {
var $container = renderContainer($targetElem, items[i]);
//$container.text(items[i]); //debug - to remove
} else if (items[i].length) {
//Container for children
renderObject(items[i], renderContainer($targetElem));
} else {
var settings = Object.keys(items[i]);
for (var k = 0; k < settings.length; j++) {
renderObject(items[i][settings[k]],renderContainer($targetElem, settings[k]));
break;
}
}
}
}