|
4 | 4 | angular.module('openshiftConsole').component('processTemplateDialog', {
|
5 | 5 | controller: [
|
6 | 6 | '$scope',
|
| 7 | + '$filter', |
| 8 | + 'Catalog', |
7 | 9 | 'DataService',
|
| 10 | + 'KeywordService', |
| 11 | + 'NotificationsService', |
| 12 | + 'ProjectsService', |
| 13 | + 'RecentlyViewedProjectsService', |
8 | 14 | ProcessTemplateDialog
|
9 | 15 | ],
|
10 | 16 | controllerAs: '$ctrl',
|
11 | 17 | bindings: {
|
12 | 18 | template: '<',
|
| 19 | + project: '<', |
| 20 | + useProjectTemplate: '<', |
13 | 21 | onDialogClosed: '&'
|
14 | 22 | },
|
15 | 23 | templateUrl: 'views/directives/process-template-dialog.html'
|
16 | 24 | });
|
17 | 25 |
|
18 |
| - function ProcessTemplateDialog($scope, DataService) { |
| 26 | + function ProcessTemplateDialog($scope, |
| 27 | + $filter, |
| 28 | + Catalog, |
| 29 | + DataService, |
| 30 | + KeywordService, |
| 31 | + NotificationsService, |
| 32 | + ProjectsService, |
| 33 | + RecentlyViewedProjectsService) { |
19 | 34 | var ctrl = this;
|
20 | 35 | var validityWatcher;
|
21 | 36 |
|
| 37 | + ctrl.selectStep = { |
| 38 | + id: 'projectTemplates', |
| 39 | + label: 'Selection', |
| 40 | + view: 'views/directives/process-template-dialog/process-template-select.html', |
| 41 | + hidden: ctrl.useProjectTemplate !== true, |
| 42 | + allowed: true, |
| 43 | + valid: false, |
| 44 | + onShow: showSelect |
| 45 | + }; |
| 46 | + |
22 | 47 | ctrl.configStep = {
|
23 | 48 | id: 'configuration',
|
24 | 49 | label: 'Configuration',
|
|
43 | 68 |
|
44 | 69 | ctrl.$onInit = function() {
|
45 | 70 | ctrl.loginBaseUrl = DataService.openshiftAPIBaseUrl();
|
| 71 | + ctrl.preSelectedProject = ctrl.selectedProject = ctrl.project; |
| 72 | + listProjects(); |
| 73 | + |
| 74 | + ctrl.projectEmptyState = { |
| 75 | + icon: 'pficon pficon-info', |
| 76 | + title: 'No Project Selected', |
| 77 | + info: 'Please select a project from the dropdown to load Templates and Images from that project.' |
| 78 | + }; |
| 79 | + |
| 80 | + ctrl.templatesEmptyState = { |
| 81 | + icon: 'pficon pficon-info', |
| 82 | + title: 'No Templates or Images', |
| 83 | + info: 'The selected project has no templates or images available to import.' |
| 84 | + }; |
| 85 | + |
| 86 | + ctrl.filterConfig = { |
| 87 | + fields: [ |
| 88 | + { |
| 89 | + id: 'keyword', |
| 90 | + title: 'Keyword', |
| 91 | + placeholder: 'Filter by Keyword', |
| 92 | + filterType: 'text' |
| 93 | + } |
| 94 | + ], |
| 95 | + resultsCount: 0, |
| 96 | + appliedFilters: [], |
| 97 | + onFilterChange: filterChange |
| 98 | + }; |
46 | 99 | };
|
47 | 100 |
|
48 | 101 | ctrl.$onChanges = function(changes) {
|
|
52 | 105 | ctrl.iconClass = getIconClass();
|
53 | 106 | }
|
54 | 107 | }
|
| 108 | + if (changes.useProjectTemplate) { |
| 109 | + initializeSteps(); |
| 110 | + } |
55 | 111 | };
|
56 | 112 |
|
57 | 113 | $scope.$on('templateInstantiated', function(event, message) {
|
|
85 | 141 | }
|
86 | 142 | };
|
87 | 143 |
|
| 144 | + ctrl.onProjectSelected = function(project) { |
| 145 | + ctrl.selectedProject = project; |
| 146 | + ctrl.configStep.valid = $scope.$ctrl.form.$valid && ctrl.selectedProject; |
| 147 | + }; |
| 148 | + |
| 149 | + ctrl.templateSelected = function(template) { |
| 150 | + ctrl.selectedTemplate = template; |
| 151 | + ctrl.template = _.get(template, 'resource'); |
| 152 | + ctrl.selectStep.valid = !!template; |
| 153 | + }; |
| 154 | + |
| 155 | + ctrl.templateProjectChange = function () { |
| 156 | + ctrl.templateProjectName = _.get(ctrl.templateProject, 'metadata.name'); |
| 157 | + |
| 158 | + // Get the templates for the selected project |
| 159 | + ctrl.catalogItems = {}; |
| 160 | + ctrl.templateSelected(); |
| 161 | + |
| 162 | + Catalog.getProjectCatalogItems(ctrl.templateProjectName, false, true).then( _.spread(function(catalogServiceItems, errorMessage) { |
| 163 | + ctrl.catalogItems = catalogServiceItems; |
| 164 | + ctrl.totalCount = ctrl.catalogItems.length; |
| 165 | + filterItems(); |
| 166 | + |
| 167 | + if (errorMessage) { |
| 168 | + NotificationsService.addNotification( |
| 169 | + { |
| 170 | + type: "error", |
| 171 | + message: errorMessage |
| 172 | + } |
| 173 | + ); |
| 174 | + } |
| 175 | + })); |
| 176 | + }; |
| 177 | + |
88 | 178 | function getIconClass() {
|
89 | 179 | var icon = _.get(ctrl, 'template.metadata.annotations.iconClass', 'fa fa-clone');
|
90 | 180 | return (icon.indexOf('icon-') !== -1) ? 'font-icon ' + icon : icon;
|
91 | 181 | }
|
92 | 182 |
|
93 | 183 | function initializeSteps() {
|
94 |
| - ctrl.steps = [ctrl.configStep, ctrl.resultsStep]; |
| 184 | + if (!ctrl.steps) { |
| 185 | + ctrl.steps = [ctrl.selectStep, ctrl.configStep, ctrl.resultsStep]; |
| 186 | + } |
95 | 187 | }
|
96 | 188 |
|
97 | 189 | function clearValidityWatcher() {
|
|
101 | 193 | }
|
102 | 194 | }
|
103 | 195 |
|
| 196 | + function showSelect() { |
| 197 | + ctrl.selectStep.selected = true; |
| 198 | + ctrl.configStep.selected = false; |
| 199 | + ctrl.resultsStep.selected = false; |
| 200 | + ctrl.nextTitle = "Next >"; |
| 201 | + clearValidityWatcher(); |
| 202 | + listProjects(); |
| 203 | + } |
| 204 | + |
104 | 205 | function showConfig() {
|
| 206 | + ctrl.selectStep.selected = false; |
105 | 207 | ctrl.configStep.selected = true;
|
106 | 208 | ctrl.resultsStep.selected = false;
|
107 | 209 | ctrl.nextTitle = "Create";
|
108 | 210 | ctrl.resultsStep.allowed = ctrl.configStep.valid;
|
109 | 211 |
|
110 | 212 | validityWatcher = $scope.$watch("$ctrl.form.$valid", function(isValid) {
|
111 |
| - ctrl.configStep.valid = isValid; |
| 213 | + ctrl.configStep.valid = isValid && ctrl.selectedProject; |
112 | 214 | ctrl.resultsStep.allowed = isValid;
|
113 | 215 | });
|
114 | 216 | }
|
115 | 217 |
|
116 | 218 | function showResults() {
|
| 219 | + ctrl.selectStep.selected = false; |
117 | 220 | ctrl.configStep.selected = false;
|
118 | 221 | ctrl.resultsStep.selected = true;
|
119 | 222 | ctrl.nextTitle = "Close";
|
|
124 | 227 | function instantiateTemplate() {
|
125 | 228 | $scope.$broadcast('instantiateTemplate');
|
126 | 229 | }
|
| 230 | + |
| 231 | + function filterForKeywords(searchText, items) { |
| 232 | + return KeywordService.filterForKeywords(items, ['name', 'tags'], KeywordService.generateKeywords(searchText)); |
| 233 | + } |
| 234 | + |
| 235 | + function filterChange(filters) { |
| 236 | + // only use filters which have a filter criteria 'value' |
| 237 | + // prevents applying an empty keyword filter |
| 238 | + // TODO: can remove the following line of code after angular-patternfly issue is fixed: |
| 239 | + // https://github.com/patternfly/angular-patternfly/issues/509 |
| 240 | + filters = _.filter(filters, 'value'); |
| 241 | + |
| 242 | + ctrl.filterConfig.appliedFilters = filters; |
| 243 | + filterItems(); |
| 244 | + } |
| 245 | + |
| 246 | + function filterItems() { |
| 247 | + ctrl.filteredItems = ctrl.catalogItems; |
| 248 | + if (ctrl.filterConfig.appliedFilters && ctrl.filterConfig.appliedFilters.length > 0) { |
| 249 | + _.each(ctrl.filterConfig.appliedFilters, function(filter) { |
| 250 | + ctrl.filteredItems = filterForKeywords(filter.value, ctrl.filteredItems); |
| 251 | + }); |
| 252 | + } |
| 253 | + |
| 254 | + // Deselect the currently selected template if it was filtered out |
| 255 | + if (!_.includes(ctrl.filteredItems, ctrl.selectedTemplate)) { |
| 256 | + ctrl.templateSelected(); |
| 257 | + } |
| 258 | + |
| 259 | + updateFilterControls(); |
| 260 | + } |
| 261 | + |
| 262 | + function updateFilterControls() { |
| 263 | + if (ctrl.filterConfig.appliedFilters.length > 0) { |
| 264 | + ctrl.filterConfig.resultsCount = ctrl.filteredItems.length; |
| 265 | + $('.toolbar-pf-results h5').text(ctrl.filterConfig.resultsCount + ' of ' + ctrl.totalCount + ' items'); |
| 266 | + } else { |
| 267 | + $('.toolbar-pf-results h5').text(ctrl.totalCount + (ctrl.totalCount === 1 ? ' item' : ' items')); |
| 268 | + if (ctrl.totalCount <= 1) { |
| 269 | + $('.filter-pf.filter-fields input').attr('disabled', ''); |
| 270 | + } else { |
| 271 | + $('.filter-pf.filter-fields input').removeAttr("disabled"); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + var updateProjects = function() { |
| 277 | + var filteredProjects = _.reject(ctrl.unfilteredProjects, 'metadata.deletionTimestamp'); |
| 278 | + var projects = _.sortBy(filteredProjects, $filter('displayName')); |
| 279 | + ctrl.searchEnabled = !_.isEmpty(filteredProjects); |
| 280 | + |
| 281 | + ctrl.templateProjects = RecentlyViewedProjectsService.orderByMostRecentlyViewed(projects); |
| 282 | + }; |
| 283 | + |
| 284 | + function listProjects() { |
| 285 | + if (!ctrl.unfilteredProjects) { |
| 286 | + ProjectsService.list().then(function(projectData) { |
| 287 | + ctrl.unfilteredProjects = _.toArray(projectData.by("metadata.name")); |
| 288 | + }, function() { |
| 289 | + ctrl.unfilteredProjects = []; |
| 290 | + }).finally(function() { |
| 291 | + updateProjects(); |
| 292 | + }); |
| 293 | + } |
| 294 | + } |
127 | 295 | }
|
128 | 296 | })();
|
0 commit comments