<commit_before>﻿var module = angular.module("OpenChainWallet.Controllers");
var ByteBuffer = dcodeIO.ByteBuffer;
var Long = dcodeIO.Long;

module.controller("AdminController", function ($scope, $rootScope, $location, walletSettings, apiService, encodingService, endpointManager) {

    if (!walletSettings.initialized) {
        $location.path("/signin");
        return;
    }

    $rootScope.selectedTab = "advanced";

    $scope.endpoints = endpointManager.endpoints;
    $scope.display = "tree-view";

    for (var key in $scope.endpoints) {
        $scope.endpoint = $scope.endpoints[key];
        break;
    }

    $scope.setEndpoint = function (endpoint) {
        $scope.endpoint = endpoint;
    }

    $scope.setView = function (view) {
        $scope.display = view;
    }
});

module.controller("TransactionController", function ($scope, $location, $q, TransactionBuilder, apiService, encodingService, walletSettings, validator) {

    $scope.mutations = [];

    $scope.addMutation = function () {
        $scope.mutations.push({
            account: "",
            asset: "",
            amount: ""
        })
    };

    $scope.remove = function (index) {
        if ($scope.mutations.length > 1) {
            $scope.mutations.splice(index, 1);
        }
    };

    $scope.submit = function () {
        var valid = true;
        var endpoint = $scope.endpoint;
        
        for (var mutationKey in $scope.mutations) {
            var mutation = $scope.mutations[mutationKey];
            mutation.amountError = !validator.isNumber(mutation.amount);
            mutation.assetError = !mutation.asset;
            mutation.accountError = !mutation.account;

            if (mutation.amountError || mutation.assetError || mutation.accountError)
                valid = false;
        }

        if (!valid)
            return;
        
        var transaction = new TransactionBuilder(endpoint);
        $q.all($scope.mutations.map(function (mutation) {
            return transaction.fetchAndAddAccountRecord(mutation.account, mutation.asset, Long.fromString(mutation.amount));
        }))
        .then(function(array) {
            return transaction.uiSubmit(walletSettings.derivedKey);
        });
    };

    $scope.addMutation();

});

module.controller("AliasEditorController", function ($scope, $location, $q, TransactionBuilder, apiService, encodingService, walletSettings) {
    $scope.fields = {
        alias: "",
        path: ""
    };

    $scope.loadAlias = function () {
        apiService.getData($scope.endpoint, "/aka/" + $scope.fields.alias + "/").then(function (result) {
            if (result.data != null) {
                $scope.fields.path = result.data;
            }
            else {
                $scope.fields.path = "";
            }
        });
    };

    $scope.submit = function () {
        var endpoint = $scope.endpoint;

        apiService.getData(endpoint, "/aka/" + $scope.fields.alias + "/").then(function (result) {

            var transaction = new TransactionBuilder(endpoint);
            transaction.addRecord(result.key, encodingService.encodeString($scope.fields.path), result.version);

            return transaction.uiSubmit(walletSettings.derivedKey);
        });
    };
});

module.controller("TreeViewController", function ($scope, apiService, encodingService) {
    apiService.getSubaccounts($scope.endpoint, "/").then(function (result) {
        var rootNode = {
            Path: "/",
            type: "",
            children: []
        };

        var treeData = [rootNode];

        function addToTree(node, account, path) {
            if (path.length == 0) {
                var child = {
                    Path: "[" + account.recordKey.recordType + "]",
                    key: account.recordKey.toString(),
                    record: account
                };

                if (account.recordKey.recordType == "ACC") {
                    child.asset = account.recordKey.components[0];
                    child.amount = encodingService.decodeInt64(account.value).toString();
                }
                else if (account.recordKey.recordType == "DATA") {
                    child.data = encodingService.decodeString(account.value);
                }

                node.children.push(child);
            }
            else {
                var part = path[0];
                for (var index = 0; index < node.children.length; index++) {
                    if (node.children[index].Path == part) {
                        addToTree(node.children[index], account, path.slice(1, path.length));
                        return;
                    }
                }

                var child = {
                    Path: part,
                    fullPath: account.recordKey.path.toString(),
                    children: []
                };

                node.children.push(child);

                addToTree(child, account, path.slice(1, path.length));
            }
        };

        for (var i in result) {
            addToTree(rootNode, result[i], result[i].recordKey.path.parts);
        }

        $scope.treeData = treeData;
        $scope.expandedNodes = [rootNode];
    });

    $scope.treeData = [];
    $scope.treeOptions = {
        dirSelectable: false,
        allowDeselect: false
    };

    $scope.selectNode = function (node, selected) {
        $scope.selectedNode = node;
    };
});<commit_msg>Update the ledger tree explorer when switching endpoint<commit_after>﻿var module = angular.module("OpenChainWallet.Controllers");
var ByteBuffer = dcodeIO.ByteBuffer;
var Long = dcodeIO.Long;

module.controller("AdminController", function ($scope, $rootScope, $location, walletSettings, apiService, encodingService, endpointManager) {

    if (!walletSettings.initialized) {
        $location.path("/signin");
        return;
    }

    $rootScope.selectedTab = "advanced";

    $scope.endpoints = endpointManager.endpoints;
    $scope.display = "tree-view";
    $scope.child = { refreshTree: function () { } };

    for (var key in $scope.endpoints) {
        $scope.endpoint = $scope.endpoints[key];
        break;
    }

    $scope.setEndpoint = function (endpoint) {
        $scope.endpoint = endpoint;
        if ($scope.display == "tree-view") {
            $scope.child.refreshTree();
        }
    }

    $scope.setView = function (view) {
        $scope.display = view;
    }
});

module.controller("TransactionController", function ($scope, $location, $q, TransactionBuilder, apiService, encodingService, walletSettings, validator) {

    $scope.mutations = [];

    $scope.addMutation = function () {
        $scope.mutations.push({
            account: "",
            asset: "",
            amount: ""
        })
    };

    $scope.remove = function (index) {
        if ($scope.mutations.length > 1) {
            $scope.mutations.splice(index, 1);
        }
    };

    $scope.submit = function () {
        var valid = true;
        var endpoint = $scope.endpoint;
        
        for (var mutationKey in $scope.mutations) {
            var mutation = $scope.mutations[mutationKey];
            mutation.amountError = !validator.isNumber(mutation.amount);
            mutation.assetError = !mutation.asset;
            mutation.accountError = !mutation.account;

            if (mutation.amountError || mutation.assetError || mutation.accountError)
                valid = false;
        }

        if (!valid)
            return;
        
        var transaction = new TransactionBuilder(endpoint);
        $q.all($scope.mutations.map(function (mutation) {
            return transaction.fetchAndAddAccountRecord(mutation.account, mutation.asset, Long.fromString(mutation.amount));
        }))
        .then(function(array) {
            return transaction.uiSubmit(walletSettings.derivedKey);
        });
    };

    $scope.addMutation();

});

module.controller("AliasEditorController", function ($scope, $location, $q, TransactionBuilder, apiService, encodingService, walletSettings) {
    $scope.fields = {
        alias: "",
        path: ""
    };

    $scope.loadAlias = function () {
        apiService.getData($scope.endpoint, "/aka/" + $scope.fields.alias + "/").then(function (result) {
            if (result.data != null) {
                $scope.fields.path = result.data;
            }
            else {
                $scope.fields.path = "";
            }
        });
    };

    $scope.submit = function () {
        var endpoint = $scope.endpoint;

        apiService.getData(endpoint, "/aka/" + $scope.fields.alias + "/").then(function (result) {

            var transaction = new TransactionBuilder(endpoint);
            transaction.addRecord(result.key, encodingService.encodeString($scope.fields.path), result.version);

            return transaction.uiSubmit(walletSettings.derivedKey);
        });
    };
});

module.controller("TreeViewController", function ($scope, apiService, encodingService) {

    var refreshTree = function () {
        apiService.getSubaccounts($scope.endpoint, "/").then(function (result) {
            var rootNode = {
                Path: "/",
                type: "",
                endpointId: $scope.endpoint.properties.id,
                children: []
            };

            var treeData = [rootNode];

            function addToTree(node, account, path) {
                if (path.length == 0) {
                    var child = {
                        Path: "[" + account.recordKey.recordType + "]",
                        key: account.recordKey.toString(),
                        endpointId: $scope.endpoint.properties.id,
                        record: account
                    };

                    if (account.recordKey.recordType == "ACC") {
                        child.asset = account.recordKey.components[0];
                        child.amount = encodingService.decodeInt64(account.value).toString();
                    }
                    else if (account.recordKey.recordType == "DATA") {
                        child.data = encodingService.decodeString(account.value);
                    }

                    node.children.push(child);
                }
                else {
                    var part = path[0];
                    for (var index = 0; index < node.children.length; index++) {
                        if (node.children[index].Path == part) {
                            addToTree(node.children[index], account, path.slice(1, path.length));
                            return;
                        }
                    }

                    var child = {
                        Path: part,
                        fullPath: account.recordKey.path.toString(),
                        endpointId: $scope.endpoint.properties.id,
                        children: []
                    };

                    node.children.push(child);

                    addToTree(child, account, path.slice(1, path.length));
                }
            };

            for (var i in result) {
                addToTree(rootNode, result[i], result[i].recordKey.path.parts);
            }

            $scope.treeData = treeData;
            $scope.expandedNodes = [rootNode];
        });
    };

    $scope.child.refreshTree = refreshTree;

    $scope.treeData = [];
    $scope.treeOptions = {
        dirSelectable: false,
        allowDeselect: false
    };

    $scope.selectNode = function (node, selected) {
        $scope.selectedNode = node;
    };

    refreshTree();
});
{'fraction_non_alphanumeric': 0.12679715918932963, 'fraction_numerical': 0.0017322016282695306, 'mean_word_length': 1.7690647482014388, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4710811', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2951, 'n_words': 548}