-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastQueries.js
467 lines (308 loc) · 12.4 KB
/
fastQueries.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// i'm pretty sure inline comments break the queries,
// so don't put those inside queries. use /* */ instead.
// this is where the queries are stored.
QUERIES = {};
// here are the default queries
QUERIES["Make a New Query"] = function(e, title, code) {
var textArea = document.getElementById("content");
var titleField = document.createElement("input");
titleField.type = "content";
titleField.style.border = "solid 1px";
titleField.id = "titleField";
if (title) {titleField.value = title;}
var codeField = document.createElement("textarea");
codeField.rows = 50;
codeField.cols = 80;
codeField.style.border = "solid 1px black";
codeField.id = "codeField";
if (code) {codeField.value = code;}
var submit = document.createElement("button");
submit.type = "button";
submit.onclick = function() {
if (document.getElementById(titleField.value)) {alert("There's already a query by this name.");}
else if (titleField.value === "") {alert("Give your query a name!");}
else {
var functionString = "(function() {" + myCodeMirror.getValue() + "})";
QUERIES[titleField.value] = eval(functionString);
localStorage[titleField.value] = myCodeMirror.getValue();
addQueryToSidebar(titleField.value);
titleField.value = "";
myCodeMirror.getDoc().setValue("");
}
};
submit.textContent = "Submit";
submit.id = "submit";
textArea.textContent = "";
textArea.appendChild(titleField);
textArea.appendChild(document.createElement("br"));
textArea.appendChild(document.createElement("br"));
textArea.appendChild(codeField);
textArea.appendChild(document.createElement("br"));
textArea.appendChild(document.createElement("br"));
textArea.appendChild(submit);
var myCodeMirror = CodeMirror.fromTextArea(codeField,
{"lineNumbers": true, "mode":"javascript", "theme": "mbo"});
};
// makes writing the Gatherer query easier
function createInputForm(id, placeholder) {
var inputForm = document.createElement("input");
inputForm.type = "content";
inputForm.id = id;
inputForm.placeholder = placeholder;
return inputForm;
}
// also for the Gatherer query
function createRegExpFromInputId(inputId) {
return new RegExp(document.getElementById(inputId).value);
}
// I'm going to have to rename this
QUERIES["Gatherer"] = function() {
var tooltip = "Search through your Hangouts, the easy way. " +
"Sender, Message Content, and Date String fields accept regular expressions. " +
"Earliest Date and Latest Date accept strings like \"May 15 2015\".";
var formContainer = createStyledMessage("Gatherer", tooltip);
var senderInput = createInputForm("sender", "Sender");
var textInput = createInputForm("text", "Message Content");
var dateFromInput = createInputForm("dateFrom", "Earliest Date");
var dateToInput = createInputForm("dateTo", "Latest Date");
var dateStringInput = createInputForm("dateString", "Date String");
formContainer.appendChild(senderInput);
formContainer.appendChild(textInput);
formContainer.appendChild(dateFromInput);
formContainer.appendChild(dateToInput);
formContainer.appendChild(dateStringInput);
var submit = createStyledMessage(undefined, "Search");
submit.className += " clickable";
submit.onclick = function() {
var resultsContainer = document.getElementById("resultsContainer");
removeChildren(resultsContainer);
var senderExpression = createRegExpFromInputId("sender");
var textExpression = createRegExpFromInputId("text");
var dateFromExpression = document.getElementById("dateFrom").value;
var dateToExpression = document.getElementById("dateTo").value;
var dateStringExpression = createRegExpFromInputId("dateString");
// in case date range fields are left blank
if (!dateFromExpression) {
// earliest date I managed to get
dateFromExpression = "Jan 1 100";
}
if (!dateToExpression) {
// this should last us a decent while
dateToExpression = "Dec 31 275759";
}
for (var i = 0; i < MESSAGES.length; i++) {
// if the messages matches all the search criteria
if (senderExpression.test(MESSAGES[i].sender) &&
textExpression.test(MESSAGES[i].text) &&
new Date(dateFromExpression) <= new Date(MESSAGES[i].time) &&
new Date(dateToExpression) >= new Date(MESSAGES[i].time) &&
dateStringExpression.test(new Date(MESSAGES[i].time).toString())) {
resultsContainer.appendChild(toString(MESSAGES[i]));
}
}
};
var resultsContainer = document.createElement("div");
resultsContainer.id = "resultsContainer";
var contentArea = document.getElementById("content");
removeChildren(contentArea);
contentArea.appendChild(formContainer);
contentArea.appendChild(submit);
contentArea.appendChild(resultsContainer);
// under construction message
// removeChildren(document.getElementById("content"));
// var underConstruction = createStyledMessage("dunstad", "Under Construction");
// document.getElementById("content").appendChild(underConstruction);
};
QUERIES["Word Count"] = function() {
var searchForm = document.createElement('input');
searchForm.type = "content";
searchForm.style.border = "solid 1px";
searchForm.id = "searchForm";
searchForm.placeholder = "Word to Count";
var submit = createStyledMessage(undefined, "Count");
submit.className += " clickable";
submit.id = "submit";
submit.onclick = function() {
/* delete old results, if present */
if (document.getElementById("results")) {
document.getElementById("content").removeChild(document.getElementById("results"));
}
var searchMessages = [];
for (var i = 0; i < MESSAGES.length; i++) {
if (MESSAGES[i].text.toLowerCase().indexOf(searchForm.value.toLowerCase()) !== -1) {
searchMessages.push(MESSAGES[i]);
}
}
var people = {};
for (var name in PROFILES) {
people[name] = 0;
}
for (var i = 0; i < searchMessages.length; i++) {
people[searchMessages[i].sender]++;
}
var spanContainer = document.createElement("span");
spanContainer.id = "results";
for (var name in people) {
spanContainer.appendChild(createStyledMessage(name, people[name]));
}
document.getElementById("content").appendChild(spanContainer);
searchForm.value = "";
};
var formContainer = createStyledMessage("Word Count", "See who said what how many times.");
formContainer.appendChild(searchForm);
var contentArea = document.getElementById("content");
removeChildren(contentArea);
contentArea.appendChild(formContainer);
contentArea.appendChild(submit);
contentArea.appendChild(document.createElement("br"));
};
QUERIES["Random Message"] = function() {
var randomMessage = MESSAGES[randInt(0, MESSAGES.length)];
removeChildren(document.getElementById("content"));
document.getElementById("content").appendChild(toString(randomMessage));
};
// a slightly less terrible implementation than the previous one
QUERIES["Whose Line is it Anyway?"] = function() {
var randomMessage = MESSAGES[randInt(0, MESSAGES.length)];
/* will run forever if nobody has ever sent a message longer than 4 characters
in which case why are you even using this program */
while (randomMessage.text.length < 4) {randomMessage = MESSAGES[randInt(0, MESSAGES.length)];}
// leave out the date
var message = createStyledMessage(randomMessage.sender, randomMessage.text);
message.firstChild.id = "hidden";
message.firstChild.style.visibility = "hidden";
var button = createStyledMessage(undefined, "Reveal Answer");
button.className += " clickable";
button.onclick = showHidden;
var textElement = document.getElementById("content");
removeChildren(textElement);
textElement.appendChild(message);
textElement.appendChild(button);
};
function showHidden() {
document.getElementById("hidden").style.visibility = "visible";
}
// not really very useful
QUERIES["Longest Hundred Messages"] = function() {
// clone the messagelist so we only have to sort it once
var messageLengths = MESSAGES.slice(0);
messageLengths.sort(function(a, b) {return (b.text.length - a.text.length);});
removeChildren(document.getElementById("content"));
for (var i = 0; i < 100; i++) {
document.getElementById("content").appendChild(toString(messageLengths[i]));
}
};
QUERIES["Number of Messages"] = function() {
var names = {};
for (var i = 0; i < MESSAGES.length; i++) {
var sender = MESSAGES[i].sender;
if (!names[sender]) {names[sender] = 1;}
else {names[sender]++;}
}
removeChildren(document.getElementById("content"));
for (var name in names) {
var result = createStyledMessage(name, names[name] + " messages, " + (Math.round(names[name] / MESSAGES.length * 10000) / 100) + "% of total");
// var result = document.createTextNode(name + ": " + names[name] + ", " + (Math.round(names[name] / MESSAGES.length * 10000) / 100) + "%");
document.getElementById("content").appendChild(result);
}
};
QUERIES["Average Message Length"] = function() {
var names = {};
for (var i = 0; i < MESSAGES.length; i++) {
var sender = MESSAGES[i].sender;
if (!names[sender]) {
names[sender] = {};
names[sender]["length"] = MESSAGES[i].text.length;
names[sender]["number"] = 1;
}
else {
names[sender]["length"] += MESSAGES[i].text.length;
names[sender]["number"] += 1;
}
}
removeChildren(document.getElementById("content"));
for (var name in names) {
var result = createStyledMessage(name, Math.round((names[name]["length"] / names[name]["number"])) + " characters");
document.getElementById("content").appendChild(result);
}
};
// list out names of chat participants and add an event handler
// entirely redundant thanks to Gatherer, but I'll leave it in for now
QUERIES["Comments by User"] = function() {
if (PROFILES[MESSAGES[0].sender] === undefined) {buildProfiles();}
removeChildren(document.getElementById("content"));
for (var name in PROFILES) {
var nameButton = createStyledMessage(name);
nameButton.className += " clickable";
nameButton.onclick = showCommentsMaker(name);
document.getElementById("content").appendChild(nameButton);
}
};
// weirdest pattern ever
function showCommentsMaker(name) {
return function(){showComments(name);};
}
// the event handler mentioned above, adds the person's comments to content
function showComments(name) {
var userComments = document.createElement("div");
for (var i = 0; i < PROFILES[name].length; i++) {
userComments.appendChild(toString(PROFILES[name][i]));
}
removeChildren(document.getElementById("content"));
document.getElementById("content").appendChild(userComments);
}
// guess what this does?
function addQueryToSidebar(query) {
var sidebar = document.getElementById("navigation");
// create query link
var queryButton = document.createElement("button");
queryButton.className = "clickable";
queryButton.textContent = query;
queryButton.onclick = QUERIES[query];
// because an img tag has to have a src
var invisibleImage = "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
// create edit button
var editButton = document.createElement("img");
editButton.className = "editButton clickable";
editButton.alt = "edit";
editButton.src = invisibleImage;
editButton.query = query;
editButton.onclick = function () {
var queryText;
if (!localStorage[this.query]) {queryText = QUERIES[this.query].toString();}
else {queryText = localStorage[this.query];}
// this should definitely not be a variable
// not sure how i would store it at the moment though
QUERIES["Make a New Query"](undefined, this.query, queryText);
};
// create delete button
var deleteButton = document.createElement("img");
deleteButton.className = "deleteButton clickable";
deleteButton.alt = "delete";
deleteButton.src = invisibleImage;
deleteButton.query = query;
deleteButton.onclick = function () {
if (confirm("Are you sure you want to delete " + query + "?")) {
delete QUERIES[this.query];
delete localStorage[this.query];
document.getElementById("navigation").removeChild(document.getElementById(this.query));
}
};
var containerSpan = document.createElement("span");
containerSpan.id = query;
containerSpan.className = "flexHorizontal";
containerSpan.appendChild(queryButton);
containerSpan.appendChild(editButton);
containerSpan.appendChild(deleteButton);
sidebar.appendChild(containerSpan);
}
// pull saved queries from localStorage
for (var query in localStorage) {
QUERIES[query] = eval("(function(e) {" + localStorage[query] + "})");
}
// create links for all of the queries
for (var query in QUERIES) {
addQueryToSidebar(query);
}
// drop us onto the search page once the json file has been read
QUERIES["Gatherer"]();