define(function () { app.registerController('AdminCtrl', ['$scope', '$http', '$sce', '$uibModal', '$rootScope', function ($scope, $http, $sce, $modal, $rootScope) { $scope.upgrade = JSON.parse(JSON.stringify($scope.semaphore)); if ($scope.upgrade && $scope.upgrade.updateBody) { $scope.upgrade.updateBody = $sce.trustAsHtml($scope.upgrade.updateBody); } $scope.checkUpdate = function () { $http.get('/upgrade').success(function (upgrade) { if (!upgrade) return; if (upgrade.updateBody) { upgrade.updateBody = $sce.trustAsHtml(upgrade.updateBody); } $scope.upgrade = upgrade; }); } $scope.doUpgrade = function () { var upgradeModal = $modal.open({ template: '', keyboard: false, size: 'sm', scope: $scope }); $http.post('/upgrade').success(function () { $scope.upgraded = true; $scope.pollUpgrade(upgradeModal, 0); }).error(function () { swal('Error upgrading', arguments, 'error'); }); } $scope.pollUpgrade = function (modalInstance, attempts) { $rootScope.refreshInfo(function (err) { if ($rootScope.semaphore.version == $scope.upgrade.update.tag_name.substr(1)) { modalInstance.dismiss(); return; } setTimeout(function () { if (attempts >= 60) { swal('Error', 'Upgrade seems to be taking a long time. Check server logs!', 'error'); modalInstance.dismiss(); return; } $scope.pollUpgrade(modalInstance, attempts+1); }, 1000); }); } }]); });