window.cls = window.cls || {};

/**
 * @constructor
 * @extends ViewBase
 */
cls.ResourceManagerAllView = function(id, name, container_class, html, default_handler) {
  this._service = new cls.ResourceManagerService();
  this._sort_by = "name";
  this._reverse = false;
  this._fields = [];

  this.createView = function(container)
  {
    this._render_main_view(container);
  };

  this._render_main_view = function(container)
  {
    var ctx = this._service.get_request_context();
    container.clearAndRender(templates.all_resources(ctx, this._fields, this._sort_by, this._reverse));
  };

  this._handle_open_resource_bound = function(evt, target)
  {
    var rid = target.getAttribute("resource-id");
    var resource = this._service.get_resource_for_id(rid);
    opera.postError("Should show " + rid);
  }.bind(this);

  this._handle_sort_resources = function(evt, target)
  {
    var col = target.getAttribute("column-name");
    if (this._sort_by == col)
    {
      this._reverse = !this._reverse;
    }
    else
    {
      this._sort_by = col;
    }
    this.update();
  }.bind(this);

  var eh = window.eventHandlers;
  eh.click["resources-all-open"] = this._handle_open_resource_bound;
  eh.click["resources-all-sort"] = this._handle_sort_resources;

  this.init(id, name, container_class, html, default_handler);
};
cls.ResourceManagerAllView.prototype = ViewBase;

window.cls = window.cls || {};

/**
 * @constructor
 * @extends ViewBase
 */
cls.ResourceManagerAllView = function(id, name, container_class, html, default_handler) {
  this._service = new cls.ResourceManagerService();
  this._sort_by = "name";
  this._reverse = false;
  this._columns = [];

  this.createView = function(container)
  {
    this._render_main_view(container);
  };

  this._render_main_view = function(container)
  {
    var ctx = this._service.get_request_context();
    container.clearAndRender(templates.all_resources(ctx, this._columns, this._sort_by, this._reverse));
  };

  this._handle_open_resource_bound = function(evt, target)
  {
    var rid = target.getAttribute("resource-id");
    var resource = this._service.get_resource_for_id(rid);
    opera.postError("Should show " + rid);
  }.bind(this);

  this._handle_sort_resources = function(evt, target)
  {
    var col = target.getAttribute("column-name");
    if (this._sort_by == col)
    {
      this._reverse = !this._reverse;
    }
    else
    {
      this._sort_by = col;
      this._reverse = false; // show unrevered when chosing new col.
    }
    this.update();
  }.bind(this);

  var eh = window.eventHandlers;
  eh.click["resources-all-open"] = this._handle_open_resource_bound;
  eh.click["resources-all-sort"] = this._handle_sort_resources;

  this.init(id, name, container_class, html, default_handler);
};
cls.ResourceManagerAllView.prototype = ViewBase;

Reset reverse flag when chosing new col to sort by