// Generated by CoffeeScript 1.12.7
var PdfFieldsFormNode,
  extend = 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;

PdfFieldsFormNode = (function(superClass) {
  extend(PdfFieldsFormNode, superClass);

  function PdfFieldsFormNode() {
    return PdfFieldsFormNode.__super__.constructor.apply(this, arguments);
  }

  PdfFieldsFormNode.prototype.getModeForNested = function() {
    return "all";
  };

  PdfFieldsFormNode.prototype.renderContent = function() {
    var nameColumn, nameLocalizedInput, nameLocalizedOutput, nameOutput, row, setState;
    setState = (function(_this) {
      return function(event) {
        var i, len, ref, sibling;
        _this.setCheckbox(_this.data._enabled);
        if (!event.ctrlKey()) {
          _this.setChildren(_this.data._enabled);
        }
        if (event.altKey() || event.shiftKey()) {
          ref = _this.father.children;
          for (i = 0, len = ref.length; i < len; i++) {
            sibling = ref[i];
            sibling.setCheckbox(_this.data._enabled);
            if (!event.ctrlKey()) {
              sibling.setChildren(_this.data._enabled);
            }
          }
        }
      };
    })(this);
    this.checkbox = new CUI.Checkbox({
      name: "_enabled",
      undo_and_changed_support: false,
      data: this.data,
      onClick: (function(_this) {
        return function(event) {
          setState(event);
        };
      })(this)
    });
    this.checkbox.start();
    nameLocalizedOutput = new CUI.Output({
      name: "name_localized",
      data: this.data
    });
    nameLocalizedOutput.start();
    nameOutput = new CUI.Output({
      name: "name",
      data: this.data,
      "class": "cui-label-appearance-secondary"
    });
    nameOutput.start();
    nameColumn = new CUI.VerticalList({
      content: [nameLocalizedOutput, nameOutput]
    });
    CUI.Events.listen({
      node: nameColumn,
      type: "click",
      call: (function(_this) {
        return function(event) {
          _this.data._enabled = !_this.data._enabled;
          setState(event);
        };
      })(this)
    });
    CUI.dom.setAttribute(nameColumn, 'data-label-clickable', '1');
    nameLocalizedInput = new CUI.MultiInput({
      name: "label",
      data: this.data,
      maximize_horizontal: true,
      control: ez5.loca.getLanguageControl()
    });
    nameLocalizedInput.start();
    row = CUI.dom.table_one_row(this.checkbox, nameColumn, nameLocalizedInput);
    CUI.Events.listen({
      node: row,
      type: "click",
      call: (function(_this) {
        return function(ev) {
          ev.stopPropagation();
        };
      })(this)
    });
    this.setCheckbox(this.data._enabled);
    return row;
  };

  PdfFieldsFormNode.prototype.nodeAdded = function(level) {
    var fieldFullName, label;
    PdfFieldsFormNode.__super__.nodeAdded.call(this, level);
    fieldFullName = this.getFieldFullName();
    if (!this._data[fieldFullName]) {
      label = {};
      label[ez5.loca.getLanguage()] = this.field.nameLocalized();
      this._data[fieldFullName] = {
        _enabled: false,
        label: label,
        name_localized: this.field.nameLocalized(),
        name: this.field.name()
      };
    }
    this.data = this._data[fieldFullName];
  };

  PdfFieldsFormNode.prototype.setChildren = function(isEnabled) {
    var c, i, len, ref, results;
    if (!this.children) {
      return;
    }
    ref = this.children;
    results = [];
    for (i = 0, len = ref.length; i < len; i++) {
      c = ref[i];
      c.setCheckbox(isEnabled);
      results.push(c.setChildren(isEnabled));
    }
    return results;
  };

  PdfFieldsFormNode.prototype.setCheckbox = function(isEnabled) {
    var ref;
    if (this.__hidden) {
      return this;
    }
    this.data._enabled = isEnabled;
    if ((ref = this.checkbox) != null) {
      ref.displayValue();
    }
    if (this.father && this.father.checkbox && this.father.isOpen()) {
      if (!this.father.children.some((function(_this) {
        return function(childNode) {
          var ref1;
          return (ref1 = childNode.checkbox) != null ? ref1.getValue() : void 0;
        };
      })(this))) {
        this.father.setCheckbox(false);
      } else {
        this.father.setCheckbox(true);
      }
    }
    return this;
  };

  PdfFieldsFormNode.prototype.open = function() {
    return PdfFieldsFormNode.__super__.open.call(this).done((function(_this) {
      return function() {
        var i, len, node, ref, results;
        if (!_this.children) {
          return;
        }
        ref = _this.children;
        results = [];
        for (i = 0, len = ref.length; i < len; i++) {
          node = ref[i];
          if (node.isHidden()) {
            results.push(node.hide());
          } else {
            results.push(void 0);
          }
        }
        return results;
      };
    })(this));
  };

  PdfFieldsFormNode.prototype.isHidden = function() {
    return this.__hidden;
  };

  PdfFieldsFormNode.prototype.hide = function() {
    var element, ref;
    this.__hidden = true;
    element = (ref = this.columns[0]) != null ? ref.getElement() : void 0;
    if (!element) {
      return;
    }
    CUI.dom.hideElement(element);
  };

  PdfFieldsFormNode.prototype.show = function() {
    var element, ref;
    this.__hidden = false;
    element = (ref = this.columns[0]) != null ? ref.getElement() : void 0;
    if (!element) {
      return;
    }
    CUI.dom.showElement(element);
  };

  PdfFieldsFormNode.prototype.getFieldFullName = function() {
    if (this.father) {
      return this.father.getFieldFullName() + "." + this.field.name();
    }
    return this.field.fullName();
  };

  PdfFieldsFormNode.prototype.getLinkedObjectRenderDepth = function() {
    return 2;
  };

  return PdfFieldsFormNode;

})(FieldsFormNode);
