<commit_before>{$, jQuery, View} = require "atom-space-pen-views"
{TreeView} = require "./tree-view"
XQUtils = require './xquery-helper'
Dialog = require './dialog'
request = require 'request'
path = require 'path'
fs = require 'fs'
tmp = require 'tmp'
mkdirp = require 'mkdirp'
{CompositeDisposable} = require 'atom'
mime = require 'mime'

module.exports =
    class EXistTreeView extends View

        @content: ->
            @div class: "existdb-tree"

        @tmpDir: null

        initialize: (@state, @config) ->
            mime.define({
                "application/xquery": ["xq", "xql", "xquery", "xqm"]
            })

            atom.workspace.observeTextEditors((editor) =>
                buffer = editor.getBuffer()
                p = buffer.getId()
                if /^xmldb:exist:\/\/\/db\/.*/.test(p) and not buffer._remote?
                    p = p.substring(14)
                    console.log("Reopen %s from database", p)
                    editor.destroy()
                    @open(path: p, buffer)
            )

            @disposables = new CompositeDisposable()
            @treeView = new TreeView

            @treeView.onSelect ({node, item}) ->
                console.log("Selected %o", item)

            @append(@treeView)

            atom.config.observe 'existdb-tree-view.scrollAnimation', (enabled) =>
                @animationDuration = if enabled then 300 else 0

            @treeView.width(@state.width) if @state?.width
            @toggle() if @state?.show

            @disposables.add atom.commands.add 'atom-workspace', 'existdb:reindex':
                (ev) => @reindex(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:reload-tree-view':
                (ev) => @load(ev.target.spacePenView.item)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:new-file':
                (ev) => @newFile(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:new-collection':
                (ev) => @newCollection(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:remove-resource':
                (ev) => @removeResource(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:reconnect': => @checkServer(() => @populate())

        serialize: ->
            width: @treeView.width()
            show: @hasParent()

        populate: ->
            root = {
                label: "db",
                path: "/db",
                icon: "icon-database",
                type: "collection",
                children: [],
                loaded: true
            }
            @treeView.setRoot(root, false)
            @checkServer(() => @load(root))

        load: (item, callback) =>
            console.log("Loading collection contents for item #{item.path}")
            self = this
            editor = atom.workspace.getActiveTextEditor()
            url = @config.getConfig(editor).server +
                "/apps/atom-editor/browse.xql?root=" + item.path
            options =
                uri: url
                method: "GET"
                json: true
                auth:
                    user: @config.getConfig(editor).user
                    pass: @config.getConfig(editor).password || ""
                    sendImmediately: true
            request(
                options,
                (error, response, body) ->
                    if error? or response.statusCode != 200
                        atom.notifications.addWarning("Failed to load database contents", detail: if response? then response.statusMessage else error)
                    else<commit_msg>Observe configuration and reload database view.<commit_after>{$, jQuery, View} = require "atom-space-pen-views"
{TreeView} = require "./tree-view"
XQUtils = require './xquery-helper'
Dialog = require './dialog'
request = require 'request'
path = require 'path'
fs = require 'fs'
tmp = require 'tmp'
mkdirp = require 'mkdirp'
{CompositeDisposable} = require 'atom'
mime = require 'mime'

module.exports =
    class EXistTreeView extends View

        @content: ->
            @div class: "existdb-tree"

        @tmpDir: null

        initialize: (@state, @config) ->
            mime.define({
                "application/xquery": ["xq", "xql", "xquery", "xqm"]
            })

            atom.workspace.observeTextEditors((editor) =>
                buffer = editor.getBuffer()
                p = buffer.getId()
                if /^xmldb:exist:\/\/\/db\/.*/.test(p) and not buffer._remote?
                    p = p.substring(14)
                    console.log("Reopen %s from database", p)
                    editor.destroy()
                    @open(path: p, buffer)
            )

            @disposables = new CompositeDisposable()
            @treeView = new TreeView

            @treeView.onSelect ({node, item}) ->
                console.log("Selected %o", item)

            @append(@treeView)

            atom.config.observe 'existdb-tree-view.scrollAnimation', (enabled) =>
                @animationDuration = if enabled then 300 else 0
            atom.config.onDidChange('existdb.server', (ev) => @checkServer(() => @populate()))
            atom.config.onDidChange('existdb.user', (ev) => @checkServer(() => @populate()))
            atom.config.onDidChange('existdb.password', (ev) => @checkServer(() => @populate()))
            atom.config.onDidChange('existdb.root', (ev) => @checkServer(() => @populate()))

            @treeView.width(@state.width) if @state?.width
            @toggle() if @state?.show

            @disposables.add atom.commands.add 'atom-workspace', 'existdb:reindex':
                (ev) => @reindex(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:reload-tree-view':
                (ev) => @load(ev.target.spacePenView.item)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:new-file':
                (ev) => @newFile(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:new-collection':
                (ev) => @newCollection(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:remove-resource':
                (ev) => @removeResource(ev.target.spacePenView)
            @disposables.add atom.commands.add 'atom-workspace', 'existdb:reconnect': => @checkServer(() => @populate())

        serialize: ->
            width: @treeView.width()
            show: @hasParent()

        populate: ->
            root = {
                label: "db",
                path: "/db",
                icon: "icon-database",
                type: "collection",
                children: [],
                loaded: true
            }
            @treeView.setRoot(root, false)
            @checkServer(() => @load(root))

        load: (item, callback) =>
            console.log("Loading collection contents for item #{item.path} using server #{@config.getConfig(editor).server}")
            self = this
            editor = atom.workspace.getActiveTextEditor()
            url = @config.getConfig(editor).server +
                "/apps/atom-editor/browse.xql?root=" + item.path
            options =
                uri: url
                method: "GET"
                json: true
                auth:
                    user: @config.getConfig(editor).user
                    pass: @config.getConfig(editor).password || ""
                    sendImmediately: true
            request(
                options,
                (error, response, body) ->
                    if error? or response.statusCode != 200
                        atom.notifications.addWarning("Failed to load database contents", detail: if response? then response.statusMessage else error)
                    else
{'fraction_non_alphanumeric': 0.13419695635769086, 'fraction_numerical': 0.0022638661803546724, 'mean_word_length': 1.7534626038781163, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '215268', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2113, 'n_words': 457}