Skip to content

Instantly share code, notes, and snippets.

@caseycesari
Created April 27, 2013 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caseycesari/5474136 to your computer and use it in GitHub Desktop.
Save caseycesari/5474136 to your computer and use it in GitHub Desktop.
old avi map
if (typeof ppiin === "undefined" || !ppiin) {
var ppiin = {};
ppiin.avi = {}
}
ppiin.avi.Point = Backbone.Model.extend({
initialize: function () {
this.convertFromDollars();
this.checkLocation();
this.dispatcher = ppiin.avi.dispatcher
},
defaults: {
est_tax: 0,
difference: "0",
lat: "",
lng: "",
homestead: false
},
events: {
"change:f_difference": this.syncDifference
},
checkLocation: function () {
if (this.get("lat") === "") {
this.getLocation()
}
},
getLocation: function () {
var a = this;
ppiin.avi.geocoder.geocode({
address: a.get("address") + ", Philadelphia, PA"
}, function (b, c) {
console.log(c);
if (c == google.maps.GeocoderStatus.OK) {
a.set("lat", b[0].geometry.location.lat());
a.set("lng", b[0].geometry.location.lng());
a.dispatcher.trigger("new:location", a)
} else {
jQuery(".message").html("Sorry, we were not able to get the location of your property.")
}
})
},
convertFromDollars: function () {
this.set("f_current_taxes", Number(this.get("current_taxes").replace(/[^0-9\.]+/g, "")), {
silent: true
});
this.set("f_sale_price", Number(this.get("sale_price").replace(/[^0-9\.]+/g, "")), {
silent: true
});
this.set("f_difference", Number(this.get("difference").replace(/[^0-9\.-]+/g, "")), {
silent: true
})
},
toGPoint: function () {
return new google.maps.LatLng(this.get("lat"), this.get("lng"))
},
getIcon: function () {
var a = this.get("f_difference");
if (a < -1e3) {
return "/apps/graphics/avi/impact-map/images/1.png"
} else if (a <= 0 && a >= -1e3) {
return "/apps/graphics/avi/impact-map/images/2.png"
} else if (a > 0 && a <= 1e3) {
return "/apps/graphics/avi/impact-map/images/3.png"
} else if (a > 1e3 && a <= 3e3) {
return "/apps/graphics/avi/impact-map/images/4.png"
} else if (a > 3e3) {
return "/apps/graphics/avi/impact-map/images/5.png"
} else {
return "/apps/graphics/avi/impact-map/images/black.png"
}
},
getContent: function () {
var a = _.template("<b>Address:</b> <%= address %><br/>" + "<b>Sale Price:</b> <%= sale_price %><br/>" + "<b>City Listed Market Value:</b> <%= city_listed_market_value %><br/>" + "<b>Total assessment:</b> <%= total_assessment %><br/>" + "<b>Current Taxes:</b> <%= current_taxes %><br/>" + "<b>Estimated tax at " + ppiin.avi.formView.tax.toFixed(2) + "%:</b> <%= est_tax %><br/>" + "<b>$ Difference:</b> <%= difference %><br/>" + "<b>% Difference:</b> <%= percent_diff %><br/>");
return a(this.toJSON())
}
});
ppiin.avi.Points = Backbone.Collection.extend({
initialize: function () {
this.dispatcher = ppiin.avi.dispatcher;
this.dispatcher.on("change:tax", this.updateTaxAll, this);
this.on("change:homestead", this.updateHomestead, this);
this.on("add", this.updateTaxSingle, this);
this.on("reset", this.updateTaxAll)
},
model: ppiin.avi.Point,
updateTaxAll: function () {
var a = ppiin.avi.formView.tax;
this.each(function (b) {
this.updateTax(b, a)
}, this)
},
updateHomestead: function (a) {
var b = ppiin.avi.formView.tax;
this.updateTax(a, b);
this.dispatcher.trigger("change:homestead", this)
},
updateTaxSingle: function (a) {
var b = ppiin.avi.formView.tax;
this.updateTax(a, b)
},
updateTax: function (a, b) {
var c;
if (a.get("homestead") === true) {
c = a.get("f_sale_price") - 3e4;
if (c < 0) {
c = 0
}
} else {
c = a.get("f_sale_price")
}
var d = c * (b.toFixed(2) / 100);
var e = d - a.get("f_current_taxes");
var f = e / a.get("f_current_taxes") * 100;
var g = f.toFixed() === "Infinity" || f.toFixed() === "NaN" ? "N/A" : f.toFixed() + "%";
a.set("f_difference", e);
a.set("difference", a.get("f_difference").toFixed());
a.set("est_tax", "$" + d.toFixed());
a.set("percent_diff", g)
}
});
ppiin.avi.GPointView = Backbone.View.extend({
initialize: function () {
this.dispatcher = ppiin.avi.dispatcher;
this.model.on("change:f_difference", this.setIcon, this);
this.model.collection.on("reset", this.removeMarker, this);
this.render()
},
render: function () {
this.marker = new google.maps.Marker({
position: this.model.toGPoint(),
map: this.options.gmap,
title: this.model.get("address"),
icon: this.model.getIcon()
});
var a = this;
google.maps.event.addListener(this.marker, "click", function () {
ppiin.avi.infowindow.close();
ppiin.avi.infowindow.setContent(a.model.getContent());
ppiin.avi.infowindow.open(a.options.gmap, a.marker);
ppiin.avi.selectedMarker = a
});
return this
},
setIcon: function () {
this.marker.setIcon(this.model.getIcon())
},
removeMarker: function () {
this.marker.setMap(null);
this.unbind();
this.remove()
}
});
ppiin.avi.GMapView = Backbone.View.extend({
el: jQuery("#map"),
initialize: function () {
this.dispatcher = ppiin.avi.dispatcher;
this.mapOptions = {
zoom: this.options.zoom,
center: new google.maps.LatLng(this.options.lat, this.options.lng),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.gmap = new google.maps.Map(document.getElementById("map"), this.mapOptions);
ppiin.avi.infowindow = new google.maps.InfoWindow;
ppiin.avi.selectedMarker = false;
google.maps.event.addListener(this.gmap, "dragstart", function () {
ppiin.avi.infowindow.close();
ppiin.avi.selectedMarker = false
});
this.render();
this.dispatcher.on("new:location", this.newLocation, this);
this.dispatcher.on("change:tax", this.updateInfowindow, this);
this.dispatcher.on("change:homestead", this.updateInfowindow, this)
},
render: function () {
this.collection.each(function (a) {
return new ppiin.avi.GPointView({
model: a,
gmap: this.gmap
})
}, this)
},
newLocation: function (a) {
var b = new ppiin.avi.GPointView({
model: a,
gmap: this.gmap
});
this.gmap.setZoom(15);
this.gmap.setCenter(a.toGPoint());
google.maps.event.trigger(b.marker, "click")
},
updateInfowindow: function (a) {
if (ppiin.avi.selectedMarker !== false) {
ppiin.avi.infowindow.setContent(ppiin.avi.selectedMarker.model.getContent())
}
}
});
ppiin.avi.FormView = Backbone.View.extend({
el: jQuery(".control-container"),
initialize: function () {
this.dispatcher = ppiin.avi.dispatcher;
this.tax = 1.3;
this.$el.find(".avi-field").html(this.tax + "%")
},
events: {
"click .tax": "changeTaxRate",
"click .est-tax": "validate",
"click #homestead": "updateHomestead",
"click .filter": "filterCollection"
},
filterCollection: function (a) {
a.preventDefault();
var b, c = " WHERE ";
this.$el.find(".filter").removeClass("active-filter");
jQuery(".map-loading").show();
switch (a.currentTarget.className) {
case "button small filter zero":
b = 0;
c = "";
break;
case "button small filter one":
b = 1;
c += "sale_price < 30000";
break;
case "button small filter two":
b = 2;
c += "sale_price >= 30000 AND sale_price < 60000";
break;
case "button small filter three":
b = 3;
c += "sale_price >= 60000 AND sale_price < 100000";
break;
case "button small filter four":
b = 4;
c += "sale_price >= 100000 AND sale_price < 200000";
break;
case "button small filter five":
b = 5;
c += "sale_price >= 200000";
break
}
jQuery(a.target).addClass("active-filter");
if (ppiin.avi.propertySet[b]) {
ppiin.avi.points.reset(ppiin.avi.propertySet[b]);
ppiin.avi.gmapView.render();
jQuery(".map-loading").hide()
} else {
ft2json.query("SELECT * FROM " + ppiin.avi.tableId + c, function (a) {
ppiin.avi.propertySet[b] = a.data;
ppiin.avi.points.reset(a.data);
ppiin.avi.gmapView.render();
jQuery(".map-loading").hide()
}, {
limit: 5e3
})
}
},
changeTaxRate: function (a) {
a.preventDefault();
switch (a.currentTarget.className) {
case "button small tax up-tenth":
this.tax = this.tax + .1;
break;
case "button small tax up-hundreth":
this.tax = this.tax + .01;
break;
case "button small tax down-tenth":
this.tax = this.tax - .1;
break;
case "button small tax down-hundreth":
this.tax = this.tax - .01;
break
}
this.$el.find(".avi-field.tax").html(this.tax.toFixed(2) + "%");
this.dispatcher.trigger("change:tax", this.tax)
},
updateHomestead: function () {
if (ppiin.avi.selectedMarker) {
ppiin.avi.selectedMarker.model.set("homestead", this.$el.find("#homestead").is(":checked"))
}
},
validate: function (a) {
a.preventDefault();
this.$el.find(".message").html("");
var b = "";
var c = this.$el.find("#sale-price").val().replace("$", "");
if (/^\d{1,3}(,?\d{3})*?(.\d{2})?$/g.test(c) === false) {
b += "Please enter an estimate of the actual value of your property, ex: $400,000.00\n"
}
if (this.$el.find("#address").val() === "") {
b += "Please enter a valid address, ex: 1200 Market St."
}
if (b === "") {
this.getUserData()
} else {
alert(b);
return false
}
},
getUserData: function () {
this.$el.find(".loading").show();
if (this.$el.find("#address").val() !== "") {
var a = encodeURIComponent(this.$el.find("#address").val());
jQuery.ajax({
url: "http://api.phillyaddress.com/address/" + a + "/callback=ppiin.avi.formView.createUserPointInstance",
dataType: "jsonp"
})
} else {
return false
}
},
createUserPointInstance: function (a) {
if (a.error || a.properties) {
this.$el.find(".message").html("Sorry, we could not retrieve tax information for the above address.")
} else {
var b = a.property;
ppiin.avi.points.add({
address: b.account_information.address,
sale_price: this.$el.find("#sale-price").val().replace(/\B(?=(\d{3})+(?!\d))/g, ","),
city_listed_market_value: b.account_details.market_value,
total_assessment: b.account_details.assessment,
current_taxes: b.account_details.real_estate_tax,
homestead: this.$el.find("#homestead").is(":checked")
})
}
this.$el.find(".loading").hide()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment