forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteLink.js
191 lines (172 loc) · 6.52 KB
/
deleteLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
angular.module("openshiftConsole")
.directive("deleteLink",
function($uibModal,
$location,
$filter,
$q,
hashSizeFilter,
APIService,
DataService,
Navigate,
NotificationsService,
Logger) {
return {
restrict: "E",
scope: {
// Resource Kind to delete (e.g., "Pod" or "ReplicationController").
kind: "@",
// Optional resource group.
group: "@?",
// Optional display name for kind.
typeDisplayName: "@?",
// Name of the resource to delete.
resourceName: "@",
// The name of the resource's project. Optional if kind === "Project".
projectName: "@",
// Alerts object for success and error alerts.
alerts: "=",
// Optional display name of the resource to delete.
displayName: "@",
// Set to true to disable the delete button.
disableDelete: "=?",
// Force the user to enter the name before we'll delete the resource (e.g. for projects).
typeNameToConfirm: "=?",
// Optional link label. Defaults to "Delete".
label: "@?",
// Only show a delete icon with no text.
buttonOnly: "@",
// Stay on the current page without redirecting to the resource list.
stayOnCurrentPage: "=?",
// Array of associated HPAs for this resource. If set, prompts the user to delete the HPA resources as well.
hpaList: "=?",
// Optional callback when the delete succeeds
success: "=?",
// Optional redirect URL when the delete succeeds
redirectUrl: "@?"
},
templateUrl: function(elem, attr) {
if (angular.isDefined(attr.buttonOnly)) {
return "views/directives/delete-button.html";
}
return "views/directives/delete-link.html";
},
// Replace so ".dropdown-menu > li > a" styles are applied.
replace: true,
link: function(scope, element, attrs) {
if (attrs.kind === 'Project') {
scope.isProject = true;
}
// Checkbox value
scope.options = {
deleteHPAs: true,
deleteImmediately: false
};
var showAlert = function(alert) {
if (scope.stayOnCurrentPage) {
scope.alerts[alert.name] = alert.data;
} else {
NotificationsService.addNotification(alert.data);
}
};
var deleteHPA = function(hpa) {
return DataService.delete({
resource: 'horizontalpodautoscalers',
group: 'autoscaling'
}, hpa.metadata.name, { namespace: scope.projectName })
.then(function() {
NotificationsService.addNotification({
type: "success",
message: "Horizontal pod autoscaler " + hpa.metadata.name + " was marked for deletion."
});
})
.catch(function(err) {
showAlert({
name: hpa.metadata.name,
data: {
type: "error",
message: "Horizontal pod autoscaler " + hpa.metadata.name + " could not be deleted."
}
});
Logger.error("HPA " + hpa.metadata.name + " could not be deleted.", err);
});
};
var navigateToList = function() {
if (scope.stayOnCurrentPage) {
return;
}
if (scope.redirectUrl) {
$location.url(scope.redirectUrl);
return;
}
if (scope.kind !== 'Project') {
Navigate.toResourceList(APIService.kindToResource(scope.kind), scope.projectName);
return;
}
if ($location.path() === '/') {
scope.$emit('deleteProject');
return;
}
var homeRedirect = URI('/');
$location.url(homeRedirect);
};
scope.openDeleteModal = function() {
if (scope.disableDelete) {
return;
}
// opening the modal with settings scope as parent
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'views/modals/delete-resource.html',
controller: 'DeleteModalController',
scope: scope
});
modalInstance.result.then(function() {
// upon clicking delete button, delete resource and send alert
var kind = scope.kind;
var resourceName = scope.resourceName;
var typeDisplayName = scope.typeDisplayName || $filter('humanizeKind')(kind);
var formattedResource = typeDisplayName + ' ' + "\'" + (scope.displayName ? scope.displayName : resourceName) + "\'";
var context = (scope.kind === 'Project') ? {} : {namespace: scope.projectName};
var deleteOptions = {};
if (scope.options.deleteImmediately) {
deleteOptions.gracePeriodSeconds = 0;
deleteOptions.propagationPolicy = null;
}
// TODO - remove once this is resolved https://github.com/kubernetes-incubator/service-catalog/issues/942
if (scope.group === 'servicecatalog.k8s.io') {
deleteOptions.propagationPolicy = null;
}
DataService.delete({
resource: APIService.kindToResource(kind),
// group or undefined
group: scope.group
}, resourceName, context, deleteOptions)
.then(function() {
NotificationsService.addNotification({
type: "success",
message: _.capitalize(formattedResource) + " was marked for deletion."
});
if (scope.success) {
scope.success();
}
// Delete any associated HPAs if requested.
if (scope.options.deleteHPAs) {
_.each(scope.hpaList, deleteHPA);
}
navigateToList();
})
.catch(function(err) {
// called if failure to delete
scope.alerts[resourceName] = {
type: "error",
message: _.capitalize(formattedResource) + "\'" + " could not be deleted.",
details: $filter('getErrorDetails')(err)
};
Logger.error(formattedResource + " could not be deleted.", err);
});
});
};
}
};
});