<commit_before>$ = require "jquery"
_ = require "underscore"
Backbone = require "backbone"
build_views = require "./build_views"
ContinuumView = require "./continuum_view"
HasProperties = require "./has_properties"
{logger} = require "./logging"
ToolManager = require "./tool_manager"
plot_template = require "./plot_template"

class ToolProxy extends Backbone.Model

  initialize: (options) ->
    super(options)
    # OK this is pretty lame but should work until we make a new
    # better grid plot. This just mimics all the events that
    # any of the tool types might expect to get.
    @listenTo(@, 'do', @do)
    @listenTo(@, 'change:active', @active)
    return null

  do: () ->
    for tool in @attributes.tools
      tool.trigger('do')
    return null

  active: () ->
    for tool in @attributes.tools
      tool.set('active', @attributes.active)
    return null

  attrs_and_props: () ->
    return @attributes.tools[0].attrs_and_props()

  get: (attr) ->
    return @attributes.tools[0].get(attr)

  set: (attr, value) ->
    super(attr, value)
    attr = _.omit(attr, "tools")
    for tool in @attributes.tools
      tool.set(attr, value)
    return null

class GridToolManager extends ToolManager.Model

  _init_tools: () ->
    # Note: no call to super(), intentionally

    inspectors = {}
    actions = {}
    gestures = {}

    for tm in @get('tool_managers')

      for et, info of tm.get('gestures')
        if et not of gestures
          gestures[et] = {}
        for tool in info.tools
          if tool.type not of gestures[et]
            gestures[et][tool.type] = []
          gestures[et][tool.type].push(tool)

      for tool in tm.get('inspectors')
        if tool.type not of inspectors
          inspectors[tool.type] = []
        inspectors[tool.type].push(tool)

      for tool in tm.get('actions')
        if tool.type not of actions
          actions[tool.type] = []
        actions[tool.type].push(tool)

    for et of gestures
      for typ, tools of gestures[et]
        if tools.length != @get('num_plots')
          continue
        proxy = new ToolProxy({tools: tools})
        @get('gestures')[et].tools.push(proxy)
        @listenTo(proxy, 'change:active', _.bind(@_active_change, proxy))

    for typ, tools of actions
      if tools.length != @get('num_plots')
        continue
      proxy = new ToolProxy({tools: tools})
      tmp = @get('actions')
      tmp.push(proxy)
      @set('actions', tmp)

    for typ, tools of inspectors
      if tools.length != @get('num_plots')
        continue
      proxy = new ToolProxy({tools: tools})
      tmp = @get('inspectors')
      tmp.push(proxy)
      @set('inspectors', tmp)

    for et, info of @get('gestures')
      tools = info.tools
      if tools.length == 0
        continue
      info.tools = _.sortBy(tools, (tool) -> tool.get('default_order'))
      info.tools[0].set('active', true)

  _active_change: (tool) =>
    et = tool.get('event_type')

    active = tool.get('active')
    if not active
      return null

    gestures = @get('gestures')
    prev = gestures[et].active
    if prev?
      logger.debug(
        "GridToolManager: deactivating tool: #{prev.type} (for event type '#{et}'"
      )
      prev.set('active', false)

    gestures[et].active = tool
    @set('gestures', gestures)
    logger.debug(
      "GridToolManager: activating tool: #{tool.type} (for event type '#{et}'"
    )
    return null

  defaults: () ->
    return _.extend {}, super(), {
      tool_manangers: []
    }

class GridViewState extends HasProperties

  setup_layout_properties: () =>
    @register_property('layout_heights', @layout_heights, false)
    @register_property('layout_widths', @layout_widths, false)
    for row in @get('viewstates')
      for viewstate in row
        @add_dependencies('layout_heights', viewstate, 'height')
        @add_dependencies('layout_widths', viewstate, 'width')

  initialize: (attrs, options) ->
    super(attrs, options)
    @setup_layout_properties()
    @listenTo(this, 'change:viewstates', @setup_layout_properties)
    calculateHeight = =>
      _.reduce @get("layout_heights"), ((x, y) -> x + y), 0
    @register_property('height', calculateHeight, false)
    @add_dependencies('height', @, 'layout_heights')

    calculateWidth = =>
      _.reduce @get("layout_widths"), ((x, y) -> x + y), 0
    @register_property('width', calculateWidth, false)
    @add_dependencies('width', @, 'layout_widths')
<commit_msg>Bring GridToolManager up to date<commit_after>$ = require "jquery"
_ = require "underscore"
Backbone = require "backbone"
build_views = require "./build_views"
ContinuumView = require "./continuum_view"
HasProperties = require "./has_properties"
{logger} = require "./logging"
ToolManager = require "./tool_manager"
plot_template = require "./plot_template"

class ToolProxy extends Backbone.Model

  initialize: (options) ->
    super(options)
    # OK this is pretty lame but should work until we make a new
    # better grid plot. This just mimics all the events that
    # any of the tool types might expect to get.
    @listenTo(@, 'do', @do)
    @listenTo(@, 'change:active', @active)
    return null

  do: () ->
    for tool in @attributes.tools
      tool.trigger('do')
    return null

  active: () ->
    for tool in @attributes.tools
      tool.set('active', @attributes.active)
    return null

  attrs_and_props: () ->
    return @attributes.tools[0].attrs_and_props()

  get: (attr) ->
    return @attributes.tools[0].get(attr)

  set: (attr, value) ->
    super(attr, value)
    attr = _.omit(attr, "tools")
    for tool in @attributes.tools
      tool.set(attr, value)
    return null

class GridToolManager extends ToolManager.Model

  _init_tools: () ->
    # Note: no call to super(), intentionally

    inspectors = {}
    actions = {}
    gestures = {}

    for tm in @get('tool_managers')

      for et, info of tm.get('gestures')
        if et not of gestures
          gestures[et] = {}
        for tool in info.tools
          if tool.type not of gestures[et]
            gestures[et][tool.type] = []
          gestures[et][tool.type].push(tool)

      for tool in tm.get('inspectors')
        if tool.type not of inspectors
          inspectors[tool.type] = []
        inspectors[tool.type].push(tool)

      for tool in tm.get('actions')
        if tool.type not of actions
          actions[tool.type] = []
        actions[tool.type].push(tool)

    for et of gestures
      for typ, tools of gestures[et]
        if tools.length != @get('num_plots')
          continue
        proxy = new ToolProxy({tools: tools})
        @get('gestures')[et].tools.push(proxy)
        @listenTo(proxy, 'change:active', _.bind(@_active_change, proxy))

    for typ, tools of actions
      if tools.length != @get('num_plots')
        continue
      proxy = new ToolProxy({tools: tools})
      tmp = @get('actions')
      tmp.push(proxy)
      @set('actions', tmp)

    for typ, tools of inspectors
      if tools.length != @get('num_plots')
        continue
      proxy = new ToolProxy({tools: tools})
      tmp = @get('inspectors')
      tmp.push(proxy)
      @set('inspectors', tmp)

    for et, info of @get('gestures')
      tools = info.tools
      if tools.length == 0
        continue
      info.tools = _.sortBy(tools, (tool) -> tool.get('default_order'))
      if et not in ['pinch', 'scroll']
        info.tools[0].set('active', true)

  _active_change: (tool) =>
    event_type = tool.get('event_type')
    gestures = @get('gestures')

    # Toggle between tools of the same type by deactivating any active ones
    currently_active_tool = gestures[event_type].active
    if currently_active_tool? and currently_active_tool != tool
      logger.debug("GridToolManager: deactivating tool: #{currently_active_tool.type} (#{currently_active_tool.id}) for event type '#{event_type}'")
      currently_active_tool.set('active', false)

    # Update the gestures with the new active tool
    gestures[event_type].active = tool
    @set('gestures', gestures)
    logger.debug("GridToolManager: activating tool: #{tool.type} (#{tool.id}) for event type '#{event_type}'")
    return null

  defaults: () ->
    return _.extend {}, super(), {
      tool_manangers: []
    }

class GridViewState extends HasProperties

  setup_layout_properties: () =>
    @register_property('layout_heights', @layout_heights, false)
    @register_property('layout_widths', @layout_widths, false)
    for row in @get('viewstates')
      for viewstate in row
        @add_dependencies('layout_heights', viewstate, 'height')
        @add_dependencies('layout_widths', viewstate, 'width')

  initialize: (attrs, options) ->
    super(attrs, options)
    @setup_layout_properties()
    @listenTo(this, 'change:viewstates', @setup_layout_properties)
    calculateHeight = =>
      _.reduce @get("layout_heights"), ((x, y) -> x + y), 0
    @register_property('height', calculateHeight, false)
    @add_dependencies('height', @, 'layout_heights')

    calculateWidth = =>
      _.reduce @get("layout_widths"), ((x, y) -> x + y), 0
    @register_property('width', calculateWidth, false)
    @add_dependencies('width', @, 'layout_widths')

{'fraction_non_alphanumeric': 0.1345169371528156, 'fraction_numerical': 0.0013070471626184512, 'mean_word_length': 2.9731717871051493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '5819646', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2769, 'n_words': 742}