'use strict';

var remote = require('remote');
var fs = require('fs');
var app = remote.require('app');

var configPath = app.getPath('userData') + '/config.json';

angular.module('gitlab.config', [])
.controller('PrefsCtrl', ['$http', '$scope', '$utils', function($http, $scope, $utils) {

  /*
    Basic
   */
  $scope.gitlab = fs.existsSync(configPath) ? require(configPath) : {};

  $scope.savePrefs = function() {
    if ($scope.gitlab.token) {
      $http.defaults.headers.common['PRIVATE-TOKEN'] = $scope.gitlab.token;
    }

    fs.writeFileSync(configPath, JSON.stringify($scope.gitlab) || {});
  };


  /*
    Repos
   */

  function load(pageNum) {
    pageNum = pageNum || 1;
    return $http.get($scope.gitlab.api + '/projects?page=' + pageNum)
  }

  $scope.page = {};
  $scope.refreshRepos = function(pageNum) {
    pageNum = pageNum || 1;
    load(1)
      .success(function(res, status, headers) {
        $scope.projects = res;
        $scope.page = $utils.pageInfo(headers('Link'));
      });
  };

  $scope.loadMore = function(page) {
    load($scope.page.next)
      .success(function(res, status, headers) {
        $scope.projects = $scope.projects.concat(res);
        $scope.page = $utils.pageInfo(headers('Link'));
      });
  };

}]);

'use strict';

var remote = require('remote');
var fs = require('fs');
var app = remote.require('app');

var configPath = app.getPath('userData') + '/config.json';

angular.module('gitlab.config', [])
.controller('PrefsCtrl', ['$http', '$scope', '$utils', function($http, $scope, $utils) {

  /*
    Basic
   */
  $scope.gitlab = fs.existsSync(configPath) ? require(configPath) : {};

  $scope.savePrefs = function() {
    if ($scope.gitlab.token) {
      $http.defaults.headers.common['PRIVATE-TOKEN'] = $scope.gitlab.token;
    }

    fs.writeFileSync(configPath, JSON.stringify($scope.gitlab) || {});
  };


  /*
    Repos
   */

  function load(pageNum) {
    pageNum = pageNum || 1;
    return $http.get($scope.gitlab.api + '/projects?order_by=last_activity_at&page=' + pageNum);
  }

  $scope.page = {};
  $scope.refreshRepos = function(pageNum) {
    pageNum = pageNum || 1;
    load(1)
      .success(function(res, status, headers) {
        $scope.projects = res;
        $scope.page = $utils.pageInfo(headers('Link'));
      });
  };

  $scope.loadMore = function(page) {
    load($scope.page.next)
      .success(function(res, status, headers) {
        $scope.projects = $scope.projects.concat(res);
        $scope.page = $utils.pageInfo(headers('Link'));
      });
  };

}]);

order projects by last_activity_at