mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			562 lines
		
	
	
		
			18 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			562 lines
		
	
	
		
			18 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| (function(cloudStack, testData) {
 | |
|   cloudStack.projects = {
 | |
|     requireInvitation: function(args) {
 | |
|       return window.g_projectsInviteRequired;
 | |
|     },
 | |
| 
 | |
|     add: function(args) {
 | |
|       setTimeout(function() {
 | |
|         $.ajax({
 | |
|           url: createURL('createProject', { ignoreProject: true }),
 | |
|           data: {
 | |
|             account: args.context.users[0].account,
 | |
|             domainId: args.context.users[0].domainid,
 | |
|             name: args.data['project-name'],
 | |
|             displayText: args.data['project-display-text']
 | |
|           },
 | |
|           dataType: 'json',
 | |
|           async: true,
 | |
|           success: function(data) {
 | |
|             args.response.success({
 | |
|               data: {
 | |
|                 id: data.createprojectresponse.id,
 | |
|                 name: args.data['project-name'],
 | |
|                 displayText: args.data['project-display-text'],
 | |
|                 users: []
 | |
|               }
 | |
|             });
 | |
|           },
 | |
|           error: function() {
 | |
|             args.response.error('Could not create project.');
 | |
|           }
 | |
|         });
 | |
|       }, 100);
 | |
|     },
 | |
|     inviteForm: {
 | |
|       noSelect: true,
 | |
|       fields: {
 | |
|         'email': { edit: true, label: 'E-mail' },
 | |
|         'account': { edit: 'ignore', label: 'Account' },
 | |
|         'state': { edit: 'ignore', label: 'Status' },
 | |
|         'add-user': { addButton: true, label: '' }
 | |
|       },
 | |
|       add: {
 | |
|         label: 'E-mail invite',
 | |
|         action: function(args) {
 | |
|           $.ajax({
 | |
|             url: createURL('addAccountToProject', { ignoreProject: true }),
 | |
|             data: {
 | |
|               projectId: args.context.projects[0].id,
 | |
|               account: args.data.account,
 | |
|               email: args.data.email
 | |
|             },
 | |
|             dataType: 'json',
 | |
|             async: true,
 | |
|             success: function(data) {
 | |
|               data: args.data,
 | |
|               args.response.success({
 | |
|                 _custom: {
 | |
|                   jobId: data.addaccounttoprojectresponse.jobid
 | |
|                 },
 | |
|                 notification: {
 | |
|                   label: 'Invited user to project',
 | |
|                   poll: pollAsyncJobResult
 | |
|                 }
 | |
|               });
 | |
|             },
 | |
|             error: function(data) {
 | |
|               args.response.error('Could not create user');
 | |
|             }
 | |
|           });
 | |
|         }
 | |
|       },
 | |
|       actionPreFilter: function(args) {
 | |
|         if (cloudStack.context.projects &&
 | |
|             cloudStack.context.projects[0] &&
 | |
|             !cloudStack.context.projects[0].isNew) {
 | |
|           return args.context.actions;
 | |
|         }
 | |
| 
 | |
|         return ['destroy'];
 | |
|       },
 | |
| 
 | |
|       actions: {
 | |
|         destroy: {
 | |
|           label: 'Revoke invitation',
 | |
|           action: function(args) {
 | |
|             $.ajax({
 | |
|               url: createURL('deleteProjectInvitation'),
 | |
|               data: {
 | |
|                 id: args.context.multiRule[0].id
 | |
|               },
 | |
|               success: function(data) {
 | |
|                 args.response.success({
 | |
|                   _custom: { jobId: data.deleteprojectinvitationresponse.jobid },
 | |
|                   notification: {
 | |
|                     label: 'Un-invited user',
 | |
|                     poll: pollAsyncJobResult
 | |
|                   }
 | |
|                 });
 | |
|               }
 | |
|             });
 | |
|           }
 | |
|         }
 | |
|       },
 | |
| 
 | |
|       // Project users data provider
 | |
|       dataProvider: function(args) {
 | |
|         $.ajax({
 | |
|           url: createURL('listProjectInvitations', { ignoreProject: true }),
 | |
|           data: {
 | |
|             projectId: args.context.projects[0].id
 | |
|           },
 | |
|           dataType: 'json',
 | |
|           async: true,
 | |
|           success: function(data) {
 | |
|             var invites = data.listprojectinvitationsresponse.projectinvitation;
 | |
|             args.response.success({
 | |
|               data: $.map(invites, function(elem) {
 | |
|                 return {
 | |
|                   id: elem.id,
 | |
|                   account: elem.account,
 | |
|                   email: elem.email,
 | |
|                   state: elem.state
 | |
|                 };
 | |
|               })
 | |
|             });
 | |
|           }
 | |
|         });
 | |
|       }
 | |
|     },
 | |
|     addUserForm: {
 | |
|       noSelect: true,
 | |
|       fields: {
 | |
|         'username': { edit: true, label: 'Account' },
 | |
|         'role': { edit: 'ignore', label: 'Role' },
 | |
|         'add-user': { addButton: true, label: '' }
 | |
|       },
 | |
|       add: {
 | |
|         label: 'Add user',
 | |
|         action: function(args) {
 | |
|           $.ajax({
 | |
|             url: createURL('addAccountToProject', { ignoreProject: true }),
 | |
|             data: {
 | |
|               projectId: args.context.projects[0].id,
 | |
|               account: args.data.username
 | |
|             },
 | |
|             dataType: 'json',
 | |
|             async: true,
 | |
|             success: function(data) {
 | |
|               args.response.success({
 | |
|                 _custom: {
 | |
|                   jobId: data.addaccounttoprojectresponse.jobid
 | |
|                 },
 | |
|                 notification: {
 | |
|                   label: 'Added user to project',
 | |
|                   poll: pollAsyncJobResult
 | |
|                 }
 | |
|               });
 | |
| 
 | |
|               if (g_capabilities.projectinviterequired) {
 | |
|                 cloudStack.dialog.notice({ message: 'Invite sent to user; they will be added to the project once they accept the invitation' });
 | |
|               }
 | |
|             }
 | |
|           });
 | |
|         }
 | |
|       },
 | |
|       actionPreFilter: function(args) {
 | |
|         if (!cloudStack.context.projects) { // This is for the new project wizard
 | |
|           return ['destroy'];
 | |
|         }
 | |
| 
 | |
|         var project = cloudStack.context.projects[0];
 | |
|         var projectOwner = project.account;
 | |
|         var rowAccount = args.context.multiRule[0].account;
 | |
|         var userAccount = cloudStack.context.users[0].account;
 | |
|         var isEditableRow = rowAccount != projectOwner && userAccount == projectOwner;
 | |
| 
 | |
|         if (isEditableRow) {
 | |
|           return args.context.actions;
 | |
|         }
 | |
| 
 | |
|         return [];
 | |
|       },
 | |
|       actions: {
 | |
|         destroy: {
 | |
|           label: 'Remove user from project',
 | |
|           action: function(args) {
 | |
|             $.ajax({
 | |
|               url: createURL('deleteAccountFromProject', { ignoreProject: true }),
 | |
|               data: {
 | |
|                 projectId: args.context.projects[0].id,
 | |
|                 account: args.context.multiRule[0].username
 | |
|               },
 | |
|               dataType: 'json',
 | |
|               async: true,
 | |
|               success: function(data) {
 | |
|                 args.response.success({
 | |
|                   _custom: {
 | |
|                     jobId: data.deleteaccountfromprojectresponse.jobid
 | |
|                   },
 | |
|                   notification: {
 | |
|                     label: 'Removed user from project',
 | |
|                     poll: pollAsyncJobResult
 | |
|                   }
 | |
|                 });
 | |
|               },
 | |
|               error: function(data) {
 | |
|                 args.response.error('Could not remove user');
 | |
|               }
 | |
|             });
 | |
|           }
 | |
|         },
 | |
| 
 | |
|         makeOwner: {
 | |
|           label: 'Make user project owner',
 | |
|           action: function(args) {
 | |
|             $.ajax({
 | |
|               url: createURL('updateProject', { ignoreProject: true }),
 | |
|               data: {
 | |
|                 id: cloudStack.context.projects[0].id,
 | |
|                 account: args.context.multiRule[0].username
 | |
|               },
 | |
|               dataType: 'json',
 | |
|               async: true,
 | |
|               success: function(data) {
 | |
|                 args.response.success({
 | |
|                   _custom: {
 | |
|                     jobId: data.updateprojectresponse.jobid
 | |
|                   },
 | |
|                   notification: {
 | |
|                     label: 'Assigned new project owner',
 | |
|                     poll: pollAsyncJobResult
 | |
|                   }
 | |
|                 });
 | |
|               }
 | |
|             });
 | |
|           }
 | |
|         }
 | |
|       },
 | |
| 
 | |
|       // Project users data provider
 | |
|       dataProvider: function(args) {
 | |
|         $.ajax({
 | |
|           url: createURL('listProjectAccounts', { ignoreProject: true }),
 | |
|           data: {
 | |
|             projectId: args.context.projects[0].id
 | |
|           },
 | |
|           dataType: 'json',
 | |
|           async: true,
 | |
|           success: function(data) {
 | |
|             args.response.success({
 | |
|               data: $.map(data.listprojectaccountsresponse.projectaccount, function(elem) {
 | |
|                 return {
 | |
|                   id: elem.accountid,
 | |
|                   role: elem.role,
 | |
|                   username: elem.role == 'Owner' ?
 | |
|                     elem.account + ' (owner)' : elem.account
 | |
|                 };
 | |
|               })
 | |
|             });
 | |
|           }
 | |
|         });
 | |
|       }
 | |
|     },
 | |
| 
 | |
|     // Project listing data provider
 | |
|     dataProvider: function(args) {
 | |
|       var user = args.context.users[0];
 | |
| 
 | |
|       $.ajax({
 | |
|         url: createURL('listProjects', { ignoreProject: true }),
 | |
|         data: {
 | |
|           accountId: user.userid
 | |
|         },
 | |
|         dataType: 'json',
 | |
|         async: true,
 | |
|         success: function(data) {
 | |
|           args.response.success({
 | |
|             data: $.map(
 | |
|               data.listprojectsresponse.project ?
 | |
|                 data.listprojectsresponse.project : [],
 | |
|               function(elem) {
 | |
|               return $.extend(elem, {
 | |
|                 displayText: elem.displaytext
 | |
|               });
 | |
|             })
 | |
|           });
 | |
|         }
 | |
|       });
 | |
|     }
 | |
|   };
 | |
| 
 | |
|   cloudStack.sections.projects = {
 | |
|     title: 'Projects',
 | |
|     id: 'projects',
 | |
|     sectionSelect: {
 | |
|       label: 'Select view'
 | |
|     },
 | |
|     sections: {
 | |
|       projects: {
 | |
|         type: 'select',
 | |
|         id: 'projects',
 | |
|         title: 'Projects',
 | |
|         listView: {
 | |
|           fields: {
 | |
|             name: { label: 'Project Name' },
 | |
|             displaytext: { label: 'Display Text' },
 | |
|             domain: { label: 'Domain' },
 | |
|             account: { label: 'Owner' },
 | |
|             state: { label: 'Status', indicator: { 'Active': 'on', 'Destroyed': 'off', 'Disabled': 'off', 'Left Project': 'off' } }
 | |
|           },
 | |
| 
 | |
|           dataProvider: function(args) {
 | |
|             $.ajax({
 | |
|               url: createURL('listProjects', { ignoreProject: true }),
 | |
|               dataType: 'json',
 | |
|               async: true,
 | |
|               success: function(data) {
 | |
|                 args.response.success({
 | |
|                   data: data.listprojectsresponse.project,
 | |
|                   actionFilter: projectsActionFilter
 | |
|                 });
 | |
|               }
 | |
|             });
 | |
|           },
 | |
| 
 | |
|           actions: {
 | |
|             add: {
 | |
|               label: 'New Project',
 | |
|               action: {
 | |
|                 custom: function(args) {
 | |
|                   $(window).trigger('cloudStack.newProject');
 | |
|                 }
 | |
|               },
 | |
| 
 | |
|               actions: {
 | |
|                 add: {
 | |
|                   label: 'New Project',
 | |
|                   action: {
 | |
|                     custom: function(args) {
 | |
|                       $(window).trigger('cloudStack.newProject');
 | |
|                     }
 | |
|                   },
 | |
| 
 | |
|                   messages: {
 | |
|                     confirm: function(args) {
 | |
|                       return 'Are you sure you want to remove ' + args.name + '?';
 | |
|                     },
 | |
|                     notification: function(args) {
 | |
|                       return 'Removed project';
 | |
|                     }
 | |
|                   },
 | |
| 
 | |
|                   notification: {
 | |
|                     poll: testData.notifications.testPoll
 | |
|                   }
 | |
|                 },
 | |
| 
 | |
|                 destroy: {
 | |
|                   label: 'Remove project',
 | |
|                   action: function(args) {
 | |
|                     $.ajax({
 | |
|                       url: createURL('deleteProject', { ignoreProject: true }),
 | |
|                       data: {
 | |
|                         id: args.data.id
 | |
|                       },
 | |
|                       dataType: 'json',
 | |
|                       async: true,
 | |
|                       success: function(data) {
 | |
|                         args.response.success({
 | |
|                           _custom: {
 | |
|                             getUpdatedItem: function(data) {
 | |
|                               return $.extend(data, { state: 'Destroyed' });
 | |
|                             },
 | |
|                             getActionFilter: function(args) {
 | |
|                               return function() {
 | |
|                                 return [];
 | |
|                               };
 | |
|                             },
 | |
|                             jobId: data.deleteprojectresponse.jobid
 | |
|                           }
 | |
|                         });
 | |
|                       }
 | |
|                     });
 | |
|                   },
 | |
| 
 | |
|                   messages: {
 | |
|                     confirm: function(args) {
 | |
|                       return 'Are you sure you want to remove ' + args.name + '?';
 | |
|                     },
 | |
|                     notification: function(args) {
 | |
|                       return 'Removed project';
 | |
|                     }
 | |
|                   },
 | |
| 
 | |
|                   notification: {
 | |
|                     poll: pollAsyncJobResult
 | |
|                   }
 | |
|                 }
 | |
|               }
 | |
|             },
 | |
| 
 | |
|             destroy: {
 | |
|               label: 'Remove project',
 | |
|               action: function(args) {
 | |
|                 $.ajax({
 | |
|                   url: createURL('deleteProject', { ignoreProject: true }),
 | |
|                   data: {
 | |
|                     id: args.data.id
 | |
|                   },
 | |
|                   dataType: 'json',
 | |
|                   async: true,
 | |
|                   success: function(data) {
 | |
|                     args.response.success({
 | |
|                       _custom: {
 | |
|                         getUpdatedItem: function(data) {
 | |
|                           return $.extend(data, { state: 'Destroyed' });
 | |
|                         },
 | |
|                         getActionFilter: function(args) {
 | |
|                           return function() {
 | |
|                             return [];
 | |
|                           };
 | |
|                         },
 | |
|                         jobId: data.deleteprojectresponse.jobid
 | |
|                       }
 | |
|                     });
 | |
|                   }
 | |
|                 });
 | |
|               },
 | |
| 
 | |
|               messages: {
 | |
|                 confirm: function(args) {
 | |
|                   return 'Are you sure you want to remove ' + args.name + '?';
 | |
|                 },
 | |
|                 notification: function(args) {
 | |
|                   return 'Removed project';
 | |
|                 }
 | |
|               },
 | |
| 
 | |
|               notification: {
 | |
|                 poll: pollAsyncJobResult
 | |
|               }
 | |
|             }
 | |
|           }
 | |
|         }
 | |
|       },
 | |
| 
 | |
|       invitations: {
 | |
|         type: 'select',
 | |
|         id: 'invitations',
 | |
|         title: 'Invitations',
 | |
|         listView: {
 | |
|           fields: {
 | |
|             project: { label: 'Project' },
 | |
|             domain: { label: 'Domain' },
 | |
|             state: {
 | |
|               label: 'Status',
 | |
|               indicator: {
 | |
|                 'Accepted': 'on', 'Completed': 'on',
 | |
|                 'Pending': 'off', 'Declined': 'off'
 | |
|               }
 | |
|             }
 | |
|           },
 | |
| 
 | |
|           dataProvider: function(args) {
 | |
|             $.ajax({
 | |
|               url: createURL('listProjectInvitations'),
 | |
|               data: {
 | |
|                 account: cloudStack.context.users[0].account,
 | |
|                 domainid: cloudStack.context.users[0].domainid,
 | |
|                 state: 'Pending'
 | |
|               },
 | |
|               success: function(data) {
 | |
|                 args.response.success({
 | |
|                   actionFilter: projectInvitationActionFilter,
 | |
|                   data: data.listprojectinvitationsresponse.projectinvitation
 | |
|                 });
 | |
|               }
 | |
|             });
 | |
|           },
 | |
| 
 | |
|           actions: {
 | |
|             accept: {
 | |
|               label: 'Accept Invitation',
 | |
|               action: function(args) {
 | |
|                 $.ajax({
 | |
|                   url: createURL('updateProjectInvitation'),
 | |
|                   data: {
 | |
|                     projectid: args.context.invitations[0].projectid,
 | |
|                     account: args.context.users[0].account,
 | |
|                     domainid: args.context.users[0].domainid,
 | |
|                     accept: true
 | |
|                   },
 | |
|                   success: function(data) {
 | |
|                     args.response.success({
 | |
|                       _custom: {
 | |
|                         jobId: data.updateprojectinvitationresponse.jobid,
 | |
|                         getUpdatedItem: function() { return { state: 'Accepted' }; }
 | |
|                       }
 | |
|                     });
 | |
|                   }
 | |
|                 });
 | |
|               },
 | |
|               messages: {
 | |
|                 confirm: function() { return 'Please confirm you wish to join this project.'; },
 | |
|                 notification: function() { return 'Accepted project invitation'; }
 | |
|               },
 | |
|               notification: { poll: pollAsyncJobResult }
 | |
|             },
 | |
| 
 | |
|             decline: {
 | |
|               label: 'Decline Invitation',
 | |
|               action: function(args) {
 | |
|                 $.ajax({
 | |
|                   url: createURL('updateProjectInvitation'),
 | |
|                   data: {
 | |
|                     projectid: args.context.invitations[0].projectid,
 | |
|                     account: args.context.users[0].account,
 | |
|                     accept: false
 | |
|                   },
 | |
| 
 | |
|                   success: function(data) {
 | |
|                     args.response.success({
 | |
|                       _custom: {
 | |
|                         jobId: data.updateprojectinvitationresponse.jobid,
 | |
|                         getUpdatedItem: function() { return { state: 'Declined' }; }
 | |
|                       }
 | |
|                     });
 | |
|                   }
 | |
|                 });
 | |
|               },
 | |
|               notification: { poll: pollAsyncJobResult },
 | |
|               messages: {
 | |
|                 confirm: function() { return 'Are you sure you want to decline this project invitation?'; },
 | |
|                 notification: function() { return 'Declined project invitation'; }
 | |
|               }
 | |
|             }
 | |
|           }
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   };
 | |
| 
 | |
|   var projectsActionFilter = function(args) {
 | |
|     if (args.context.item.account == cloudStack.context.users[0].account || args.context.users[0].role == '1') {
 | |
|       return ['destroy'];
 | |
|     }
 | |
| 
 | |
|     return [];
 | |
|   };
 | |
| 
 | |
|   var projectInvitationActionFilter = function(args) {
 | |
|     var state = args.context.item.state;
 | |
| 
 | |
|     if (state == 'Accepted' || state == 'Completed' || state == 'Declined') {
 | |
|       return [];
 | |
|     }
 | |
| 
 | |
|     return ['accept', 'decline'];
 | |
|   };
 | |
| } (cloudStack, testData));
 |