fix some js lint

This commit is contained in:
Damien Elmes 2017-07-28 16:48:49 +10:00
parent b8e306062b
commit 44d0766cc5
3 changed files with 55 additions and 53 deletions

View file

@ -12,23 +12,23 @@ String.prototype.format = function () {
function setFGButton(col) { function setFGButton(col) {
$("#forecolor")[0].style.backgroundColor = col; $("#forecolor")[0].style.backgroundColor = col;
}; }
function saveNow() { function saveNow() {
clearChangeTimer(); clearChangeTimer();
if (currentField) { if (currentField) {
currentField.blur(); currentField.blur();
} }
}; }
function onKey() { function onKey() {
// esc clears focus, allowing dialog to close // esc clears focus, allowing dialog to close
if (window.event.which == 27) { if (window.event.which === 27) {
currentField.blur(); currentField.blur();
return; return;
} }
// catch enter key in prewrap mode // catch enter key in prewrap mode
if (window.event.which == 13 && prewrapMode) { if (window.event.which === 13 && prewrapMode) {
window.event.preventDefault(); window.event.preventDefault();
insertNewline(); insertNewline();
return; return;
@ -38,7 +38,7 @@ function onKey() {
updateButtonState(); updateButtonState();
saveField("key"); saveField("key");
}, 600); }, 600);
}; }
function insertNewline() { function insertNewline() {
if (!inPreEnvironment()) { if (!inPreEnvironment()) {
@ -60,7 +60,7 @@ function insertNewline() {
var oldHeight = currentField.clientHeight; var oldHeight = currentField.clientHeight;
setFormat("inserthtml", "\\n"); setFormat("inserthtml", "\\n");
if (currentField.clientHeight == oldHeight) { if (currentField.clientHeight === oldHeight) {
setFormat("inserthtml", "\\n"); setFormat("inserthtml", "\\n");
} }
} }
@ -68,17 +68,17 @@ function insertNewline() {
// is the cursor in an environment that respects whitespace? // is the cursor in an environment that respects whitespace?
function inPreEnvironment() { function inPreEnvironment() {
var n = window.getSelection().anchorNode; var n = window.getSelection().anchorNode;
if (n.nodeType == 3) { if (n.nodeType === 3) {
n = n.parentNode; n = n.parentNode;
} }
return window.getComputedStyle(n).whiteSpace.startsWith("pre"); return window.getComputedStyle(n).whiteSpace.startsWith("pre");
} }
function checkForEmptyField() { function checkForEmptyField() {
if (currentField.innerHTML == "") { if (currentField.innerHTML === "") {
currentField.innerHTML = "<br>"; currentField.innerHTML = "<br>";
} }
}; }
function updateButtonState() { function updateButtonState() {
var buts = ["bold", "italic", "underline", "superscript", "subscript"]; var buts = ["bold", "italic", "underline", "superscript", "subscript"];
@ -93,7 +93,7 @@ function updateButtonState() {
// fixme: forecolor // fixme: forecolor
// 'col': document.queryCommandValue("forecolor") // 'col': document.queryCommandValue("forecolor")
}; }
function toggleEditorButton(buttonid) { function toggleEditorButton(buttonid) {
if ($(buttonid).hasClass("highlighted")) { if ($(buttonid).hasClass("highlighted")) {
@ -101,7 +101,7 @@ function toggleEditorButton(buttonid) {
} else { } else {
$(buttonid).addClass("highlighted"); $(buttonid).addClass("highlighted");
} }
}; }
function setFormat(cmd, arg, nosave) { function setFormat(cmd, arg, nosave) {
document.execCommand(cmd, false, arg); document.execCommand(cmd, false, arg);
@ -109,14 +109,14 @@ function setFormat(cmd, arg, nosave) {
saveField('key'); saveField('key');
updateButtonState(); updateButtonState();
} }
}; }
function clearChangeTimer() { function clearChangeTimer() {
if (changeTimer) { if (changeTimer) {
clearTimeout(changeTimer); clearTimeout(changeTimer);
changeTimer = null; changeTimer = null;
} }
}; }
function onFocus(elem) { function onFocus(elem) {
currentField = elem; currentField = elem;
@ -160,13 +160,13 @@ function onPaste(elem) {
} }
function caretToEnd() { function caretToEnd() {
var r = document.createRange() var r = document.createRange();
r.selectNodeContents(currentField); r.selectNodeContents(currentField);
r.collapse(false); r.collapse(false);
var s = document.getSelection(); var s = document.getSelection();
s.removeAllRanges(); s.removeAllRanges();
s.addRange(r); s.addRange(r);
}; }
function onBlur() { function onBlur() {
if (currentField) { if (currentField) {
@ -174,7 +174,7 @@ function onBlur() {
} }
clearChangeTimer(); clearChangeTimer();
disableButtons(); disableButtons();
}; }
function saveField(type) { function saveField(type) {
if (!currentField) { if (!currentField) {
@ -184,39 +184,39 @@ function saveField(type) {
// type is either 'blur' or 'key' // type is either 'blur' or 'key'
pycmd(type + ":" + currentField.innerHTML); pycmd(type + ":" + currentField.innerHTML);
clearChangeTimer(); clearChangeTimer();
}; }
function wrappedExceptForWhitespace(text, front, back) { function wrappedExceptForWhitespace(text, front, back) {
var match = text.match(/^(\s*)([^]*?)(\s*)$/); var match = text.match(/^(\s*)([^]*?)(\s*)$/);
return match[1] + front + match[2] + back + match[3]; return match[1] + front + match[2] + back + match[3];
}; }
function disableButtons() { function disableButtons() {
$("button.linkb").prop("disabled", true); $("button.linkb").prop("disabled", true);
}; }
function enableButtons() { function enableButtons() {
$("button.linkb").prop("disabled", false); $("button.linkb").prop("disabled", false);
}; }
// disable the buttons if a field is not currently focused // disable the buttons if a field is not currently focused
function maybeDisableButtons() { function maybeDisableButtons() {
if (!document.activeElement || document.activeElement.className != "field") { if (!document.activeElement || document.activeElement.className !== "field") {
disableButtons(); disableButtons();
} else { } else {
enableButtons(); enableButtons();
} }
}; }
function wrap(front, back) { function wrap(front, back) {
if (currentField.dir == "rtl") { if (currentField.dir === "rtl") {
front = "&#8235;" + front + "&#8236;"; front = "&#8235;" + front + "&#8236;";
back = "&#8235;" + back + "&#8236;"; back = "&#8235;" + back + "&#8236;";
} }
var s = window.getSelection(); var s = window.getSelection();
var r = s.getRangeAt(0); var r = s.getRangeAt(0);
var content = r.cloneContents(); var content = r.cloneContents();
var span = document.createElement("span") var span = document.createElement("span");
span.appendChild(content); span.appendChild(content);
var new_ = wrappedExceptForWhitespace(span.innerHTML, front, back); var new_ = wrappedExceptForWhitespace(span.innerHTML, front, back);
setFormat("inserthtml", new_); setFormat("inserthtml", new_);
@ -228,7 +228,7 @@ function wrap(front, back) {
s.removeAllRanges(); s.removeAllRanges();
s.addRange(r); s.addRange(r);
} }
}; }
function setFields(fields, focusTo, prewrap) { function setFields(fields, focusTo, prewrap) {
var txt = ""; var txt = "";
@ -257,7 +257,7 @@ function setFields(fields, focusTo, prewrap) {
if (prewrap) { if (prewrap) {
$(".field").addClass("prewrap"); $(".field").addClass("prewrap");
} }
}; }
function setBackgrounds(cols) { function setBackgrounds(cols) {
for (var i = 0; i < cols.length; i++) { for (var i = 0; i < cols.length; i++) {
@ -267,9 +267,10 @@ function setBackgrounds(cols) {
function setFonts(fonts) { function setFonts(fonts) {
for (var i = 0; i < fonts.length; i++) { for (var i = 0; i < fonts.length; i++) {
$("#f" + i).css("font-family", fonts[i][0]); var n = $("#f" + i);
$("#f" + i).css("font-size", fonts[i][1]); n.css("font-family", fonts[i][0])
$("#f" + i)[0].dir = fonts[i][2] ? "rtl" : "ltr"; .css("font-size", fonts[i][1]);
n[0].dir = fonts[i][2] ? "rtl" : "ltr";
} }
} }
@ -316,7 +317,7 @@ allowedTags["IMG"] = {"attrs": ["SRC"]};
var blockRegex = /^(address|blockquote|br|center|div|dl|h[1-6]|hr|ol|p|pre|table|ul|dd|dt|li|tbody|td|tfoot|th|thead|tr)$/i; var blockRegex = /^(address|blockquote|br|center|div|dl|h[1-6]|hr|ol|p|pre|table|ul|dd|dt|li|tbody|td|tfoot|th|thead|tr)$/i;
function isBlockLevel(n) { function isBlockLevel(n) {
return blockRegex.test(n.nodeName); return blockRegex.test(n.nodeName);
}; }
function isInlineElement(n) { function isInlineElement(n) {
return n && !isBlockLevel(n); return n && !isBlockLevel(n);
@ -334,11 +335,11 @@ function convertDivToNewline(node, isParagraph) {
html += "\\n"; html += "\\n";
} }
node.outerHTML = html; node.outerHTML = html;
}; }
var filterNode = function (node) { var filterNode = function (node) {
// text node? // text node?
if (node.nodeType == 3) { if (node.nodeType === 3) {
if (prewrapMode) { if (prewrapMode) {
// collapse standard whitespace // collapse standard whitespace
var val = node.nodeValue.replace(/^[ \\r\\n\\t]+$/g, " "); var val = node.nodeValue.replace(/^[ \\r\\n\\t]+$/g, " ");
@ -355,14 +356,15 @@ var filterNode = function (node) {
// elements due to node modifications otherwise // elements due to node modifications otherwise
var nodes = []; var nodes = [];
for (var i = 0; i < node.childNodes.length; i++) { var i;
for (i = 0; i < node.childNodes.length; i++) {
nodes.push(node.childNodes[i]); nodes.push(node.childNodes[i]);
} }
for (var i = 0; i < nodes.length; i++) { for (i = 0; i < nodes.length; i++) {
filterNode(nodes[i]); filterNode(nodes[i]);
} }
if (node.tagName == "ANKITOP") { if (node.tagName === "ANKITOP") {
return; return;
} }
@ -373,23 +375,23 @@ var filterNode = function (node) {
} else { } else {
node.outerHTML = node.innerHTML; node.outerHTML = node.innerHTML;
} }
} else if (prewrapMode && node.tagName == "BR") { } else if (prewrapMode && node.tagName === "BR") {
node.outerHTML = "\\n"; node.outerHTML = "\\n";
} else if (prewrapMode && node.tagName == "DIV") { } else if (prewrapMode && node.tagName === "DIV") {
convertBlockToNewline(node, false); convertBlockToNewline(node, false);
} else if (prewrapMode && node.tagName == "P") { } else if (prewrapMode && node.tagName === "P") {
convertBlockToNewline(node, true); convertBlockToNewline(node, true);
} else { } else {
// allowed, filter out attributes // allowed, filter out attributes
var toRemove = []; var toRemove = [];
for (var i = 0; i < node.attributes.length; i++) { for (i = 0; i < node.attributes.length; i++) {
var attr = node.attributes[i]; var attr = node.attributes[i];
var attrName = attr.name.toUpperCase(); var attrName = attr.name.toUpperCase();
if (tag.attrs.indexOf(attrName) == -1) { if (tag.attrs.indexOf(attrName) === -1) {
toRemove.push(attr); toRemove.push(attr);
} }
} }
for (var i = 0; i < toRemove.length; i++) { for (i = 0; i < toRemove.length; i++) {
node.removeAttributeNode(toRemove[i]); node.removeAttributeNode(toRemove[i]);
} }
} }
@ -400,25 +402,25 @@ var mouseDown = 0;
$(function () { $(function () {
document.body.onmousedown = function () { document.body.onmousedown = function () {
mouseDown++; mouseDown++;
} };
document.body.onmouseup = function () { document.body.onmouseup = function () {
mouseDown--; mouseDown--;
} };
document.onclick = function (evt) { document.onclick = function (evt) {
var src = window.event.srcElement; var src = window.event.srcElement;
if (src.tagName == "IMG") { if (src.tagName === "IMG") {
// image clicked; find contenteditable parent // image clicked; find contenteditable parent
var p = src; var p = src;
while (p = p.parentNode) { while (p = p.parentNode) {
if (p.className == "field") { if (p.className === "field") {
$("#" + p.id).focus(); $("#" + p.id).focus();
break; break;
} }
} }
} }
} };
// prevent editor buttons from taking focus // prevent editor buttons from taking focus
$("button.linkb").on("mousedown", function (e) { $("button.linkb").on("mousedown", function (e) {

View file

@ -11,8 +11,9 @@ $(function () {
}); });
var updateTime = function () { var updateTime = function () {
var timeNode = $("#time");
if (!maxTime) { if (!maxTime) {
$("#time").text(""); timeNode.text("");
return; return;
} }
time = Math.min(maxTime, time); time = Math.min(maxTime, time);
@ -21,13 +22,12 @@ var updateTime = function () {
if (s < 10) { if (s < 10) {
s = "0" + s; s = "0" + s;
} }
var e = $("#time"); if (maxTime === time) {
if (maxTime == time) { timeNode.html("<font color=red>" + m + ":" + s + "</font>");
e.html("<font color=red>" + m + ":" + s + "</font>");
} else { } else {
e.text(m + ":" + s); timeNode.text(m + ":" + s);
} }
} };
function showQuestion(txt, maxTime_) { function showQuestion(txt, maxTime_) {
// much faster than jquery's .html() // much faster than jquery's .html()

View file

@ -20,7 +20,7 @@ function _updateQA(q, answerMode, klass) {
// don't allow drags of images, which cause them to be deleted // don't allow drags of images, which cause them to be deleted
$("img").attr("draggable", false); $("img").attr("draggable", false);
MathJax.Hub.Queue(["Typeset", MathJax.Hub]); MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}; }
function _toggleStar(show) { function _toggleStar(show) {
if (show) { if (show) {