dojo.provide("parus.treeDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

dojo.require("parus.parus8");
dojo.require("parus.dialog");

dojo.declare("parus.treeDialog", [parus.Dialog], {

  validate: function() {
    return (this.catalogTree.model.isValid(this.catalogTree.selectedItem));
  },

  treeChange: function() {
    this.OKButton.attr("disabled", this.validate() ? false : true);

    if (this.menu) this._checkTreePopupMenu();
  },

  title: "Выбор каталога",

  model: null,
  //to be set
  key: null,

  restoreSelected: true,

  //to override
  menu: null,
  checkPopupMenu: null,
  //function(dialog, menu, grid)
  postCreate: function() {
    this.inherited(arguments);

    if (this.menu) {
      this.menu.dialogObject = this;

      this.menu.bindDomNode(this.catalogTree.domNode);
    }
  },

  _checkTreePopupMenu: function() {
    parus.catalogActions.checkPopupMenuItemAction(this.menu, this.catalogTree);

    if (this.checkPopupMenu) this.checkPopupMenu.call(this, this, this.menu, this.catalogTree);
  },

  //to be set
  onDefaultNeeded: function() {
    //to be overriden
    return null;
  },

  onSingle: function() {
    //to be overriden
    return null;
  },

  onPerform: function(identity, label) {
    //to be overriden
    return true;
  },

  value: null,

  show: function() {
    var single = this.onSingle();
    if (single) {
      if (this.onPerform(single, null)) return new dojo.DeferredList([]);
    }
    var settings = parus.userdata.get(this.declaredClass + (this.key ? "-" + this.key : "")) || {};
    this.userSize = settings["userSize"] || null;
    this.value = this.value || (this.restoreSelected ? settings["selected"] : null) || this.onDefaultNeeded() || null;
    return this.inherited(arguments);
  },


  _onShow: function() {
    this.catalogTree.model = this.model;

    if (this.catalogTree.dndController && !dojo.isString(this.catalogTree.dndController)) {
      var controllerClass = this.catalogTree.dndController.declaredClass;
      this.catalogTree.dndController.destroy();
      this.catalogTree.tree.dndController = controllerClass;
    };

    this.catalogTree.postMixInProperties();
    this.catalogTree.postCreate();

    if (this.value) this.catalogTree.attr("path", this.value.split(";")).then(dojo.hitch(this, this.treeChange));

    this.inherited(arguments);
  },

  onApply: function() {
    return this.onPerform(this.catalogTree.model.getIdentity(this.catalogTree.selectedItem), this.catalogTree.model.getLabel(this.catalogTree.selectedItem));
  },

  _onClose: function() {
    var path = [];
    if (this.restoreSelected) {
      path = dojo.map(this.catalogTree.path || [], dojo.hitch(this, function(item) {
        return this.catalogTree.model.getIdentity(item);
      }));
    }

    parus.userdata.set(this.declaredClass + (this.key ? "-" + this.key : ""), {
      selected: path.length ? path.join(";") : null,
      userSize: this.userSize
    }, true);

    this.inherited(arguments);
  }

});