dojo.provide("parus.dictionaryDialog");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.ComboBox");

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

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

  dictionary: null,

  columns: null,

  value: null,

  label: null,

  multiselect: false,

  _refreshGridTimeoutID: null,
  _refreshDetailGridTimeoutID: null,

  catalogsQueryID: null,

  labelField: null,

  keyField: null,

  pathQueryID: null,

  queryID: null,

  catalogLinkFieldName: null,

  unitCode: null,

  gridConditions: null,

  gridPersistent: null,

  gridOnComplete: null,

  hideCancel: false,

  catalogMenu: null,
  menu: null,
  allowRefresh: true,
  detailMenu: null,
  detailAllowRefresh: true,

  _attrDescriptors: null,

  userDataSection: null,

  catalogConditions: null,

  queryParams: null,

  detailQueryID: null,
  detailColumns: null,
  detailTitle: null,

  close: function() {

    if (this.catalogTree) {
      var path = [];
      dojo.forEach(this.catalogTree.path, dojo.hitch(this, function(item) {
        path.push(this.catalogTree.model.getIdentity(item));
      }));

      var checks = [];
      if (this.catalogTree.checked.length > 1) dojo.forEach(this.catalogTree.checked, dojo.hitch(this, function(node) {
        checks.push(dojo.map(node.getTreePath(node), dojo.hitch(this, function(item) {
          return this.catalogTree.model.getIdentity(item);
        })).join("/"));
      }));

      if (path.length != 0) parus.userdata.set(this.userDataSection + "selectedCatalog", {
        selectedCatalog: path.join(";"),
        checks: checks.join(";")
      }, true);
    }

    parus.userdata.set(this.userDataSection, {
      userSize: this.userSize || null
    }, true);

    if (this._refreshGridTimeoutID) clearTimeout(this._refreshGridTimeoutID);
    if (this._refreshDetailGridTimeoutID) clearTimeout(this._refreshDetailGridTimeoutID);
    this._refreshGridTimeoutID = null;
    this._refreshDetailGridTimeoutID = null;
    this._onQueryComplete = null;

    return this.inherited(arguments);
  },

  catalogTreeStoreModel: function() {
    return new parus.catalogTreeModel({
      queryid: this.catalogsQueryID,
      onGetConditions: dojo.hitch(this, function(model, parentNode) {


        if (this.catalogTree.hiddenMode) return {
          CRN: parus.parus8.CLASS_EmptyData
        };


        var conditions = {
          "UNITCODE": this.unitCode
        };
        if (this.catalogConditions) dojo.mixin(conditions, this.catalogConditions);
        if (this.catalogLinkFieldName) conditions[this.catalogLinkFieldName] = parentNode ? model.getIdentity(parentNode) : "!*";
        conditions["CRN"] = parentNode ? model.getIdentity(parentNode) : "!*";
        return conditions;
      })
    })
  },

  __getCatalogTreeRN: function() {
    return ((!this.catalogTree || this.catalogTree.listMode) ? null : ((this.catalogTree.selectedItem && this.catalogTree.model.isValid(this.catalogTree.selectedItem)) ? this.catalogTree.model.getIdentity(this.catalogTree.selectedItem) : parus.parus8.CLASS_EmptyData));
  },

  gridStore: function() {
    if (!this.gridPersistent) this.gridPersistent = new parus.gridData({
      queryid: this.queryID,
      onGetConditions: dojo.hitch(this, function(store) {

        var conditions = {};

        dojo.mixin(conditions, this.gridConditions || {});

        if (this.queryParams) for (var key in this.queryParams) if (dojo.isFunction(this.queryParams[key])) conditions[key] = this.queryParams[key].apply(this)
        else conditions[key] = this.queryParams[key];

        if (this.catalogTree) {


          if (!this.catalogTree.selectedItem && (this._alreadySet || (!this.value && !this.label))) {

            this.catalogTree.model.getRoot(dojo.hitch(this, function(root) {

              var path = [];
              var selectedCatalog = parus.userdata.get(this.userDataSection + "selectedCatalog");
              if (selectedCatalog.selectedCatalog) path = selectedCatalog.selectedCatalog.split(";");

              if ((path.length == 0) || (path[0] == "")) {
                var response = parus.database.execute("findAvailableCatalog", {
                  UNITCODE: this.unitCode
                });
                if (response) {
                  var node = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "PATH");
                  if (node) path = node.split(";");
                }
              };

              if ((path.length == 0) || (path[0] == "")) path = [root];

              if (selectedCatalog.checks) {
                this.catalogTree.setCheckedPaths(
                dojo.map(selectedCatalog.checks.split(";"), function(path) {
                  return path.split("/");
                }));
              };

              this.catalogTree.attr("path", path);

              if (this.catalogTree.checked.length) {
                var items = [];
                dojo.forEach(this.catalogTree.checked, dojo.hitch(this, function(node) {
                  items.push(this.catalogTree.model.getIdentity(node.item));
                }));

                conditions[this.catalogLinkFieldName] =
                this.catalogTree.listMode ? null : items.join(";");
              }

              else {
                conditions[this.catalogLinkFieldName] =
                this.catalogTree.listMode ? null : ((this.catalogTree.selectedItem && this.catalogTree.model.isValid(this.catalogTree.selectedItem)) ? this.catalogTree.model.getIdentity(this.catalogTree.selectedItem) : parus.parus8.CLASS_EmptyData);
              }
            }));

            conditions[this.catalogLinkFieldName] =
            this.catalogTree.listMode ? null : parus.parus8.CLASS_EmptyData;

          }
          else {

            if (this.catalogTree.checked.length) {
              var items = [];
              dojo.forEach(this.catalogTree.checked, dojo.hitch(this, function(node) {
                items.push(this.catalogTree.model.getIdentity(node.item));
              }));

              conditions[this.catalogLinkFieldName] =
              this.catalogTree.listMode ? null : items.join(";");
            }
            else conditions[this.catalogLinkFieldName] = this.catalogTree.listMode ? null : ((this.catalogTree.selectedItem && this.catalogTree.model.isValid(this.catalogTree.selectedItem)) ? this.catalogTree.model.getIdentity(this.catalogTree.selectedItem) : parus.parus8.CLASS_EmptyData);

          }

          if (conditions[this.catalogLinkFieldName] == "-1") conditions[this.catalogLinkFieldName] = "-1;0";

          conditions["CRN"] = conditions[this.catalogLinkFieldName];

          store.catalogRN = conditions[this.catalogLinkFieldName];
        }

        return conditions;
      })
    });

    dojo.mixin(this.gridPersistent, {
      onComplete: dojo.hitch(this, function(store, items) {
        if (!this.grid) return;
        if ((items.length > 0) && !this.grid.selection.getSelectedCount()) {
          if (!this.multiselect) {
            this.grid.selection.select(0);
            this.grid.updateRow(0);
            this.grid.scrollToRow(0);
            this.grid.autoSelected = true;
          }
        };

        this._checkGridPopupMenu();

        if (this.gridOnComplete) {
          this.gridOnComplete.call(this, store, items);
        }

      })
    });

    return this.gridPersistent;
  },

  detailGridStore: function() {
    return new parus.gridData({
      queryid: this.detailQueryID,

      onGetConditions: dojo.hitch(this, function(astore) {
        var store = this.grid.store;
        var index = this.grid.selection.getSelectedIndex();
        var item = (index >= 0) ? this.grid.getItem(index) : null;

        astore.parentRN = store.getValue(item, "RN");
        return {
          PRN: astore.parentRN || parus.parus8.CLASS_EmptyData
        };
      }),

      onComplete: dojo.hitch(this, function(store, items, keywordArgs) {
        if ((items.length > 0) && !this.detailGrid.selection.getSelectedCount() && !keywordArgs.start) parus.gridActions.autoSelectRow(this.detailGrid);
        else this.detailGrid.onSelected();

        this._checkDetailGridPopupMenu();
      })
    });
  },

  postCreate: function() {
    if (!this.multiselect) {
      var gridNode = dojo.query("[dojoAttachPoint='grid']", this.domNode)[0];
      gridNode.setAttribute("selectionMode", "single");
      gridNode.setAttribute("plugins", "{status: {visible: false}, columnsFilter: {visible: false}}");
    };

    if (!this.catalogsQueryID) {
      dojo.query("[dojoAttachPoint='catalogPane']", this.domNode).forEach(function(node) {
        node.parentNode.removeChild(node);
      });

      dojo.query("[design='sidebar']", this.domNode).forEach(function(node) {
        node.setAttribute("design", "headline");
      });

      dojo.mixin(this, {
        catalogPane: null,
        catalogTree: null,
        catalogMenu: null
      });
    }

    if (!this.detailQueryID) {
      dojo.query("[dojoAttachPoint='detailPane']", this.domNode).forEach(function(node) {
        node.parentNode.removeChild(node);
      });

      dojo.mixin(this, {
        detailPane: null,
        detailGrid: null,
        detailMenu: null
      });
    }
    else {
      this.userSizeDataSection = this.declaredClass + "-withDetails-userSize";

      //this.autoSize = true;
      if (!this.defaultUserSize) this.defaultUserSize = {
        width: "800px",
        height: "700px"
      }
    }

    if (this.defaultUserSize) {
      dojo.query("[dojoAttachPoint='dialogContentPane']", this.domNode).forEach(dojo.hitch(this, function(node) {
        dojo.style(node, this.defaultUserSize);
      }));
    }

    this.inherited(arguments);

    this.onCreateMenu();

    this.userDataSection = this.userDataSection || (this.declaredClass + "-" + (this.dictionary ? this.dictionary.code : "nodictionary"));

    this.dialog.titleNode.innerHTML = this.title;
    this.dialog.title = this.title;

    this.headerPaneText.domNode.appendChild(document.createTextNode(this.title));

    this.dialog.containerNode.style.padding = "0";
    dojo.addClass(this.dialog.containerNode, "dijitBorderContainer");

    var settings = parus.userdata.get(this.userDataSection);
    if (settings) if (this.catalogTree) parus.gridSettingsDialog.setCatalogTreeVisible(this.catalogTree, !settings.hideCatalogTree);


    if (this.unitCode && this.dictionary.hasAttributes) this._attrDescriptors = parus.parus8._attrDescriptors ? parus.parus8.rowNodes(parus.database.execute("attrDescriptors", {
      "UNITCODE": this.unitCode
    }).documentElement.childNodes[0].childNodes[0].childNodes[0]) || [] : [];
    else this._attrDescriptors = [];

    if (!this.columns.attrLinked) {

      dojo.forEach(this._attrDescriptors, dojo.hitch(this, function(descriptor) {

        this.columns.push({
          colSpan: 1,
          attrRN: parus.parus8.nodeField(descriptor, "RN"),
          dataType: parus.parus8.nodeField(descriptor, "DATATYPE"),
          dataSubType: 0,
          field: "attr" + parus.parus8.nodeField(descriptor, "RN"),
          name: parus.parus8.nodeField(descriptor, "NAME"),
          formatter: parus.balance.formatAttrValue,
          width: (parus.parus8.nodeField(descriptor, "ACTIVE") == "1") ? "121px" : "0px",
          hidden: (parus.parus8.nodeField(descriptor, "ACTIVE") != "1"),
          styles: "text-align:" + ((parus.parus8.nodeField(descriptor, "DATATYPE") == 0) ? "left" : "right") + ";",
          sortField: (parus.parus8.nodeField(descriptor, "PARAMCOUNT") == "2") ? (parus.parus8.nodeField(descriptor, "STOREDPROC") + "(T.COMPANY, T.RN)") : (parus.parus8.nodeField(descriptor, "STOREDPROC") + "(T.RN)")
        })

      }));
      this.columns.attrLinked = true;
    };

    if (this.catalogTree) {
      this.catalogTree.model = this.catalogTreeStoreModel();

      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();

      this.catalogMenu = this._createCatalogMenu(this.catalogMenu, this.catalogTree);
      this.catalogMenu.bindDomNode(this.catalogTree.domNode);
    }

    this.grid.attr("structure", this.columns);
    this.grid.attr("store", this.gridStore());

    if (this.detailGrid) {
      this.detailGrid.attr("structure", this.detailColumns);
      this.detailGrid.attr("store", this.detailGridStore());
      this.detailGrid.setQuery(this.detailGrid.query || {});
    }

    if (this.hideCancel) {
      this.cancelButton.destroyRecursive();
      this.cancelButton = null;
      this.OKButton.set("label", "<b>Закрыть</b>");
    };


    this.menu = this._createMenu(this.menu, this.grid, this.allowRefresh, this.userDataSection);
    parus.balance._createActionsButton(this.menu, this.headerPane);
    this.menu.bindDomNode(this.grid.viewsNode);

    if (settings) {
      parus.gridSettingsDialog.mergeMenuSettings(this.menu, settings.actionSettings, settings.userProcsActions, settings.userReportsActions, settings.userAppsActions);
      parus.gridSettingsDialog.mergeGridSettings(this.grid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.grid, settings.colorConditions);
      parus.gridSettingsDialog.mergeFilterBar(this.grid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
      this.userSize = settings.userSize;
    };

    if (this.detailGrid) {
      this.detailMenu = this._createMenu(this.detailMenu, this.detailGrid, this.detailAllowRefresh, this.userDataSection + "-detailGrid");
      parus.balance._createActionsButton(this.detailMenu, this.detailHeaderPane);
      this.detailMenu.bindDomNode(this.detailGrid.viewsNode);

      settings = parus.userdata.get(this.userDataSection + "-detailGrid");
      if (settings) {
        parus.gridSettingsDialog.mergeMenuSettings(this.detailMenu, settings.actionSettings, settings.userProcsActions, settings.userReportsActions, settings.userAppsActions);
        parus.gridSettingsDialog.mergeGridSettings(this.detailGrid, settings.gridSettings);
        parus.gridSettingsDialog.mergeColorConditions(this.detailGrid, settings.colorConditions);
        parus.gridSettingsDialog.mergeFilterBar(this.detailGrid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
      };

      if (this.detailTitle) this.detailHeaderPaneText.domNode.appendChild(document.createTextNode(this.detailTitle));
    }

    if (this.label) this.setLabel(this.label)
    else if (this.value) this.setValue(this.value)
    else this.grid.setQuery(this.gridConditions || {});

  },

  //to override
  onCreateMenu: function() {

  },

  _createCatalogMenu: function(menu, catalogTree) {
    var hasMenu = !! menu;
    if (!hasMenu) menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    var index = 0;

    menu.addChild(new dijit.CheckedMenuItem({
      label: "Все",
      onClick: dojo.hitch(this, function() {
        parus.catalogActions.listCatalogTree(catalogTree);
      })
    }), index++);

    menu.addChild(new dijit.MenuItem({
      label: "Список",
      onClick: dojo.hitch(this, function() {
        parus.catalogActions.checkChildren(catalogTree);
      })
    }), index++);

    menu.addChild(new dijit.MenuItem({
      label: "Очистить выбор",
      onClick: dojo.hitch(this, function() {
        catalogTree.uncheckAll();
      })
    }), index++);

    menu.addChild(new dijit.MenuItem({
      label: "Развернуть",
      onClick: dojo.hitch(this, function() {
        parus.catalogActions.expandCatalogTree(catalogTree);
      })
    }), index++);
    menu.addChild(new dijit.MenuSeparator(), index++);

    if (hasMenu) menu.addChild(new dijit.MenuSeparator());

    menu.addChild(new dijit.MenuItem({
      label: "Обновить",
      defaultIcon: "PCisyncCustomIcon",
      onClick: dojo.hitch(this, function() {
        parus.catalogActions.refreshCatalogTree(catalogTree);
      })
    }));

    if (hasMenu) {
      menu.addChild(new dijit.MenuSeparator());
      menu.addChild(new dijit.MenuItem({
        label: "Настройки...",
        onClick: dojo.hitch(this, function() {
          parus.balance.executeSettings(null, menu, this.userDataSection + "-catalogTree");
        })
      }));
    }

    menu.dialogObject = this;
    menu.startup();

    return menu;
  },

  _createMenu: function(menu, grid, allowRefresh, userDataSection) {
    var hasMenu = !! menu;
    if (!hasMenu) menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    if (hasMenu) menu.addChild(new dijit.MenuSeparator());

    if (allowRefresh) {
      menu.addChild(new dijit.MenuItem({
        label: "Обновить",
        defaultIcon: "PCisyncCustomIcon",
        onClick: dojo.hitch(this, function() {
          parus.balance.refreshGrid(grid);
        })
      }));
      menu.addChild(new dijit.MenuSeparator());
    }

    menu.addChild(new dijit.MenuItem({
      label: "Экспорт таблицы в файл...",
      onClick: dojo.hitch(this, function() {
        parus.balance.exportGrid(grid, null, null);
      })
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Настройки...",
      onClick: dojo.hitch(this, function() {
        this._alreadySet = true;
        parus.balance.executeSettings(grid, hasMenu ? menu : null, userDataSection, this.catalogTree, null, null, null, null);
      })
    }));

    menu.dialogObject = this;
    menu.startup();

    return menu;
  },

  _catalogChangeBuffer: null,

  catalogTreeChange: function() {

    var selection = null;

    if (this.catalogTree.checked.length) {
      var items = [];
      dojo.forEach(this.catalogTree.checked, dojo.hitch(this, function(node) {
        items.push(this.catalogTree.model.getIdentity(node.item));
      }));
      selection = this.catalogTree.listMode ? null : items.join(";");
    }
    else {
      selection = this.catalogTree.listMode ? null : ((this.catalogTree.selectedItem && this.catalogTree.model.isValid(this.catalogTree.selectedItem)) ? this.catalogTree.model.getIdentity(this.catalogTree.selectedItem) : parus.parus8.CLASS_EmptyData);
    }

    if (this._catalogChangeBuffer != (selection || "")) {
      this._catalogChangeBuffer = (selection || "");
      if (this._refreshGridTimeoutID) clearTimeout(this._refreshGridTimeoutID);
      this._refreshGridTimeoutID = setTimeout(dojo.hitch(this, this._refreshGrid), 300);

      if (this.detailGrid) {
        this.gridChange();
      }
    };

  },

  _onQueryComplete: null,

  show: function() {

    (new dojo.DeferredList([this.inherited(arguments)])).addCallback(
    dojo.hitch(this, function(result) {
      this.grid.focus.focusGrid();
      this.grid.focus.setFocusIndex(this.grid.getItemIndex(this.grid.selection.getFirstSelected()), 0);
      return result;
    }));

  },

  _refreshGrid: function() {
    if (this._refreshGridTimeoutID) clearTimeout(this._refreshGridTimeoutID);
    this._refreshGridTimeoutID = null;

    if (this.catalogTree) if ((this._alreadySet || (!this.value && !this.label)) && !this.catalogTree.selectedItem) this.catalogTree.attr("path", [this.catalogTree.rootNode.item]);

    var query = this.grid.query || {};
    dojo.mixin(query, {
      onComplete: this._onQueryComplete
    });
    this.grid.setQuery(query);
  },

  _getGridItem: function(fieldName) {
    var value = [];
    dojo.forEach(this.grid.selection.getSelected(), dojo.hitch(this, function(item) {
      value.push(this.gridStore().getValue(item, fieldName, ""));
    }));

    return this.multiselect ? value.join(";") : value[0];
  },

  getValue: function() {
    return this._getGridItem(this.keyField);
  },

  getLabel: function() {
    return this._getGridItem(this.labelField);
  },

  getFieldValue: function(fieldName) {
    return this._getGridItem(fieldName);
  },

  _alreadySet: null,

  _setGridItem: function(value, fieldName) {
    if (!value) return;
    if (!fieldName) return;

    this._alreadySet = false;

    if (this.catalogTree) {
      if (!this.catalogTree.hiddenMode) {


        var conditions = {};

        dojo.mixin(conditions, this.gridConditions || {});

        if (this.queryParams) for (var key in this.queryParams) if (!dojo.isFunction(this.queryParams[key])) conditions[key] = this.queryParams[key];

        dojo.mixin(conditions, {
          "UNITCODE": this.unitCode,
          "LABEL": value
        });

        conditions[fieldName] = value;

        parus.database.query(this.pathQueryID, conditions, dojo.hitch(this, function(response) {
          var path = [];
          dojo.forEach(parus.parus8.rowNodes(response), dojo.hitch(this, function(item) {
            var value = parus.parus8.nodeField(item, this.keyField);
            if (value) path.push(value);
          }));


          if (path.length) {
            this.catalogTree.attr("path", path).then(dojo.hitch(this, this._refreshGrid));

            this._onQueryComplete = dojo.hitch(this, function(store) {
              if (!this._alreadySet) {
                var item = store.findIndex(fieldName, value);
                if (item >= 0) {
                  this.grid.scrollToRow(item);
                  this.grid.selection.select(item);
                  this.grid.updateRow(item);
                  var top = this.grid.scroller.findScrollTop(item);
                  this._alreadySet = (this.grid.views.setScrollTop(top) == top);
                  if (this._alreadySet) {
                    this.grid.focus.focusGrid();
                    this.grid.focus.setFocusIndex(item, 0);
                    this._onQueryComplete = null;
                  }
                }
              };
            });
          }
          else {
            this._alreadySet = true;
            this.catalogTree.model.getRoot(dojo.hitch(this, function(root) {
              this.catalogTree.attr("path", [root]);
            }));
          }
        }));
      }
    }
    else {
      this._onQueryComplete = dojo.hitch(this, function(store, data, keywordArgs) {
        if (!this._alreadySet) {
          var item = store.findIndex(fieldName, value);
          if (item >= 0) {
            setTimeout(dojo.hitch(this, function() {
              if (!this._onQueryComplete) return;
              this._onQueryComplete = null;
              this.grid.scrollToRow(item);
              this.grid.selection.select(item);
              this.grid.updateRow(item);

              var top = this.grid.scroller.findScrollTop(item);
              this._alreadySet = (this.grid.views.setScrollTop(top) == top);
              if (this._alreadySet) {
                this.grid.focus.focusGrid();
                this.grid.focus.setFocusIndex(item, 0);
              }

            }), 100);

          };
        };
      });

      if (this.grid._requestsPending(0)) {
        setTimeout(dojo.hitch(this, function() {
          this._setGridItem(value, fieldName);
        }), 100)
      }
      else {
        var query = this.grid.query || {};

        dojo.mixin(query, {
          onComplete: this._onQueryComplete
        });
        this.grid.setQuery(query);
      }
    }
  },

  setValue: function(value) {
    this._setGridItem(value, this.keyField);
  },

  setLabel: function(value) {
    this._setGridItem(value, this.labelField);
  },

  executeConditions: function() {

  },

  detailExecuteConditions: function() {

  },

  gridDoubleClick: function() {
    if (!parus.gridActions.checkPopupMenuDefaultAction(this.menu)) this.apply();
  },

  detailGridDoubleClick: function() {
    parus.gridActions.checkPopupMenuDefaultAction(this.detailMenu);
  },

  gridFilterChanged: function(values, sortInfo) {
    parus.balance.gridFilterChanged(this.userDataSection, values, sortInfo);
  },

  detailGridFilterChanged: function(values, sortInfo) {
    parus.balance.gridFilterChanged(this.userDataSection + "-detailGrid", values, sortInfo);
  },

  _masterChangeBuffer: null,

  gridChange: function() {
    this._checkGridPopupMenu();

    if (this.detailGrid) {
      if (this._refreshDetailGridTimeoutID) clearTimeout(this._refreshDetailGridTimeoutID);

      this._refreshDetailGridTimeoutID = setTimeout(dojo.hitch(this, function() {
        if (this._refreshDetailGridTimeoutID) clearTimeout(this._refreshDetailGridTimeoutID);
        this._refreshDetailGridTimeoutID = null;

        parus.balance.refreshGrid(this.detailGrid);
      }), 300);
    }
  },

  detailGridChange: function() {
    this._checkDetailGridPopupMenu();
  },

  _checkGridPopupMenu: function() {
    parus.gridActions.checkPopupMenuItemAction(this.menu, this.grid);

    if (this.checkGridPopupMenu) this.checkGridPopupMenu.call(this, this, this.menu, this.grid);
  },

  _checkDetailGridPopupMenu: function() {
    parus.gridActions.checkPopupMenuItemAction(this.detailMenu, this.detailGrid);

    if (this.checkDetailGridPopupMenu) this.checkDetailGridPopupMenu.call(this, this, this.detailMenu, this.detailGrid);
  },

  //to override
  checkGridPopupMenu: null,
  //function(dialog, menu, grid)
  //to override
  checkDetailGridPopupMenu: null //function(dialog, menu, grid)

});