-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapplication.js
More file actions
396 lines (375 loc) · 13.3 KB
/
application.js
File metadata and controls
396 lines (375 loc) · 13.3 KB
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
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery-ui/widgets/draggable
//= require jquery-ui/widgets/resizable
//= require jquery-ui/widgets/sortable
//= require jquery-ui/widgets/selectable
//= require jquery_ujs
//= require jquery.matchHeight
//= require jquery-tablesorter
//= require jquery.keyDecoder
//= require cocoon
//= require moment
//= require moment-timezone-with-data
//= require bootstrap-sprockets
//= require bootstrap-datetimepicker
//= require bootstrap.treeview
//= require bootstrap-toggle
//= require codemirror/lib/codemirror
//= require codemirror/addon/runmode/runmode
//= require codemirror/addon/selection/active-line
//= require codemirror/mode/clike/clike
//= require codemirror/mode/mllike/mllike
//= require codemirror/mode/haskell/haskell
//= require codemirror/mode/haskell-literate/haskell-literate
//= require codemirror/mode/ebnf/ebnf
//= require codemirror/mode/javascript/javascript
//= require codemirror/mode/markdown/markdown
//= require codemirror/mode/scheme/scheme
//= require codemirror/mode/python/python
//= require codemirror/mode/css/css
//= require codemirror/mode/stex/stex
//= require codemirror/mode/xml/xml
//= require codemirror/mode/yaml/yaml
//= require codemirror/mode/htmlmixed/htmlmixed
//= require pyret-codemirror-mode/mode/pyret
//= require_tree .
function enableShowUsernames(elts) {
$(elts).click(function(){
$(this).toggleClass("active");
$("#" + $(this).data("target")).toggleClass("showusernames");
});
}
// Based on https://stackoverflow.com/questions/14324919/status-of-rails-link-to-function-deprecation
function enableReflectiveCalls() {
$('[data-on][data-call][data-args]').each(function(d){
if ($(this).data("already-enabled-reflective-call")) return;
var event = $(this).data('on');
$(this).on(event, function(e) {
var toCall = $(this).data('call');
var args = $(this).data('args')
if (typeof(window[toCall]) !== 'function')
throw new Error("No such function to call: " + toCall);
if (!(args instanceof Array))
throw new Error("Arguments are not an array: " + args);
args = args.slice();
args.push(e);
window[toCall].apply(this, args);
});
$(this).data("already-enabled-reflective-call", true);
});
}
var validKeys = {
"ArrowLeft": true,
"ArrowRight": true,
"ArrowUp": true,
"ArrowDown": true,
"Backspace": true,
"Delete": true,
"End": true,
"Home": true,
"Tab": true,
};
var validKeyCodes = {
9: true, // Tab
};
function validateNumericInput(e) {
if (e.key === undefined) {
// We're on Safari :(
switch (e.which) {
case 8: e.key = "Backspace"; break;
case 9: e.key = "Tab"; break;
case 35: e.key = "End"; break;
case 36: e.key = "Home"; break;
case 37: e.key = "ArrowLeft"; break;
case 38: e.key = "ArrowUp"; break;
case 39: e.key = "ArrowRight"; break;
case 40: e.key = "ArrowDown"; break;
case 46: e.key = "Delete"; break;
case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
e.key = String.fromCharCode(e.which); break;
case 189: e.key = "-"; break;
case 190: e.key = "."; break;
default: e.key = "Unknown";
}
}
if (validKeys[e.key] || validKeyCodes[e.keyCode] || validKeyCodes[e.which]) return;
if (e.key.match(/^F\d+$/)) return;
if (!Number.isNaN(Number(e.key)) && (Number(e.key) == Number.parseInt(e.key))) return;
if (e.key === "." && e.currentTarget.value.indexOf(".") < 0) return;
if (e.key === "-" && e.currentTarget.value.indexOf("-") < 0 && e.currentTarget.selectionStart === 0) return;
if (e.ctrlKey || e.altKey || e.metaKey) return;
e.preventDefault();
};
function ensureValidNumericInputOnSubmit(e, sel) {
var problems = false;
sel = sel || "input.numeric";
$(sel).each(function(elt) {
if (Number.isNaN(Number.parseFloat($(this).val()))) {
problems = true;
$(this)
.addClass("badAnswer")
.one("focus", function() { $(this).removeClass("badAnswer"); });
}
});
if (problems) {
e.preventDefault();
alert("There are invalid values for some of the fields; please correct them before submitting.");
}
return !problems;
}
function ensureFilesPresentOnSubmit(e, sel) {
var problems = false;
sel = sel || "input[type='file']";
$(sel).each(function(elt) {
if (this.files.length === 0) {
problems = true;
$(this)
.addClass("badAnswer")
.one("focus", function() { $(this).removeClass("badAnswer"); });
}
});
if (problems) {
e.preventDefault();
alert("There are missing required files for some of the fields; please correct them before submitting.");
}
return !problems;
}
function makeFriendlyDate(str, showTime) {
var dd = moment(Date.parse(str));
if (!dd.isValid()) { dd = moment(str); }
if (dd.isValid()) {
var today = moment().startOf('day');
var tomorrow = moment(today).add(1, 'days');
var twodays = moment(tomorrow).add(1, 'days');
if (today.isSameOrBefore(dd) && dd.isBefore(tomorrow))
return ("Today, " + dd.format("h:mm:ssa"));
else if (tomorrow.isSameOrBefore(dd) && dd.isBefore(twodays))
return ("Tomorrow, " + dd.format("h:mm:ssa"));
else if (showTime)
return (dd.format("MMM D YYYY, h:mm:ssa"));
else
return (dd.format("MMM D, YYYY"));
}
}
// convert human-readable datetimes to ISO8601 times, including timezone
function makeDatesIso8601($dates) {
var savedDates = [];
$dates.each(function(index, dtp) {
var $dtp = $(dtp);
savedDates[index] = $dtp.val();
$dtp.val($dtp.data("DateTimePicker").date().format());
});
return savedDates;
}
function restoreDatesFromIso8601($dates, savedDates) {
$dates.each(function(index, dtp) {
$(dtp).val(savedDates[index]);
});
}
function activate_file_picker($e) {
$e.find(".custom-file").change(function() {
var label = $(this).val().replace(/\\/g, '/').replace(/.*\//, '');
$e.find(".current_file").text("New file: " + label);
$e.find(".remove-custom-file").prop('disabled', false).removeClass("btn-default").addClass("btn-warning");
$e.find("input.remove-custom-file").val('');
});
$e.find(".remove-custom-file").click(function() {
$e.find("input.custom-file").replaceWith(
$e.find("input.custom-file").clone(true));
$(this).prop('disabled', true).addClass("btn-default").removeClass("btn-warning");
$e.find(".current_file").text("New file: <nothing>");
$e.find("input.remove-custom-file").val('remove');
});
}
$(function() {
$('[data-toggle="tooltip"]').each(function(elt) {
$(this).tooltip({
animated: 'fade',
placement: $(this).data("placement") || 'right',
html: true
});
});
$('.local-time').each(function(_) {
$(this).text(makeFriendlyDate($(this).text(), true));
});
$("input.numeric").on("keydown", validateNumericInput);
});
function activateSpinner(obj, options) {
var spinner = $(obj || this);
var input = spinner.find('input');
var upArrow = spinner.find('.btn:first-of-type');
var downArrow = spinner.find('.btn:last-of-type');
var upInterval, downInterval;
var delta = input.data("delta") || 1;
var max = input.data("max");
var min = input.data("min");
var val = parseFloat(input.val(), 10);
var precision = parseInt((options && options.precision) || spinner.data("precision") || "0");
if (max !== undefined && val >= max)
upArrow.addClass("disabled");
if (min !== undefined && val <= min)
downArrow.addClass("disabled");
function validate() {
max = input.data("max");
min = input.data("min");
var disabled = input.prop("disabled");
var val = parseFloat(input.val(), 10);
if (max !== undefined && val >= max) {
upArrow.addClass("disabled");
clearInterval(upInterval);
upInterval = undefined;
}
if (!disabled && (min === undefined || val > min))
downArrow.removeClass("disabled");
if (min !== undefined && val <= min) {
downArrow.addClass("disabled");
clearInterval(downInterval);
downInterval = undefined;
}
if (!disabled && (max === undefined || val < max))
upArrow.removeClass("disabled");
}
input.on("change", validate);
function increment() {
var newVal = (parseFloat(input.val(), 10) || 0) + delta;
if (max !== undefined) {
newVal = Math.min(max, newVal);
}
input.val(newVal.toFixed(precision)).change();
}
function decrement() {
var newVal = (parseFloat(input.val(), 10) || 0) - delta;
if (min !== undefined) {
newVal = Math.max(min, newVal);
}
input.val(newVal.toFixed(precision)).change();
}
input.on("keydown", function(e) {
if (input.prop("disabled")) return;
validateNumericInput(e);
if (e.key === "ArrowUp") { increment(); return; }
if (e.key === "ArrowDown") { decrement(); return; }
if (e.key === "ArrowLeft" || e.key === "ArrowRight") { return; }
var curVal = $(this).val();
var newVal;
if (e.key === "Backspace") {
if (this.selectionStart == this.selectionEnd) {
newVal = curVal.slice(0, this.selectionStart - 1) + curVal.slice(this.selectionEnd, curVal.length);
} else {
newVal = curVal.slice(0, this.selectionStart) + curVal.slice(this.selectionEnd, curVal.length);
}
} else if (e.key === "Delete") {
if (this.selectionStart == this.selectionEnd) {
newVal = curVal.slice(0, this.selectionStart) + curVal.slice(this.selectionEnd + 1, curVal.length);
} else {
newVal = curVal.slice(0, this.selectionStart) + curVal.slice(this.selectionEnd, curVal.length);
}
} else {
newVal = curVal.slice(0, this.selectionStart) + e.key + curVal.slice(this.selectionEnd, curVal.length);
}
newVal = parseFloat(newVal, 10);
if (max !== undefined && newVal > max) { e.preventDefault(); }
if (min !== undefined && newVal < min) { e.preventDefault(); }
});
input.on("deactivate", function(e) {
input.prop("disabled", true);
upArrow.addClass("disabled");
downArrow.addClass("disabled");
});
input.on("reactivate", function(e) {
input.prop("disabled", false);
validate();
});
input.bind("paste", function(e) { e.preventDefault(); });
$(upArrow).on('mousedown', function() {
if (input.prop("disabled")) return;
upInterval = setInterval(increment, 200);
increment();
});
$(downArrow).on('mousedown', function() {
if (input.prop("disabled")) return;
downInterval = setInterval(decrement, 200);
decrement();
});
$(document).on('mouseup', function() {
if (upInterval) clearInterval(upInterval);
if (downInterval) clearInterval(downInterval);
upInterval = undefined;
downInterval = undefined;
return false;
});
return input;
}
function disableSpinner(divOrInput) {
$(divOrInput).find("input").addBack().trigger("deactivate");
}
function enableSpinner(divOrInput) {
$(divOrInput).find("input").addBack().trigger("reactivate");
}
function makeSpinner(options) {
var input = $("<input>")
.addClass("form-control numeric")
.val(options.val || 0)
.bind("paste", function(e) { e.preventDefault(); });
if (options.klass !== undefined)
input.addClass(options.klass);
if (options.max !== undefined) input.data("max", options.max);
if (options.min !== undefined) input.data("min", options.min);
if (options.delta !== undefined) input.data("delta", options.delta);
var div = $("<div>").addClass("input-group spinner")
.append(input)
.append($("<div>").addClass("input-group-btn-vertical")
.append($("<button>").addClass("btn btn-default")
.append($("<i>").addClass("fa fa-caret-up")))
.append($("<button>").addClass("btn btn-default")
.append($("<i>").addClass("fa fa-caret-down"))));
activateSpinner(div, options);
return div;
}
// Based on npm package "google-charts"
var GoogleCharts = {
loader: function() {
if (!GoogleCharts.loaderPromise) {
GoogleCharts.loaderPromise = new Promise((resolve) => {
const head = document.getElementsByTagName('head')[0];
const script = document.createElement('script');
script.type = 'text/javascript';
script.onload = function () {
GoogleCharts.api = window.google;
GoogleCharts.api.charts.load('current', {'packages': ['corechart']});
GoogleCharts.api.charts.setOnLoadCallback(() => {
resolve();
})
}
script.src = 'https://www.gstatic.com/charts/loader.js';
head.appendChild(script);
});
}
return GoogleCharts.loaderPromise;
},
load: function(type, callback) {
return GoogleCharts.loader().then(() => {
if (type) {
if(!type.length) {
type = [type];
}
GoogleCharts.api.charts.load('current', {'packages': type});
GoogleCharts.api.charts.setOnLoadCallback(callback);
} else {
callback();
}
})
}
};