// Generated by CoffeeScript 1.3.3
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  __hasProp = {}.hasOwnProperty,
  __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; };

define(['environ', 'mediator', 'jquery', 'underscore', 'backbone', 'serrano/channels', 'views/columns', 'views/table'], function(environ, mediator, $, _, Backbone, channels, Columns, Table) {
  var ReviewArea, modifyColumnsButton;
  modifyColumnsButton = _.template('<button class=btn title="Show/Hide Columns"><i class=icon-list alt="Show/Hide Columns"></i></button>');
  return ReviewArea = (function(_super) {

    __extends(ReviewArea, _super);

    function ReviewArea() {
      this.loadData = __bind(this.loadData, this);
      return ReviewArea.__super__.constructor.apply(this, arguments);
    }

    ReviewArea.prototype.id = 'review-area';

    ReviewArea.prototype.options = {
      perPage: 100
    };

    ReviewArea.prototype.events = {
      'click table thead th': 'sort',
      'click .pinned-thead div': 'sort'
    };

    ReviewArea.prototype.deferred = {
      loadData: true
    };

    ReviewArea.prototype.initialize = function() {
      var $modifyColumns,
        _this = this;
      ReviewArea.__super__.initialize.apply(this, arguments);
      this.$el.hide().appendTo('#main-area .inner');
      this.$toolbar = $('<ul>').addClass('nav pull-right').hide().appendTo('#subnav .container-fluid');
      this.columns = new Columns({
        collection: App.DataConcept
      });
      this.columns.$el.appendTo('body');
      $modifyColumns = $(modifyColumnsButton()).on('click', function(event) {
        return _this.columns.show();
      });
      this.$toolbar.append($('<li>').html($modifyColumns));
      this.table = new Table;
      this.$el.append(this.table.el);
      this.table.$el.scroller({
        relative: 'table',
        trigger: function() {
          return _this.loadData(true);
        }
      });
      mediator.subscribe(channels.DATAVIEW_SYNCED, function() {
        return _this.loadData();
      });
      mediator.subscribe(channels.DATACONTEXT_SYNCED, function() {
        return _this.loadData();
      });
      this.page = 1;
      this.perPage = this.options.perPage;
      return this.loadData();
    };

    ReviewArea.prototype.load = function() {
      this.$el.fadeIn(100);
      this.$toolbar.fadeIn(100);
      if (this.tableScrollTop) {
        return this.table.$el.scrollTop(this.tableScrollTop);
      }
    };

    ReviewArea.prototype.unload = function() {
      this.tableScrollTop = this.table.$el.scrollTop();
      this.$el.hide();
      return this.$toolbar.hide();
    };

    ReviewArea.prototype.loadData = function(append) {
      var url,
        _this = this;
      if (append == null) {
        append = false;
      }
      if (append && this.page === this.numPages) {
        return;
      }
      url = "" + App.urls.exporter + "?per_page=" + this.perPage;
      if (append) {
        url = "" + url + "&page=" + (this.page + 1);
      } else {
        this.table.$el.addClass('loading');
      }
      return Backbone.ajax({
        url: url,
        success: function(resp) {
          if (append) {
            _this.page++;
          } else {
            _this.page = 1;
            _this.numPages = resp.num_pages;
            _this.table.setHeader(resp.keys);
          }
          return _this.table.setBody(resp.objects, append);
        },
        complete: function() {
          _this.table.$el.removeClass('loading');
          return _this.table.$el.scroller('reset');
        }
      });
    };

    ReviewArea.prototype.sort = function(event) {
      var $target, direction, ordering, prev;
      $target = $(event.target);
      if (!(prev = $target.data('direction'))) {
        direction = 'asc';
      } else if (prev === 'asc') {
        direction = 'desc';
      } else {
        direction = null;
      }
      $target.data('direction', direction);
      $target.removeClass(prev).addClass(direction);
      if (direction) {
        ordering = [[$target.data('id'), direction]];
      } else {
        ordering = [];
      }
      return mediator.publish(channels.DATAVIEW_ORDERING, ordering);
    };

    return ReviewArea;

  })(Backbone.View);
});
