2016-07-07 18:03:32 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2016-04-17 17:53:26 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.details')
|
|
|
|
.directive('retrieveProjects', retrieveProjects);
|
2016-04-26 12:23:34 +02:00
|
|
|
|
2016-07-02 12:20:31 +02:00
|
|
|
RetrieveProjectsController.$inject = ['$scope', 'nameFilter', '$filter', 'trFilter', 'ListProjectService', '$location', 'getParameterByName', 'CurrentProjectMemberService', '$window'];
|
2016-04-19 19:20:07 +02:00
|
|
|
|
2016-07-02 12:20:31 +02:00
|
|
|
function RetrieveProjectsController($scope, nameFilter, $filter, trFilter, ListProjectService, $location, getParameterByName, CurrentProjectMemberService, $window) {
|
2016-04-26 12:23:34 +02:00
|
|
|
var vm = this;
|
2016-05-17 05:40:36 +02:00
|
|
|
|
2016-05-03 12:29:58 +02:00
|
|
|
vm.projectName = '';
|
2016-05-19 12:54:30 +02:00
|
|
|
vm.isOpen = false;
|
2016-09-02 12:16:58 +02:00
|
|
|
vm.isProjectMember = false;
|
2016-09-13 11:02:31 +02:00
|
|
|
vm.target = $location.path().substr(1) || 'repositories';
|
2016-09-02 12:16:58 +02:00
|
|
|
|
|
|
|
vm.isPublic = Number(getParameterByName('is_public', $location.absUrl()));
|
2016-09-30 07:23:50 +02:00
|
|
|
|
|
|
|
var DEFAULT_PAGE = 1;
|
2016-10-12 08:59:40 +02:00
|
|
|
|
2016-10-12 06:37:15 +02:00
|
|
|
vm.page = DEFAULT_PAGE;
|
2016-10-12 08:59:40 +02:00
|
|
|
|
2016-10-12 06:37:15 +02:00
|
|
|
vm.projects = [];
|
2016-05-04 12:42:05 +02:00
|
|
|
vm.retrieve = retrieve;
|
2016-07-02 12:20:31 +02:00
|
|
|
vm.filterInput = '';
|
2016-06-01 08:50:34 +02:00
|
|
|
vm.selectItem = selectItem;
|
|
|
|
vm.checkProjectMember = checkProjectMember;
|
2016-09-13 18:40:42 +02:00
|
|
|
|
2016-05-04 12:42:05 +02:00
|
|
|
function retrieve() {
|
2016-10-12 08:59:40 +02:00
|
|
|
ListProjectService(vm.projectName, vm.isPublic, vm.page)
|
2016-10-12 06:37:15 +02:00
|
|
|
.then(getProjectSuccess, getProjectFailed);
|
2016-05-04 12:42:05 +02:00
|
|
|
}
|
2016-05-03 12:29:58 +02:00
|
|
|
|
2016-10-12 06:37:15 +02:00
|
|
|
$scope.$watch('vm.page', function(current) {
|
|
|
|
if(current) {
|
|
|
|
vm.retrieve();
|
|
|
|
}
|
|
|
|
});
|
2016-09-13 18:40:42 +02:00
|
|
|
|
|
|
|
$scope.$watch('vm.isPublic', function(current) {
|
|
|
|
vm.projectType = vm.isPublic === 0 ? 'my_project_count' : 'public_project_count';
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.$watch('vm.selectedProject', function(current) {
|
|
|
|
if(current) {
|
|
|
|
vm.selectedId = current.project_id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-12 08:59:40 +02:00
|
|
|
function parseNextLink(link) {
|
|
|
|
if(link === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var parts = link.split(",");
|
|
|
|
for(var i in parts) {
|
|
|
|
var groups = /^\<(.*)\>;\srel=\"(\w+)\"$/.exec($.trim(parts[i]));
|
|
|
|
if(groups && groups.length > 2){
|
|
|
|
var url = groups[1];
|
|
|
|
var rel = groups[2];
|
|
|
|
if(rel === 'next') {
|
|
|
|
return {
|
|
|
|
'page': getParameterByName('page', url),
|
|
|
|
'rel' : rel
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 06:37:15 +02:00
|
|
|
function getProjectSuccess(response) {
|
|
|
|
|
|
|
|
var partialProjects = response.data || [];
|
|
|
|
for(var i in partialProjects) {
|
|
|
|
vm.projects.push(partialProjects[i]);
|
2016-05-11 09:57:52 +02:00
|
|
|
}
|
2016-10-12 06:37:15 +02:00
|
|
|
|
2016-10-12 08:59:40 +02:00
|
|
|
var nextLink = parseNextLink(response.headers("Link") || '');
|
2016-10-12 06:37:15 +02:00
|
|
|
|
2016-10-12 08:59:40 +02:00
|
|
|
if(nextLink) {
|
|
|
|
vm.page = parseInt(nextLink.page);
|
2016-10-12 06:37:15 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if(vm.projects.length == 0 && vm.isPublic === 0){
|
|
|
|
$window.location.href = '/project';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(getParameterByName('project_id', $location.absUrl())){
|
|
|
|
for(var i in vm.projects) {
|
|
|
|
var project = vm.projects[i];
|
|
|
|
if(project['project_id'] == getParameterByName('project_id', $location.absUrl())) {
|
|
|
|
vm.selectedProject = project;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$location.search('project_id', vm.selectedProject.project_id);
|
|
|
|
vm.checkProjectMember(vm.selectedProject.project_id);
|
|
|
|
|
|
|
|
vm.resultCount = vm.projects.length;
|
|
|
|
|
|
|
|
$scope.$watch('vm.filterInput', function(current, origin) {
|
|
|
|
vm.resultCount = $filter('name')(vm.projects, vm.filterInput, 'name').length;
|
|
|
|
});
|
2016-05-03 12:29:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 06:37:15 +02:00
|
|
|
function getProjectFailed(response) {
|
2016-07-07 07:15:00 +02:00
|
|
|
$scope.$emit('modalTitle', $filter('tr')('error'));
|
|
|
|
$scope.$emit('modalMessage', $filter('tr')('failed_to_get_project'));
|
|
|
|
$scope.$emit('raiseError', true);
|
2016-07-06 06:56:44 +02:00
|
|
|
console.log('Failed to list projects.');
|
2016-05-03 12:29:58 +02:00
|
|
|
}
|
2016-06-01 08:01:35 +02:00
|
|
|
|
2016-04-26 12:23:34 +02:00
|
|
|
function selectItem(item) {
|
2016-04-27 12:29:55 +02:00
|
|
|
vm.selectedProject = item;
|
2016-06-14 08:54:00 +02:00
|
|
|
$location.search('project_id', vm.selectedProject.project_id);
|
2016-09-05 12:09:05 +02:00
|
|
|
$scope.$emit('projectChanged', true);
|
2016-04-26 12:23:34 +02:00
|
|
|
}
|
2016-06-01 08:01:35 +02:00
|
|
|
|
|
|
|
$scope.$on('$locationChangeSuccess', function(e) {
|
2016-09-02 12:16:58 +02:00
|
|
|
vm.projectId = getParameterByName('project_id', $location.absUrl());
|
|
|
|
vm.isOpen = false;
|
2016-09-05 12:09:05 +02:00
|
|
|
vm.checkProjectMember(vm.selectedProject.project_id);
|
2016-06-01 08:01:35 +02:00
|
|
|
});
|
2016-04-27 12:29:55 +02:00
|
|
|
|
2016-06-01 08:50:34 +02:00
|
|
|
function checkProjectMember(projectId) {
|
|
|
|
CurrentProjectMemberService(projectId)
|
|
|
|
.success(getCurrentProjectMemberSuccess)
|
|
|
|
.error(getCurrentProjectMemberFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentProjectMemberSuccess(data, status) {
|
|
|
|
console.log('Successful get current project member:' + status);
|
|
|
|
vm.isProjectMember = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentProjectMemberFailed(data, status) {
|
2016-09-02 12:16:58 +02:00
|
|
|
vm.isProjectMember = false;
|
2016-07-02 12:20:31 +02:00
|
|
|
console.log('Current user has no member for the project:' + status + ', location.url:' + $location.url());
|
2016-09-13 18:40:42 +02:00
|
|
|
vm.target = 'repositories';
|
2016-06-01 08:50:34 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 17:53:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function retrieveProjects() {
|
|
|
|
var directive = {
|
|
|
|
restrict: 'E',
|
2016-06-16 08:10:35 +02:00
|
|
|
templateUrl: '/static/resources/js/components/details/retrieve-projects.directive.html',
|
2016-04-17 17:53:26 +02:00
|
|
|
scope: {
|
2016-09-02 12:16:58 +02:00
|
|
|
'target': '=',
|
2016-04-19 19:20:07 +02:00
|
|
|
'isOpen': '=',
|
2016-05-04 12:42:05 +02:00
|
|
|
'selectedProject': '=',
|
2016-09-13 18:40:42 +02:00
|
|
|
'isPublic': '=',
|
2016-05-04 12:42:05 +02:00
|
|
|
'isProjectMember': '='
|
2016-04-17 17:53:26 +02:00
|
|
|
},
|
2016-05-19 12:54:30 +02:00
|
|
|
link: link,
|
2016-04-17 17:53:26 +02:00
|
|
|
controller: RetrieveProjectsController,
|
|
|
|
bindToController: true,
|
|
|
|
controllerAs: 'vm'
|
2016-05-23 12:29:17 +02:00
|
|
|
};
|
2016-04-17 17:53:26 +02:00
|
|
|
|
|
|
|
return directive;
|
2016-04-28 19:36:39 +02:00
|
|
|
|
2016-05-19 12:54:30 +02:00
|
|
|
function link(scope, element, attrs, ctrl) {
|
|
|
|
$(document).on('click', clickHandler);
|
2016-09-02 12:16:58 +02:00
|
|
|
|
2016-05-19 12:54:30 +02:00
|
|
|
function clickHandler(e) {
|
2016-05-24 07:50:09 +02:00
|
|
|
$('[data-toggle="popover"]').each(function () {
|
2016-06-01 08:50:34 +02:00
|
|
|
if (!$(this).is(e.target) &&
|
|
|
|
$(this).has(e.target).length === 0 &&
|
|
|
|
$('.popover').has(e.target).length === 0) {
|
|
|
|
$(this).parent().popover('hide');
|
2016-05-24 04:53:52 +02:00
|
|
|
}
|
|
|
|
});
|
2016-05-19 12:54:30 +02:00
|
|
|
var targetId = $(e.target).attr('id');
|
|
|
|
if(targetId === 'switchPane' ||
|
|
|
|
targetId === 'retrievePane' ||
|
|
|
|
targetId === 'retrieveFilter') {
|
|
|
|
return;
|
|
|
|
}else{
|
|
|
|
ctrl.isOpen = false;
|
|
|
|
scope.$apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:53:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|