This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/directives/ui/header-bar.js
2015-04-07 18:38:58 -04:00

234 lines
No EOL
7.2 KiB
JavaScript

/**
* The application header bar.
*/
angular.module('quay').directive('headerBar', function () {
var number = 0;
var directiveDefinitionObject = {
priority: 0,
templateUrl: '/static/directives/header-bar.html',
replace: false,
transclude: false,
restrict: 'C',
scope: {
},
controller: function($rootScope, $scope, $element, $location, $timeout, UserService, PlanService, ApiService, NotificationService, Config, CreateService) {
$scope.notificationService = NotificationService;
$scope.searchVisible = false;
$scope.currentSearchQuery = null;
$scope.searchResultState = null;
$scope.showBuildDialogCounter = 0;
// Monitor any user changes and place the current user into the scope.
UserService.updateUserIn($scope);
$scope.isNewLayout = Config.isNewLayout();
$scope.currentPageContext = {};
$rootScope.$watch('currentPage.scope.viewuser', function(u) {
$scope.currentPageContext['viewuser'] = u;
});
$rootScope.$watch('currentPage.scope.organization', function(o) {
$scope.currentPageContext['organization'] = o;
});
$rootScope.$watch('currentPage.scope.repository', function(r) {
$scope.currentPageContext['repository'] = r;
});
var conductSearch = function(query) {
if (!query) { $scope.searchResultState = null; return; }
$scope.searchResultState = {
'state': 'loading'
};
var params = {
'query': query
};
ApiService.conductSearch(null, params).then(function(resp) {
if (!$scope.searchVisible) { return; }
$scope.searchResultState = {
'state': resp.results.length ? 'results' : 'no-results',
'results': resp.results,
'current': -1
};
}, function(resp) {
$scope.searchResultState = null;
}, /* background */ true);
};
$scope.$watch('currentSearchQuery', conductSearch);
$scope.signout = function() {
ApiService.logout().then(function() {
UserService.load();
$location.path('/');
});
};
$scope.appLinkTarget = function() {
if ($("div[ng-view]").length === 0) {
return "_self";
}
return "";
};
$scope.getEnterpriseLogo = function() {
if (!Config.ENTERPRISE_LOGO_URL) {
return '/static/img/quay-logo.png';
}
return Config.ENTERPRISE_LOGO_URL;
};
$scope.toggleSearch = function() {
$scope.searchVisible = !$scope.searchVisible;
if ($scope.searchVisible) {
$('#search-box-input').focus();
if ($scope.currentSearchQuery) {
conductSearch($scope.currentSearchQuery);
}
} else {
$scope.searchResultState = null;
}
};
$scope.getSearchBoxClasses = function(searchVisible, searchResultState) {
var classes = searchVisible ? 'search-visible ' : '';
if (searchResultState) {
classes += 'results-visible';
}
return classes;
};
$scope.handleSearchKeyDown = function(e) {
if (e.keyCode == 27) {
$scope.toggleSearch();
return;
}
if (!$scope.searchResultState) { return; }
if (e.keyCode == 40) {
$scope.searchResultState['current']++;
e.preventDefault();
} else if (e.keyCode == 38) {
$scope.searchResultState['current']--;
e.preventDefault();
} else if (e.keyCode == 13) {
var current = $scope.searchResultState['current'];
if (current >= 0 &&
current < $scope.searchResultState['results'].length) {
$scope.showResult($scope.searchResultState['results'][current]);
}
e.preventDefault();
}
if (!$scope.searchResultState) { return; }
if ($scope.searchResultState['current'] < -1) {
$scope.searchResultState['current'] = $scope.searchResultState['results'].length - 1;
} else if ($scope.searchResultState['current'] >= $scope.searchResultState['results'].length) {
$scope.searchResultState['current'] = 0;
}
};
$scope.showResult = function(result) {
$scope.toggleSearch();
$timeout(function() {
$location.url(result['href'])
}, 500);
};
$scope.setCurrentResult = function(result) {
if (!$scope.searchResultState) { return; }
$scope.searchResultState['current'] = result;
};
$scope.getNamespace = function(context) {
if (!context) { return null; }
if (context.repository && context.repository.namespace) {
return context.repository.namespace;
}
if (context.organization && context.organization.name) {
return context.organization.name;
}
if (context.viewuser && context.viewuser.username) {
return context.viewuser.username;
}
return null;
};
$scope.canAdmin = function(namespace) {
if (!namespace) { return false; }
return UserService.isNamespaceAdmin(namespace);
};
$scope.isOrganization = function(namespace) {
if (!namespace) { return false; }
return UserService.isOrganization(namespace);
};
$scope.startBuild = function(context) {
$scope.showBuildDialogCounter++;
};
$scope.handleBuildStarted = function(build, context) {
$location.url('/repository/' + context.repository.namespace + '/' + context.repository.name + '/build/' + build.id);
};
$scope.createRobot = function(context) {
var namespace = $scope.getNamespace(context);
if (!namespace || !UserService.isNamespaceAdmin(namespace)) { return; }
var isorg = UserService.isOrganization(namespace);
bootbox.prompt('Enter the name of the new robot account', function(robotname) {
if (!robotname) { return; }
var regex = new RegExp(ROBOT_PATTERN);
if (!regex.test(robotname)) {
bootbox.alert('Invalid robot account name');
return;
}
CreateService.createRobotAccount(ApiService, isorg, namespace, robotname, function(created) {
if (isorg) {
$location.url('/organization/' + namespace + '?tab=robots');
} else {
$location.url('/user/' + namespace + '?tab=robots');
}
});
});
};
$scope.createTeam = function(context) {
var namespace = $scope.getNamespace(context);
if (!namespace || !UserService.isNamespaceAdmin(namespace)) { return; }
bootbox.prompt('Enter the name of the new team', function(teamname) {
if (!teamname) { return; }
var regex = new RegExp(TEAM_PATTERN);
if (!regex.test(teamname)) {
bootbox.alert('Invalid team name');
return;
}
CreateService.createOrganizationTeam(ApiService, namespace, teamname, function(created) {
$location.url('/organization/' + namespace + '/teams/' + teamname);
});
});
};
}
};
return directiveDefinitionObject;
});