<commit_before>CocoView = require 'views/core/CocoView'
template = require 'templates/editor/level/scripts_tab'
Level = require 'models/Level'
Surface = require 'lib/surface/Surface'
nodes = require './../treema_nodes'
defaultScripts = require 'lib/DefaultScripts'
require 'vendor/treema'

module.exports = class ScriptsTabView extends CocoView
  id: 'editor-level-scripts-tab-view'
  template: template
  className: 'tab-pane'

  subscriptions:
    'editor:level-loaded': 'onLevelLoaded'
    'editor:thangs-edited': 'onThangsEdited'

  constructor: (options) ->
    super options
    @world = options.world
    @files = options.files
    $(window).on 'resize', @onWindowResize

  destroy: ->
    @scriptTreema?.destroy()
    @scriptTreemas?.destroy()
    $(window).off 'resize', @onWindowResize
    super()

  onLoaded: ->
  onLevelLoaded: (e) ->
    @level = e.level
    @dimensions = @level.dimensions()
    scripts = $.extend(true, [], @level.get('scripts') ? [])
    if scripts.length is 0
      scripts = $.extend(true, [], defaultScripts)
    treemaOptions =
      schema: Level.schema.properties.scripts
      data: scripts
      callbacks:
        change: @onScriptsChanged
        select: @onScriptSelected
        addChild: @onNewScriptAdded
        removeChild: @onScriptDeleted
      nodeClasses:
        array: ScriptsNode
        object: ScriptNode
      view: @
    @scriptsTreema = @$el.find('#scripts-treema').treema treemaOptions
    @scriptsTreema.build()
    if @scriptsTreema.childrenTreemas[0]?
      @scriptsTreema.childrenTreemas[0].select()
      @scriptsTreema.childrenTreemas[0].broadcastChanges() # can get rid of this after refactoring treema

  onScriptsChanged: (e) =>
    @level.set 'scripts', @scriptsTreema.data

  onScriptSelected: (e, selected) =>
    selected = if selected.length > 1 then selected[0].getLastSelectedTreema() else selected[0]
    unless selected
      @$el.find('#script-treema').replaceWith($('<div id="script-treema"></div>'))
      @selectedScriptPath = null
      return

    @thangIDs = @getThangIDs()
    treemaOptions =
      world: @world
      filePath: "db/level/#{@level.get('original')}"
      files: @files
      view: @
      schema: Level.schema.properties.scripts.items
      data: selected.data
      thangIDs: @thangIDs
      dimensions: @dimensions
      supermodel: @supermodel
      readOnly: me.get('anonymous')
      callbacks:
        change: @onScriptChanged
      nodeClasses:
        object: PropertiesNode
        'event-value-chain': EventPropsNode
        'event-prereqs': EventPrereqsNode
        'event-prereq': EventPrereqNode
        'event-channel': ChannelNode
        'thang': nodes.ThangNode
        'milliseconds': nodes.MillisecondsNode
        'seconds': nodes.SecondsNode
        'point2d': nodes.WorldPointNode
        'viewport': nodes.WorldViewportNode
        'bounds': nodes.WorldBoundsNode

    newPath = selected.getPath()
    return if newPath is @selectedScriptPath
    #@scriptTreema?.destroy() # TODO: get this to work
    @scriptTreema = @$el.find('#script-treema').treema treemaOptions
    @scriptTreema.build()
    @scriptTreema.childrenTreemas?.noteChain?.open()
    @selectedScriptPath = newPath

  getThangIDs: ->
    (t.id for t in @level.get('thangs') ? [])

  onNewScriptAdded: (scriptNode) =>
    return unless scriptNode
    if scriptNode.data.id is undefined
      scriptNode.disableTracking()
      scriptNode.set '/id', 'Script-' + @scriptsTreema.data.length
      scriptNode.enableTracking()

  onScriptDeleted: =>
    for key, treema of @scriptsTreema.childrenTreemas
      key = parseInt(key)
      treema.disableTracking()
      if /Script-[0-9]*/.test treema.data.id
        existingKey = parseInt(treema.data.id.substr(7))
        if existingKey isnt key+1
          treema.set 'id', 'Script-' + (key+1)
      treema.enableTracking()

  onScriptChanged: =>
    return unless @selectedScriptPath
    @scriptsTreema.set(@selectedScriptPath, @scriptTreema.data)
<commit_msg>Open scripts to depth 5 at first in level editor scripts tab<commit_after>CocoView = require 'views/core/CocoView'
template = require 'templates/editor/level/scripts_tab'
Level = require 'models/Level'
Surface = require 'lib/surface/Surface'
nodes = require './../treema_nodes'
defaultScripts = require 'lib/DefaultScripts'
require 'vendor/treema'

module.exports = class ScriptsTabView extends CocoView
  id: 'editor-level-scripts-tab-view'
  template: template
  className: 'tab-pane'

  subscriptions:
    'editor:level-loaded': 'onLevelLoaded'
    'editor:thangs-edited': 'onThangsEdited'

  constructor: (options) ->
    super options
    @world = options.world
    @files = options.files
    $(window).on 'resize', @onWindowResize

  destroy: ->
    @scriptTreema?.destroy()
    @scriptTreemas?.destroy()
    $(window).off 'resize', @onWindowResize
    super()

  onLoaded: ->
  onLevelLoaded: (e) ->
    @level = e.level
    @dimensions = @level.dimensions()
    scripts = $.extend(true, [], @level.get('scripts') ? [])
    if scripts.length is 0
      scripts = $.extend(true, [], defaultScripts)
    treemaOptions =
      schema: Level.schema.properties.scripts
      data: scripts
      callbacks:
        change: @onScriptsChanged
        select: @onScriptSelected
        addChild: @onNewScriptAdded
        removeChild: @onScriptDeleted
      nodeClasses:
        array: ScriptsNode
        object: ScriptNode
      view: @
    @scriptsTreema = @$el.find('#scripts-treema').treema treemaOptions
    @scriptsTreema.build()
    if @scriptsTreema.childrenTreemas[0]?
      @scriptsTreema.childrenTreemas[0].select()
      @scriptsTreema.childrenTreemas[0].broadcastChanges() # can get rid of this after refactoring treema

  onScriptsChanged: (e) =>
    @level.set 'scripts', @scriptsTreema.data

  onScriptSelected: (e, selected) =>
    selected = if selected.length > 1 then selected[0].getLastSelectedTreema() else selected[0]
    unless selected
      @$el.find('#script-treema').replaceWith($('<div id="script-treema"></div>'))
      @selectedScriptPath = null
      return

    @thangIDs = @getThangIDs()
    treemaOptions =
      world: @world
      filePath: "db/level/#{@level.get('original')}"
      files: @files
      view: @
      schema: Level.schema.properties.scripts.items
      data: selected.data
      thangIDs: @thangIDs
      dimensions: @dimensions
      supermodel: @supermodel
      readOnly: me.get('anonymous')
      callbacks:
        change: @onScriptChanged
      nodeClasses:
        object: PropertiesNode
        'event-value-chain': EventPropsNode
        'event-prereqs': EventPrereqsNode
        'event-prereq': EventPrereqNode
        'event-channel': ChannelNode
        'thang': nodes.ThangNode
        'milliseconds': nodes.MillisecondsNode
        'seconds': nodes.SecondsNode
        'point2d': nodes.WorldPointNode
        'viewport': nodes.WorldViewportNode
        'bounds': nodes.WorldBoundsNode

    newPath = selected.getPath()
    return if newPath is @selectedScriptPath
    #@scriptTreema?.destroy() # TODO: get this to work
    @scriptTreema = @$el.find('#script-treema').treema treemaOptions
    @scriptTreema.build()
    @scriptTreema.childrenTreemas?.noteChain?.open(5)
    @selectedScriptPath = newPath

  getThangIDs: ->
    (t.id for t in @level.get('thangs') ? [])

  onNewScriptAdded: (scriptNode) =>
    return unless scriptNode
    if scriptNode.data.id is undefined
      scriptNode.disableTracking()
      scriptNode.set '/id', 'Script-' + @scriptsTreema.data.length
      scriptNode.enableTracking()

  onScriptDeleted: =>
    for key, treema of @scriptsTreema.childrenTreemas
      key = parseInt(key)
      treema.disableTracking()
      if /Script-[0-9]*/.test treema.data.id
        existingKey = parseInt(treema.data.id.substr(7))
        if existingKey isnt key+1
          treema.set 'id', 'Script-' + (key+1)
      treema.enableTracking()

  onScriptChanged: =>
    return unless @selectedScriptPath
    @scriptsTreema.set(@selectedScriptPath, @scriptTreema.data)

{'fraction_non_alphanumeric': 0.12254536415610241, 'fraction_numerical': 0.003479990057171265, 'mean_word_length': 3.2175052410901466, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '2975412', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2435, 'n_words': 507}