<commit_before>###
This file provides the "SurveyApp" object which is an extension of
Backbone.View and builds the XL(S)Form Editor in the browser.
###

class XlformError extends Error
  constructor: (@message)->
    @name = "XlformError"

# $().editInPlace seems to depend on $.browser
$.browser || $.browser = {}

class XlfDetailView extends Backbone.View
  ###
  The XlfDetailView class is a base class for details
  of each row of the XLForm. When the view is initialized,
  a mixin from "DetailViewMixins" is applied.
  ###
  className: "detail-view"
  initialize: ({@rowView})->
    unless @model.key
      throw new XlformError "RowDetail does not have key"
    if (viewMixin = DetailViewMixins[@model.key])
      _.extend(@, viewMixin)

  render: ()->
    rendered = @html()
    unless rendered is @$el or rendered is @el
      @$el.html rendered
    @
  html: ()->
    """
    <code>#{@model.key}:</code>
    <code>#{@model.get("value")}</code>
    """
  insertInDOM: (rowView)->
    rowView.$el.append(@el)

  renderInRowView: (rowView)->
    @render()
    @afterRender && @afterRender()
    @insertInDOM(rowView)
    @

class XlfRowView extends Backbone.View
  tagName: "li"
  initialize: ()->
    @model.on "change", @render, @
    typeDetail = @model.get("type")
    typeDetail.on "change:value", _.bind(@render, @)
    typeDetail.on "change:listName", _.bind(@render, @)
  render: ->
    @$el.html """
      <a href="#" class="delete-row" data-row-cid="#{@model.cid}" title="drc#{@model.cid}" txitle="This will remove the question from the survey">&times;</a>
      <div class="post-row-buttons">-<button class="insert-row">+</button>-</div>
    """
    @$el.data("row-model-id", @model.cid)

    for [key, val] in @model.attributesArray()
      v = new XlfDetailView(model: val, rowView: @).renderInRowView(@)
    @
  newListView: (rv)->
    lv = new XLF.EditListView(choiceList: new XLF.ChoiceList(), survey: @model._parent, rowView: @)
    $lvel = lv.render().$el.css @$(".select-list").position()
    @$el.append $lvel

class @SurveyApp extends Backbone.View
  className: "formbuilder-wrap"
  events:
    "click .delete-row": "clickRemoveRow"
    "click .insert-row": "clickInsertRow"
    "click .edit-list": "editList"
    "click #add-question": "addNewRow"
    "click #publish-survey": "publishButtonClick"

  initialize: (options)->
    @survey = new XLF.Survey(options)

    @survey.rows.on "add", @reset, @
    @survey.rows.on "reset", @reset, @
    @survey.on "change", @reset, @

    @onPublish = options.publish || $.noop

  render: ()->
    @$el.html """
      <div class="stats">
        <h1 class="form_title"></h1>
        <h2 class="description"></h2>
        <div id="survey-stats"></div>
      </div>
      <div class="form-editor-wrap">
        <ul class="-form-editor">
          <li class="loading"><span>Loading...</span></li>
        </ul>
        <div class="trailing-buttons">
          <button id="add-question" class="attached"><span class="glyphicon-circle-plus"></span> Add question</button>
          <button id="publish-survey">Preview</button>
        </div>
      </div>
      <div class="stats row-details" id="additional-options">&nbsp;</div>
    """
    @formEditorEl = @$(".-form-editor")

    addOpts      = @$("#additional-options")

    # watch for changes on the title and description
    for _s in ["form_title", "description"]
      do (setting=_s)=>
        elem = @$(".#{setting}")
        elem.html es  if (es = @survey.settings.get(setting))
        eip =
          callback: (u, ent)=>
            @survey.settings.set setting, ent
            ent
        elem.editInPlace eip

    @survey.rows.trigger("reset")

    for detail in @survey.surveyDetails.models
      addOpts.append((new XlfSurveyDetailView(model: detail)).render().el)
    @
  reset: ->
    fe = @formEditorEl.empty()
    @survey.forEachRow (row)->
      fe.append(new XlfRowView(model: row).render().el)
  clickRemoveRow: (evt)->
    evt.preventDefault()
    if confirm("Are you sure you want to delete this question? This action cannot be undone.")<commit_msg>dealing with disappearing edit boxes<commit_after>###
This file provides the "SurveyApp" object which is an extension of
Backbone.View and builds the XL(S)Form Editor in the browser.
###

class XlformError extends Error
  constructor: (@message)->
    @name = "XlformError"

# $().editInPlace seems to depend on $.browser
$.browser || $.browser = {}

class XlfDetailView extends Backbone.View
  ###
  The XlfDetailView class is a base class for details
  of each row of the XLForm. When the view is initialized,
  a mixin from "DetailViewMixins" is applied.
  ###
  className: "detail-view"
  initialize: ({@rowView})->
    unless @model.key
      throw new XlformError "RowDetail does not have key"
    if (viewMixin = DetailViewMixins[@model.key])
      _.extend(@, viewMixin)

  render: ()->
    rendered = @html()
    unless rendered is @$el or rendered is @el
      @$el.html rendered
    @
  html: ()->
    """
    <code>#{@model.key}:</code>
    <code>#{@model.get("value")}</code>
    """
  insertInDOM: (rowView)->
    rowView.$el.append(@el)

  renderInRowView: (rowView)->
    @render()
    @afterRender && @afterRender()
    @insertInDOM(rowView)
    @

class XlfRowView extends Backbone.View
  tagName: "li"
  initialize: ()->
    @model.on "change", @render, @
    typeDetail = @model.get("type")
    typeDetail.on "change:value", _.bind(@render, @)
    typeDetail.on "change:listName", _.bind(@render, @)
  render: ->
    @$el.html """
      <a href="#" class="delete-row" data-row-cid="#{@model.cid}" title="drc#{@model.cid}" txitle="This will remove the question from the survey">&times;</a>
      <div class="post-row-buttons">-<button class="insert-row">+</button>-</div>
    """
    @$el.data("row-model-id", @model.cid)

    for [key, val] in @model.attributesArray()
      v = new XlfDetailView(model: val, rowView: @).renderInRowView(@)
    @
  newListView: (rv)->
    lv = new XLF.EditListView(choiceList: new XLF.ChoiceList(), survey: @model._parent, rowView: @)
    $lvel = lv.render().$el.css @$(".select-list").position()
    @$el.append $lvel

class @SurveyApp extends Backbone.View
  className: "formbuilder-wrap"
  events:
    "click .delete-row": "clickRemoveRow"
    "click .insert-row": "clickInsertRow"
    "click .edit-list": "editList"
    "click #add-question": "addNewRow"
    "click #publish-survey": "publishButtonClick"

  initialize: (options)->
    @survey = new XLF.Survey(options)

    @survey.rows.on "add", @reset, @
    @survey.rows.on "reset", @reset, @
    @survey.on "change", @reset, @

    @onPublish = options.publish || $.noop

  render: ()->
    @$el.html """
      <div class="stats">
        <h1 class="form_title"></h1>
        <h2 class="description"></h2>
        <div id="survey-stats"></div>
      </div>
      <div class="form-editor-wrap">
        <ul class="-form-editor">
          <li class="loading"><span>Loading...</span></li>
        </ul>
        <div class="trailing-buttons">
          <button id="add-question" class="attached"><span class="glyphicon-circle-plus"></span> Add question</button>
          <button id="publish-survey">Preview</button>
        </div>
      </div>
      <div class="stats row-details" id="additional-options">&nbsp;</div>
    """
    @formEditorEl = @$(".-form-editor")

    addOpts      = @$("#additional-options")

    # watch for changes on the title and description
    do =>
      setting = "form_title"
      elem = @$(".#{setting}")
      if (es = @survey.settings.get(setting))
        elem.html es 
      eip =
        default_text: "New survey"
        callback: (u, ent)=>
          @survey.settings.set setting, ent
          ent
      elem.editInPlace eip
    do =>
      setting = "description"
      elem = @$(".description")
      default_text = "[survey description]"
      if (es = @survey.settings.get(setting))
        elem.html es
      eip =
        save_if_nothing_changed: true
        default_text: default_text
        callback: (u, ent)=>
          if ent is ""
            default_text
          else
            @survey.settings.set setting, ent
            ent
      elem.editInPlace eip

    @survey.rows.trigger("reset")

    for detail in @survey.surveyDetails.models
      addOpts.append((new XlfSurveyDetailView(model: detail)).render().el)
    @
  reset: ->
    fe = @formEditorEl.empty()
    @survey.forEachRow (row)->
      fe.append(new XlfRowView(model: row).render().el)
  clickRemoveRow: (evt)->
    evt.preventDefault()
    if confirm("Are you sure you want to delete this question? This action cannot be undone.")
{'fraction_non_alphanumeric': 0.16651259533163854, 'fraction_numerical': 0.0009244280101687081, 'mean_word_length': 2.930517711171662, 'pattern_counts': {'":': 10, '<': 75, '<?xml version=': 0, '>': 109, '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': '927797', 'n_tokens_mistral': 2859, 'n_tokens_neox': 2827, 'n_words': 672}