-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListBoy.js
282 lines (282 loc) · 11.7 KB
/
ListBoy.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function isIRenderSelf(item) {
return 'Render' in item; // I could also check if it's a function or something, maybe later.
}
function isString(data) {
return data.constructor === String;
}
var identity = {
object: function (input) { return input; },
string: function (input) { return input; },
};
function normalize(mappers) {
return __assign(__assign({}, identity), mappers);
}
/** These are the CSS classes that ListBoy emits and recommends get styled by the caller */
var CSSClasses;
(function (CSSClasses) {
/** A div rendered from an array */
CSSClasses["Array"] = "array";
/** A div rendered from a raw JSON object */
CSSClasses["Dictionary"] = "dictionary";
/** A div for a Dictionary Entry that is just `string: string` */
CSSClasses["SimpleDictionaryEntry"] = "simple-dictionary-entry";
/** A div for a Dicionary Entry that is more complex than `string: string` */
CSSClasses["ComplexDictionaryEntry"] = "complex-dictionary-entry";
/** A default span for a raw string */
CSSClasses["StringDefault"] = "string-default";
/** A default span for the key of a dictionary, i.e. a name of a JSON member, with a string value */
CSSClasses["SimpleKeyDefault"] = "simple-key-default";
/** An empty span between a simple key and a string value*/
CSSClasses["SimpleSeparator"] = "simple-separator";
/** A default span for the key of a dictionary, i.e. a name of a JSON member, with a complex value */
CSSClasses["ComplexKeyDefault"] = "complex-key-default";
/** A div which always contains the header of a complex entry */
CSSClasses["ComplexEntryHeader"] = "complex-entry-header";
/** A div which always contains the body of a complex entry */
CSSClasses["ComplexEntryBody"] = "complex-entry-body";
/** A div which always contains the header of a null-valued entry */
CSSClasses["NullDictionaryEntry"] = "null-dictionary-entry";
/** A default span for the key of a dictionary, i.e. a name of a JSON member, with a complex value */
CSSClasses["NullKeyDefault"] = "null-key-default";
/** A span which is a placeholder for a null value */
CSSClasses["NullValue"] = "null-value";
})(CSSClasses || (CSSClasses = {}));
var MarkdownFormatting;
(function (MarkdownFormatting) {
MarkdownFormatting["Emphasis"] = "em";
MarkdownFormatting["Strong"] = "strong";
})(MarkdownFormatting || (MarkdownFormatting = {}));
/**
* The class that knows how to render data objects
* Usage:
* ListBoy.RenderTo({}, "main");
*/
var ListBoy = /** @class */ (function () {
function ListBoy() {
}
// private static fixMappers(){}
/**
* Renders the data object into the target DOM object (when it's ready to do so)
* @param dataObject The data to render
* @param targetId The ID of the DOM object to render it into
* @param mappers mapping functions that will be called on appropriate objects in the tree (default is the identity function)
*/
ListBoy.RenderTo = function (dataObject, targetId, mappers) {
var _this = this;
var cleanMappers = normalize(mappers);
if (document.readyState === "complete") {
this.ReadyToRenderTo(dataObject, targetId, cleanMappers);
}
else {
document.addEventListener("DOMContentLoaded", function (event) {
_this.ReadyToRenderTo(dataObject, targetId, cleanMappers);
});
}
};
/**
* Renders the data object into the target DOM object (presuming it's ready to do so)
* Precondition: The document is ready to get the document elements
* @param dataObject The data to render
* @param targetId The ID of the DOM object to render it into
* @param objectMapper A mapping function that will be called on all (JSON) objects in the tree
*/
ListBoy.ReadyToRenderTo = function (dataObject, targetId, mappers) {
try {
var target = document.getElementById(targetId);
if (target === null) {
throw new Error("ListBoy couldn't find your target: " + targetId);
}
target.appendChild(this.CreateItem(dataObject, mappers));
}
catch (ex) {
alert(ex);
}
};
/**
* Builds a single item (not recommended for external use)
* @param item The item to build
* @param objectMapper A mapping function that will be called on all (JSON) objects in the tree (default is the identity function)
*/
ListBoy.CreateItem = function (item, mappers) {
if (item === null) {
return this.CreateNull();
}
else if (item.constructor == Object) { // JSON
return this.CreateData(mappers.object(item), mappers);
}
else if (typeof (item) == "number") {
return document.createTextNode(item.toString());
}
else if (typeof (item) == "string") {
return this.CreateText(mappers.string(item), CSSClasses.StringDefault);
}
else if (Array.isArray(item)) {
return this.CreateArray(item, mappers);
}
else if (typeof (item) === "object") {
if (isIRenderSelf(item)) {
return item.Render(mappers);
}
else if (item.tagName === "SPAN") {
return item;
}
else {
throw new Error("Don't know how to build an object with tag: " + item.tagName);
}
}
else {
throw new Error("Don't know how to build a " + typeof item);
}
};
ListBoy.MarkdownTag = function (content, format) {
var element = document.createElement(format);
element.innerHTML = content;
return element;
};
/**
* Creates a text Node, potentially inside a span (dealing with pseudo markdown)
* @param item The string for the span
* @param defaultClass The class to use (if it isn't markdown)
*/
ListBoy.CreateText = function (item, defaultClass) {
if (defaultClass === void 0) { defaultClass = null; }
if (item[0] === "`") { // This is the indicator for markdown mode
var pieces = item.substring(1).split("*");
var format = null; // presume no formatting
var displayedFormat = format;
var container = document.createElement("span");
for (var _i = 0, pieces_1 = pieces; _i < pieces_1.length; _i++) {
var piece = pieces_1[_i];
if (piece !== "") {
container.appendChild(this.MarkdownTag(piece, format));
displayedFormat = format;
}
// adjust language state
if (format === null) { // First star always puts us into italics
format = MarkdownFormatting.Emphasis;
}
else {
if (piece === "") { // This means two consecutive stars
// This can't nest ** and *
if (format === MarkdownFormatting.Emphasis) {
// We need to know what we last printed
if (displayedFormat === MarkdownFormatting.Strong) {
format = null;
}
else {
format = MarkdownFormatting.Strong;
}
}
else {
format = MarkdownFormatting.Emphasis;
}
}
else {
if (format === MarkdownFormatting.Emphasis) {
format = null;
}
else {
format = MarkdownFormatting.Emphasis;
}
}
}
}
return container;
}
else {
var element = document.createElement("span");
element.className = defaultClass;
element.innerHTML = item;
return element;
}
};
/**
* Creates a null value
*/
ListBoy.CreateNull = function () {
var container = document.createElement('span');
container.className = CSSClasses.NullValue;
return container;
};
/**
* Creates data from an array
* @param data The array data
*/
ListBoy.CreateArray = function (data, mappers) {
var container = document.createElement("div");
container.className = CSSClasses.Array;
for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
var item = data_1[_i];
if (typeof (item) === "function") {
item(container);
}
else {
container.appendChild(this.CreateItem(item, mappers));
}
}
return container;
};
/** Builds as if from a dictionary */
ListBoy.CreateData = function (data, mappers) {
var container = document.createElement("div");
container.className = CSSClasses.Dictionary;
for (var _i = 0, _a = Object.entries(data); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
if (key === this.classListAdd) {
container.classList.add(value);
}
else if (key === this.setId) {
container.id = value;
}
else if (typeof (value) === "function") {
value(container);
}
else {
var itemContainer = document.createElement("div");
container.appendChild(itemContainer);
if (value === null) {
itemContainer.classList.add(CSSClasses.NullDictionaryEntry);
itemContainer.appendChild(this.CreateText(key, CSSClasses.NullKeyDefault));
itemContainer.appendChild(this.CreateItem(null, mappers));
}
else if (isString(value)) {
itemContainer.className = CSSClasses.SimpleDictionaryEntry;
itemContainer.appendChild(this.CreateText(key, CSSClasses.SimpleKeyDefault));
itemContainer.appendChild(this.CreateText("", CSSClasses.SimpleSeparator));
itemContainer.appendChild(this.CreateItem(value, mappers));
}
else {
itemContainer.className = CSSClasses.ComplexDictionaryEntry;
var entryHeader = document.createElement("div");
entryHeader.className = CSSClasses.ComplexEntryHeader;
entryHeader.appendChild(this.CreateText(key, CSSClasses.ComplexKeyDefault));
itemContainer.appendChild(entryHeader);
var entryBody = document.createElement("div");
entryBody.className = CSSClasses.ComplexEntryBody;
entryBody.appendChild(this.CreateItem(value, mappers));
itemContainer.appendChild(entryBody);
}
}
}
return container;
};
/**
* A variable (that is overridable in case you really need this text as a key) which is used to
* add the specified class to the object.
*/
ListBoy.classListAdd = "target.classList.add";
ListBoy.setId = "target.id";
return ListBoy;
}());
//# sourceMappingURL=ListBoy.js.map