<commit_before>'use strict';

var fileshareApp = angular.module('fileshareApp', []);

fileshareApp.directive('indexEntry', function() {
    return {
        restrict: 'AE',
        replace: 'true',
        templateUrl: '/static/index-entry.html'
    };
});

fileshareApp.controller('ShareCtrl', [
    '$scope', '$http',
    function($scope, $http) {

        // gets the last element of a filepath (unix)
        $scope.lastPathElementRegexp = /[^/]+\/*$/;
        $scope.shareRoot = "/home/gaige/Dropbox/";
        $scope.currentPath = "";
        $scope.index = [];

        // When we get back an object from the api we need to sort it.
        // We might also want to extract filename/last index of the path
        // for display purposes. This could also be done on the lisp side
        // pretty easily I think but that means more data being transfered

        var sortIndex = function(a, b) {
            // sort by file or directory status
            if (a.kind > b.kind) return -1;
            if (a.kind < b.kind) return 1;
            // sort by name
            if (a.path < b.path) return -1;
            if (a.path > b.path) return 1;

            return 0;
        };

        $scope.handleItem = function(item) {
            if (item.kind == "Folder") {
                //console.log("Request to download tree: ", item.path);
                //$scope.loadNewIndex(item.path);
                $scope.downloadDirectory(item.path);
            } else if (item.kind == "File") {
                window.open('/download/' + item.path.replace($scope.shareRoot, ''), '_blank', '');
                console.log("Request to download file: ", item.path);
            }
        };

        $scope.downloadDirectory = function(path) {
            $http.post("/download-directory", {
                "path": path
            }).success(function(data, status, headers, config) {
                console.log("SUCCESS: ", data);
                window.open('/download/' + data.path.replace($scope.shareRoot, ''), '_blank', '');
                //$scope.currentPath = data.currentPath;
                //$scope.index = data.index.sort(sortIndex);
            }).error(function(data, status, headers, config) {
                console.log("ERROR: ", data, status, headers, config);
            });
        };

        $scope.loadNewIndex = function(path) {
            $http.post("/index", {
                "path": path
            }).success(function(data, status, headers, config) {
                console.log("SUCCESS: ", data);
                $scope.currentPath = data.currentPath;
                $scope.index = data.index.sort(sortIndex);
            }).error(function(data, status, headers, config) {
                console.log("ERROR: ", data, status, headers, config);
            });
        };

        $scope.loadNewIndex("/home/gaige/Dropbox/");

    }
]);<commit_msg>Moved http calls into a service.<commit_after>'use strict';

var fileshareApp = angular.module('fileshareApp', []);

fileshareApp.directive('indexEntry', function() {
    return {
        restrict: 'AE',
        replace: 'true',
        templateUrl: '/static/index-entry.html'
    };
});

fileshareApp.controller('ShareCtrl', [
    '$scope', '$http', '$location', 'ShareService',
    function($scope, $http, $location, ShareService) {

        // gets the last element of a filepath (unix)
        $scope.lastPathElementRegexp = /[^/]+\/*$/;
        $scope.shareRoot = "/home/gaige/Dropbox/";
        $scope.currentPath = "";
        $scope.crumbs = [];
        $scope.index = [];

        // When we get back an object from the api we need to sort it.
        // We might also want to extract filename/last index of the path
        // for display purposes. This could also be done on the lisp side
        // pretty easily I think but that means more data being transfered

        //$scope.splitCurrentPath = function()

        ShareService.loadNewIndex("/home/gaige/Dropbox/").then(function(data) {
            // get and sort the index
            $scope.index = data.index.sort(ShareService.sortIndex);
        });

        $scope.handleItem = function(item) {
            console.log("Reqeust");
            if (item.kind == "Folder") {
                ShareService.loadNewIndex(item.path).then(function(data) {
                    $scope.index = data.index.sort(ShareService.sortIndex);
                    $location.path(item.path);
                });
            } else if (item.kind == "File") {
                window.open('/download/' + item.path.replace($scope.shareRoot, ''), '_blank', '');
                console.log("Request to download file: ", item.path);
            }
        };

        $scope.joinCrumbs = function(index) {
            console.log(index);
            var path = "/";
            for (var i = 0; i <= index; i++) {
                path += $scope.crumbs[i] + "/";
            }
            console.log(path);
            return path;
        };

    }
]);
{'fraction_non_alphanumeric': 0.14850491671683724, 'fraction_numerical': 0.0012040939193257074, 'mean_word_length': 1.8205998868138087, 'pattern_counts': {'":': 2, '<': 6, '<?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': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '183159', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1365, 'n_words': 330}