-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.js
225 lines (217 loc) · 10.3 KB
/
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
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
(function () {
var root = this, exports = {};
// The jade runtime:
var jade=function(exports){Array.isArray||(Array.isArray=function(arr){return"[object Array]"==Object.prototype.toString.call(arr)}),Object.keys||(Object.keys=function(obj){var arr=[];for(var key in obj)obj.hasOwnProperty(key)&&arr.push(key);return arr}),exports.merge=function merge(a,b){var ac=a["class"],bc=b["class"];if(ac||bc)ac=ac||[],bc=bc||[],Array.isArray(ac)||(ac=[ac]),Array.isArray(bc)||(bc=[bc]),ac=ac.filter(nulls),bc=bc.filter(nulls),a["class"]=ac.concat(bc).join(" ");for(var key in b)key!="class"&&(a[key]=b[key]);return a};function nulls(val){return val!=null}return exports.attrs=function attrs(obj,escaped){var buf=[],terse=obj.terse;delete obj.terse;var keys=Object.keys(obj),len=keys.length;if(len){buf.push("");for(var i=0;i<len;++i){var key=keys[i],val=obj[key];"boolean"==typeof val||null==val?val&&(terse?buf.push(key):buf.push(key+'="'+key+'"')):0==key.indexOf("data")&&"string"!=typeof val?buf.push(key+"='"+JSON.stringify(val)+"'"):"class"==key&&Array.isArray(val)?buf.push(key+'="'+exports.escape(val.join(" "))+'"'):escaped&&escaped[key]?buf.push(key+'="'+exports.escape(val)+'"'):buf.push(key+'="'+val+'"')}}return buf.join(" ")},exports.escape=function escape(html){return String(html).replace(/&(?!(\w+|\#\d+);)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")},exports.rethrow=function rethrow(err,filename,lineno){if(!filename)throw err;var context=3,str=require("fs").readFileSync(filename,"utf8"),lines=str.split("\n"),start=Math.max(lineno-context,0),end=Math.min(lines.length,lineno+context),context=lines.slice(start,end).map(function(line,i){var curr=i+start+1;return(curr==lineno?" > ":" ")+curr+"| "+line}).join("\n");throw err.path=filename,err.message=(filename||"Jade")+":"+lineno+"\n"+context+"\n\n"+err.message,err},exports}({});
// create our folder objects
// errors.jade compiled template
exports.errors = function anonymous(locals) {
var buf = [];
with (locals || {}) {
var hasErrors = !!field.errors.length;
if (hasErrors) {
buf.push('<div class="errorContainer">');
(function() {
var $$obj = field.errors;
if ("number" == typeof $$obj.length) {
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) {
var error = $$obj[$index];
if (error) {
buf.push('<span class="error">' + (null == (jade.interp = error) ? "" : jade.interp) + "</span>");
}
}
} else {
var $$l = 0;
for (var $index in $$obj) {
$$l++;
if ($$obj.hasOwnProperty($index)) {
var error = $$obj[$index];
if (error) {
buf.push('<span class="error">' + (null == (jade.interp = error) ? "" : jade.interp) + "</span>");
}
}
}
}
}).call(this);
buf.push("</div>");
}
}
return buf.join("");
};
// field.jade compiled template
exports.field = function anonymous(locals) {
var buf = [];
with (locals || {}) {
var hidden = field.type === "hidden";
buf.push("<div" + jade.attrs({
id: field.containerId,
"class": "fieldContainer clearfix" + (field.errors.length ? " error" : "")
}, {
id: true,
"class": true
}) + ">");
if (field.type !== "hidden") {
if (field.errorPlacement === "before") {
var hasErrors = !!field.errors.length;
if (hasErrors) {
buf.push('<div class="errorContainer">');
(function() {
var $$obj = field.errors;
if ("number" == typeof $$obj.length) {
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) {
var error = $$obj[$index];
if (error) {
buf.push('<span class="error">' + (null == (jade.interp = error) ? "" : jade.interp) + "</span>");
}
}
} else {
var $$l = 0;
for (var $index in $$obj) {
$$l++;
if ($$obj.hasOwnProperty($index)) {
var error = $$obj[$index];
if (error) {
buf.push('<span class="error">' + (null == (jade.interp = error) ? "" : jade.interp) + "</span>");
}
}
}
}
}).call(this);
buf.push("</div>");
}
}
if (field.label) {
buf.push("<label" + jade.attrs({
"for": field.id
}, {
"for": true
}) + ">" + jade.escape(null == (jade.interp = field.label) ? "" : jade.interp) + "</label>");
}
if (field.textarea) {
buf.push("<textarea" + jade.attrs({
id: field.id,
name: field.name,
placeholder: field.placeholder
}, {
id: true,
name: true,
placeholder: true
}) + "></textarea>");
}
if (field.select) {
buf.push("<select" + jade.attrs({
id: field.id,
name: field.name,
"class": field.class
}, {
id: true,
name: true,
"class": true
}) + ">");
(function() {
var $$obj = field.options;
if ("number" == typeof $$obj.length) {
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) {
var option = $$obj[$index];
buf.push("<option" + jade.attrs({
value: option.val
}, {
value: true
}) + ">" + jade.escape(null == (jade.interp = option.text) ? "" : jade.interp) + "</option>");
}
} else {
var $$l = 0;
for (var $index in $$obj) {
$$l++;
if ($$obj.hasOwnProperty($index)) {
var option = $$obj[$index];
buf.push("<option" + jade.attrs({
value: option.val
}, {
value: true
}) + ">" + jade.escape(null == (jade.interp = option.text) ? "" : jade.interp) + "</option>");
}
}
}
}).call(this);
buf.push("</select>");
}
}
if (field.input) {
buf.push("<input" + jade.attrs({
id: field.id,
min: field.get("min"),
max: field.get("max"),
step: field.get("step"),
placeholder: field.get("placeholder"),
type: field.type,
name: field.name,
autocomplete: field.autocomplete || null
}, {
id: true,
min: true,
max: true,
step: true,
placeholder: true,
type: true,
name: true,
autocomplete: true
}) + "/>");
}
if (field.errorPlacement === "after" && field.type !== "hidden") {
var hasErrors = !!field.errors.length;
if (hasErrors) {
buf.push('<div class="errorContainer">');
(function() {
var $$obj = field.errors;
if ("number" == typeof $$obj.length) {
for (var $index = 0, $$l = $$obj.length; $index < $$l; $index++) {
var error = $$obj[$index];
if (error) {
buf.push('<span class="error">' + (null == (jade.interp = error) ? "" : jade.interp) + "</span>");
}
}
} else {
var $$l = 0;
for (var $index in $$obj) {
$$l++;
if ($$obj.hasOwnProperty($index)) {
var error = $$obj[$index];
if (error) {
buf.push('<span class="error">' + (null == (jade.interp = error) ? "" : jade.interp) + "</span>");
}
}
}
}
}).call(this);
buf.push("</div>");
}
}
if (field.type !== "hidden" && field.helpText) {
buf.push("<p" + jade.attrs({
id: field.id + "_helpText",
"class": "helpText"
}, {
id: true
}) + ">" + jade.escape(null == (jade.interp = field.helpText) ? "" : jade.interp) + "</p>");
}
buf.push("</div>");
}
return buf.join("");
};
// form.jade compiled template
exports.form = function anonymous(locals) {
var buf = [];
with (locals || {}) {
buf.push('<form><div class="fields"></div><button type="submit">Submit</button><button type="reset">Cancel</button></form>');
}
return buf.join("");
};
// attach to window or export with commonJS
if (typeof module !== "undefined") {
module.exports = exports;
} else if (typeof define === "function" && define.amd) {
define(exports);
} else {
root.templatizer = exports;
}
})();