<commit_before>define (require) ->
  $ = require('jquery')
  _ = require('underscore')
  Backbone = require('backbone')
  settings = require('cs!settings')

  class Region
    constructor: (el, parent) ->
      @parent = parent
      @el = el

    show: (view) ->
      @empty()
      @append(view)

    append: (view) ->
      @appendAs('div', view)

    appendAs: (type, view) ->
      @$el = @parent.$el.find(@el)
      view.parent = @parent
      @views ?= []
      @views.push(view)
      view.setElement($("<#{type}>").appendTo(@$el)).render()

    empty: () ->
      _.each @views, (view) ->
        view.close()

      @$el?.empty()
      @$el = null
      @views = null

    close: () ->
      @empty()
      delete @[key] for key of @

  class Regions
    constructor: (regions = {}, $context) ->
      _.each _.keys(regions), (region) =>
        @[region] = new Region(regions[region], $context)

  return class BaseView extends Backbone.View
    initialize: () ->
      @_popovers = []
      @regions = new Regions(@regions, @)

    _renderDom: (data) ->
      @$el?.html(@template?(data) or @template)

    # Update page title
    updateTitle: () ->
      if @pageTitle
        document.title = settings.titlePrefix + @pageTitle

    _render: () ->
      data = @model?.toJSON() or @collection?.toJSON() or {}

      if typeof @templateHelpers is 'function'
        _.extend(data, @templateHelpers())
      else
        # Add data from template helpers to the model's data
        _.each @templateHelpers, (value, key) =>
          if typeof value is 'function'
            data[key] = value.apply(@)
          else
            data[key] = value

      @updateTitle()
      @_renderDom(data)

    render: () ->
      @onBeforeRender?()
      @detachPopovers()
      @_render()
      @onRender?()
      if @_rendered then @onDomRefresh?() else @_rendered = true

      return @

    close: () ->
      @onBeforeClose?()

      _.each @regions, (region) ->
        region.close()

      @detachPopovers()
      @remove()
      @unbind()
      delete @[key] for key of @
      return @

    detachPopovers: () ->
      @_popovers.pop().close() while @_popovers?.length

    attachPopover: (popover) ->
      @_popovers.push popover<commit_msg>Refactor to add a self-referential region to attach views to<commit_after>define (require) ->
  $ = require('jquery')
  _ = require('underscore')
  Backbone = require('backbone')
  settings = require('cs!settings')

  class Region
    constructor: (@el, @parent) ->

    show: (view) ->
      @empty()
      @append(view)

    append: (view) ->
      @appendAs('div', view)

    appendAs: (type, view) ->
      @$el = @parent.$el
      if @el
        @$el = @parent.$el.find(@el)
      view.parent = @parent
      @views ?= []
      @views.push(view)
      view.setElement($("<#{type}>").appendTo(@$el)).render()

    empty: () ->
      _.each @views, (view) ->
        view.close()

      @$el?.empty()
      @$el = null
      @views = null

    close: () ->
      @empty()
      delete @[key] for key of @

  class Regions
    constructor: (regions = {}, $context) ->
      _.each _.keys(regions), (region) =>
        @[region] = new Region(regions[region], $context)

      # Add a self-referential region to attach views to
      @self = new Region(null, $context)

  return class BaseView extends Backbone.View
    initialize: () ->
      @regions = new Regions(@regions, @)

    _renderDom: (data) ->
      @$el?.html(@template?(data) or @template)

    # Update page title
    updateTitle: () ->
      if @pageTitle
        document.title = settings.titlePrefix + @pageTitle

    _render: () ->
      data = @model?.toJSON() or @collection?.toJSON() or {}

      if typeof @templateHelpers is 'function'
        _.extend(data, @templateHelpers())
      else
        # Add data from template helpers to the model's data
        _.each @templateHelpers, (value, key) =>
          if typeof value is 'function'
            data[key] = value.apply(@)
          else
            data[key] = value

      @updateTitle()
      @_renderDom(data)

    render: () ->
      @onBeforeRender?()
      @_render()
      @onRender?()
      if @_rendered then @onDomRefresh?() else @_rendered = true

      return @

    close: () ->
      @onBeforeClose?()

      _.each @regions, (region) ->
        region.close()

      @remove()
      @unbind()
      delete @[key] for key of @
      return @
{'fraction_non_alphanumeric': 0.1581081081081081, 'fraction_numerical': 0.0, 'mean_word_length': 2.1563610518834397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '6807526', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1364, 'n_words': 335}