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

128 lines
3.7 KiB
JavaScript
Raw Normal View History

(function() {
'use strict';
angular
.module('harbor.details')
.directive('retrieveProjects', retrieveProjects);
RetrieveProjectsController.$inject = ['$scope', 'nameFilter', '$filter', 'CurrentProjectMemberService', 'ListProjectService', '$routeParams', '$route', '$location'];
2016-04-19 19:20:07 +02:00
function RetrieveProjectsController($scope, nameFilter, $filter, CurrentProjectMemberService, ListProjectService, $routeParams, $route, $location) {
var vm = this;
vm.projectName = '';
2016-05-04 12:42:05 +02:00
if($route.current.params.is_public) {
vm.isPublic = $route.current.params.is_public === 'true' ? 1 : 0;
vm.publicity = (vm.isPublic === 1) ? true : false;
}
2016-05-04 12:42:05 +02:00
vm.retrieve = retrieve;
vm.retrieve();
vm.checkProjectMember = checkProjectMember;
$scope.$watch('vm.selectedProject', function(current, origin) {
if(current) {
vm.selectedId = current.ProjectId;
}
});
vm.filterInput = "";
vm.selectItem = selectItem;
$scope.$watch('vm.publicity', function(current, origin) {
vm.publicity = current ? 1 : 0;
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(!angular.isDefined(vm.projects)) {
vm.isPublic = 1;
vm.publicity = 1;
vm.projectType = 'public_projects';
console.log('vm.projects is undefined, load public projects.');
}
vm.selectedProject = vm.projects[0];
if($routeParams.project_id){
angular.forEach(vm.projects, function(value, index) {
if(value['ProjectId'] == $routeParams.project_id) {
vm.selectedProject = value;
}
2016-05-04 12:42:05 +02:00
});
}
2016-05-04 12:42:05 +02:00
$location.search('project_id', vm.selectedProject.ProjectId);
vm.checkProjectMember(vm.selectedProject.ProjectId);
vm.resultCount = vm.projects.length;
$scope.$watch('vm.filterInput', function(current, origin) {
vm.resultCount = $filter('name')(vm.projects, vm.filterInput, 'Name').length;
});
}
function getProjectFailed(response) {
console.log('Failed to list projects:' + response);
}
2016-05-04 12:42:05 +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;
}
2016-04-28 19:36:39 +02:00
2016-05-04 12:42:05 +02:00
function getCurrentProjectMemberFailed(data, status) {
console.log('Failed get current project member:' + status);
vm.isProjectMember = false;
}
function selectItem(item) {
2016-04-27 12:29:55 +02:00
vm.selectedId = item.ProjectId;
vm.selectedProject = item;
2016-05-04 12:42:05 +02:00
vm.checkProjectMember(vm.selectedProject.ProjectId);
2016-04-27 12:29:55 +02:00
vm.isOpen = false;
2016-05-04 12:42:05 +02:00
$location.search('project_id', vm.selectedProject.ProjectId);
}
2016-04-27 12:29:55 +02:00
}
function retrieveProjects() {
var directive = {
restrict: 'E',
templateUrl: '/static/ng/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': '='
},
replace: true,
controller: RetrieveProjectsController,
bindToController: true,
controllerAs: 'vm'
}
return directive;
2016-04-28 19:36:39 +02:00
}
})();