<commit_before>/*jslint nomen: true */
/*globals define: true */
/*globals console: true */

define([
  'jquery',
  'lib/lodash',
  'backbone',

  // LocalData
  'settings',
  'api',

  'views/forms'
],

function($, _, Backbone, settings, api, FormViews) {
  'use strict';

  var Builder = {};

  Builder.BuilderView = Backbone.View.extend({

    el: '#builder-view-container',

    boxAnswersByQuestionId: {},
    formQuestions: $('#editor'),
    questionsByParentId: {},
    repeatCounter: {},
    templates: {
      question: _.template($('#question-edit-template').html()),
      answer: _.template($('#answer-edit-template').html())
    },

    initialize: function(options) {
      _.bindAll(this, 'render', 'save', 'done', 'makeBlankQuestion', 'renderForm',
        'suffix', 'editQuestion', 'editQuestionType', 'deleteQuestion',
        'createQuestion', 'createAnswer', 'addSubQuestion', 'renderQuestion',
         'editAnswer', 'deleteAnswer', 'slugify', 'updatePreview');
      
      this.forms = options.forms;
    },

    render: function() {
      this.renderForm();
      this.updatePreview();
    },

    save: function(event) {
      event.preventDefault();
      console.log('Save clicked');

      api.createForm(settings.formData, function(){
        console.log('Form successfully saved');
        $(".saved").fadeIn().css("display","inline-block").delay(2000).fadeOut();
      });
    },

    done: function(event) {
      event.preventDefault();
      console.log('Done editing');
      this.trigger("done");
    },

    // The blank question template.
    makeBlankQuestion: function(){
      return {
        "text": "",
        "value": "",
        "answers": [
          {
            "text": "",
            "value": ""
          },
          {
            "text": "",
            "value": ""
          }
        ]
      };
    },

    renderForm: function() {
      // Render the editor template
      $(this.el).html(_.template($('#form-editor-template').html(), {}));

      // Clear out the editor, just in case.
      this.formQuestions = $('#editor');
      this.formQuestions.html('');

      // Handle save events
      $(this.el).find('.save').click(this.save);
      $(this.el).find('.done').click(this.done);

      // Default to a blank question if the form is empty
      if (settings.formData === undefined) {
        settings.formData = {};
        settings.formData.questions = [];
        settings.formData.questions.push(this.makeBlankQuestion());
      }

      // Render form
      _.each(settings.formData.questions, function (question, questionIndex) {
        this.renderQuestion(question, undefined, undefined, undefined, undefined, questionIndex, undefined);

        // last param was settings.formData.questions
      }, this);<commit_msg>Handle creation/deletion of top-level questions<commit_after>/*jslint nomen: true */
/*globals define: true */
/*globals console: true */

define([
  'jquery',
  'lib/lodash',
  'backbone',

  // LocalData
  'settings',
  'api',

  'views/forms'
],

function($, _, Backbone, settings, api, FormViews) {
  'use strict';

  var Builder = {};

  Builder.BuilderView = Backbone.View.extend({

    el: '#builder-view-container',

    boxAnswersByQuestionId: {},
    formQuestions: $('#editor'),
    questionsByParentId: {},
    repeatCounter: {},
    templates: {
      question: _.template($('#question-edit-template').html()),
      answer: _.template($('#answer-edit-template').html())
    },

    initialize: function(options) {
      _.bindAll(this, 'render', 'save', 'done', 'makeBlankQuestion', 'renderForm',
        'suffix', 'editQuestion', 'editQuestionType', 'deleteQuestion',
        'createQuestion', 'createAnswer', 'addSubQuestion', 'renderQuestion',
         'editAnswer', 'deleteAnswer', 'slugify', 'updatePreview');
      
      this.forms = options.forms;
    },

    render: function() {
      this.renderForm();
      this.updatePreview();
    },

    save: function(event) {
      event.preventDefault();
      console.log('Save clicked');

      api.createForm(settings.formData, function(){
        console.log('Form successfully saved');
        $(".saved").fadeIn().css("display","inline-block").delay(2000).fadeOut();
      });
    },

    done: function(event) {
      event.preventDefault();
      console.log('Done editing');
      this.trigger("done");
    },

    // The blank question template.
    makeBlankQuestion: function(){
      return {
        "text": "",
        "value": "",
        "answers": [
          {
            "text": "",
            "value": ""
          },
          {
            "text": "",
            "value": ""
          }
        ]
      };
    },

    renderForm: function() {
      // Render the editor template
      $(this.el).html(_.template($('#form-editor-template').html(), {}));

      // Clear out the editor, just in case.
      this.formQuestions = $('#editor');
      this.formQuestions.html('');

      // Handle save events
      $(this.el).find('.save').click(this.save);
      $(this.el).find('.done').click(this.done);

      // Default to a blank question if the form is empty
      if (settings.formData === undefined) {
        settings.formData = {};
        settings.formData.questions = [];
        settings.formData.questions.push(this.makeBlankQuestion());
      }

      // Render form
      _.each(settings.formData.questions, function (question, questionIndex) {
        this.renderQuestion(question, undefined, undefined, undefined, undefined, questionIndex, settings.formData.questions);

        // last param was settings.formData.questions
      }, this);
{'fraction_non_alphanumeric': 0.17192484934420418, 'fraction_numerical': 0.0014179369018078695, 'mean_word_length': 2.507147296457427, 'pattern_counts': {'":': 14, '<': 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': '6771886', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1570, 'n_words': 309}