forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteLink.js
178 lines (163 loc) · 6.2 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
'use strict';
angular.module("openshiftConsole")
.directive("deleteLink", function ($uibModal, $location, $filter, $q, hashSizeFilter, APIService, DataService, AlertMessageService, Navigate, 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: "=?"
},
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
};
var showAlert = function(alert) {
if (scope.stayOnCurrentPage) {
scope.alerts[alert.name] = alert.data;
} else {
AlertMessageService.addAlert(alert);
}
};
var deleteHPA = function(hpa) {
return DataService.delete({
resource: 'horizontalpodautoscalers',
group: 'extensions'
}, hpa.metadata.name, { namespace: scope.projectName })
.then(function() {
showAlert({
name: hpa.metadata.name,
data: {
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.kind !== 'Project') {
Navigate.toResourceList(APIService.kindToResource(scope.kind), scope.projectName);
}
else {
if ($location.path() === '/') {
scope.$emit('deleteProject');
} else if ($location.path().indexOf('settings') > '-1') {
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};
DataService.delete({
resource: APIService.kindToResource(kind),
// group or undefined
group: scope.group
}, resourceName, context)
.then(function() {
showAlert({
name: resourceName,
data: {
type: "success",
message: _.capitalize(formattedResource) + " was marked for deletion."
}
});
if (scope.success) {
scope.success();
}
// Delete any associated HPAs if requested.
var promises = [];
if (scope.options.deleteHPAs) {
_.forEach(scope.hpaList, function(hpa) {
promises.push(deleteHPA(hpa));
});
}
if (!promises.length) {
navigateToList();
} else {
// Wait until all promises resolve so that we can add alerts to
// AlertMessageService before navigating, otherwise they aren't
// displayed.
$q.all(promises).then(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);
});
});
};
}
};
});