<commit_before>var taskApp = angular.module('taskApp.controllers', []);

taskApp.controller('MainController', ['$scope', '$http', '$timeout', 'taskFactory',function($scope, $http, $timeout, taskFactory){

    $scope.newTaskInputField = true;

    /* ID of in-progress task */
    $scope.currentTaskId = null;

    /* tasks container from factory */
    $scope.tasksContainer = taskFactory;

    /* temporary container for task for deletion */
    $scope.tmpTaskContainer = [];

    /**
     * Removes the selected task from the list. It's also advisable to cleanup tmpTaskCo[ntainer to avoid
     * keeping of stale data
     */
    $scope.removeTasks = function() {
        if ($scope.tmpTaskContainer.length > 0) {
            var tmpTasks = [];

            angular.forEach($scope.tmpTaskContainer, function(taskId, key){
                $scope.tasksContainer.removeItem(taskId);
                tmpTasks.push(taskId);
            });

            // cleanup temporary task container
            angular.forEach(tmpTasks, function(taskId, key){
                if ($scope.tmpTaskContainer.indexOf(taskId) != -1) {
                    $scope.tmpTaskContainer.splice($scope.tmpTaskContainer.indexOf(taskId));
                }
            });
        }
    }

    /**
     * Updates the temp container with the selected task ready to be removed from the list.
     *
     * @param string    task id
     */
    $scope.updateSelection = function(taskId) {
        if($scope.tmpTaskContainer.indexOf(taskId) == -1) {
            $scope.tmpTaskContainer.push(taskId);
        } else {
            $scope.tmpTaskContainer.splice($scope.tmpTaskContainer.indexOf(taskId),1);
        }
    }

    $scope.addTask = function(task) {
        if (task) {

            var newTaskId = $scope.tasksContainer.addItem(task);

            if ($scope.currentTaskId) {
                $scope.tasksContainer.update($scope.currentTaskId);
            }
            $scope.currentTaskId = newTaskId;
        }
    }

}]);

taskApp.directive('stNewtask', function(){
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            scope.$watch(attr.stNewtask, function(n, o){
                if (n != 0 && n) {
                    element[0].focus();
                }
            });

            element.bind('keydown', function(e){
                if(e.keyCode == 13) {
                    scope.$apply('addTask(newTask)');
                    scope.$apply('newTask=""');
                }
            });
        }
    };
});<commit_msg>Added PouchDB support<commit_after>var taskApp = angular.module('taskApp.controllers', []);

taskApp.controller('MainController', ['$scope', '$http', '$timeout', 'db', function($scope, $http, $timeout, db){
    $scope.list = [];
    $scope.tmpTaskContainer = [];

    $scope.tasks = function() {
        db.allDocs({include_docs:true}, function(error, doc){
            $scope.$apply(function(){
                angular.forEach(doc.rows, function(task, key){
                   $scope.list.push(task.doc);
                });
            });
        });
    }
    $scope.tasks();

    /**
     * Adds a new task
     *
     * @param string    task name
     */
    $scope.add = function(task) {
        var _doc = {
            name: task,
            start_date: new Date().getTime(),
            end_date: null,
            duration: null
        };

        db.post(_doc, function(err, doc){
            db.get(doc.id, function(err, doc){
                $scope.$apply(function(){
                    $scope.list.push(doc);
                });
            });
        });
    }

    /**
     * Removes the selected task from the list. It's also advisable to cleanup tmpTaskContainer to avoid
     * keeping of stale data
     */
    $scope.removeTasks = function() {
        if ($scope.tmpTaskContainer.length > 0) {
            var tmpTasks = [];

            angular.forEach($scope.tmpTaskContainer, function(taskId, key){
                db.get(taskId, function(err, doc){
                    db.remove(doc, function(err, doc){});
                    $scope.$apply(function(){
                        $scope.list.splice($scope.list.map(function(e){ return e._id;}).indexOf(taskId), 1);
                    });
                });
                tmpTasks.push(taskId);
            });

            // cleanup temporary task container
            angular.forEach(tmpTasks, function(taskId, key){
                if ($scope.tmpTaskContainer.indexOf(taskId) != -1) {
                    $scope.tmpTaskContainer.splice($scope.tmpTaskContainer.indexOf(taskId));
                }
            });
        }
    }

    /**
     * Updates the temp container with the selected task ready to be removed from the list.
     *
     * @param string    task id
     */
    $scope.updateSelection = function(taskId) {
        if($scope.tmpTaskContainer.indexOf(taskId) == -1) {
            $scope.tmpTaskContainer.push(taskId);
        } else {
            $scope.tmpTaskContainer.splice($scope.tmpTaskContainer.indexOf(taskId),1);
        }
    }

}]);

taskApp.directive('stNewtask', function(){
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            scope.$watch(attr.stNewtask, function(n, o){
                if (n != 0 && n) {
                    element[0].focus();
                }
            });

            element.bind('keydown', function(e){
                if(e.keyCode == 13) {
                    scope.$apply('add(newTask)');
                    scope.$apply('newTask=""');
                }
            });
        }
    };
});
{'fraction_non_alphanumeric': 0.13981168946526915, 'fraction_numerical': 0.0030200746136081007, 'mean_word_length': 1.6989453499520613, 'pattern_counts': {'":': 0, '<': 3, '<?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': '931462', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1523, 'n_words': 292}