So this is an app I'm working on, and I'd like to get some feedback on it. I'm leaving some key parts out, as I don't want everyone everywhere to have access to the complete code. The main pieces that I would like you guys to look at are still there though.
// Does a remote AJAX request that scrapes the URL and parses it
function doAjax(url) {
$.getJSON(URL,
function (data) {
if (data.results[0]) {
$("#content").html(""); // Reset
var number = $(filterData(data.results[0])).find("#gtv_leftcolumn table:gt(1)");
for (var i = 0; i < number.length; i++) {
var name = $(filterData(data.results[0])).find("#gtv_leftcolumn table:gt(1) .maintext p:eq(" + i + ")").text();
var type = $(filterData(data.results[0])).find("#gtv_leftcolumn table:gt(1) .trafficbriefs:nth-child(even) p:eq(" + i + ")").text();
// Redacted
}
if (doAjax) {
// Redacted
if (number.length === 0) {
// Checks to see if there are any elements on the page, and if 0, runs this
// Redacted
} else {
checkFavorite();
var mySearch = $('input#id_search').quicksearch('#content .row', {
clearSearch: '#clearsearch'
});
mySearch.cache();
console.log("Loaded " + number.length);
console.log("Cached");
}
}
} else {
console.log("error");
}
});
}
function filterData(data) {
data = data.replace(/<?\/body[^>]*>/g, '');
data = data.replace(/[\r|\n]+/g, '');
data = data.replace(/<--[\S\s]*?-->/g, '');
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
data = data.replace(/<script.*\/>/, '');
data = data.replace(/<img[^>]*>/g, '');
return data;
}
// On Load
doAjax("http://www.codekraken.com/testing/snowday/wgrz.html");
$("#info").click(showInfo);
$(".info").click(closeInfo);
$("#reload").click(reaload);
$("#clearsearch").click(clearSearchBox);
$(".clear").click(clearFavorite);
setFavorite();
// You can clear the favorite item you set in setFavorite()
function clearFavorite() {
localStorage.removeItem("favorite");
localStorage.removeItem("favorite-status");
$(".star-inside").removeClass("favorite");
$(".clear span").text("");
}
// Clear search box
function clearSearchBox() {
$("#id_search").val("");
$('#id_search').trigger('keyup');
}
// Show info box
function showInfo() {
// Redacted
}
// Close info box
function closeInfo() {
// Redacted
}
// Reload AJAX request
function reload() {
closeInfo();
doAjax("URL");
}
// Set favorite item. This enables you to swipe on any .row element, and once it swipes, it sets the row you swipe on as the favorite. Swiping again unfavorites it. I mainly want help on this, as far as cleaning it up.
function setFavorite() {
var threshold = {
x: 30,
y: 10
};
var originalCoord = {
x: 0,
y: 0
};
var finalCoord = {
x: 0,
y: 0
};
function touchMove() {
console.log(event.targetTouches);
finalCoord.x = event.targetTouches[0].pageX;
changeX = originalCoord.x - finalCoord.x;
var changeY = originalCoord.y - finalCoord.y;
if (changeY < threshold.y && changeY > (threshold.y * -1)) {
changeX = originalCoord.x - finalCoord.x;
if (changeX > threshold.x) {
window.removeEventListener('touchmove', touchMove, false);
$(document).off("touchmove", ".row");
if ($(event.target).attr("class") === "row-inside") {
var element = $(event.target);
}
if ($(event.target).attr("class") === "row-l") {
var element = $(event.target).parent();
}
if ($(event.target).attr("class") === "row-r") {
var element = $(event.target).parent();
}
var text = $(element).find(".row-l").text();
var favstatus = $(element).find(".row-r").text();
var thisStar = $(element).parent().find(".star-inside");
$(element).css("margin-left", "-75px");
if ($(thisStar).hasClass("favorite")) {
$(".clear span").text("");
$(thisStar).removeClass("favorite");
localStorage.removeItem("favorite");
localStorage.removeItem("favorite-status");
} else {
$(".clear span").text("\"" + text + "\"");
localStorage.setItem("favorite", text);
localStorage.setItem("favorite-status", favstatus);
$(".star-inside").not(thisStar).removeClass("favorite");
$(thisStar).addClass("favorite");
}
setTimeout(function () {
$(element).css("margin-left", "0px");
}, 500);
setTimeout(function () {
$(document).on("touchmove", ".row", function () {
touchMove();
});
}, 800);
}
}
}
function touchStart() {
originalCoord.x = event.targetTouches[0].pageX;
finalCoord.x = originalCoord.x;
}
$(document).on("touchmove", ".row", function () {
touchMove();
});
$(document).on("touchstart", ".row", function () {
touchStart();
});
}
// Check favorite set in setFavorite()
function checkFavorite() {
if (localStorage.getItem("favorite") !== null) {
var name = localStorage.getItem("favorite");
var favstatus = localStorage.getItem("favorite-status");
var favstatusSplit = favstatus.substr(2);
var favstatusLower = favstatusSplit.toLowerCase();
var string = $(".row-l").text().toLowerCase();
var re = new RegExp(name.toLowerCase(), 'g');
var test = string.match(re);
$(".row-l:contains(" + name + ")").parent().parent().find(".star-inside").addClass("favorite");
if (test !== null) {
$(".fav_school_inside").text(name + " - " + favstatusLower + "!");
$(".clear span").text("\"" + name + "\"");
setTimeout(function () {
$(".fav_school").addClass("top");
}, 1000);
setTimeout(function () {
$(".fav_school").removeClass("top");
}, 6000);
}
}
}