'use strict';

/* Controllers */

function mkUpdateOrderBy($scope) {
    return function(column) {
        var rev = column == $scope.orderProp;
        $scope.sortCls = {};
        $scope.sortCls[column] = 'sort-' + (rev ? 'desc' : 'asc');
        if (rev) {
            column = '-' + column;
        }
        $scope.orderProp = column;
    };
}

angular.module('swiftBrowser.controllers', ['swiftBrowser.swift'])
    .controller('RootCtrl', ['$scope', '$swift', function($scope, $swift) {
        $scope.containers = [];
        $scope.updateOrderBy = mkUpdateOrderBy($scope);
        $scope.updateOrderBy('name');

        $scope.toggleAll = function () {
            var newValue = !$scope.allSelected();
            for (var i = 0; i < $scope.containers.length; i++) {
                $scope.containers[i].selected = newValue;
            }
        };

        $scope.allSelected = function () {
            for (var i = 0; i < $scope.containers.length; i++) {
                if (!$scope.containers[i].selected) {
                    return false;
                }
            }
            return true;
        };

        $swift.listContainers().then(function (result) {
            $scope.containers = result.data;
        });
    }])
    .controller('ContainerCtrl', [
        '$scope', '$swift', '$routeParams', '$location',
        function($scope, $swift, $routeParams, $location) {
            var container = $routeParams.container;
            var path = $routeParams.path || '';
            $scope.container = container;
            $scope.updateOrderBy = mkUpdateOrderBy($scope);
            $scope.updateOrderBy('name');

            $scope.breadcrumbs = [{name: '', title: 'Root'}];

            var parts = path.split('/');
            parts.unshift(container);
            for (var i = 0; i < parts.length - 1; i++) {
                var crumb = {name: parts.slice(0, i + 1).join('/') + '/',
                             title: parts[i]};
                $scope.breadcrumbs.push(crumb);
            }

            var params = {prefix: path, delimiter: '/'};
            $swift.listObjects(container, params).then(function (result) {
                var items = result.data;
                for (var i = 0; i < items.length; i++) {
                    if (items[i].subdir == path + '/') {
                        // Add trailing slash for pseudo-directory
                        $location.path($location.path() + '/');
                        return;
                    }
                }

                $scope.items = $.map(items, function (item) {
                    var parts = (item.subdir || item.name).split('/');

                    if (item.subdir) {
                        return {name: item.subdir,
                                title: parts[parts.length - 2] + '/',
                                bytes: '\u2014'}; // em dash
                    } else {
                        item.title = parts[parts.length - 1];
                        return item;
                    }
                });
            });
        }
    ]);

@@ -347,304 +347,350 @@
 %0A%7D%0A%0A
-angular.module('swiftBrowser.controllers', %5B'swiftBrowser.swift'%5D)%0A    .controller('RootCtrl', %5B'$scope', '$swift', function($scope, $swift) %7B%0A        $scope.containers = %5B%5D;%0A        $scope.updateOrderBy = mkUpdateOrderBy($scope);%0A        $scope.updateOrderBy('name');%0A%0A        $scope.toggleAll =
+function mkAllSelected($scope, key) %7B%0A    return function () %7B%0A        var collection = $scope%5Bkey%5D;%0A        for (var i = 0; i %3C collection.length; i++) %7B%0A            if (!collection%5Bi%5D.selected) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D;%0A%7D%0A%0Afunction mkToggleAll($scope, key, allSelected) %7B%0A    return
  fun
@@ -686,33 +686,32 @@
  return function
- 
 () %7B%0A           
@@ -695,32 +695,66 @@
 unction() %7B%0A    
+    var collection = $scope%5Bkey%5D;%0A
         var newV
@@ -761,23 +761,16 @@
 alue = !
-$scope.
 allSelec
@@ -776,20 +776,16 @@
 cted();%0A
-    
         
@@ -804,33 +804,26 @@
  0; i %3C 
-$scope.containers
+collection
 .length;
@@ -842,37 +842,26 @@
         
-   
- $scope.containers
+collection
 %5Bi%5D.sele
@@ -889,221 +889,416 @@
     
-    %7D%0A   
+%7D%0A
     
- 
 %7D;%0A
-%0A        $scope.allSelected = function () %7B%0A            for (var i = 0; i %3C $scope.containers.length; i++) %7B%0A         
+%7D%0A%0Aangular.module('swiftBrowser.controllers', %5B'swiftBrowser.swift'%5D)%0A    .controller('RootCtrl', %5B'$scope', '$swift', function($scope, $swift) %7B%0A        $scope.containers = %5B%5D;%0A        $scope.updateOrderBy = mkUpdateOrderBy($scope);%0A
        
-if (!
+ 
 $scope.
-containers%5Bi%5D.selected) %7B%0A                    return false;
+updateOrderBy('name');%0A%0A        $scope.allSelected = mkAllSelected($scope, 'containers');%0A        $scope.toggleAll = mkToggleAll($scope, 'containers',
 %0A   
@@ -1302,34 +1302,32 @@
                 
-%7D%0A
             %7D%0A  
@@ -1314,34 +1314,32 @@
                 
-%7D%0A
             retu
@@ -1337,31 +1337,27 @@
     
- return true;%0A        %7D
+$scope.allSelected)
 ;%0A%0A 

refactor allSelected and toggleAll funcs