Handlebars.registerHelper('displayableTitleCredentials', function(credentialObject) {

  if (credentialObject.type === constants.CREDENTIALS_TYPE.PASSWORD) {
    return getUsernamePasswordString(credentialObject.username, credentialObject.password);

  } else if (credentialObject.type === constants.CREDENTIALS_TYPE.PRIVATE_KEY) {
    let privateKey = truncateContent(utils.maskValueIfEncrypted(credentialObject.privateKey));

    return credentialObject.username + '\n' + privateKey;

  } else if (credentialObject.type === constants.CREDENTIALS_TYPE.PUBLIC_KEY) {
    let privateKey = truncateContent(utils.maskValueIfEncrypted(credentialObject.privateKey));
    let publicKey = truncateContent(credentialObject.publicKey);

    return privateKey + '\n' + publicKey;

  } else if (credentialObject.type === constants.CREDENTIALS_TYPE.PUBLIC) {
    let publicKey = truncateContent(credentialObject.publicKey);

    return publicKey;

  } else {
    return 'Unknown [' + credentialObject.type + ']';
  }
});
Handlebars.registerHelper('displayableCredentials', function(credentialObject) {

  if (credentialObject.type === constants.CREDENTIALS_TYPE.PASSWORD) {
    return formatUtils.escapeHtml(
      getUsernamePasswordString(credentialObject.username, credentialObject.password));

  } else if (credentialObject.type === constants.CREDENTIALS_TYPE.PRIVATE_KEY) {
    return formatUtils.escapeHtml(credentialObject.username) + ' / ' +
      '<div class="truncateText">' +
      formatUtils.escapeHtml(utils.maskValueIfEncrypted(credentialObject.privateKey)) +
      '</div>';

  } else if (credentialObject.type === constants.CREDENTIALS_TYPE.PUBLIC_KEY) {
    return '<div class="truncateText">' +
      formatUtils.escapeHtml(credentialObject.publicKey) +
      '</div>' +
      '<div class="truncateText">' +
      formatUtils.escapeHtml(utils.maskValueIfEncrypted(credentialObject.privateKey)) +
      '</div>';

  } else if (credentialObject.type === constants.CREDENTIALS_TYPE.PUBLIC) {
    return '<div class="truncateText">' +
      formatUtils.escapeHtml(credentialObject.publicKey) +
      '</div>';

  } else {
    return 'Unknown [' + formatUtils.escapeHtml(credentialObject.type) + ']';
  }
});
CredentialsRowEditor.prototype.setData = function(data) {
  this.credentialsObject = data.item;

  this.$el.find('.name-input').val('').removeAttr('disabled');

  // Fix for css navigation transition glitch
  // https://bugs.chromium.org/p/chromium/issues/detail?id=97367
  setTimeout(() => {
    this.$el.find('.name-input').focus();
  }, 310);

  this.$el.find('.username-input').val('');
  this.$el.find('.password-input').val('');
  this.$el.find('.showPassword').addClass('hide');
  this.$el.find('.usePrivateKey').removeClass('hide');
  this.$el.find('.password-input-shown').val('');
  this.$el.find('.password-input-holder').removeClass('hide');
  this.$el.find('.password-input-shown-holder').addClass('hide');
  this.$el.find('.private-key-input-holder').addClass('hide');
  this.$el.find('.private-key-input').val('');

  this.$el.find('.public-certificate-input').val('');
  this.$el.find('.private-certificate-input').val('');

  // Checked by default
  this.$el.find('#credentialTypePassword')[0].checked = true;
  this.$el.find('#credentialTypeCertificate')[0].checked = false;
  this.$el.find('#credentialTypePublic')[0].checked = false;

  this.$el.find('.inline-edit-save').removeClass('loading').removeAttr('disabled');

  if (this.credentialsObject) {
    this.$el.find('.title').html(i18n.t('app.credential.edit.update'));
    this.$el.find('.name-input').val(this.credentialsObject.name).attr('disabled', true);

    if (this.credentialsObject.type === constants.CREDENTIALS_TYPE.PASSWORD
      || this.credentialsObject.type === constants.CREDENTIALS_TYPE.PRIVATE_KEY) {
      this.$el.find('.username-input').val(this.credentialsObject.username);

      let usePassword = this.credentialsObject.type === constants.CREDENTIALS_TYPE.PASSWORD;
      if (usePassword) {
        this.$el.find('.private-key-input-holder').addClass('hide');

        this.$el.find('.usePrivateKey').addClass('hide');

        let password = this.credentialsObject.password;
        if (!password.startsWith(constants.CREDENTIALS_PASSWORD_ENCRYPTED)) {
          this.$el.find('.showPassword').removeClass('hide');
        }

        this.$el.find('.password-input').val(this.credentialsObject.password);
        this.$el.find('.password-input-holder').removeClass('hide');
      } else {
        this.$el.find('.password-input-holder').addClass('hide');
        this.$el.find('.private-key-input').val(
                          utils.maskValueIfEncrypted(this.credentialsObject.privateKey));
        this.$el.find('.private-key-input-holder').removeClass('hide');
      }

    } else if (this.credentialsObject.type === constants.CREDENTIALS_TYPE.PUBLIC_KEY) {
      this.$el.find('#credentialTypeCertificate')[0].checked = true;
      this.$el.find('#credentialTypePassword')[0].checked = false;
      this.$el.find('.public-certificate-input').val(this.credentialsObject.publicKey);
      this.$el.find('.private-certificate-input').val(
                          utils.maskValueIfEncrypted(this.credentialsObject.privateKey));
    } else {
      this.$el.find('#credentialTypePublic')[0].checked = true;
      this.$el.find('#credentialTypePassword')[0].checked = false;
      this.$el.find('.public-input').val(this.credentialsObject.publicKey);
    }

    handleCredentialsTypeInputs(this.$el, this.credentialsObject.type);

    this.customProperties.setData(utils.objectToArray(this.credentialsObject.customProperties));
  } else {
    this.$el.find('.title').html(i18n.t('app.credential.edit.createNew'));
    handleCredentialsTypeInputs(this.$el, constants.CREDENTIALS_TYPE.PASSWORD);

    this.customProperties.setData(null);
  }

  applyValidationErrors.call(this, this.$el, data.validationErrors);

  toggleButtonsState(this.$el);
};