<commit_before>var $ = require('jquery');
var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
var PrivacyDialogView = require('./privacy-dialog-view');
var PasswordDialogView = require('./password-dialog-view');
var TabPaneView = require('../tab-pane/tab-pane-view');
var TabPaneCollection = require('../tab-pane/tab-pane-collection');
var CustomListCollection = require('../custom-list/custom-list-collection');
var template = require('./privacy-dropdown.tpl');
var templateTabPane = require('./tab-pane.tpl');

var _ = require('underscore');

var ESCAPE_KEY_CODE = 27;

var REQUIRED_OPTS = [
  'visDefinitionModel',
  'userModel',
  'privacyCollection'
];

var PRIVACY_MAP = {
  public: 'is-green',
  link: 'is-orange',
  password: '<PASSWORD>',
  private: 'is-red'
};

module.exports = CoreView.extend({

  events: {
    'click .js-toggle': '_onToggleDialogClicked'
  },

  initialize: function (opts) {
    _.each(REQUIRED_OPTS, function (item) {
      if (!opts[item]) throw new Error(item + ' is required');
      this['_' + item] = opts[item];
    }, this);

    this._triggerElementID = 'toggle' + this.cid;

    this.model = new Backbone.Model({
      privacy: opts.visDefinitionModel.get('privacy'),
      state: 'show'
    });

    this._onEscapePressed = this._onEscapePressed.bind(this);
    this._onDocumentElementClicked = this._onDocumentElementClicked.bind(this);

    this._configPrivacyCollection();
    this._configPanes();
    this._initBinds();
  },

  render: function () {
    var privacy = this.model.get('privacy');
    var cssClass = PRIVACY_MAP[privacy.toLowerCase()];

    this.clearSubViews();
    this._hideDialog();
    this.$el.html(template({
      privacy: privacy,
      cssClass: cssClass,
      isLoading: this.model.get('state') === 'loading',
      isDropdown: this._customCollection.length > 1
    }));
    this._initViews();
    return this;
  },

  _initBinds: function () {
    this._customCollection.on('change:selected', function (menuItem) {
      if (menuItem.get('val') === 'password') {
        this._showPasswordDialog();
      } else {
        this._onToggleDialogClicked();
        this._setPrivacy(menuItem.get('val'));
      }
    }, this);

    this.add_related_model(this._customCollection);

    this.model.on('change:privacy', this._savePrivacy, this);
    this.model.on('change:state', this.render, this);
    this.add_related_model(this.model); // explicit
  },

  _makePrivacyDialog: function () {
    return new PrivacyDialogView({
      model: this.model,
      collection: this._customCollection
    });
  },

  _makePasswordDialog: function () {
    return new PasswordDialogView({
      onBack: this._showPrivacyDialog.bind(this),
      onEdit: this._setPassword.bind(this)
    });
  },

  _setPrivacy: function (privacy) {
    this.model.set({privacy: privacy});
  },

  _setPassword: function (password) {
    if (password !== '') {
      this._privacyCollection.passwordOption().set({
        password: password
      });

      this.model.set({
        privacy: 'password',
        password: password
      });
    }
  },

  _savePrivacy: function () {
    var self = this;
    var privacy = this.model.get('privacy').toUpperCase();
    var vis = this._visDefinitionModel;
    this.model.set({state: 'loading'});
    this._privacyCollection.searchByPrivacy(privacy).saveToVis(vis, {
      success: function () {
        self.model.set({state: 'show'});
      },
      error: function () {
        self.model.set({state: 'error'});
      }
    });
  },

  _transformPrivacyOptions: function () {
    return this._privacyCollection.map(function (item) {
      if (item.get('disabled') === false || item.get('privacy') === 'PUBLIC') {
        return {
          label: item.get('privacy'),
          val: item.get('privacy').toLowerCase(),
          renderOptions: {
            cssClass: item.get('cssClass')
          }
        };
      }
    }).filter(Boolean);
  },

  _configPrivacyCollection: function () {
    var models = this._transformPrivacyOptions();
    this._customCollection = new CustomListCollection(models);
  },<commit_msg>Capitalize only the first letter of privacy options.<commit_after>var $ = require('jquery');
var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
var PrivacyDialogView = require('./privacy-dialog-view');
var PasswordDialogView = require('./password-dialog-view');
var TabPaneView = require('../tab-pane/tab-pane-view');
var TabPaneCollection = require('../tab-pane/tab-pane-collection');
var CustomListCollection = require('../custom-list/custom-list-collection');
var template = require('./privacy-dropdown.tpl');
var templateTabPane = require('./tab-pane.tpl');

var _ = require('underscore');

var ESCAPE_KEY_CODE = 27;

var REQUIRED_OPTS = [
  'visDefinitionModel',
  'userModel',
  'privacyCollection'
];

var PRIVACY_MAP = {
  public: 'is-green',
  link: 'is-orange',
  password: '<PASSWORD>',
  private: 'is-red'
};

module.exports = CoreView.extend({

  events: {
    'click .js-toggle': '_onToggleDialogClicked'
  },

  initialize: function (opts) {
    _.each(REQUIRED_OPTS, function (item) {
      if (!opts[item]) throw new Error(item + ' is required');
      this['_' + item] = opts[item];
    }, this);

    this._triggerElementID = 'toggle' + this.cid;

    this.model = new Backbone.Model({
      privacy: opts.visDefinitionModel.get('privacy'),
      state: 'show'
    });

    this._onEscapePressed = this._onEscapePressed.bind(this);
    this._onDocumentElementClicked = this._onDocumentElementClicked.bind(this);

    this._configPrivacyCollection();
    this._configPanes();
    this._initBinds();
  },

  render: function () {
    var privacy = this.model.get('privacy');
    var cssClass = PRIVACY_MAP[privacy.toLowerCase()];

    this.clearSubViews();
    this._hideDialog();
    this.$el.html(template({
      privacy: privacy,
      cssClass: cssClass,
      isLoading: this.model.get('state') === 'loading',
      isDropdown: this._customCollection.length > 1
    }));
    this._initViews();
    return this;
  },

  _initBinds: function () {
    this._customCollection.on('change:selected', function (menuItem) {
      if (menuItem.get('val') === 'password') {
        this._showPasswordDialog();
      } else {
        this._onToggleDialogClicked();
        this._setPrivacy(menuItem.get('val'));
      }
    }, this);

    this.add_related_model(this._customCollection);

    this.model.on('change:privacy', this._savePrivacy, this);
    this.model.on('change:state', this.render, this);
    this.add_related_model(this.model); // explicit
  },

  _makePrivacyDialog: function () {
    return new PrivacyDialogView({
      model: this.model,
      collection: this._customCollection
    });
  },

  _makePasswordDialog: function () {
    return new PasswordDialogView({
      onBack: this._showPrivacyDialog.bind(this),
      onEdit: this._setPassword.bind(this)
    });
  },

  _setPrivacy: function (privacy) {
    this.model.set({privacy: privacy});
  },

  _setPassword: function (password) {
    if (password !== '') {
      this._privacyCollection.passwordOption().set({
        password: password
      });

      this.model.set({
        privacy: 'password',
        password: password
      });
    }
  },

  _savePrivacy: function () {
    var self = this;
    var privacy = this.model.get('privacy').toUpperCase();
    var vis = this._visDefinitionModel;
    this.model.set({state: 'loading'});
    this._privacyCollection.searchByPrivacy(privacy).saveToVis(vis, {
      success: function () {
        self.model.set({state: 'show'});
      },
      error: function () {
        self.model.set({state: 'error'});
      }
    });
  },

  _transformPrivacyOptions: function () {
    var self = this;
    return this._privacyCollection.map(function (item) {
      if (item.get('disabled') === false || item.get('privacy') === 'PUBLIC') {
        return {
          label: self._capitalizeFirstLetter(item.get('privacy')),
          val: item.get('privacy').toLowerCase(),
          renderOptions: {
            cssClass: item.get('cssClass')
          }
        };
      }
    }).filter(Boolean);
  },

  _capitalizeFirstLetter: function (string) {
    string = string.toLowerCase();
    return string.charAt(0).toUpperCase() + string.slice(1);
  },

  _configPrivacyCollection: function () {
    var models = this._transformPrivacyOptions();
    this._customCollection = new CustomListCollection(models);
  },
{'fraction_non_alphanumeric': 0.1568627450980392, 'fraction_numerical': 0.0009392978748385582, 'mean_word_length': 3.214745175655616, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501417', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2621, 'n_words': 458}