<commit_before>
# Class which represent Views for Application
# Fluent interface for manipulate views
#
# @note By default this function mark elements with id
#
# @example:
#
#   myView = new Sirius.View("body", (content) -> "<div>#{content}</div>"
#   # in controller
#   myView.render(results_from_ajax_to_html).swap() # change body content
#
#   myView.clear().render('results')
#   myListView.render("<li>new element</li>").append()
#   myTableView.render("<tr><td>top</td></tr>").prepend()
#
class Sirius.View

  name: () -> 'View' # define name, because not work in IE: constructor.name

  # @param [String] - selector for element
  # @param [Function] - transform function for new content
  #
  constructor: (@element, clb = (txt) -> txt) ->
    @_result_fn = (args...) =>
      clb.apply(null, args...)

  render: (args...) ->
    @_result = @_result_fn(args)
    @

  # swap content for given element
  # @return null
  swap: (attributes...) ->

    real_attributes = for a in attributes when a != null then a
    if real_attributes.length == 0
      Sirius.Application.adapter.swap(@element, @_result)
    else
      for attr in real_attributes
        Sirius.Application.adapter.set_attr(@element, attr, @_result)
    null

  # append to current element new content in bottom
  # @return null
  append: () ->
    Sirius.Application.adapter.append(@element, @_result)
    null

  # prepend to current element new content in top
  prepend: () ->
    Sirius.Application.adapter.prepend(@element, @_result)
    null

  # clear element content
  clear: () ->
    Sirius.Application.adapter.clear(@element)
    @


  #
  # when we have:
  # ### 1 View to View relation with change text
  #   view1.bind(view2)
  # if view1 element have onchange event then we use this event
  # if view1 element does not have onchange event we should use Dom level 3\4 events see #observer.coffee
  #
  # ### 2 View to View relation with change attributes in View2
  #
  # for view1 we should use Dom level 3\4 events
  # also we should know which attributes changed (filter), therefore
  # view1.bind(view2, to: ["id"])
  # when we change text in view1, then should changes in view2 id attribute
  #
  # ### 3 View to View relation change attributes in View1
  #
  # view1.bind(view2, from: ["id"])
  #
  # when change id in view1, we should change text in view2
  #
  # ### 4 Combination of 3 and 4
  # view1.bind(view2, from: ["id"], to: ["class"]<commit_msg>refactor code, add _apply_strategy<commit_after>
# Class which represent Views for Application
# Fluent interface for manipulate views
#
# @note By default this function mark elements with id
#
# @example:
#
#   myView = new Sirius.View("body", (content) -> "<div>#{content}</div>"
#   # in controller
#   myView.render(results_from_ajax_to_html).swap() # change body content
#
#   myView.clear().render('results')
#   myListView.render("<li>new element</li>").append()
#   myTableView.render("<tr><td>top</td></tr>").prepend()
#
class Sirius.View

  name: () -> 'View' # define name, because not work in IE: constructor.name

  # @param [String] - selector for element
  # @param [Function] - transform function for new content
  #
  constructor: (@element, clb = (txt) -> txt) ->
    @_result_fn = (args...) =>
      clb.apply(null, args...)

  render: (args...) ->
    @_result = @_result_fn(args)
    @

  # swap content for given element
  # @return null
  swap: (attributes...) ->
    @_apply_strategy
      attributes: for a in attributes when a != null then a
      name : 'swap'
      transform: (old, result) -> "#{result}"

  # append to current element new content in bottom
  # @param [Array] list of attributes for append new value
  # when attributes is empty(is default for text|value) then only append
  # @return null
  append: (attributes...) ->
    @_apply_strategy
      attributes: for a in attributes when a != null then a
      name : 'append'
      transform: (old, result) -> "#{old}#{result}"

  # prepend to current element new content in top
  # @return null
  prepend: (attributes...) ->
    @_apply_strategy
      attributes: for a in attributes when a != null then a
      name : 'prepend'
      transform: (old, result) -> "#{result}#{old}"




  _apply_strategy: (object = {attributes: [], name: 'swap', transform: (old, result) -> "#{result}" }) ->
    adapter = Sirius.Application.adapter
    if object.attributes.length == 0
      adapter[object.name](@element, @_result)
    else
      for attr in object.attributes
        do(attr) =>
          if attr == 'text'
            adapter[object.name](@element, @_result)
          else
            old_val = adapter.get_attr(@element, attr)
            adapter.set_attr(@element, attr, object.transform(old_val, @_result))
    null


  # clear element content
  clear: () ->
    Sirius.Application.adapter.clear(@element)
    @


  #
  # when we have:
  # ### 1 View to View relation with change text
  #   view1.bind(view2)
  # if view1 element have onchange event then we use this event
  # if view1 element does not have onchange event we should use Dom level 3\4 events see #observer.coffee
  #
  # ### 2 View to View relation with change attributes in View2
  #
  # for view1 we should use Dom level 3\4 events
  # also we should know which attributes changed (filter), therefore
  # view1.bind(view2, to: ["id"])
  # when we change text in view1, then should changes in view2 id attribute
  #
  # ### 3 View to View relation change attributes in View1
  #
  # view1.bind(view2, from: ["id"])
  #
  # when change id in view1, we should change text in view2
  #
  # ### 4 Combination of 3 and 4
  # view1.bind(view2, from: ["id"], to: ["class"]
{'fraction_non_alphanumeric': 0.12882787750791974, 'fraction_numerical': 0.009855684618092221, 'mean_word_length': 3.1391114348142755, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 45, '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': '3463051', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1731, 'n_words': 594}