<commit_before>'use strict';

var task = task || {};

(function() {
  task.NodesView = Backbone.View.extend({
    initialize: function(options) {
      this.collection = options.collection;
      this.level = options.level || 0;
      this.nodeViews = [];

      this.listenTo(this.collection, 'add', this.renderNode);
    },

    render: function() {
      this.collection.each(function(node) {
        this.listenTo(node, 'change:nodes', this.renderSubNodesNew);
        this.renderNode(node);

        var nodes = node.get('nodes');
        if (!_.isEmpty(nodes)) {
          var nodesView = new task.NodesView({
            collection: nodes,
            level: this.level + 1
          });
          this.$el.append(nodesView.render().el);
        }
      }, this);

      this.renderEmptyNode();

      return this;
    },

    renderNode: function(node) {
      var nodeView = new task.NodeView({ model: node, level: this.level });
      this.nodeViews.push(nodeView);
      this.$el.append(nodeView.render().el);
    },

    renderEmptyNode: function() {
      var node = new task.Node({ isEmpty: true });
      this.emptyNodeView = new task.NodeView({ model: node, level: this.level });
      this.$el.append(this.emptyNodeView.render().el);

      this.listenTo(node, 'change:name', this.nodeAdded);
    },

    renderSubNodesNew: function(node) {
      var nodesView = new task.NodesView({ collection: node.get('nodes'), level: this.level + 1 });
      var index = this.collection.indexOf(node);
      this.nodeViews[index].$el.after(nodesView.render().el);
    },

    nodeAdded: function(node) {
      if (!_.isEmpty(node.get('name'))) {
        this.stopListening(node);
        this.emptyNodeView.$el.detach();

        node.set('isEmpty', false);
        this.collection.add(node);

        this.renderEmptyNode(this.level);
      }
    }
  });
})();<commit_msg>Fix issue when newly added node doesn't create subnodes.<commit_after>'use strict';

var task = task || {};

(function() {
  task.NodesView = Backbone.View.extend({
    initialize: function(options) {
      this.collection = options.collection;
      this.level = options.level || 0;
      this.nodeViews = [];

      this.listenTo(this.collection, 'add', this.renderNode);
    },

    render: function() {
      this.collection.each(function(node) {
        this.listenTo(node, 'change:nodes', this.renderSubNodesNew);
        this.renderNode(node);

        var nodes = node.get('nodes');
        if (!_.isEmpty(nodes)) {
          var nodesView = new task.NodesView({
            collection: nodes,
            level: this.level + 1
          });
          this.$el.append(nodesView.render().el);
        }
      }, this);

      this.renderEmptyNode();

      return this;
    },

    renderNode: function(node) {
      var nodeView = new task.NodeView({ model: node, level: this.level });
      this.nodeViews.push(nodeView);
      this.$el.append(nodeView.render().el);
    },

    renderEmptyNode: function() {
      var node = new task.Node({ isEmpty: true });
      this.emptyNodeView = new task.NodeView({ model: node, level: this.level });
      this.$el.append(this.emptyNodeView.render().el);

      this.listenTo(node, 'change:name', this.nodeAdded);
    },

    renderSubNodesNew: function(node) {
      var nodesView = new task.NodesView({ collection: node.get('nodes'), level: this.level + 1 });
      var index = this.collection.indexOf(node);
      this.nodeViews[index].$el.after(nodesView.render().el);
    },

    nodeAdded: function(node) {
      if (!_.isEmpty(node.get('name'))) {
        this.stopListening(node);
        this.emptyNodeView.$el.detach();

        node.set('isEmpty', false);
        this.collection.add(node);
        this.listenTo(node, 'change:nodes', this.renderSubNodesNew);

        this.renderEmptyNode(this.level);
      }
    }
  });
})();
{'fraction_non_alphanumeric': 0.15822130299896586, 'fraction_numerical': 0.0015511892450879006, 'mean_word_length': 2.5790934320074004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3484676', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1149, 'n_words': 213}