<commit_before>var remote = require('remote')
var fs = require('fs')
var shell = require('shell')
var sgf = require('../module/sgf')
var gametree = require('../module/gametree')
var uuid = require('../lib/node-uuid')
var process = remote.require('process')
var app = remote.require('app')
var dialog = remote.require('dialog')
var dns = remote.require('dns')
var https = remote.require('https')
var setting = remote.require('./module/setting')

var Tuple = require('../lib/tuple')
var Board = require('../module/board')
var Scrollbar = require('../lib/gemini-scrollbar')
var Menu = remote.require('menu')

/**
 * Getter & setter
 */

function getRootTree() {
    if (!getCurrentTreePosition()) return null

    return getCurrentTreePosition().unpack(function(tree, index) {
        while (tree.parent != null) tree = tree.parent
        return tree
    })
}

function setRootTree(tree) {
    if (tree.nodes.length == 0) return

    tree.parent = null
    setCurrentTreePosition(sgf.addBoard(tree), 0)

    // Update UI
    if (getShowSidebar()) {
        updateGraph()
        updateSlider()
    }

    if ('PB' in tree.nodes[0]) setPlayerName(1, tree.nodes[0].PB[0])
    if ('PW' in tree.nodes[0]) setPlayerName(-1, tree.nodes[0].PW[0])
}

function getGraphMatrixDict() {
    return $('graph').retrieve('graphmatrixdict')
}

function setGraphMatrixDict(matrixdict) {
    if (!getShowSidebar()) return

    var s = $('graph').retrieve('sigma')
    $('graph').store('graphmatrixdict', matrixdict)

    s.graph.clear()
    s.graph.read(gametree.matrixdict2graph(matrixdict))

    s.refresh()
}

function setCurrentTreePosition(tree, index) {
    if (!tree || getScoringMode()) return

    // Remove current graph node color
    var n = getCurrentGraphNode()
    if (n && n != getGraphNode(tree, index)) delete n.color

    $('goban').store('position', new Tuple(tree, index))

    // Set current path
    var t = tree
    while (t.parent) {
        t.parent.current = t.parent.subtrees.indexOf(t)
        t = t.parent
    }

    // Update graph and slider
    var n = getCurrentGraphNode()

    if (!n && tree.parent && tree.parent.collapsed) {
        tree.parent.collapsed = false
        updateGraph()
    }

    setTimeout(function() {
        if (n && getCurrentGraphNode() == n)
            centerGraphCameraAt(n)
        updateSlider()
    }, setting.get('graph.delay'))

    setBoard(sgf.addBoard(tree, index).nodes[index].board)

    // Determine current player
    setCurrentPlayer(1)

    if ('B' in tree.nodes[index]) setCurrentPlayer(-1)
    else if ('W' in tree.nodes[index]) setCurrentPlayer(1)
    else if ('PL' in tree.nodes[index])
        setCurrentPlayer(tree.nodes[index].PL[0] == 'W' ? -1 : 1)
    else if ('HA' in tree.nodes[index] && tree.nodes[index].HA[0].toInt() >= 1)
        setCurrentPlayer(-1)
}

function getCurrentTreePosition() {
    return $('goban').retrieve('position')
}

function getCurrentGraphNode() {
    if (!getCurrentTreePosition()) return null
    return getCurrentTreePosition().unpack(getGraphNode)
}

function getGraphNode(tree, index) {
    var id = typeof tree === 'object' ? tree.id + '-' + index : tree
    var s = $('graph').retrieve('sigma')
    return s.graph.nodes(id)
}

function getSelectedTool() {
    var li = $$('#edit .selected')[0]
    var tool = li.get('class').replace('selected', '').replace('-tool', '').trim()

    if (tool == 'stone') {
        return li.getElement('img').get('src').contains('_1') ? 'stone_1' : 'stone_-1'
    } else {
        return tool
    }
}

function setSelectedTool(tool) {
    if (!getEditMode()) {
        setEditMode(true)
        if (getSelectedTool().contains(tool)) return
    }

    $$('#edit .' + tool + '-tool a').fireEvent('click')
}

function getBoard() {
    return $('goban').retrieve('board')
}

function setBoard(board) {
    if (!getBoard() || getBoard().size != board.size) {
        $('goban').store('board', board)
        buildBoard()
    }

    $('goban').store('board', board)
    setCaptures(board.captures)

    for (var x = 0; x < board.size; x++) {
        for (var y = 0; y < board.size; y++) {
            var li = $('goban').getElement('.pos_' + x + '-' + y)
            var sign = board.arrangement[li.retrieve('tuple')]
            var types = ['ghost_1', 'ghost_-1', 'circle', 'triangle',
                'cross', 'square', 'label', 'point']

            types.each(function(x) {
                if (li.hasClass(x)) li.removeClass(x)
            })

            if (li.retrieve('tuple') in board.overlays) {
                board.overlays[li.retrieve('tuple')].unpack(function(type, ghost, label) {
                    if (type != '') li.addClass(type)
                    if (ghost != 0) li.addClass('ghost_' + ghost)
                    if (label != '') li.set('title', label)
                })
            }

            if (li.hasClass('sign_' + sign)) continue

            for (var i = -1; i <= 1; i++) {
                if (li.hasClass('sign_' + i)) li.removeClass('sign_' + i)
            }

            li.addClass('sign_' + sign)
                .getElement('img').set('src', '../img/goban/stone_' + sign + '.png')
        }
    }
}

function getScoringMethod() {
    return $$('#score .method .territory')[0].hasClass('current') ? 'territory' : 'area'
}

function setScoringMethod(method) {
    $$('#score .method li').removeClass('current')
    $$('#score .method .' + method).addClass('current')<commit_msg>Fix bug where label titles are not removed<commit_after>var remote = require('remote')
var fs = require('fs')
var shell = require('shell')
var sgf = require('../module/sgf')
var gametree = require('../module/gametree')
var uuid = require('../lib/node-uuid')
var process = remote.require('process')
var app = remote.require('app')
var dialog = remote.require('dialog')
var dns = remote.require('dns')
var https = remote.require('https')
var setting = remote.require('./module/setting')

var Tuple = require('../lib/tuple')
var Board = require('../module/board')
var Scrollbar = require('../lib/gemini-scrollbar')
var Menu = remote.require('menu')

/**
 * Getter & setter
 */

function getRootTree() {
    if (!getCurrentTreePosition()) return null

    return getCurrentTreePosition().unpack(function(tree, index) {
        while (tree.parent != null) tree = tree.parent
        return tree
    })
}

function setRootTree(tree) {
    if (tree.nodes.length == 0) return

    tree.parent = null
    setCurrentTreePosition(sgf.addBoard(tree), 0)

    // Update UI
    if (getShowSidebar()) {
        updateGraph()
        updateSlider()
    }

    if ('PB' in tree.nodes[0]) setPlayerName(1, tree.nodes[0].PB[0])
    if ('PW' in tree.nodes[0]) setPlayerName(-1, tree.nodes[0].PW[0])
}

function getGraphMatrixDict() {
    return $('graph').retrieve('graphmatrixdict')
}

function setGraphMatrixDict(matrixdict) {
    if (!getShowSidebar()) return

    var s = $('graph').retrieve('sigma')
    $('graph').store('graphmatrixdict', matrixdict)

    s.graph.clear()
    s.graph.read(gametree.matrixdict2graph(matrixdict))

    s.refresh()
}

function setCurrentTreePosition(tree, index) {
    if (!tree || getScoringMode()) return

    // Remove current graph node color
    var n = getCurrentGraphNode()
    if (n && n != getGraphNode(tree, index)) delete n.color

    $('goban').store('position', new Tuple(tree, index))

    // Set current path
    var t = tree
    while (t.parent) {
        t.parent.current = t.parent.subtrees.indexOf(t)
        t = t.parent
    }

    // Update graph and slider
    var n = getCurrentGraphNode()

    if (!n && tree.parent && tree.parent.collapsed) {
        tree.parent.collapsed = false
        updateGraph()
    }

    setTimeout(function() {
        if (n && getCurrentGraphNode() == n)
            centerGraphCameraAt(n)
        updateSlider()
    }, setting.get('graph.delay'))

    setBoard(sgf.addBoard(tree, index).nodes[index].board)

    // Determine current player
    setCurrentPlayer(1)

    if ('B' in tree.nodes[index]) setCurrentPlayer(-1)
    else if ('W' in tree.nodes[index]) setCurrentPlayer(1)
    else if ('PL' in tree.nodes[index])
        setCurrentPlayer(tree.nodes[index].PL[0] == 'W' ? -1 : 1)
    else if ('HA' in tree.nodes[index] && tree.nodes[index].HA[0].toInt() >= 1)
        setCurrentPlayer(-1)
}

function getCurrentTreePosition() {
    return $('goban').retrieve('position')
}

function getCurrentGraphNode() {
    if (!getCurrentTreePosition()) return null
    return getCurrentTreePosition().unpack(getGraphNode)
}

function getGraphNode(tree, index) {
    var id = typeof tree === 'object' ? tree.id + '-' + index : tree
    var s = $('graph').retrieve('sigma')
    return s.graph.nodes(id)
}

function getSelectedTool() {
    var li = $$('#edit .selected')[0]
    var tool = li.get('class').replace('selected', '').replace('-tool', '').trim()

    if (tool == 'stone') {
        return li.getElement('img').get('src').contains('_1') ? 'stone_1' : 'stone_-1'
    } else {
        return tool
    }
}

function setSelectedTool(tool) {
    if (!getEditMode()) {
        setEditMode(true)
        if (getSelectedTool().contains(tool)) return
    }

    $$('#edit .' + tool + '-tool a').fireEvent('click')
}

function getBoard() {
    return $('goban').retrieve('board')
}

function setBoard(board) {
    if (!getBoard() || getBoard().size != board.size) {
        $('goban').store('board', board)
        buildBoard()
    }

    $('goban').store('board', board)
    setCaptures(board.captures)

    for (var x = 0; x < board.size; x++) {
        for (var y = 0; y < board.size; y++) {
            var li = $('goban').getElement('.pos_' + x + '-' + y)
            var sign = board.arrangement[li.retrieve('tuple')]
            var types = ['ghost_1', 'ghost_-1', 'circle', 'triangle',
                'cross', 'square', 'label', 'point']

            types.each(function(x) {
                if (li.hasClass(x)) li.removeClass(x)
            })
            li.set('title', '')

            if (li.retrieve('tuple') in board.overlays) {
                board.overlays[li.retrieve('tuple')].unpack(function(type, ghost, label) {
                    if (type != '') li.addClass(type)
                    if (ghost != 0) li.addClass('ghost_' + ghost)
                    if (label != '') li.set('title', label)
                })
            }

            if (li.hasClass('sign_' + sign)) continue

            for (var i = -1; i <= 1; i++) {
                if (li.hasClass('sign_' + i)) li.removeClass('sign_' + i)
            }

            li.addClass('sign_' + sign)
                .getElement('img').set('src', '../img/goban/stone_' + sign + '.png')
        }
    }
}

function getScoringMethod() {
    return $$('#score .method .territory')[0].hasClass('current') ? 'territory' : 'area'
}

function setScoringMethod(method) {
    $$('#score .method li').removeClass('current')
    $$('#score .method .' + method).addClass('current')
{'fraction_non_alphanumeric': 0.17602483337898292, 'fraction_numerical': 0.005843147995982836, 'mean_word_length': 2.737291026953258, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '5780555', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3448, 'n_words': 757}