<commit_before>###
  todo:

    - make save dialog a view with pistachio
    - put listeners in methods
    - make this splittable

###

class AceView extends JView

  constructor:(options, file)->

    super

    @listenWindowResize()

    @saveButton = new KDButtonViewWithMenu
      title         : "Save"
      style         : "editor-button save-menu"
      type          : "contextmenu"
      delegate      : @
      menu          : @getSaveMenu.bind @
      callback      : ()=>
        @ace.requestSave()

    @caretPosition = new KDCustomHTMLView
      tagName       : "div"
      cssClass      : "caret-position section"
      partial       : "<span>1</span>:<span>1</span>"

    @ace = new Ace {}, file

    @advancedSettings = new KDButtonViewWithMenu
      style         : 'editor-advanced-settings-menu'
      icon          : yes
      iconOnly      : yes
      iconClass     : "cog"
      type          : "contextmenu"
      delegate      : @
      itemClass     : AceSettingsView
      click         : (pubInst, event)-> @contextMenu event
      menu          : @getAdvancedSettingsMenuItems.bind @

    publicUrlCheck = /.*\/(.*\.koding.com)\/website\/(.*)/
    @previewButton = new KDButtonView
      style     : "editor-button"
      icon      : yes
      iconOnly  : yes
      iconClass : "preview"
      callback  : =>
        publicPath = @getData().path.replace publicUrlCheck, 'http://$1/$2'
        return if publicPath is @getData().path
        appManager.openFileWithApplication publicPath, "Viewer"

    @previewButton.hide() unless publicUrlCheck.test(@getData().path)

    @compileAndRunButton = new KDButtonView
      style     : "editor-button"
      icon      : yes
      iconOnly  : yes
      iconClass : "compile"
      tooltip   :
        title   : "Compile & Run"
      callback  : =>
        manifest = KodingAppsController.getManifestFromPath @getData().parentPath
        @getSingleton('kodingAppsController').compileApp manifest.name, =>
          @ace.notify "App compiled!", "success"
          @getSingleton('kodingAppsController').runApp manifest

    @compileAndRunButton.hide() unless /\.kdapp\//.test @getData().path

    @setViewListeners()

  setViewListeners:->

    @ace.on "ace.changeSetting", (setting, value)=>
      @ace["set#{setting.capitalize()}"]? value

    @advancedSettings.emit "ace.settingsView.setDefaults", @ace

    $spans = @caretPosition.$('span')

    @ace.on "ace.change.cursor", (cursor)=>
      $spans.eq(0).text ++cursor.row
      $spans.eq(1).text ++cursor.column

    @ace.on "ace.requests.saveAs", (contents)=>
      @openSaveDialog()

    @ace.on "ace.requests.save", (contents)=>
      if /localfile:/.test @getData().path
        @openSaveDialog()
      else
        @getData().emit "file.requests.save", contents

  viewAppended:->

    super
    @_windowDidResize()

  pistachio:->

    """
    <div class="kdview editor-header">
      <div class="kdview header-buttons">
        {{> @compileAndRunButton}}
        {{> @previewButton}}
        {{> @saveButton}}
      </div>
    </div>
    <div class="kdview editor-main">
      {{> @ace}}
      <div class="editor-bottom-bar clearfix">
        {{> @caretPosition}}
        {{> @advancedSettings}}
      </div>
    </div>
    """

  getAdvancedSettingsMenuItems:->

    settings      :
      type        : 'customView'
      view        : new AceSettingsView
        delegate  : @ace

  getSaveMenu:->

    "Save as..." :
      id         : 13
      parentId   : null
      callback   : =>
        @openSaveDialog()

  _windowDidResize:->

    height = @getHeight() - 10
    editorHeight = height - @$('.editor-header').height()
    bottomBarHeight = @$('.editor-bottom-bar').height()
    @$('.editor-main').height editorHeight
    @ace.setHeight editorHeight - bottomBarHeight

  openSaveDialog: (callback) ->

    file = @getData()
    @addSubView saveDialog = new KDDialogView
      cssClass      : "save-as-dialog"
      duration      : 200
      topOffset     : 0
      overlay       : yes
      height        : "auto"
      buttons       :
        Save        :
          style     : "modal-clean-gray"
          callback  : ()=>
            [node] = @finderController.treeController.selectedNodes
            name   = @inputFileName.getValue()

            if name is '' or /^([a-zA-Z]:\\)?[^\x00-\x1F"<>\|:\*\?/]+$/.test(name) is false
              @_message 'Wrong file name', "Please type valid file name"
              @ace.notify "Please type valid file name!", "error"
              return

            unless node
              @ace.notify "Please select a folder to save!", "error"
              return

            parent = node.getData()
            file.emit "file.requests.saveAs", @ace.getContents(), name, parent.path
            saveDialog.hide()
        Cancel :
          style     : "modal-cancel"
          callback  : ()->
            saveDialog.hide()

    saveDialog.addSubView wrapper = new KDView
      cssClass : "kddialog-wrapper"

    wrapper.addSubView form = new KDFormView

    form.addSubView labelFileName = new KDLabelView
      title : "Filename:"

    form.addSubView @inputFileName = inputFileName = new KDInputView
      label        : labelFileName
      defaultValue : file.name

    form.addSubView labelFinder = new KDLabelView
      title : "Select a folder:"

    saveDialog.show()
    inputFileName.setFocus()

    @finderController = new NFinderController
      treeItemClass     : NFinderItem
      nodeIdPath        : "path"
      nodeParentIdPath  : "parentPath"
      dragdrop          : yes
      foldersOnly       : yes
      contextMenu       : no

    finder = @finderController.getView()

    form.addSubView finderWrapper = new KDView cssClass : "save-as-dialog file-container",null
    finderWrapper.addSubView finder
    finderWrapper.setHeight 200
<commit_msg>Update Compile & Run Button on Ace<commit_after>###
  todo:

    - make save dialog a view with pistachio
    - put listeners in methods
    - make this splittable

###

class AceView extends JView

  constructor:(options, file)->

    super

    @listenWindowResize()

    @saveButton = new KDButtonViewWithMenu
      title         : "Save"
      style         : "editor-button save-menu"
      type          : "contextmenu"
      delegate      : @
      menu          : @getSaveMenu.bind @
      callback      : ()=>
        @ace.requestSave()

    @caretPosition = new KDCustomHTMLView
      tagName       : "div"
      cssClass      : "caret-position section"
      partial       : "<span>1</span>:<span>1</span>"

    @ace = new Ace {}, file

    @advancedSettings = new KDButtonViewWithMenu
      style         : 'editor-advanced-settings-menu'
      icon          : yes
      iconOnly      : yes
      iconClass     : "cog"
      type          : "contextmenu"
      delegate      : @
      itemClass     : AceSettingsView
      click         : (pubInst, event)-> @contextMenu event
      menu          : @getAdvancedSettingsMenuItems.bind @

    publicUrlCheck = /.*\/(.*\.koding.com)\/website\/(.*)/
    @previewButton = new KDButtonView
      style     : "editor-button"
      icon      : yes
      iconOnly  : yes
      iconClass : "preview"
      callback  : =>
        publicPath = @getData().path.replace publicUrlCheck, 'http://$1/$2'
        return if publicPath is @getData().path
        appManager.openFileWithApplication publicPath, "Viewer"

    @previewButton.hide() unless publicUrlCheck.test(@getData().path)

    @compileAndRunButton = new KDButtonView
      style     : "editor-button"
      title     : "Compile & Run"
      loader    :
        color   : "#444444"
        diameter: 12
      callback  : =>
        manifest = KodingAppsController.getManifestFromPath @getData().parentPath
        @ace.notify "Compiling...", null, yes
        @getSingleton('kodingAppsController').compileApp manifest.name, =>
          @ace.notify "App compiled!", "success"
          @getSingleton('kodingAppsController').runApp manifest
          @compileAndRunButton.hideLoader()

    @compileAndRunButton.hide() unless /\.kdapp\//.test @getData().path

    @setViewListeners()

  setViewListeners:->

    @ace.on "ace.changeSetting", (setting, value)=>
      @ace["set#{setting.capitalize()}"]? value

    @advancedSettings.emit "ace.settingsView.setDefaults", @ace

    $spans = @caretPosition.$('span')

    @ace.on "ace.change.cursor", (cursor)=>
      $spans.eq(0).text ++cursor.row
      $spans.eq(1).text ++cursor.column

    @ace.on "ace.requests.saveAs", (contents)=>
      @openSaveDialog()

    @ace.on "ace.requests.save", (contents)=>
      if /localfile:/.test @getData().path
        @openSaveDialog()
      else
        @getData().emit "file.requests.save", contents

  viewAppended:->

    super
    @_windowDidResize()

  pistachio:->

    """
    <div class="kdview editor-header">
      <div class="kdview header-buttons">
        {{> @compileAndRunButton}}
        {{> @previewButton}}
        {{> @saveButton}}
      </div>
    </div>
    <div class="kdview editor-main">
      {{> @ace}}
      <div class="editor-bottom-bar clearfix">
        {{> @caretPosition}}
        {{> @advancedSettings}}
      </div>
    </div>
    """

  getAdvancedSettingsMenuItems:->

    settings      :
      type        : 'customView'
      view        : new AceSettingsView
        delegate  : @ace

  getSaveMenu:->

    "Save as..." :
      id         : 13
      parentId   : null
      callback   : =>
        @openSaveDialog()

  _windowDidResize:->

    height = @getHeight() - 10
    editorHeight = height - @$('.editor-header').height()
    bottomBarHeight = @$('.editor-bottom-bar').height()
    @$('.editor-main').height editorHeight
    @ace.setHeight editorHeight - bottomBarHeight

  openSaveDialog: (callback) ->

    file = @getData()
    @addSubView saveDialog = new KDDialogView
      cssClass      : "save-as-dialog"
      duration      : 200
      topOffset     : 0
      overlay       : yes
      height        : "auto"
      buttons       :
        Save        :
          style     : "modal-clean-gray"
          callback  : ()=>
            [node] = @finderController.treeController.selectedNodes
            name   = @inputFileName.getValue()

            if name is '' or /^([a-zA-Z]:\\)?[^\x00-\x1F"<>\|:\*\?/]+$/.test(name) is false
              @_message 'Wrong file name', "Please type valid file name"
              @ace.notify "Please type valid file name!", "error"
              return

            unless node
              @ace.notify "Please select a folder to save!", "error"
              return

            parent = node.getData()
            file.emit "file.requests.saveAs", @ace.getContents(), name, parent.path
            saveDialog.hide()
        Cancel :
          style     : "modal-cancel"
          callback  : ()->
            saveDialog.hide()

    saveDialog.addSubView wrapper = new KDView
      cssClass : "kddialog-wrapper"

    wrapper.addSubView form = new KDFormView

    form.addSubView labelFileName = new KDLabelView
      title : "Filename:"

    form.addSubView @inputFileName = inputFileName = new KDInputView
      label        : labelFileName
      defaultValue : file.name

    form.addSubView labelFinder = new KDLabelView
      title : "Select a folder:"

    saveDialog.show()
    inputFileName.setFocus()

    @finderController = new NFinderController
      treeItemClass     : NFinderItem
      nodeIdPath        : "path"
      nodeParentIdPath  : "parentPath"
      dragdrop          : yes
      foldersOnly       : yes
      contextMenu       : no

    finder = @finderController.getView()

    form.addSubView finderWrapper = new KDView cssClass : "save-as-dialog file-container",null
    finderWrapper.addSubView finder
    finderWrapper.setHeight 200

{'fraction_non_alphanumeric': 0.12296747967479675, 'fraction_numerical': 0.0040650406504065045, 'mean_word_length': 2.03573264781491, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 81, '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': '933120', 'n_tokens_mistral': 3552, 'n_tokens_neox': 3413, 'n_words': 742}