harbor/static/resources/js/components/details/retrieve-projects.directive.js

159 lines
5.0 KiB
JavaScript
Raw Normal View History

(function() {
'use strict';
angular
.module('harbor.details')
.directive('retrieveProjects', retrieveProjects);
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) {
var vm = this;
vm.projectName = '';
vm.isOpen = false;
2016-05-04 12:42:05 +02:00
if(getParameterByName('is_public', $location.absUrl())) {
vm.isPublic = getParameterByName('is_public', $location.absUrl()) === 'true' ? 1 : 0;
vm.publicity = (vm.isPublic === 1) ? true : false;
}
2016-05-04 12:42:05 +02:00
vm.retrieve = retrieve;
2016-07-02 12:20:31 +02:00
vm.filterInput = '';
vm.selectItem = selectItem;
vm.checkProjectMember = checkProjectMember;
2016-05-04 12:42:05 +02:00
$scope.$watch('vm.selectedProject', function(current, origin) {
if(current) {
vm.selectedId = current.project_id;
2016-05-04 12:42:05 +02:00
}
});
2016-05-04 12:42:05 +02:00
$scope.$watch('vm.publicity', function(current, origin) {
vm.publicity = current ? true : false;
vm.isPublic = vm.publicity ? 1 : 0;
vm.projectType = (vm.isPublic === 1) ? 'public_projects' : 'my_projects';
vm.retrieve();
2016-05-04 12:42:05 +02:00
});
function retrieve() {
ListProjectService(vm.projectName, vm.isPublic)
.success(getProjectSuccess)
.error(getProjectFailed);
}
function getProjectSuccess(data, status) {
vm.projects = data;
if(vm.projects == null) {
vm.isPublic = 1;
vm.publicity = true;
vm.projectType = 'public_projects';
console.log('vm.projects is null, load public projects.');
return;
}
if(angular.isArray(vm.projects) && vm.projects.length > 0) {
vm.selectedProject = vm.projects[0];
}else{
$window.location.href = '/project';
}
if(getParameterByName('project_id', $location.absUrl())){
angular.forEach(vm.projects, function(value, index) {
if(value['project_id'] === Number(getParameterByName('project_id', $location.absUrl()))) {
vm.selectedProject = value;
}
2016-05-04 12:42:05 +02:00
});
}
$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;
});
}
function getProjectFailed(data) {
$scope.$emit('modalTitle', $filter('tr')('error'));
$scope.$emit('modalMessage', $filter('tr')('failed_to_get_project'));
$scope.$emit('raiseError', true);
console.log('Failed to list projects.');
}
function selectItem(item) {
2016-04-27 12:29:55 +02:00
vm.selectedProject = item;
$location.search('project_id', vm.selectedProject.project_id);
}
$scope.$on('$locationChangeSuccess', function(e) {
var projectId = getParameterByName('project_id', $location.absUrl());
vm.isOpen = false;
});
2016-04-27 12:29:55 +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) {
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());
}
}
function retrieveProjects() {
var directive = {
restrict: 'E',
templateUrl: '/static/resources/js/components/details/retrieve-projects.directive.html',
scope: {
2016-04-19 19:20:07 +02:00
'isOpen': '=',
2016-05-04 12:42:05 +02:00
'selectedProject': '=',
'publicity': '=',
'isProjectMember': '='
},
link: link,
controller: RetrieveProjectsController,
bindToController: true,
controllerAs: 'vm'
2016-05-23 12:29:17 +02:00
};
return directive;
2016-04-28 19:36:39 +02:00
function link(scope, element, attrs, ctrl) {
$(document).on('click', clickHandler);
function clickHandler(e) {
$('[data-toggle="popover"]').each(function () {
if (!$(this).is(e.target) &&
$(this).has(e.target).length === 0 &&
$('.popover').has(e.target).length === 0) {
$(this).parent().popover('hide');
}
});
var targetId = $(e.target).attr('id');
if(targetId === 'switchPane' ||
targetId === 'retrievePane' ||
targetId === 'retrieveFilter') {
return;
}else{
ctrl.isOpen = false;
scope.$apply();
}
}
}
}
})();