/
widgets.js
77 lines (68 loc) · 1.93 KB
/
widgets.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
var textWidgetGenerator = function( tagname ){
return function(){
var i, output = ["<", tagname];
for( i in this ){
if(this.hasOwnProperty(i) && i !== "content" && 1 !== "tagname")
output.push(" ", i, "='", this[i], "'");
}
output.push(">", this.content, "</", tagname, ">");
return output.join("");
}
}
/**
* Renders a paragraph. This is fairly simple.
*/
exports.p = textWidgetGenerator("p");
/**
* Renders a paragraph. This is fairly simple.
*/
exports.h1 = textWidgetGenerator("h1");
/**
* Renders a paragraph. This is fairly simple.
*/
exports.h2 = textWidgetGenerator("h2");
/**
* Renders a paragraph. This is fairly simple.
*/
exports.h3 = textWidgetGenerator("h3")
/**
* Renders an oredered list. This is fairly simple.
*/
exports.ol = textWidgetGenerator("ol");
/**
* Renders an unoredered list. This is fairly simple.
*/
exports.ul = textWidgetGenerator("ul");
/**
* Renders a blockquote. This is fairly simple.
*/
exports.q = textWidgetGenerator("q");
/**
* Renders a simple composite widget.
*/
exports.list = exports.image = function( context ){
var i, output = ["<div"];
for( i in this ){
if(this.hasOwnProperty(i) && i !== "content" && 1 !== "tagname")
output.push(" ", i, "='", this[i], "'");
}
output.push(" data-tagname='", this.tagname, "'");
output.push(">", this.content, "</div>");
return output.join("");
}
/**
* This helper renders a slot independently from a template.
*/
exports.renderSlot = function(slotContent, context){
var widget, slot = slotContent || [], output = [];
for( var i=0, j=slot.length; i<j; ++i){
widget = slot[i];
if( widget === undefined || widget === null || widget === false )
continue;
if( typeof widget == 'string' )
output.push(widget)
else if(widget.tagname && typeof exports[widget.tagname] === "function")
output.push(exports[widget.tagname].call(widget, context));
}
return output.join("");
}