-
Notifications
You must be signed in to change notification settings - Fork 4
/
old-app.js
337 lines (309 loc) · 10.3 KB
/
old-app.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
// -*- indent-tabs-mode: nil; tab-width: 2; js2-basic-offset: 2; coding: utf-8 -*-
/* global $, history, console, repl, external */
/**
* TODO:
* - Add an 'errorclass'-parameter to the list of errors/API? (for highlighting colours)
* - Make «ignore» function actually work (localStorage?)
* - Prettier
*/
(function(){
"use strict";
/* :: type reps = Array<string> */
/* :: type errlist = Array<[string, number, number, string, string, Array<string>]> */
/* :: type result = {text: string, errs: errlist} */
/* :: type cb = (X:result) => void */
var debug = window.location.protocol === "file:";
var log = debug ? console.log.bind(window.console) : function() {};
var port/*:number*/ = 8081;
var hostname/*:string*/ = window.location.hostname === "" ? "localhost" : window.location.hostname;
var protocol/*:string*/ = window.location.protocol === "file:" ? "http:" : window.location.protocol;
var checkUrl/*:string*/ = protocol+"//"+hostname+":"+(port.toString())+"/check";
log(checkUrl);
var basicAuthHeader = function () {
// If we stored a previously successful auth *and* password is
// unset, first try that:
var prev = window.localStorage["auth"];
if($("#password").val()==="" && prev != undefined) {
return prev;
}
else {
var userpass = $("#username").val() + ":" + $("#password").val();
return "Basic " + btoa(userpass);
}
};
var servercheck = function(plaintext/*:string*/,
cb/*:cb*/
)/*:void*/ {
// TODO: Is post going to be synchronous? We can't really change
// the text after the user has typed unless the text still
// matches what we sent.
var res = $.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", basicAuthHeader());
},
type: "POST",
url: checkUrl,
data: { q: plaintext },
success: cb,
error: function(jqXHR, textStatus/*:string*/, err/*:string*/) {
if(textStatus === "error" && err === "Unauthorized") {
$("#serverfault").html("Feil brukarnamn/passord; ver venleg og logg inn: ");
}
else{
$("#serverfault").html("Ånei! Fekk<div>"+textStatus+": "+err+"</div>frå tenaren");
}
$("#serverfault").show();
$("#login").show();
window.localStorage.removeItem("auth");
},
dataType: "json"
});
log(res);
};
/**
* Get plaintext while preserving line breaks; might break on some browsers? TODO test
* http://stackoverflow.com/a/3813520/69663
*/
var getInnerText = function(el/*:Node*/)/*:string*/ {
var sel, range, innerText = "";
if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
range = document.body.createTextRange();
range.moveToElementText(el);
innerText = range.text;
}
else if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
sel = window.getSelection();
sel.selectAllChildren(el);
innerText = "" + sel;
sel.removeAllRanges();
}
return innerText;
};
/**
* Get plain text of element
*/
var toPlainText = function(el/*:Node*/)/*:string*/ {
return getInnerText(el)
.replace(/\n\n+/, "\n☃\n") // preserve double line breaks, but
.replace(/\s\s+/g, " ") // turn any other multi-space into single-space
.replace(/☃/,"") // (assuming no snowmen in input)
.trim();
};
var passwordIsOK = function()/*:void*/ {
// If we got this far, then username and password must be correct:
$("#login").hide();
window.localStorage["auth"] = basicAuthHeader();
};
/**
* Gather plaintext, call server, change DOM
*/
var checkit = function(e)/*:void*/ {
//log("checkit");
e.stopPropagation();
$("#serverfault").hide();
var plaintext = toPlainText($("#form").get(0));
window.localStorage["text"] = plaintext;
servercheck(plaintext,
function(res) {
passwordIsOK();
squiggle(res.text, res.errs);
if(debug && $('.error').length > 0) {
$('.error')[0].click();
}
});
};
var appendText = function(el, text/*:string*/) {
// log(text);
var s = text.split(/\n/g);
// log(s);
for(var i=0; i<s.length; i++) {
if(i>0) {
// log("br");
el.append($(document.createElement('br')));
}
// log("s",s[i]);
el.append($(document.createTextNode(s[i])));
}
};
/**
* Changes DOM
*
* The text replaces the content of #form, and adds spans to errors
* of errs.
*
* @param {string} text
* @param {Array} errs
*/
var squiggle = function(text/*:string*/, errors/*:errlist*/)/*:void*/ {
//log("squiggle");
// Ensure the first error (by start-offset) is first:
errors.sort(function(a,b){return a[1] - b[1];});
var form = $('#form');
form.empty();
for(var i=0, done=0; i < errors.length; i++) {
var str = errors[i][0],
beg = errors[i][1],
end = errors[i][2],
typ = errors[i][3],
msg = errors[i][4],
rep = errors[i][5],
pre = text.slice(done, beg),
err = text.slice(beg, end),
span = $(document.createElement('span'));
if(beg < done) {
log("Overlapping (or unsorted) errors! Skipping error ", errors[i]);
continue;
}
if(end < beg) {
log("Impossible offsets! Skipping error ", errors[i]);
continue;
}
// log("!",done,beg,end,typ,pre,"←pre,err→",err);
appendText(form, pre);
appendText(span, err);
span.click({typ:typ, msg:msg, rep:rep},
function (e) {
e.stopPropagation();
return showrep(this,
e.data.typ,
e.data.msg,
e.data.rep);
});
span.addClass("error");
form.append(span);
done = end;
}
appendText(form, text.slice(done));
};
/**
* Changes DOM
*/
var hiderep = function()/*:void*/ {
//log("hiderep");
var repmenu = $('#repmenu');
repmenu.offset({top:0, left:0}); // avoid some potential bugs with misplacement
repmenu.hide();
};
/**
* Changes DOM
* TODO: populate menu, handle replacement
*/
var showrep = function(span/*:Node*/,
typ/*:string*/,
msg/*:string*/,
rep/*:reps*/
)/*:void*/ {
//log("showrep");
var spanoff = $(span).offset();
var newoff = { top: spanoff.top+20,
left: spanoff.left };
var repmenu = $('#repmenu');
var at_same_err = repmenu.offset().top == newoff.top && repmenu.offset().left == newoff.left;
if(repmenu.is(":visible") && at_same_err) {
hiderep();
}
else {
repmenu.show();
repmenu.offset(newoff);
if(!at_same_err) {
makerepmenu(span, msg, rep);
}
}
};
/**
* Changes DOM
* Populates menu.
* TODO: ignore-button
*/
var makerepmenu = function(span/*:Node*/,
msg/*:string*/,
rep/*:reps*/
)/*:void*/ {
// We're looking at a new error, populate the table anew:
$("#repmenu_tbl").empty();
var tbody = $(document.createElement('tbody')),
tr_title = $(document.createElement('tr')),
td_title = $(document.createElement('td')),
a_title = $(document.createElement('a'));
a_title.text(msg);
a_title.attr("aria-disabled", "true");
a_title.attr("role", "option");
td_title.append(a_title);
td_title.addClass("repmenu_title");
tr_title.append(td_title);
tbody.append(tr_title);
rep.map(function(r){
var tr_rep = $(document.createElement('tr')),
td_rep = $(document.createElement('td')),
a_rep = $(document.createElement('a'));
a_rep.text(r);
a_rep.attr("aria-disabled", "true");
a_rep.attr("role", "option");
td_rep.append(a_rep);
td_rep.addClass("repmenu_rep");
td_rep.addClass("repmenu_nonfirst");
// has to be on td since <a> doesn't fill the whole td
td_rep.click({span:span, r:r},
function(e){
var r = e.data.r,
span = $(e.data.span);
span.replaceWith(r);
hiderep();
});
tr_rep.append(td_rep);
tbody.append(tr_rep);
});
var tr_ign = $(document.createElement('tr')),
td_ign = $(document.createElement('td')),
a_ign = $(document.createElement('a'));
a_ign.text("Ignorer feiltypen");
a_ign.attr("aria-disabled", "true");
a_ign.attr("role", "option");
td_ign.append(a_ign);
td_ign.addClass("repmenu_ign");
td_ign.addClass("repmenu_nonfirst");
tr_ign.append(td_ign);
tbody.append(tr_ign);
$("#repmenu_tbl").append(tbody);
};
var check_on_enter = function (e)/*:bool*/ {
if(e.which === 13)
{
// TODO: In Firefox, this gives NS_ERROR_FAILURE in line 85
// (sel.selectAllChildren(el);)
$("#check_b").trigger("click");
return false;
}
return true;
};
var init = function ()/*:void*/ {
var exampletext = $("#form").text();
if(window.localStorage["exampletext"] != exampletext) {
// If the index.html example text changed, we want to show that to the user:
$("#form").text(window.localStorage["exampletext"]);
window.localStorage["exampletext"] = exampletext;
}
else if(window.localStorage["text"] != undefined) {
$("#form").text(window.localStorage["text"]);
};
$("#check_b").click(checkit);
$("#username").keypress(check_on_enter);
$("#password").keypress(check_on_enter);
$("#form").click(hiderep);
$("#spinner").hide();
$("#form").removeClass("loading");
$(document)
.ajaxStart(function () {
$("#spinner").show();
$("#form").addClass("loading");
})
.ajaxStop(function () {
$("#spinner").hide();
$("#form").removeClass("loading");
});
if(debug) {
$("#check_b").trigger("click");
}
};
window.onload=init;
})();