// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.services', ['core']);

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('core').config(function($routeProvider) {
    $routeProvider.when('/view/services', {
      templateUrl: '/services:resources/partial/index.html',
      controller: 'ServicesIndexController'
    });
    return $routeProvider.when('/view/services/:managerId', {
      templateUrl: '/services:resources/partial/index.html',
      controller: 'ServicesIndexController'
    });
  });

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.services').controller('ServicesIndexController', function($scope, $routeParams, notify, pageTitle, services) {
    pageTitle.set('Services');
    $scope.services = [];
    services.getManagers().then(function(managers) {
      var manager, _i, _len, _ref, _results;
      $scope.managers = managers;
      _ref = $scope.managers;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        manager = _ref[_i];
        if ($routeParams.managerId && manager.id !== $routeParams.managerId) {
          continue;
        }
        _results.push(services.getServices(manager.id).then(function(services) {
          var service, _j, _len1, _results1;
          _results1 = [];
          for (_j = 0, _len1 = services.length; _j < _len1; _j++) {
            service = services[_j];
            _results1.push($scope.services.push(service));
          }
          return _results1;
        }));
      }
      return _results;
    });
    return $scope.runOperation = function(service, operation) {
      return services.runOperation(service, operation).then(function() {
        return services.getService(service.managerId, service.id).then(function(data) {
          angular.copy(data, service);
          return notify.success('Done');
        });
      })["catch"](function(err) {
        return notify.error('Service operation failed', err.message);
      });
    };
  });

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.services').controller('ServiceWidgetController', function($scope, services, notify) {
    $scope.$on('widget-update', function($event, id, data) {
      if (id !== $scope.widget.id) {
        return;
      }
      return $scope.service = data;
    });
    return $scope.runOperation = function(o) {
      var svc;
      svc = {
        managerId: $scope.widget.config.manager_id,
        id: $scope.widget.config.service_id
      };
      return services.runOperation(svc, o)["catch"](function(e) {
        return notify.error('Service operation failed', e.message);
      });
    };
  });

  angular.module('ajenti.services').controller('ServiceWidgetConfigController', function($scope, services) {
    $scope.services = [];
    return services.getManagers().then(function(managers) {
      var manager, _i, _len, _ref, _results;
      $scope.managers = managers;
      _ref = $scope.managers;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        manager = _ref[_i];
        _results.push(services.getServices(manager.id).then(function(services) {
          var service, _j, _len1, _results1;
          _results1 = [];
          for (_j = 0, _len1 = services.length; _j < _len1; _j++) {
            service = services[_j];
            _results1.push($scope.services.push(service));
          }
          return _results1;
        }));
      }
      return _results;
    });
  });

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.services').service('services', function($http, $q) {
    this.getManagers = function() {
      var q;
      q = $q.defer();
      $http.get("/api/services/managers").success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.getServices = function(managerId) {
      var q;
      q = $q.defer();
      $http.get("/api/services/list/" + managerId).success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.getService = function(managerId, serviceId) {
      var q;
      q = $q.defer();
      $http.get("/api/services/get/" + managerId + "/" + serviceId).success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.runOperation = function(service, operation) {
      var q;
      q = $q.defer();
      $http.get("/api/services/do/" + operation + "/" + service.managerId + "/" + service.id).success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    return this;
  });

}).call(this);

