<commit_before>
if(typeof define !== 'function') {
  var define = require('amdefine')(module);
}

define(function(require) {
  var View = inServer ? require('../../lib/View') : require('View')
    , ConditionView = require('./ConditionView')
    , InputView = require('./InputView')
    , ElseView = require('./ElseView')
    , template = inServer ? content_appTmpls : require('contentTmpls')
    , _ = require('underscore');

  return View.extend({

    /**
     * Initializer
     *
     * @return {void}
     * @api public
     */

    initialize : function(model) {
      this.model = model;
      this._conditionViews = [];
      this._inputViews = [];
      if(inClient) {
        this._bindMethods();
      }
    },

    /**
     * Bind model
     *
     * @return {void}
     * @api public
     */

    bindModel : function() {
      this._bindConditionsAddition();
      this._bindConditionRemoval();
      this._bindInputAddition();
      this._bindElseAddition();
    },

    /**
     * Bind else addition
     *
     * @return {void}
     * @api private
     */

    _bindElseAddition : function() {
      var _this = this;
      this.model.on('change:else', function(_else) {
        this._elseView = new ElseView(_else.get('else'));
        _this.$('[data-row="0"]').after(this._elseView.render());
        this._elseView.setElement('[data-row="1"]');
        this._elseView.bindDOM();
      });
    },

    /**
     * Bind condition addition
     *
     * @return {void}
     * @api private
     */

    _bindConditionsAddition : function() {
      var _this = this;
      this.model.on('add:conditions', function(condition) {
        var view = new ConditionView(condition)
          , insertingRow = condition.get('row')
          , $condition;

        // We need a defer block because firstOperand and lastOperans relation
        // are not set yet.
        _.defer(function() {
          // If we insert a condition by choosing `and` or `or` in the `then` dropdown
          // We need to increase the row property on other conditions
          // and inputs. Otherwise some DOM bindings/event handling
          // will be wrong. We check that this is condition insertion from the `then`
          // dropdown by checking if inserting row is equals the `else` row minus 2
          if(insertingRow !== _this.model.get('else').get('row')) {
            _this.model.get('conditions').forEach(function(_condition) {
              var currentRow = _condition.get('row');
              if(currentRow >= insertingRow && condition.cid !== _condition.cid) {
                _condition.set('row', currentRow + 1);
              }
            });
            _this.model.get('inputs').forEach(function(input) {
              var currentRow = input.get('row');
              if(currentRow >= insertingRow) {
                input.set('row', currentRow + 1);
              }
            });
            var _else = _this.model.get('else');
            _else.set('row', _else.get('row') + 1);
            $condition = $(view.render());
          }
          else {
            if(insertingRow === 0) {
              _this.model.get('inputs').forEach(function(input) {
                var currentRow = input.get('row');
                if(currentRow >= insertingRow) {
                  input.set('row', currentRow + 1);
                }
              });
            }
            $condition = $(view.render()).addClass('invisible');
          }

          // bind conditions' DOM
          if(insertingRow > 0) {
            this.$('[data-row="' + (insertingRow - 1) + '"]').after($condition);
          }
          else {
            this.$('[data-row="1"]').before($condition);
          }

          var conditionSelector = '.condition[data-row="' + insertingRow + '"]';
          view.setElement(conditionSelector);
          view.bindDOM();
          view.firstOperandView.setElement(conditionSelector + ' .condition-first-operand');
          view.firstOperandView.bindDOM();
          view.lastOperandView.setElement(conditionSelector + ' .condition-last-operand');
          view.lastOperandView.bindDOM();
          _this._conditionViews.push(view);
        });
      });
    },

    /**
     * Bind condition removal
     *
     * @return {void}
     * @api private
     */

    _bindConditionRemoval : function() {
      var _this = this;
      this.model.on('remove:conditions', function(condition) {
        var row = condition.get('row');
        if(row === 0) {
          return;
        }
        _this.model.get('conditions').some(function(_condition) {
          if(_condition.get('row') === row - 1) {
            _this._removeCondition(condition);
            return true;
          }
          return false;
        });
        _this.model.get('inputs').some(function(input) {
          if(input.get('row') === row - 1) {

          }
          return false;
        });
      });
    },

    /**
     * Remove condition only. This method only removes the `condition` from
     * the DOM by calling `Backbone.View.prototype.remove`.
     *
     * @return {void}
     * @api private
     */

    _removeCondition : function(condition) {
      var _this = this;
      this._conditionViews.some(function(conditionView, index) {
        if(conditionView.model === condition) {
          conditionView.remove();
          // We remove the view pointer from TranslatioView`
          _this._conditionViews.splice(index, 1);
          return true;
        }
        return false;
      });
    },

    /**
     * Bind condition removal
     *
     * @return {void}
     * @api private
     */

    _bindInputAddition : function() {
      var _this = this;
      this.model.on('add:inputs', function(input) {
        var row = input.get('row')
          , view = new InputView(input);

        _this.$('[data-row="' + (row - 1) + '"]').after(view.render());
        view.setElement('[data-row="' + row + '"]');
        view.bindDOM();

        _this._inputViews.push(view);
      });
    },

    /**<commit_msg>Fixed small bug with removing conditionViews<commit_after>
if(typeof define !== 'function') {
  var define = require('amdefine')(module);
}

define(function(require) {
  var View = inServer ? require('../../lib/View') : require('View')
    , ConditionView = require('./ConditionView')
    , InputView = require('./InputView')
    , ElseView = require('./ElseView')
    , template = inServer ? content_appTmpls : require('contentTmpls')
    , _ = require('underscore');

  return View.extend({

    /**
     * Initializer
     *
     * @return {void}
     * @api public
     */

    initialize : function(model) {
      this.model = model;
      this._conditionViews = [];
      this._inputViews = [];
      if(inClient) {
        this._bindMethods();
      }
    },

    /**
     * Bind model
     *
     * @return {void}
     * @api public
     */

    bindModel : function() {
      this._bindConditionsAddition();
      this._bindConditionRemoval();
      this._bindInputAddition();
      this._bindElseAddition();
    },

    /**
     * Bind else addition
     *
     * @return {void}
     * @api private
     */

    _bindElseAddition : function() {
      var _this = this;
      this.model.on('change:else', function(_else) {
        this._elseView = new ElseView(_else.get('else'));
        _this.$('[data-row="0"]').after(this._elseView.render());
        this._elseView.setElement('[data-row="1"]');
        this._elseView.bindDOM();
      });
    },

    /**
     * Bind condition addition
     *
     * @return {void}
     * @api private
     */

    _bindConditionsAddition : function() {
      var _this = this;
      this.model.on('add:conditions', function(condition) {
        var view = new ConditionView(condition)
          , insertingRow = condition.get('row')
          , $condition;

        // We need a defer block because firstOperand and lastOperans relation
        // are not set yet.
        _.defer(function() {
          // If we insert a condition by choosing `and` or `or` in the `then` dropdown
          // We need to increase the row property on other conditions
          // and inputs. Otherwise some DOM bindings/event handling
          // will be wrong. We check that this is condition insertion from the `then`
          // dropdown by checking if inserting row is equals the `else` row minus 2
          if(insertingRow !== _this.model.get('else').get('row')) {
            _this.model.get('conditions').forEach(function(_condition) {
              var currentRow = _condition.get('row');
              if(currentRow >= insertingRow && condition.cid !== _condition.cid) {
                _condition.set('row', currentRow + 1);
              }
            });
            _this.model.get('inputs').forEach(function(input) {
              var currentRow = input.get('row');
              if(currentRow >= insertingRow) {
                input.set('row', currentRow + 1);
              }
            });
            var _else = _this.model.get('else');
            _else.set('row', _else.get('row') + 1);
            $condition = $(view.render());
          }
          else {
            if(insertingRow === 0) {
              _this.model.get('inputs').forEach(function(input) {
                var currentRow = input.get('row');
                if(currentRow >= insertingRow) {
                  input.set('row', currentRow + 1);
                }
              });
            }
            $condition = $(view.render()).addClass('invisible');
          }

          // bind conditions' DOM
          if(insertingRow > 0) {
            this.$('[data-row="' + (insertingRow - 1) + '"]').after($condition);
          }
          else {
            this.$('[data-row="1"]').before($condition);
          }

          var conditionSelector = '.condition[data-row="' + insertingRow + '"]';
          view.setElement(conditionSelector);
          view.bindDOM();
          view.firstOperandView.setElement(conditionSelector + ' .condition-first-operand');
          view.firstOperandView.bindDOM();
          view.lastOperandView.setElement(conditionSelector + ' .condition-last-operand');
          view.lastOperandView.bindDOM();
          _this._conditionViews.push(view);
        });
      });
    },

    /**
     * Bind condition removal
     *
     * @return {void}
     * @api private
     */

    _bindConditionRemoval : function() {
      var _this = this;
      this.model.on('remove:conditions', function(condition) {
        var row = condition.get('row');
        if(row === 0) {
          _this._removeConditionAndInputs(condition);
          return;
        }
        _this.model.get('conditions').some(function(_condition) {
          if(_condition.get('row') === row - 1) {
            _this._removeSingleCondition(condition);
            return true;
          }
          return false;
        });
        _this.model.get('inputs').some(function(input) {
          if(input.get('row') === row - 1) {
            _this._removeConditionAndInputs(condition);
          }
          return false;
        });
      });
    },

    /**
     * Remove condition only. This method only removes the `condition` from
     * the DOM by calling `Backbone.View.prototype.remove`.
     *
     * @return {void}
     * @api private
     */

    _removeSingleCondition : function(condition) {
      var _this = this, removalIndex, removalRow;
      this._conditionViews.forEach(function(conditionView, index) {
        if(conditionView.model === condition) {
          conditionView.remove();
          // We remove the view pointer from TranslatioView`
          removalRow = conditionView.model.get('row');
          removalIndex = index;
        }

        // We must decrease the row index on all subsequent conditions
        if(removalIndex && index > removalIndex) {
          conditionView.model.set('row', conditionView.model.get('row') - 1);
        }
      });

      if(removalRow) {
        this._inputViews.forEach(function(inputView) {
          var row = inputView.model.get('row');
          if(row > removalRow) {
            inputView.model.set('row', row - 1);
          }
        });

        if(this._elseView) {
          this._elseView.model.set('row', this._elseView.model.get('row') - 1);
        }

        _this._conditionViews.splice(removalIndex, 1);
      }
    },

    /**
     * Remove condition only. This method only removes the `condition` from
     * the DOM by calling `Backbone.View.prototype.remove`.
     *
     * @return {void}
     * @api private
     */

    _removeConditionElseAndInputs : function() {
      this.model.else.destroy();

    },

    /**
     * Bind condition removal
     *
     * @return {void}
     * @api private
     */

    _bindInputAddition : function() {
      var _this = this;
      this.model.on('add:inputs', function(input) {
        var row = input.get('row')
          , view = new InputView(input);

        _this.$('[data-row="' + (row - 1) + '"]').after(view.render());
        view.setElement('[data-row="' + row + '"]');
        view.bindDOM();

        _this._inputViews.push(view);
      });
    },

    /**
{'fraction_non_alphanumeric': 0.14554846551200243, 'fraction_numerical': 0.0026587663324217563, 'mean_word_length': 1.8946789797713282, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '7313415', 'n_tokens_mistral': 3769, 'n_tokens_neox': 3632, 'n_words': 735}