<commit_before>'use strict';

angular.module('arachne.directives')

    .directive('arImagegrid', ['arachneSettings', '$http', '$sce', '$window', function (arachneSettings, $http, $sce, $window) {
        return {
            scope: {
                cells: '=',
                columns: '@',
                margin: '@',
                hideTitle: '@',
                complete: '=?'
            },
            templateUrl: 'partials/directives/ar-imagegrid.html',

            link: function (scope, element, attrs) {

                scope.loadImage = function (cell) {
                    cell.img = new Image();
                    cell.img.addEventListener("load", function () {
                        // custom event handlers need to be wrapped in $apply
                        scope.$apply(function () {
                            cell.complete = true;
                            scope.resizeRow(cell.row);
                        });
                    });
                    cell.img.addEventListener("error", function () {
                        scope.$apply(function () {
                            cell.complete = true;
                            cell.img = scope.placeholder;
                            scope.resizeRow(cell.row);
                        });
                    });
                    $http.get(cell.imgUri, {responseType: 'blob'})
                        .success(function (data) {
                            var blob = new Blob([data], {type: 'image/jpeg'});
                            cell.img.src = window.URL.createObjectURL(blob);
                        }).error(function (response) {
                        cell.img.src = scope.placeholder.src;
                    });
                };

                scope.resizeRow = function (row) {

                    // only resize if every cell in the row is complete
                    for (var i = 0; i < row.length; i++) {
                        if (!row[i].complete) return;
                    }

                    var imagesWidth = 0;
                    var maxHeight = 0;

                    for (var i = 0; i < row.length; i++) {
                        imagesWidth += row[i].img.naturalWidth;
                    }

                    var columns = scope.columns;
                    var totalWidth = element[0].clientWidth - 1;
                    totalWidth -= columns * scope.margin * 2;
                    // fill rows with fewer columns
                    if (row.length < columns) {
                        imagesWidth += (columns - row.length) * (totalWidth / columns)
                    }
                    var scalingFactor = totalWidth / imagesWidth;

                    for (var i = 0; i < row.length; i++) {
                        row[i].width = row[i].img.naturalWidth * scalingFactor;
                        if (scalingFactor > 1) {
                            row[i].imgWidth = row[i].img.naturalWidth;
                        } else {
                            row[i].imgWidth = row[i].width;
                        }
                        var height;
                        if (scalingFactor > 1) {
                            height = row[i].img.naturalHeight;
                        } else {
                            height = row[i].img.naturalHeight * scalingFactor;
                        }
                        if (height > maxHeight) maxHeight = height;
                    }

                    for (var i = 0; i < row.length; i++) {
                        row[i].height = maxHeight;
                    }

                    row.complete = true;
                    for (var i = 0; i < scope.grid.length; i++) {
                        if (!scope.grid[i].complete) {
                            scope.complete = false;
                            break;
                        } else {
                            scope.complete = true;
                        }
                    }

                };

                angular.element($window).bind('resize', function () {
                    scope.$apply(function () {
                        scope.complete = false;
                        for (var i = 0; i < scope.grid.length; i++) {
                            var row = scope.grid[i];
                            row.complete = false;
                            scope.resizeRow(row);
                        }
                    });
                });

            },

            controller: ['$scope', function ($scope) {

                $scope.placeholder = new Image();
                $scope.placeholder.src = 'img/imagePlaceholder.png';
                $scope.complete = false;

                $scope.$watch('cells', function (newCells, oldCells) {

                    if (typeof newCells == 'undefined' || !newCells) return;

                    var columns = $scope.columns;
                    var rows = Math.ceil($scope.cells.length / columns);
                    $scope.grid = new Array(rows);
                    for (var i = 0; i < rows; i++) {
                        $scope.grid[i] = new Array(columns);
                        for (var k = 0; k < columns; k++) {
                            if (i * columns + k >= $scope.cells.length) break;
                            var index = i * columns + k;
                            var cell = $scope.cells[index];
                            $scope.grid[i][k] = cell;
                            cell.row = $scope.grid[i];
                            $scope.grid[i].complete = false;
                            if (typeof cell.imgUri == 'undefined') {
                                cell.imgUri = $scope.placeholder.src;
                            }
                            $scope.loadImage(cell);
                        }
                    }

                });

            }]
        }
    }]);<commit_msg>Refactor for-loops and variable declarations.<commit_after>'use strict';

angular.module('arachne.directives')

    .directive('arImagegrid', ['arachneSettings', '$http', '$sce', '$window', function (arachneSettings, $http, $sce, $window) {
        return {
            scope: {
                cells: '=',
                columns: '@',
                margin: '@',
                hideTitle: '@',
                complete: '=?'
            },
            templateUrl: 'partials/directives/ar-imagegrid.html',

            link: function (scope, element, attrs) {

                scope.loadImage = function (cell) {

                    cell.img = new Image();
                    cell.img.addEventListener("load", function () {
                        // custom event handlers need to be wrapped in $apply
                        scope.$apply(function () {
                            cell.complete = true;
                            scope.resizeRow(cell.row);
                        });
                    });
                    cell.img.addEventListener("error", function () {
                        scope.$apply(function () {
                            cell.complete = true;
                            cell.img = scope.placeholder;
                            scope.resizeRow(cell.row);
                        });
                    });
                    $http.get(cell.imgUri, {responseType: 'blob'})
                        .success(function (data) {
                            var blob = new Blob([data], {type: 'image/jpeg'});
                            cell.img.src = window.URL.createObjectURL(blob);
                        }).error(function (response) {
                        cell.img.src = scope.placeholder.src;
                    });
                };

                scope.resizeRow = function (row) {

                    var i, len;
                    
                    len = row.length;

                    // only resize if every cell in the row is complete
                    for (i = 0; i < len; i++) {
                        if (!row[i].complete) return;
                    }

                    var imagesWidth = 0;
                    var maxHeight = 0;

                    for (i = 0; i < len; i++) {
                        imagesWidth += row[i].img.naturalWidth;
                    }

                    var columns = scope.columns;
                    var totalWidth = element[0].clientWidth - 1;
                    totalWidth -= columns * scope.margin * 2;
                    
                    // fill rows with fewer columns
                    if (len < columns) {
                        imagesWidth += (columns - len) * (totalWidth / columns)
                    }
                    var scalingFactor = totalWidth / imagesWidth;

                    var currentRow;
                    for (i = 0; i < len; i++) {

                        currentRow = row[i];
                        
                        currentRow.width = currentRow.img.naturalWidth * scalingFactor;
                        if (scalingFactor > 1) {
                            currentRow.imgWidth = currentRow.img.naturalWidth;
                        } else {
                            currentRow.imgWidth = currentRow.width;
                        }
                        var height;
                        if (scalingFactor > 1) {
                            height = currentRow.img.naturalHeight;
                        } else {
                            height = currentRow.img.naturalHeight * scalingFactor;
                        }
                        if (height > maxHeight) maxHeight = height;
                    }

                    for (i = 0; i < len; i++) {
                        row[i].height = maxHeight;
                    }

                    row.complete = true;
                    len = scope.grid.length;
                    
                    for (i = 0; i < len; i++) {
                        
                        if (!scope.grid[i].complete) {
                            scope.complete = false;
                            break;
                        } else {
                            scope.complete = true;
                        }
                    }

                };

                angular.element($window).bind('resize', function () {
                    scope.$apply(function () {

                        scope.complete = false;
                        var row, len = scope.grid.length;

                        for (var i = 0; i < len; i++) {

                            row = scope.grid[i];
                            row.complete = false;
                            scope.resizeRow(row);
                        }
                    });
                });

            },

            controller: ['$scope', function ($scope) {

                $scope.placeholder = new Image();
                $scope.placeholder.src = 'img/imagePlaceholder.png';
                $scope.complete = false;

                $scope.$watch('cells', function (newCells, oldCells) {

                    if (typeof newCells == 'undefined' || !newCells) return;

                    var columns = $scope.columns;
                    var rows = Math.ceil($scope.cells.length / columns);
                    $scope.grid = new Array(rows);

                    for (var i = 0; i < rows; i++) {

                        $scope.grid[i] = new Array(columns);

                        for (var k = 0; k < columns; k++) {

                            if (i * columns + k >= $scope.cells.length) break;
                            var index = i * columns + k;
                            var cell = $scope.cells[index];
                            $scope.grid[i][k] = cell;
                            cell.row = $scope.grid[i];
                            $scope.grid[i].complete = false;
                            if (typeof cell.imgUri == 'undefined') {
                                cell.imgUri = $scope.placeholder.src;
                            }
                            $scope.loadImage(cell);
                        }
                    }

                });

            }]
        }
    }]);
{'fraction_non_alphanumeric': 0.10612211084417199, 'fraction_numerical': 0.0024852953359290864, 'mean_word_length': 0.8380024360535931, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1756960', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2595, 'n_words': 597}