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

module.controller('scrollableCalendarController', function($scope) {
    var scrollbarOffset = 20;
    $scope.setup = function(baseDate) {
        var day = moment(baseDate).utc();
        day.startOf('month');  // first day of the month
        day.startOf('week');   // first day of the week (the row)
        return day;
    }
    $scope.computeDimentions = function(element) {
        var parentWidth = jQuery(element).innerWidth() - scrollbarOffset;
        var width = Math.floor(parentWidth / 7);
        $scope.unitWidth = width;
    }
    $scope.dump = function() {
        console.log("A:" + $scope.rangeA + " B:" + $scope.rangeB);
    }
    $scope.selectRange = function(isSelecting) {
        var startDay = moment($scope.rangeA).utc();
        var endDay = moment($scope.rangeB).utc();
        var numDays = endDay.diff(startDay, 'days');
        $scope.dateFlipped = false;
        if (numDays < 0) {
            // flip
            var tmpDay = startDay;
            startDay = endDay;
            endDay = tmpDay;
            numDays = -1 * numDays;
            $scope.dateFlipped = true;
        }
        // Update model if isSelecting == true
        if (isSelecting) {
            $scope.startDate = startDay.format('YYYY-MM-DD');
            $scope.endDate = endDay.format('YYYY-MM-DD');
        }
        for (var i = 0; i <= numDays; ++i) {
            var id = startDay.format('YYYY-MM-DD');
            var div = $scope.body.find("#" + id);
            if (isSelecting) {
                div.css({'background-color': '#ddd'});
            } else {
                div.css({'background-color': ''});
            }
            startDay.add('days', 1);
        }
    }
    $scope.updateRange = function(rangeA, rangeB) {
        // Unselect previous selection
        $scope.selectRange(false);
        // Make a new selection
        $scope.rangeA = rangeA;
        $scope.rangeB = rangeB;
        $scope.selectRange(true);
    }
    $scope.generateRow = function(param) {
        var day = param.clone();
        var html = jQuery('<div class="cal-row"></div>');
        html.css({'width': $scope.unitWidth * 7 + "px"});

        for (var i = 0; i < 7; ++i) {
            var dayDiv = jQuery('<div class="col-column"></div>');
            // dayDiv.text(day.utc().format('MMM D'));
            dayDiv.css({'width': $scope.unitWidth + "px"});
            dayDiv.css({'height': $scope.unitWidth + "px"});
            dayDiv.attr('id', day.utc().format('YYYY-MM-DD'));

            var dayMonth = jQuery('<div class="cal-day-month"></div>');
            dayMonth.css({'height':  $scope.unitWidth / 3 + "px"});
            if (day.date() == 1) {
                dayMonth.text(day.utc().format('MMM'))
            }
            var dayNumber = jQuery('<div class="cal-day-number"></div>');
            dayNumber.css({'height': 2 * $scope.unitWidth / 3 + "px"});
            dayNumber.css({'line-height': 2 * $scope.unitWidth / 3 + "px"});

            dayNumber.text(day.utc().format('D'))

            dayDiv.append(dayMonth);
            dayDiv.append(dayNumber);

            /*
            dayDiv.bind('mousedown', function(event_info) {
                // Select the day
                $scope.updateRange(event_info.currentTarget.id, event_info.currentTarget.id);
                $scope.selecting = true;
                $scope.$apply();
            });
            dayDiv.bind('mousemove', function(event_info) {
                var leftButtonPressed = false;
                if (event_info.buttons != undefined) {
                    // Firefox returns event_info.which == 1 even if no mouse keys are pressed.
                    // event_info.buttons is available on Firefox but not on Chrome.
                    if (event_info.buttons == 1) {
                        leftButtonPressed = true;
                    }
                } else {
                    if (event_info.which == 1) {
                        leftButtonPressed = true;
                    }
                }

                if (leftButtonPressed) {
                    if ($scope.rangeB != event_info.currentTarget.id) {
                        $scope.updateRange($scope.rangeA, event_info.currentTarget.id);
                        $scope.$apply();
                    }
                }
            });
            */
            $scope.firstClick = true;
            dayDiv.bind('mouseup', function(event_info) {
                if ($scope.firstClick) {
                    $scope.updateRange(event_info.currentTarget.id, event_info.currentTarget.id);
                    $scope.firstClick = false;
                } else {
                    $scope.updateRange($scope.rangeA, event_info.currentTarget.id);
                    $scope.firstClick = true;
                }
                $scope.selecting = false;
                $scope.$apply();
                if ($scope.callback) {
                    $scope.callback($scope.startDate, $scope.endDate);
                }
            });

            html.append(dayDiv);
            day.add('days', 1);
        }
        return html;
    }
    $scope.setupHeading = function() {
        var height = $scope.unitWidth * 1.5;
        var width = $scope.unitWidth * 7
        $scope.head.css({'height':height, 'width': width});
        $scope.headYear = jQuery('<div id="cal-head-year"></div>');
        $scope.headYear.css({'height':$scope.unitWidth, 'width': width});


        $scope.headDayOfWeek = jQuery('<div id="cal-head-dow"></div>');
        $scope.headDayOfWeek.css({'height':$scope.unitWidth*0.5});

        var day = moment().utc().startOf('week');
        for (var i = 0; i < 7; ++i) {
            var dayDiv = jQuery('<div class="col-column"></div>');
            dayDiv.text(day.utc().format('ddd'));
            dayDiv.css({'width': $scope.unitWidth + "px"});
            $scope.headDayOfWeek.append(dayDiv);
            day.add('days', 1);
        }
        $scope.head.append($scope.headYear);
        $scope.head.append($scope.headDayOfWeek);
    }
    $scope.setupBody = function(parentElement) {
        var height = parentElement.innerHeight() - $scope.head.innerHeight();
        var width = parentElement.innerWidth();
        $scope.body.css({'height': height, 'width': width});

    }
    $scope.refreshHeading = function() {
        var top = $scope.body.scrollTop();
        var height = $scope.body[0].scrollHeight;
        var visibleHeight = $scope.body.innerHeight();
        var bottom = top + visibleHeight;

        var centerDay = $scope.beginDay.clone();
        centerDay.add('days', Math.floor((top + visibleHeight/2) / $scope.unitWidth) * 7);

        $scope.headYear.text(centerDay.format('YYYY MMMM'));
    }
    // Angular -> UI<commit_msg>Fixed a bug that depends on timezone, Angular ended up with nested digest/apply loop.<commit_after>var module = angular.module('scrollableCalendarModule', []);

module.controller('scrollableCalendarController', function($scope) {
    var scrollbarOffset = 20;
    $scope.setup = function(baseDate) {
        var day = moment.utc(baseDate);
        day.startOf('month');  // first day of the month
        day.startOf('week');   // first day of the week (the row)
        return day;
    }
    $scope.computeDimentions = function(element) {
        var parentWidth = jQuery(element).innerWidth() - scrollbarOffset;
        var width = Math.floor(parentWidth / 7);
        $scope.unitWidth = width;
    }
    $scope.dump = function() {
        console.log("A:" + $scope.rangeA + " B:" + $scope.rangeB);
    }
    $scope.selectRange = function(isSelecting) {
        var startDay = moment.utc($scope.rangeA);
        var endDay = moment.utc($scope.rangeB);
        var numDays = endDay.diff(startDay, 'days');
        $scope.dateFlipped = false;
        if (numDays < 0) {
            // flip
            var tmpDay = startDay;
            startDay = endDay;
            endDay = tmpDay;
            numDays = -1 * numDays;
            $scope.dateFlipped = true;
        }
        // Update model if isSelecting == true
        if (isSelecting) {
            $scope.startDate = startDay.format('YYYY-MM-DD');
            $scope.endDate = endDay.format('YYYY-MM-DD');
        }
        for (var i = 0; i <= numDays; ++i) {
            var id = startDay.format('YYYY-MM-DD');
            var div = $scope.body.find("#" + id);
            if (isSelecting) {
                div.css({'background-color': '#ddd'});
            } else {
                div.css({'background-color': ''});
            }
            startDay.add('days', 1);
        }
    }
    $scope.updateRange = function(rangeA, rangeB) {
        // Unselect previous selection
        $scope.selectRange(false);
        // Make a new selection
        $scope.rangeA = rangeA;
        $scope.rangeB = rangeB;
        $scope.selectRange(true);
    }
    $scope.generateRow = function(param) {
        var day = param.clone();
        var html = jQuery('<div class="cal-row"></div>');
        html.css({'width': $scope.unitWidth * 7 + "px"});

        for (var i = 0; i < 7; ++i) {
            var dayDiv = jQuery('<div class="col-column"></div>');
            // dayDiv.text(day.utc().format('MMM D'));
            dayDiv.css({'width': $scope.unitWidth + "px"});
            dayDiv.css({'height': $scope.unitWidth + "px"});
            dayDiv.attr('id', day.utc().format('YYYY-MM-DD'));

            var dayMonth = jQuery('<div class="cal-day-month"></div>');
            dayMonth.css({'height':  $scope.unitWidth / 3 + "px"});
            if (day.date() == 1) {
                dayMonth.text(day.utc().format('MMM'))
            }
            var dayNumber = jQuery('<div class="cal-day-number"></div>');
            dayNumber.css({'height': 2 * $scope.unitWidth / 3 + "px"});
            dayNumber.css({'line-height': 2 * $scope.unitWidth / 3 + "px"});

            dayNumber.text(day.utc().format('D'))

            dayDiv.append(dayMonth);
            dayDiv.append(dayNumber);

            /*
            dayDiv.bind('mousedown', function(event_info) {
                // Select the day
                $scope.updateRange(event_info.currentTarget.id, event_info.currentTarget.id);
                $scope.selecting = true;
                $scope.$apply();
            });
            dayDiv.bind('mousemove', function(event_info) {
                var leftButtonPressed = false;
                if (event_info.buttons != undefined) {
                    // Firefox returns event_info.which == 1 even if no mouse keys are pressed.
                    // event_info.buttons is available on Firefox but not on Chrome.
                    if (event_info.buttons == 1) {
                        leftButtonPressed = true;
                    }
                } else {
                    if (event_info.which == 1) {
                        leftButtonPressed = true;
                    }
                }

                if (leftButtonPressed) {
                    if ($scope.rangeB != event_info.currentTarget.id) {
                        $scope.updateRange($scope.rangeA, event_info.currentTarget.id);
                        $scope.$apply();
                    }
                }
            });
            */
            $scope.firstClick = true;
            dayDiv.bind('mouseup', function(event_info) {
                if ($scope.firstClick) {
                    $scope.updateRange(event_info.currentTarget.id, event_info.currentTarget.id);
                    $scope.firstClick = false;
                } else {
                    $scope.updateRange($scope.rangeA, event_info.currentTarget.id);
                    $scope.firstClick = true;
                }
                $scope.selecting = false;
                $scope.$apply();
                if ($scope.callback) {
                    $scope.callback($scope.startDate, $scope.endDate);
                }
            });

            html.append(dayDiv);
            day.add('days', 1);
        }
        return html;
    }
    $scope.setupHeading = function() {
        var height = $scope.unitWidth * 1.5;
        var width = $scope.unitWidth * 7
        $scope.head.css({'height':height, 'width': width});
        $scope.headYear = jQuery('<div id="cal-head-year"></div>');
        $scope.headYear.css({'height':$scope.unitWidth, 'width': width});


        $scope.headDayOfWeek = jQuery('<div id="cal-head-dow"></div>');
        $scope.headDayOfWeek.css({'height':$scope.unitWidth*0.5});

        var day = moment.utc().startOf('week');
        for (var i = 0; i < 7; ++i) {
            var dayDiv = jQuery('<div class="col-column"></div>');
            dayDiv.text(day.utc().format('ddd'));
            dayDiv.css({'width': $scope.unitWidth + "px"});
            $scope.headDayOfWeek.append(dayDiv);
            day.add('days', 1);
        }
        $scope.head.append($scope.headYear);
        $scope.head.append($scope.headDayOfWeek);
    }
    $scope.setupBody = function(parentElement) {
        var height = parentElement.innerHeight() - $scope.head.innerHeight();
        var width = parentElement.innerWidth();
        $scope.body.css({'height': height, 'width': width});

    }
    $scope.refreshHeading = function() {
        var top = $scope.body.scrollTop();
        var height = $scope.body[0].scrollHeight;
        var visibleHeight = $scope.body.innerHeight();
        var bottom = top + visibleHeight;

        var centerDay = $scope.beginDay.clone();
        centerDay.add('days', Math.floor((top + visibleHeight/2) / $scope.unitWidth) * 7);

        $scope.headYear.text(centerDay.format('YYYY MMMM'));
    }
    // Angular -> UI
{'fraction_non_alphanumeric': 0.1521897810218978, 'fraction_numerical': 0.004525547445255474, 'mean_word_length': 1.7347305389221557, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 33, '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': '5966204', 'n_tokens_mistral': 4073, 'n_tokens_neox': 3893, 'n_words': 730}