<commit_before>var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
require('bootstrap-colorpicker');
var template = require('./template.tpl');
var colorPickerTemplate = require('./color-picker-template.tpl');
var Utils = require('../../../../../helpers/utils');

var ESCAPE_KEY_CODE = 27;

module.exports = CoreView.extend({
  events: {
    'blur .js-hex': '_onChangeHex',
    'blur .js-inputColor': '_onChangeColorValue',
    'blur .js-a': '_onChangeOpacity'
  },

  initialize: function (opts) {
    var opacity = opts.opacity != null ? opts.opacity : 1;

    this.model = new Backbone.Model({
      hex: opts.value,
      opacity: opacity
    });

    this._onEscape = this._onEscapePressed.bind(this);
    this._initBinds();
  },

  render: function () {
    var rgb = Utils.hexToRGB(this.model.get('hex'));

    var color = _.extend(rgb, {
      hex: this.model.get('hex'),
      opacity: this.model.get('opacity'),
      opacityDisabled: this.options.disableOpacity
    });

    this.$el.append(template(color));

    var rgbaTemplate = _.template('rgba(<%- r %>, <%- g %>, <%- b %>, <%- opacity %>)');

    this.$('.js-colorPicker').colorpicker({
      color: rgbaTemplate(color),
      format: 'rgba',
      container: true,
      horizontal: true,
      inline: true,
      customClass: 'ColorPicker',
      template: colorPickerTemplate(),
      slidersHorz: {
        saturation: {
          maxLeft: 206,
          maxTop: 95,
          callLeft: 'setSaturation',
          callTop: 'setBrightness'
        },
        hue: {
          maxLeft: 180,
          maxTop: 0,
          callLeft: 'setHue',
          callTop: false
        },
        alpha: {
          maxLeft: 180,
          maxTop: 0,
          callLeft: 'setAlpha',
          callTop: false
        }
      }
    });

    if (this.options.disableOpacity) {
      this.$('.js-colorPicker .js-alpha').addClass('is-hidden');
    }

    var setNewColor = function (e) {
      var rgb = e.color.toRGB();
      var hex = e.color.toHex();
      this.model.set({ hex: hex, r: rgb.r, g: rgb.g, b: rgb.b, opacity: rgb.a });
      this.$('.js-color').css('opacity', rgb.a);
    }.bind(this);

    this.$('.js-colorPicker').colorpicker().on('changeColor', _.debounce(setNewColor, 250));

    this._initDocumentBinds();
    return this;
  },

  _initBinds: function () {
    this.model.bind('change:hex', this._onChangeColor, this);
    this.model.bind('change:r', this._onChangeColor, this);
    this.model.bind('change:g', this._onChangeColor, this);
    this.model.bind('change:b', this._onChangeColor, this);
    this.model.bind('change:opacity', this._onChangeColor, this);
  },

  _onEscapePressed: function (ev) {
    if (ev.which === ESCAPE_KEY_CODE) {
      this.remove();
    }
  },
<commit_msg>No need for debounce.<commit_after>var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
require('bootstrap-colorpicker');
var template = require('./template.tpl');
var colorPickerTemplate = require('./color-picker-template.tpl');
var Utils = require('../../../../../helpers/utils');

var ESCAPE_KEY_CODE = 27;

module.exports = CoreView.extend({
  events: {
    'blur .js-hex': '_onChangeHex',
    'blur .js-inputColor': '_onChangeColorValue',
    'blur .js-a': '_onChangeOpacity'
  },

  initialize: function (opts) {
    var opacity = opts.opacity != null ? opts.opacity : 1;

    this.model = new Backbone.Model({
      hex: opts.value,
      opacity: opacity
    });

    this._onEscape = this._onEscapePressed.bind(this);
    this._initBinds();
  },

  render: function () {
    var rgb = Utils.hexToRGB(this.model.get('hex'));

    var color = _.extend(rgb, {
      hex: this.model.get('hex'),
      opacity: this.model.get('opacity'),
      opacityDisabled: this.options.disableOpacity
    });

    this.$el.append(template(color));

    var rgbaTemplate = _.template('rgba(<%- r %>, <%- g %>, <%- b %>, <%- opacity %>)');

    this.$('.js-colorPicker').colorpicker({
      color: rgbaTemplate(color),
      format: 'rgba',
      container: true,
      horizontal: true,
      inline: true,
      customClass: 'ColorPicker',
      template: colorPickerTemplate(),
      slidersHorz: {
        saturation: {
          maxLeft: 206,
          maxTop: 95,
          callLeft: 'setSaturation',
          callTop: 'setBrightness'
        },
        hue: {
          maxLeft: 180,
          maxTop: 0,
          callLeft: 'setHue',
          callTop: false
        },
        alpha: {
          maxLeft: 180,
          maxTop: 0,
          callLeft: 'setAlpha',
          callTop: false
        }
      }
    });

    if (this.options.disableOpacity) {
      this.$('.js-colorPicker .js-alpha').addClass('is-hidden');
    }

    var setNewColor = function (e) {
      var rgb = e.color.toRGB();
      var hex = e.color.toHex();
      this.model.set({ hex: hex, r: rgb.r, g: rgb.g, b: rgb.b, opacity: rgb.a });
      this.$('.js-color').css('opacity', rgb.a);
    }.bind(this);

    this.$('.js-colorPicker').colorpicker().on('changeColor', setNewColor);

    this._initDocumentBinds();
    return this;
  },

  _initBinds: function () {
    this.model.bind('change:hex', this._onChangeColor, this);
    this.model.bind('change:r', this._onChangeColor, this);
    this.model.bind('change:g', this._onChangeColor, this);
    this.model.bind('change:b', this._onChangeColor, this);
    this.model.bind('change:opacity', this._onChangeColor, this);
  },

  _onEscapePressed: function (ev) {
    if (ev.which === ESCAPE_KEY_CODE) {
      this.remove();
    }
  },

{'fraction_non_alphanumeric': 0.174118884797475, 'fraction_numerical': 0.006137120813606874, 'mean_word_length': 2.5942028985507246, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6822055', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1824, 'n_words': 356}