text: // Generated by CoffeeScript 1.9.0
  (function() {
    var Filter,
      __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
      __hasProp = {}.hasOwnProperty;
  
    Filter = (function(_super) {
      __extends(Filter, _super);
  
      Filter.prototype._getDimensionData = function(dimension) {
        if (dimension == null) {
          dimension = 'airquality_raw';
        }
        return _.where(this.data, {
          name: dimension
        });
      };
  
      function Filter(_at_app, _at_params, _at_data, _at_city, _at_helpers) {
        var combinedData, self;
        this.app = _at_app;
        this.params = _at_params;
        this.data = _at_data;
        this.city = _at_city;
        this.helpers = _at_helpers;
        self = this;
        this.dimension = 'airquality_raw';
        this.monthFilter = null;
        this.timeOfDayFilter = null;
        this.workingData = this._getDimensionData(this.dimension);
        this.dataMonthly = _.findWhere(this.workingData, {
          chart: 'month'
        });
        this.dataTime = _.findWhere(this.workingData, {
          chart: 'time_of_day'
        });
        this.el = this.params.el;
        combinedData = this.dataMonthly.data.concat(this.dataTime.data);
        this.scaleX = this._getScaleX(combinedData);
        this.monthFormat = d3.time.format("%b");
        this.dataMonthly = this.dataMonthly.data;
        this.buttonsMonthly = d3.select("#filter-month-buttons").append("div");
        this.buttonsMonthly.selectAll("button").data(this.dataMonthly).enter().append("div").append("button").attr("type", "button").attr("class", "btn btn-default btn-sm btn-compact btn-monthly btn-filter").attr("id", function(d) {
          return "id" + d.time;
        }).attr("value", (function(_this) {
          return function(d) {
            return _this.monthFormat(new Date(d.time));
          };
        })(this)).style("width", "65px").style("margin-bottom", "1px").html((function(_this) {
          return function(d) {
            return _this.monthFormat(new Date(d.time));
          };
        })(this)).on("click", (function(_this) {
          return function(d) {
            return _this._filterCharts(d.time, "btn-monthly");
          };
        })(this));
        this.chartMonthly = d3.select("#filter-month-chart").append("div").style("width", "112px");
        this.barsMonthly = this.chartMonthly.selectAll(".bar").data(this.dataMonthly, function(d) {
          return d.time;
        }).enter().append("div").attr("class", "bar").style("height", "17px").style("background", "#ddd").style("margin-bottom", "5px").style("width", (function(_this) {
          return function(d) {
            return (_this.scaleX(d.value)) + "px";
          };
        })(this)).style("text-align", "right").append("span").attr("class", "bar-text").style("font-size", "11px").style("vertical-align", "top").style("padding-right", "6px").style("color", "#666").html(function(d) {
          return Math.round(d.value);
        });
        this.dataTime = _.reject(this.dataTime.data, function(d) {
          return parseInt(d.time) % 2 !== 0;
        });
        this.buttonsTime = d3.select("#filter-time-buttons").append("div");
        this.buttonsTime.selectAll("button").data(this.dataTime).enter().append("div").append("button").attr("type", "button").attr("class", "btn btn-default btn-sm btn-compact btn-time btn-filter").attr("id", function(d) {
          return "id" + d.time;
        }).attr("value", function(d) {
          return d.time;
        }).style("width", "65px").style("margin-bottom", "1px").html((function(_this) {
          return function(d) {
            var date, et, st;
            date = "2015-03-02T" + d.time;
            st = moment(date);
            et = moment(st).add(2, "hours");
            return st.format('h') + '-' + et.format('ha');
          };
        })(this)).on("click", (function(_this) {
          return function(d) {
            return _this._filterCharts(d.time + "", "btn-time");
          };
        })(this));
        this.chartTime = d3.select("#filter-time-chart").append("div").style("width", "112px");
        this.barsTime = this.chartTime.selectAll(".bar");
        this.barsTime.data(this.dataTime, function(d) {
          return d.time;
        }).enter().append("div").attr("class", "bar").style("height", "17px").style("background", "#ddd").style("margin-bottom", "5px").style("width", (function(_this) {
          return function(d) {
            return (_this.scaleX(d.value)) + "px";
          };
        })(this)).style("text-align", "right").append("span").attr("class", "bar-text").style("font-size", "11px").style("vertical-align", "top").style("padding-right", "6px").style("color", "#666").html(function(d) {
          return Math.round(d.value);
        });
        d3.select("#reset-filters").on("click", (function(_this) {
          return function() {
            return _this._filterCharts(null, null);
          };
        })(this));
      }
  
      Filter.prototype._filterCharts = function(filter, btnClass) {
        var data, self;
        $("#spinner").show();
        if (filter) {
          d3.selectAll("." + btnClass).classed({
            'on': false
          });
          d3.select("." + btnClass + "#id" + filter).classed({
            'on': true
          });
          this.monthFilter = _.pluck(d3.selectAll(".btn-monthly.on")[0], 'value')[0] || null;
          this.timeOfDayFilter = _.pluck(d3.selectAll(".btn-time.on")[0], 'value')[0] || null;
          data = {
            'month': this.monthFilter,
            'time_of_day': this.timeOfDayFilter,
            'city': this.city
          };
        } else {
          this.monthFilter = null;
          this.timeOfDayFilter = null;
          d3.selectAll(".btn-filter").classed({
            'on': false
          });
          data = {
            'city': this.city
          };
        }
        self = this;
        return $.ajax({
          url: "/update/",
          data: data
        }).done(function(data) {
          $("#spinner").hide();
          return self.app.update(data.data);
        });
      };
  
      Filter.prototype._getScaleX = function(data) {
        var domainX, rangeX;
        domainX = this._getDomain(data);
        rangeX = [0, this.params.width - (this.params.margin.left + this.params.margin.right + 10)];
        return this.params.scale().domain(domainX).range(rangeX);
      };
  
      Filter.prototype._getDomain = function(data) {
        var max;
        max = _.max(_.pluck(data, "value"));
        return [0, max];
      };
  
      Filter.prototype.update = function(data) {
        var combinedData, i, self;
        self = this;
        this.data = data;
        this.scaleX = this._getScaleX();
        this.workingData = this._getDimensionData(this.dimension);
        this.newDataMonthly = _.findWhere(this.workingData, {
          chart: 'month'
        });
        this.newDataTime = _.findWhere(this.workingData, {
          chart: 'time_of_day'
        });
        combinedData = this.newDataMonthly.data.concat(this.newDataTime.data);
        this.scaleX = this._getScaleX(combinedData);
        if (this.newDataMonthly.data.length === 1) {
          this.newDataMonthly = this.newDataMonthly.data[0];
          for (i in this.dataMonthly) {
            if (this.dataMonthly[i].time === this.newDataMonthly.time) {
              this.dataMonthly[i].value = this.newDataMonthly.value;
            } else {
              this.dataMonthly[i].value = 0;
            }
          }
        } else {
          this.dataMonthly = this.newDataMonthly.data;
        }
        if (this.newDataTime.data.length === 2) {
          this.newDataTime = this.newDataTime.data[0];
          for (i in this.dataTime) {
            if (this.dataTime[i].time === this.newDataTime.time) {
              this.dataTime[i].value = this.newDataTime.value;
            } else {
              this.dataTime[i].value = 0;
            }
          }
        } else {
          this.dataTime = this.newDataTime.data;
        }
        this.chartMonthly.selectAll(".bar").data(this.dataMonthly, function(d) {
          return d.time;
        }).transition().duration(1000).style("width", (function(_this) {
          return function(d) {
            return (_this.scaleX(d.value)) + "px";
          };
        })(this));
        this.chartMonthly.selectAll(".bar").each(function(d, i) {
          return d3.select(this).select(".bar-text").html(function() {
            if (d.value === 0) {
              return "";
            }
            return Math.round(d.value);
          });
        });
        this.chartTime.selectAll(".bar").data(this.dataTime, function(d) {
          return d.time;
        }).transition().duration(1000).style("width", (function(_this) {
          return function(d) {
            return (_this.scaleX(d.value)) + "px";
          };
        })(this));
        return this.chartTime.selectAll(".bar").each(function(d, i) {
          return d3.select(this).select(".bar-text").html(function() {
            if (d.value === 0) {
              return "";
            }
            return Math.round(d.value);
          });
        });
      };
  
      Filter.prototype.getFilters = function() {
        var date, endMonth, et, st, startMonth, timeOfDayFilter;
        startMonth = this.monthFormat(new Date(this.dataMonthly[0].time));
        endMonth = this.monthFormat(new Date(this.dataMonthly[this.dataMonthly.length - 1].time));
        if (this.timeOfDayFilter) {
          date = "2015-03-02T" + this.timeOfDayFilter;
          st = moment(date);
          et = moment(st).add(2, "hours");
          timeOfDayFilter = st.format('h') + ' and ' + et.format('ha');
        } else {
          timeOfDayFilter = null;
        }
        return {
          'monthFilter': this.monthFilter,
          'timeOfDayFilter': timeOfDayFilter,
          'startMonth': startMonth,
          'endMonth': endMonth
        };
      };
  
      return Filter;
  
    })(APP.charts['Chart']);
  
    APP.charts['Filter'] = Filter;
  
  }).call(this);
  