<commit_before>(function($, Conditions) {
  var DEFAULT_CONDITION = {condition_type: 'false'};
  var SPINNER_ICON = '../img/spinner.svg';

  var $errorBox;

  // Controls for building a switch
  var SwitchBuilder = function(initial, admin) {
    var self = this;

    initial = initial || {};
    self.name = initial.name || 'New switch';
    self.condition = initial.condition || {switch_type: 'false'};
    self.initialCondition = self.condition;
    self.admin = admin;
    self.deleted = false;

    self.baseUrl = self.admin.baseUrl;
    self.displayError = self.admin.displayError;
    self.displaySuccess = self.admin.displaySuccess;

    self.isUnsaved = function() {
      if (self.deleted) {
        return false;
      }

      if (!self.widget) {
        return false;
      }

      if (!self.widget.clean()) {
        return true;
      }

      return (
        JSON.stringify(self.widget.buildJSON()) !==
        JSON.stringify(self.condition)
      );
    };

    self.save = function() {
      var $save = self.$controls.find('[data-action="save"]');
      $save.attr('disabled', true);

      // Update the JSON in the textarea from the GUI components
      if (!self.widget.clean()) {
        self.displayError(
          self.widget.error || 'Unspecified validation failure',
          'Form validation error'
        );
        $save.attr('disabled', false);
        return;
      }
      var condition = self.widget.buildJSON();

      var success = function() {
        $save.attr('disabled', false);
        self.condition = condition;
        self.reset();

        self.displaySuccess(
          'Switch <span class="switch-name">' + self.name + '</span> successfully updated.',
          'Switch saved'
        );
      };
      var err = function() {
        $save.attr('disabled', false);
      };
      self.updateSwitch(condition, success, err);
    };

    self.render = function() {
      var $controls = $('<div>').addClass('switch').attr('data-name', self.name);
      $controls.append($('<span>').addClass('name').text(self.name));
      $controls.append(self.renderGui(self.condition));

      var $persistControls = $('<div>').addClass('persist');
      var $saveButton = $('<button>')
        .text('Save')
        .addClass('save')
        .attr('data-action', 'save')
        .click(self.save);

      var $cancelButton = $('<button>').text('Cancel').addClass('cancel');
      $cancelButton.click(function() {
        self.reset();
      });

      var $deleteButton = $('<button>').text('Delete').addClass('delete');
      $deleteButton.click(function() {
        if(!confirm('Are you sure you want to delete the ' + self.name + ' switch?')) {
          return;
        }

        self.deleteSwitch();
      });

      $persistControls.append($saveButton).append($cancelButton).append($deleteButton);
      $controls.append($persistControls);

      self.$controls = $controls;
      return self.$controls;
    };

    self.reset = function() {
      self.$controls.find('.switch-builder').replaceWith(self.renderGui(self.condition));
    };

    self.renderGui = function(initial) {
      var $builder = $('<div>').addClass('switch-builder');
      var $select = Conditions.baseConditions.asOptions().addClass(
        'main-condition-selector'
      );
      var $stage = $('<div>').addClass('stage');

      var renderWidget = function(type, data) {
        if (!type) {
          return;
        }

        var previousWidget = self.widget;
        self.widget = new Conditions.createCondition(type);

        if (previousWidget && type === 'raw' && previousWidget.clean()) {
          self.widget.init(previousWidget.buildJSON());
        }

        var $condForm = data ? self.widget.init(data) : self.widget.renderForm();
        $stage.html($condForm);
      };

      $select.on('change', function() {
        renderWidget($(this).val());
      });

      renderWidget(
        Conditions.conditionTypes[initial.condition_type] || initial.condition_type,
        initial
      );
      $builder.html($select).append($stage);
      return $builder;
    };

    self.updateSwitch = function(condition, success, err) {
      success = success || function() {};
      err = err || function() {};

      $.ajax({
        url: self.baseUrl + 'switch/' + self.name + '/',
        type: 'POST',
        data: JSON.stringify(self.condition),
        dataType: 'json',
        contentType: 'application/json',
        success: success,
        error: function(resp) {
          self.displayError(resp.responseText, resp.status + ' ' + resp.statusText);
          err();
        }
      });
    };

    self.deleteSwitch = function() {
      $.ajax({
        url: self.baseUrl + 'switch/' + self.name + '/',
        type: 'DELETE',
        success: function() {
          self.deleted = true;
          self.admin.switchDeleted(self.name);
        },
        error: function(resp) {
          self.displayError(resp.responseText, resp.status + ' ' + resp.statusText);
        }
      });
    };
<commit_msg>HOTFIX: updateSwitch: condition, not self.<commit_after>(function($, Conditions) {
  var DEFAULT_CONDITION = {condition_type: 'false'};
  var SPINNER_ICON = '../img/spinner.svg';

  var $errorBox;

  // Controls for building a switch
  var SwitchBuilder = function(initial, admin) {
    var self = this;

    initial = initial || {};
    self.name = initial.name || 'New switch';
    self.condition = initial.condition || {switch_type: 'false'};
    self.initialCondition = self.condition;
    self.admin = admin;
    self.deleted = false;

    self.baseUrl = self.admin.baseUrl;
    self.displayError = self.admin.displayError;
    self.displaySuccess = self.admin.displaySuccess;

    self.isUnsaved = function() {
      if (self.deleted) {
        return false;
      }

      if (!self.widget) {
        return false;
      }

      if (!self.widget.clean()) {
        return true;
      }

      return (
        JSON.stringify(self.widget.buildJSON()) !==
        JSON.stringify(self.condition)
      );
    };

    self.save = function() {
      var $save = self.$controls.find('[data-action="save"]');
      $save.attr('disabled', true);

      // Update the JSON in the textarea from the GUI components
      if (!self.widget.clean()) {
        self.displayError(
          self.widget.error || 'Unspecified validation failure',
          'Form validation error'
        );
        $save.attr('disabled', false);
        return;
      }
      var condition = self.widget.buildJSON();

      var success = function() {
        $save.attr('disabled', false);
        self.condition = condition;
        self.reset();

        self.displaySuccess(
          'Switch <span class="switch-name">' + self.name + '</span> successfully updated.',
          'Switch saved'
        );
      };
      var err = function() {
        $save.attr('disabled', false);
      };
      self.updateSwitch(condition, success, err);
    };

    self.render = function() {
      var $controls = $('<div>').addClass('switch').attr('data-name', self.name);
      $controls.append($('<span>').addClass('name').text(self.name));
      $controls.append(self.renderGui(self.condition));

      var $persistControls = $('<div>').addClass('persist');
      var $saveButton = $('<button>')
        .text('Save')
        .addClass('save')
        .attr('data-action', 'save')
        .click(self.save);

      var $cancelButton = $('<button>').text('Cancel').addClass('cancel');
      $cancelButton.click(function() {
        self.reset();
      });

      var $deleteButton = $('<button>').text('Delete').addClass('delete');
      $deleteButton.click(function() {
        if(!confirm('Are you sure you want to delete the ' + self.name + ' switch?')) {
          return;
        }

        self.deleteSwitch();
      });

      $persistControls.append($saveButton).append($cancelButton).append($deleteButton);
      $controls.append($persistControls);

      self.$controls = $controls;
      return self.$controls;
    };

    self.reset = function() {
      self.$controls.find('.switch-builder').replaceWith(self.renderGui(self.condition));
    };

    self.renderGui = function(initial) {
      var $builder = $('<div>').addClass('switch-builder');
      var $select = Conditions.baseConditions.asOptions().addClass(
        'main-condition-selector'
      );
      var $stage = $('<div>').addClass('stage');

      var renderWidget = function(type, data) {
        if (!type) {
          return;
        }

        var previousWidget = self.widget;
        self.widget = new Conditions.createCondition(type);

        if (previousWidget && type === 'raw' && previousWidget.clean()) {
          self.widget.init(previousWidget.buildJSON());
        }

        var $condForm = data ? self.widget.init(data) : self.widget.renderForm();
        $stage.html($condForm);
      };

      $select.on('change', function() {
        renderWidget($(this).val());
      });

      renderWidget(
        Conditions.conditionTypes[initial.condition_type] || initial.condition_type,
        initial
      );
      $builder.html($select).append($stage);
      return $builder;
    };

    self.updateSwitch = function(condition, success, err) {
      success = success || function() {};
      err = err || function() {};

      $.ajax({
        url: self.baseUrl + 'switch/' + self.name + '/',
        type: 'POST',
        data: JSON.stringify(condition),
        dataType: 'json',
        contentType: 'application/json',
        success: success,
        error: function(resp) {
          self.displayError(resp.responseText, resp.status + ' ' + resp.statusText);
          err();
        }
      });
    };

    self.deleteSwitch = function() {
      $.ajax({
        url: self.baseUrl + 'switch/' + self.name + '/',
        type: 'DELETE',
        success: function() {
          self.deleted = true;
          self.admin.switchDeleted(self.name);
        },
        error: function(resp) {
          self.displayError(resp.responseText, resp.status + ' ' + resp.statusText);
        }
      });
    };

{'fraction_non_alphanumeric': 0.1623517786561265, 'fraction_numerical': 0.0, 'mean_word_length': 2.310762185148839, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '2085460', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2832, 'n_words': 538}