<commit_before>{Brick} = require './brick'

#
# Defines the View class.
#
exports.View = class View extends Brick
  
  ############################################################## wOptions

  defaults:
    url: (v)-> 
      v.wQid()
    selector: (v)->
      "#" + v.wName()
    template: (v)->
      v._template ?= $.ajax(url: v.url(), async: false).responseText
    handler:
      'server'
    renderData: {}
    render: (v)->
      switch v.handler()
        when 'server'
          $.ajax(url: v.url(), async: false).responseText
        when 'mustache'
          v.mustacheRender()

  ############################################################## wInit

  wInit: (parent, name) ->
    this._normalize_autorefresh()
    super
  
  ############################################################## Public API

  refresh: =>
    sel = this.selector()
    $(sel).replaceWith(this.toString())

  toString: =>
    this.render().toString()

  ############################################################## Renderers

  wCallRenderer: (text, render)=>
    call = render(text)
    call += "()" unless call[call.length - 1] == ')'
    call = "function(){ this.#{call}; }"
    call = "$.wCall('#{this.wQid()}/..', #{call});"
    console.log "Rendering |#{call}|"
    call

  mustacheRender: =>
	  tpl = this.template()
	  data = this.renderData()
	  callRenderer = this.wCallRenderer
	  data.wCall = -> callRenderer
	  Mustache.to_html(tpl, data)

  ############################################################## Private API

  _normalize_autorefresh: ->
    ar = @options['autorefresh']
    ar = [] unless ar?<commit_msg>Mustache rendering of views is such that View's parent is automatically in scope.<commit_after>{Brick} = require './brick'

#
# Defines the View class.
#
exports.View = class View extends Brick
  
  ############################################################## wOptions

  defaults:
    url: (v)-> 
      v.wQid()
    selector: (v)->
      "#" + v.wName()
    template: (v)->
      v._template ?= $.ajax(url: v.url(), async: false).responseText
    handler:
      'server'
    renderData: {}
    render: (v)->
      switch v.handler()
        when 'server'
          $.ajax(url: v.url(), async: false).responseText
        when 'mustache'
          v.mustacheRender()

  ############################################################## wInit

  wInit: (parent, name) ->
    this._normalize_autorefresh()
    super
  
  ############################################################## Public API

  refresh: =>
    sel = this.selector()
    $(sel).replaceWith(this.toString())

  toString: =>
    this.render().toString()

  ############################################################## Renderers

  wCallRenderer: (text, render)=>
    call = render(text)
    call += "()" unless call[call.length - 1] == ')'
    call = "function(){ this.#{call}; }"
    call = "$.wCall('#{this.wQid()}/..', #{call});"
    call

  mustacheRender: =>
	  tpl = this.template()
	  callRenderer = this.wCallRenderer      
	  data = $.extend({}, this.wParent(), {wCall: -> callRenderer}, this.renderData())
	  Mustache.to_html(tpl, data)

  ############################################################## Private API

  _normalize_autorefresh: ->
    ar = @options['autorefresh']
    ar = [] unless ar?
{'fraction_non_alphanumeric': 0.3259416767922236, 'fraction_numerical': 0.0006075334143377885, 'mean_word_length': 3.3500660501981505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '4775339', 'n_tokens_mistral': 987, 'n_tokens_neox': 934, 'n_words': 202}